index.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <template>
  2. <view class="container" :style="{ '--font-scale': fontScale }">
  3. <!-- 登录浮层(全局组件,页面内弹出底部登录) -->
  4. <login-sheet ref="loginSheet" @success="onSheetLoginSuccess" />
  5. <view v-if="!isLoggedIn" class="login-prompt">
  6. <view class="prompt-icon">👤</view>
  7. <text class="prompt-title">登录浠艾福</text>
  8. <text class="prompt-desc">登录后可查看个人资料</text>
  9. <button class="login-btn" @click="goLogin">登录 / 注册</button>
  10. </view>
  11. <template v-else>
  12. <!-- 从富页面进入的返回入口 -->
  13. <view v-if="fromWealth" class="back-to-wealth" @click="goToWealth">
  14. <text class="back-arrow">‹</text>
  15. <text class="back-label">返回富页面</text>
  16. </view>
  17. <ProfileHeader @avatar-click="goToEditInfo" />
  18. <ProfileMenu :role="role" @invite-generate="onInviteGenerate" />
  19. <view class="sso-entry" @click="goTrain">
  20. <text class="sso-entry-icon">🎓</text>
  21. <text class="sso-entry-text">进入训练营</text>
  22. <text class="sso-entry-arrow">›</text>
  23. </view>
  24. </template>
  25. </view>
  26. </template>
  27. <script>
  28. import ProfileHeader from '@/pages/profile/components/ProfileHeader.vue'
  29. import ProfileMenu from '@/pages/profile/components/ProfileMenu.vue'
  30. import { getReferralCode, extractReferralCode } from '../../utils/api.js'
  31. import { jumpToTrain } from '../../utils/sso.js'
  32. import { getFontScale } from '../../utils/fontScale.js'
  33. export default {
  34. components: {
  35. ProfileHeader,
  36. ProfileMenu
  37. },
  38. data() {
  39. return {
  40. // 登录态:响应式字段(非 computed)。computed 依赖非响应式的 Storage 读取,
  41. // 登录成功写入 token 后不会触发重算,导致页面仍显示登录前状态。
  42. // 改为 data 字段并在 onShow / 登录成功回调中重新赋值,登录后页面即时刷新。
  43. isLoggedIn: !!uni.getStorageSync('token'),
  44. shareData: null,
  45. fromWealth: false,
  46. referralCode: '',
  47. referralCodeLoaded: false,
  48. fontScale: 1
  49. }
  50. },
  51. computed: {
  52. role() {
  53. return uni.getStorageSync('currentRole') || uni.getStorageSync('role') || 'parent'
  54. }
  55. },
  56. onLoad(options) {
  57. // 支持 wealth→profile 的跳转来源标识
  58. if (options && options.from === 'wealth') {
  59. this.fromWealth = true
  60. }
  61. this.fontScale = getFontScale()
  62. var self = this
  63. this._fontScaleHandler = function() {
  64. self.fontScale = getFontScale()
  65. }
  66. // 登录成功后 mixin 会重新调用 onLoad 刷新页面,先解绑旧监听避免重复注册
  67. if (this._fontScaleHandler) {
  68. uni.$off('fontScaleChanged', this._fontScaleHandler)
  69. }
  70. uni.$on('fontScaleChanged', this._fontScaleHandler)
  71. },
  72. onShow() {
  73. this.fontScale = getFontScale()
  74. // 重新读取登录态(登录浮层成功后 mixin 会触发 onShow),驱动页面切换到已登录视图
  75. this.isLoggedIn = !!uni.getStorageSync('token')
  76. if (this.isLoggedIn) {
  77. this.loadReferralCode()
  78. }
  79. },
  80. onUnload() {
  81. if (this._fontScaleHandler) {
  82. uni.$off('fontScaleChanged', this._fontScaleHandler)
  83. }
  84. },
  85. onShareAppMessage() {
  86. if (this.shareData) {
  87. var path = this.shareData.path
  88. if (this.referralCode && path.indexOf('refCode=') === -1 && path.indexOf('inviteCode=') === -1) {
  89. var sep = path.indexOf('?') === -1 ? '?' : '&'
  90. path = path + sep + 'refCode=' + this.referralCode
  91. }
  92. return {
  93. title: this.shareData.title,
  94. path: path,
  95. imageUrl: '/static/invite-card.png'
  96. }
  97. }
  98. },
  99. onShareTimeline() {
  100. var query = ''
  101. if (this.shareData && this.shareData.path) {
  102. var qi = this.shareData.path.indexOf('?')
  103. if (qi !== -1) query = this.shareData.path.substring(qi + 1)
  104. }
  105. if (this.referralCode && query.indexOf('refCode=') === -1 && query.indexOf('inviteCode=') === -1) {
  106. query = query + (query ? '&' : '') + 'refCode=' + this.referralCode
  107. }
  108. return {
  109. title: this.shareData ? this.shareData.title : '浠艾福 — 给全家的一站式幸福提案',
  110. query: query,
  111. imageUrl: '/static/invite-card.png'
  112. }
  113. },
  114. methods: {
  115. /** 获取自己的邀请码用于分享 */
  116. async loadReferralCode() {
  117. if (this.referralCodeLoaded || !this.isLoggedIn) return
  118. try {
  119. var res = await getReferralCode()
  120. this.referralCode = extractReferralCode(res)
  121. } catch (e) {}
  122. this.referralCodeLoaded = true
  123. },
  124. goToWealth() {
  125. uni.navigateTo({ url: '/pages/wealth/index' })
  126. },
  127. goLogin() {
  128. this.openLoginSheet()
  129. },
  130. // 登录浮层成功回调:立即切换到已登录视图并加载登录后数据(推荐码等)
  131. onLoginSuccess() {
  132. this.isLoggedIn = true
  133. this.loadReferralCode()
  134. },
  135. goToEditInfo() {
  136. uni.navigateTo({
  137. url: '/pages/user-edit/user-edit'
  138. })
  139. },
  140. goTrain() {
  141. jumpToTrain()
  142. },
  143. onInviteGenerate(data) {
  144. this.shareData = data
  145. uni.showToast({
  146. title: '点击右上角转发给TA',
  147. icon: 'none'
  148. })
  149. }
  150. }
  151. }
  152. </script>
  153. <style scoped lang="scss">
  154. .container {
  155. min-height: 100vh;
  156. background: $app-bg;
  157. padding-bottom: 120rpx;
  158. }
  159. .login-prompt {
  160. display: flex;
  161. flex-direction: column;
  162. align-items: center;
  163. justify-content: center;
  164. min-height: 80vh;
  165. padding: 60rpx;
  166. }
  167. .prompt-icon {
  168. font-size: calc(120rpx * var(--font-scale, 1));
  169. margin-bottom: $uni-space-lg;
  170. width: 160rpx;
  171. height: 160rpx;
  172. line-height: 160rpx;
  173. text-align: center;
  174. background: $facade-fill-2;
  175. border-radius: 50%;
  176. }
  177. .prompt-title {
  178. font-size: calc(36rpx * var(--font-scale, 1));
  179. font-weight: bold;
  180. color: $facade-text-strong;
  181. margin-bottom: 16rpx;
  182. }
  183. .prompt-desc {
  184. font-size: calc(26rpx * var(--font-scale, 1));
  185. color: $facade-text-muted;
  186. text-align: center;
  187. margin-bottom: 40rpx;
  188. }
  189. .login-prompt .login-btn {
  190. width: 60%;
  191. height: 80rpx;
  192. line-height: 80rpx;
  193. background: linear-gradient(135deg, $brand-primary, $brand-primary-dark);
  194. color: $app-surface;
  195. font-size: calc(30rpx * var(--font-scale, 1));
  196. font-weight: bold;
  197. border-radius: 24rpx;
  198. text-align: center;
  199. border: none;
  200. }
  201. .login-prompt .login-btn::after {
  202. border: none;
  203. }
  204. /* 从富页面返回入口 */
  205. .back-to-wealth {
  206. display: flex;
  207. align-items: center;
  208. margin: 0 $uni-space-lg 20rpx;
  209. padding: 20rpx 24rpx;
  210. background: $app-surface;
  211. border-radius: 16rpx;
  212. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.05);
  213. }
  214. .back-arrow {
  215. font-size: calc(40rpx * var(--font-scale, 1));
  216. color: $dim-wealth;
  217. margin-right: 16rpx;
  218. font-weight: bold;
  219. }
  220. .back-label {
  221. font-size: calc(28rpx * var(--font-scale, 1));
  222. color: $facade-text-body;
  223. }
  224. /* 跨小程序单点登录入口 */
  225. .sso-entry {
  226. display: flex;
  227. align-items: center;
  228. margin: 20rpx $uni-space-lg 0;
  229. padding: 28rpx 24rpx;
  230. background: $app-surface;
  231. border-radius: 16rpx;
  232. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.05);
  233. }
  234. .sso-entry:active {
  235. background: $facade-fill-slate-50;
  236. }
  237. .sso-entry-icon {
  238. font-size: calc(40rpx * var(--font-scale, 1));
  239. margin-right: 16rpx;
  240. }
  241. .sso-entry-text {
  242. flex: 1;
  243. font-size: calc(30rpx * var(--font-scale, 1));
  244. color: $facade-text-strong;
  245. }
  246. .sso-entry-arrow {
  247. font-size: calc(40rpx * var(--font-scale, 1));
  248. color: $uni-border-color;
  249. }
  250. </style>