login.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. <template>
  2. <view class="login-container">
  3. <!-- 顶部品牌区 -->
  4. <view class="brand-section">
  5. <image class="logo" src="/static/logo.png" mode="aspectFit" @error="handleImageError"></image>
  6. <text class="app-name">浠艾福</text>
  7. <text class="identity">您的专属家庭主动健康管理师</text>
  8. <text class="slogan">家里每个人的状态,你真的看见了?</text>
  9. </view>
  10. <!-- 主动健康理念 + 闭环 -->
  11. <view class="concept-card">
  12. <view class="concept-desc">
  13. 不等问题发生,先一步<text class="concept-highlight">识别风险 · 主动干预 · 持续评估</text>,把全家健康管理融入每一天。
  14. </view>
  15. <view class="loop-row">
  16. <view class="loop-pill loop-pill-indigo"><text>🔍</text><text class="loop-pill-text">提前识别</text></view>
  17. <text class="loop-arrow">→</text>
  18. <view class="loop-pill loop-pill-green"><text>🛡️</text><text class="loop-pill-text">风险管控</text></view>
  19. <text class="loop-arrow">→</text>
  20. <view class="loop-pill loop-pill-amber"><text>⚡</text><text class="loop-pill-text">主动干预</text></view>
  21. <text class="loop-arrow">→</text>
  22. <view class="loop-pill loop-pill-red"><text>📊</text><text class="loop-pill-text">效果评估</text></view>
  23. </view>
  24. </view>
  25. <!-- 五维标签 -->
  26. <view class="dims-row">
  27. <view class="dim-tag dim-tag-body"><text>🌏</text><text>身·健康</text></view>
  28. <view class="dim-tag dim-tag-wisdom"><text>⚡</text><text>智·赋能</text></view>
  29. <view class="dim-tag dim-tag-wealth"><text>💧</text><text>富·创富</text></view>
  30. <view class="dim-tag dim-tag-action"><text>🌿</text><text>行·和谐</text></view>
  31. <view class="dim-tag dim-tag-mind"><text>🔥</text><text>心·大爱</text></view>
  32. </view>
  33. <!-- 登录表单 -->
  34. <view class="login-form">
  35. <view class="phone-section">
  36. <!--
  37. disabled 必须同时受 agreed 和 loading 约束。
  38. open-type="getPhoneNumber" 在「点击那一刻」就由渲染层向微信发起授权请求,
  39. 早于 @getphonenumber 回调执行 —— 也就是说每一次点击都会消耗一次平台调用配额,
  40. JS 里的 if (!this.agreed) return 只能弹 toast,拦不住已经发生的调用。
  41. 未同意协议时按钮若仍可点,用户会因「点了没反应」而反复点击,最终触发微信限频:
  42. [渲染层错误] invoke getPhoneNumber too frequently。
  43. 故未勾选协议时直接禁用按钮,让无效点击根本不会发生。
  44. -->
  45. <button
  46. class="phone-btn"
  47. open-type="getPhoneNumber"
  48. @getphonenumber="getPhoneNumber"
  49. @tap="onPhoneBtnTap"
  50. :loading="loading"
  51. :disabled="!agreed || loading || tapLock"
  52. >
  53. <text class="phone-btn-icon">📱</text>
  54. <text>手机号快捷登录</text>
  55. </button>
  56. </view>
  57. <!-- 暂不登录 -->
  58. <view class="skip-section">
  59. <text class="skip-btn" @click="handleSkipLogin">暂不登录,先逛逛</text>
  60. </view>
  61. <view class="agreement">
  62. <checkbox-group @change="onAgreementChange">
  63. <label>
  64. <checkbox value="agree" :checked="agreed" />
  65. <text class="agreement-text">我已阅读并同意</text>
  66. </label>
  67. </checkbox-group>
  68. <text class="link" @click="showAgreement">《用户协议》</text>
  69. <text class="agreement-text">和</text>
  70. <text class="link" @click="showPrivacy">《隐私政策》</text>
  71. </view>
  72. </view>
  73. </view>
  74. </template>
  75. <script>
  76. import { wechatPhoneLogin, checkTeamInviteStatus, joinTeamByInvite, bindReferralIfNotSelf } from '../../utils/api.js'
  77. export default {
  78. data() {
  79. return {
  80. loading: false,
  81. // tapLock:在「点击」瞬间上锁,用于关闭授权弹窗弹出前的连点窗口(见 onPhoneBtnTap)
  82. tapLock: false,
  83. agreed: false,
  84. nickname: '',
  85. avatar: '',
  86. redirectUrl: ''
  87. }
  88. },
  89. onLoad(options) {
  90. // 处理邀请码
  91. if (options.inviteCode) {
  92. uni.setStorageSync('inviteCode', options.inviteCode)
  93. uni.setStorageSync('inviteType', options.inviteType || 'family')
  94. }
  95. // 处理推荐码(refCode):来自 join.vue 的推广绑定流程,推荐码统一存 refCode storage
  96. if (options.refCode) {
  97. uni.setStorageSync('refCode', options.refCode)
  98. }
  99. // 存储登录后跳转地址
  100. if (options.redirect) {
  101. this.redirectUrl = decodeURIComponent(options.redirect)
  102. }
  103. // 注意:不在 onLoad 预取 login code —— 微信 code 一次性有效(5分钟且用过即废),
  104. // 预取后如果用户隔几分钟再点击授权,code 已失效会报 invalid code (40029)。
  105. // 正确做法:getPhoneNumber 中用户点击授权时实时获取。
  106. },
  107. onUnload() {
  108. // 清理 tap 锁兜底定时器,避免登录成功 reLaunch 后定时器仍在已销毁页面上置值
  109. if (this._tapLockTimer) {
  110. clearTimeout(this._tapLockTimer)
  111. this._tapLockTimer = null
  112. }
  113. },
  114. methods: {
  115. handleImageError(e) {
  116. console.log('Logo 加载失败,使用默认显示')
  117. },
  118. onAgreementChange(e) {
  119. this.agreed = e.detail.value.length > 0
  120. },
  121. showAgreement() {
  122. uni.navigateTo({
  123. url: '/pages/policy/agreement'
  124. })
  125. },
  126. showPrivacy() {
  127. uni.navigateTo({
  128. url: '/pages/policy/privacy'
  129. })
  130. },
  131. handleSkipLogin() {
  132. // 不登录直接进入首页
  133. uni.switchTab({ url: '/pages/index-home/index' })
  134. },
  135. // 点击瞬间上锁,关闭「授权弹窗弹出前」的连点窗口。
  136. //
  137. // 为什么不能只在 getPhoneNumber 里判 loading:
  138. // open-type="getPhoneNumber" 在点击那一刻就由渲染层向微信发起了授权调用,
  139. // @getphonenumber 回调要等用户在系统弹窗里点完才返回。这中间几百毫秒内
  140. // loading 还是 false、按钮仍可点,用户「以为没反应」就连点,
  141. // 每次点击都真实消耗一次平台调用配额 → invoke getPhoneNumber too frequently。
  142. // 在 @tap 上锁能让按钮在一个渲染周期内变为不可点,早于弹窗出现。
  143. onPhoneBtnTap() {
  144. if (this.loading || this.tapLock) return
  145. this.tapLock = true
  146. // 兜底:若因系统异常导致 getphonenumber 始终不回调,3 秒后强制解锁,
  147. // 避免按钮永久锁死导致无法再登录。
  148. if (this._tapLockTimer) clearTimeout(this._tapLockTimer)
  149. this._tapLockTimer = setTimeout(() => {
  150. this.tapLock = false
  151. this._tapLockTimer = null
  152. }, 3000)
  153. },
  154. getPhoneNumber: function(e) {
  155. // 授权流程已结束,释放 tap 锁与兜底定时器,按钮恢复可点。
  156. // 注意:这里原有的 `if (this.loading) return` 已移除 —— loading 在本函数内才置 true,
  157. // 保留它会把首个合法点击也拦掉;防重复消费改由下面的 tapLock 承担。
  158. if (this._tapLockTimer) {
  159. clearTimeout(this._tapLockTimer)
  160. this._tapLockTimer = null
  161. }
  162. this.tapLock = false
  163. if (!this.agreed) {
  164. uni.showToast({ title: '请先同意用户协议', icon: 'none' })
  165. return
  166. }
  167. if (!e.detail || !e.detail.code) {
  168. uni.showToast({ title: '授权失败,请重试', icon: 'none' })
  169. return
  170. }
  171. var self = this
  172. self.loading = true
  173. // 点击授权时实时获取 login code(微信 code 一次性有效,不能复用旧 code)
  174. uni.login({
  175. provider: 'weixin',
  176. success: function(loginRes) {
  177. if (!loginRes.code) {
  178. uni.showToast({ title: '网络异常,请重试', icon: 'none' })
  179. self.loading = false
  180. return
  181. }
  182. wechatPhoneLogin({ code: loginRes.code, phoneCode: e.detail.code }).then(function(res) {
  183. self.completeLogin(res.data)
  184. }).catch(function(err) {
  185. uni.showToast({ title: err.message || '登录失败', icon: 'none' })
  186. self.loading = false
  187. })
  188. },
  189. fail: function() {
  190. uni.showToast({ title: '网络异常,请重试', icon: 'none' })
  191. self.loading = false
  192. }
  193. })
  194. },
  195. completeLogin: function(data) {
  196. var self = this
  197. uni.setStorageSync('token', data.token)
  198. uni.setStorageSync('userId', data.userId)
  199. uni.setStorageSync('role', data.role)
  200. uni.setStorageSync('currentRole', data.role)
  201. uni.setStorageSync('isSwitchedChild', false)
  202. uni.setStorageSync('isSwitchedTeacher', false)
  203. if (data.openid) uni.setStorageSync('openid', data.openid)
  204. // familyId 必须持久化:家庭在注册时已由后端创建,缺失会导致页面误判无家庭而重复创建
  205. uni.setStorageSync('userInfo', { nickname: data.nickname, userId: data.userId, familyId: data.familyId })
  206. if (data.phone) uni.setStorageSync('phone', data.phone)
  207. if (data.teacherStatus) uni.setStorageSync('teacherStatus', data.teacherStatus)
  208. if (data.teacherRejectReason) uni.setStorageSync('teacherRejectReason', data.teacherRejectReason)
  209. self.$store.commit('setFamilyId', data.familyId)
  210. uni.showToast({ title: '登录成功', icon: 'success' })
  211. self.loading = false
  212. // 注册零填写:新用户(needsRoleSelect)不再跳信息填写页,直接进首页
  213. // 家庭邀请衔接:有 pendingFamilyInviteCode(join.vue 写入)→ 直接进 join-form 填真实姓名+家庭身份
  214. var pendingInvite = uni.getStorageSync('pendingFamilyInviteCode')
  215. if (pendingInvite) {
  216. uni.redirectTo({ url: '/pages/invite/join-form?familyInviteCode=' + encodeURIComponent(pendingInvite) })
  217. } else {
  218. self.handlePostLoginRouting()
  219. }
  220. },
  221. handlePostLoginRouting() {
  222. const inviteCode = uni.getStorageSync('inviteCode')
  223. const inviteType = uni.getStorageSync('inviteType')
  224. const refCode = uni.getStorageSync('refCode')
  225. if (inviteCode && inviteType === 'team') {
  226. this.handleTeamInviteFlow(inviteCode)
  227. } else if (refCode) {
  228. this.handleReferralBind(refCode)
  229. } else if (inviteCode) {
  230. // 兼容:历史 storage inviteCode 承载推荐码的残留
  231. this.handleReferralBind(inviteCode)
  232. } else {
  233. this.navigateToHome()
  234. }
  235. },
  236. async handleReferralBind(refCode) {
  237. try {
  238. var res = await bindReferralIfNotSelf(refCode)
  239. if (res && res.code === 200) {
  240. console.log('登录后绑定推荐人成功', refCode)
  241. }
  242. } catch (e) {
  243. // 后端 Result.error(code=500) → request() 已 toast「已绑定推荐人,不可修改」等业务消息
  244. if (e && e.code !== undefined && e.code !== 401) {
  245. console.log('登录后推荐码业务拒绝', refCode, e.message || '')
  246. } else {
  247. console.log('登录后推荐码绑定失败', e)
  248. }
  249. }
  250. uni.removeStorageSync('refCode')
  251. uni.removeStorageSync('inviteCode')
  252. uni.removeStorageSync('inviteType')
  253. this.navigateToHome()
  254. },
  255. async handleTeamInviteFlow(inviteCode) {
  256. try {
  257. const res = await checkTeamInviteStatus(inviteCode)
  258. const status = res.data.status
  259. if (status === 'not_planner') {
  260. uni.redirectTo({ url: `/pages/guide/register/index?inviteCode=${inviteCode}` })
  261. } else if (status === 'not_in_team') {
  262. uni.showModal({
  263. title: '加入团队',
  264. content: '是否加入该规划师团队?',
  265. success: async (confirmRes) => {
  266. if (confirmRes.confirm) {
  267. try {
  268. await joinTeamByInvite(inviteCode)
  269. uni.showToast({ title: '加入成功', icon: 'success' })
  270. uni.removeStorageSync('inviteCode')
  271. uni.removeStorageSync('inviteType')
  272. this.navigateToHome()
  273. } catch (e) {
  274. uni.showToast({ title: e.message || '加入失败', icon: 'none' })
  275. }
  276. } else {
  277. this.navigateToHome()
  278. }
  279. }
  280. })
  281. } else if (status === 'already_in_team') {
  282. uni.showModal({
  283. title: '加入新团队',
  284. content: '您已在其他团队中,离开当前团队并加入新团队?',
  285. success: async (confirmRes) => {
  286. if (confirmRes.confirm) {
  287. try {
  288. await joinTeamByInvite(inviteCode)
  289. uni.showToast({ title: '加入成功', icon: 'success' })
  290. uni.removeStorageSync('inviteCode')
  291. uni.removeStorageSync('inviteType')
  292. this.navigateToHome()
  293. } catch (e) {
  294. uni.showToast({ title: e.message || '加入失败', icon: 'none' })
  295. }
  296. } else {
  297. this.navigateToHome()
  298. }
  299. }
  300. })
  301. } else {
  302. this.navigateToHome()
  303. }
  304. } catch (e) {
  305. uni.showToast({ title: e.message || '处理邀请失败', icon: 'none' })
  306. this.navigateToHome()
  307. }
  308. },
  309. navigateToHome() {
  310. if (this.redirectUrl) {
  311. const redirect = this.redirectUrl
  312. this.redirectUrl = ''
  313. // 注意: redirectTo 不支持跳转 tabBar 页面,必须用 switchTab
  314. // 只有 pages.json tabBar.list 注册的页面才能用 switchTab
  315. var tabBarPages = ['/pages/index-home/index', '/pages/wealth/index']
  316. if (tabBarPages.indexOf(redirect) !== -1) {
  317. uni.switchTab({ url: redirect })
  318. } else {
  319. uni.redirectTo({ url: redirect })
  320. }
  321. } else {
  322. uni.switchTab({ url: '/pages/index-home/index' })
  323. }
  324. }
  325. }
  326. }
  327. </script>
  328. <style scoped>
  329. .login-container {
  330. min-height: 100vh;
  331. background: linear-gradient(180deg, #FFF9F0 0%, #FFEEDB 100%);
  332. padding: 40rpx 36rpx 32rpx;
  333. display: flex;
  334. flex-direction: column;
  335. }
  336. /* ===== 顶部品牌区 ===== */
  337. .brand-section {
  338. text-align: center;
  339. margin-bottom: 32rpx;
  340. }
  341. .logo {
  342. width: 104rpx;
  343. height: 104rpx;
  344. border-radius: 52rpx;
  345. background: #fff;
  346. box-shadow: 0 8rpx 24rpx rgba(249,115,22,0.2);
  347. }
  348. .app-name {
  349. display: block;
  350. font-size: 44rpx;
  351. font-weight: bold;
  352. color: #7C2D12;
  353. margin-top: 14rpx;
  354. letter-spacing: 6rpx;
  355. }
  356. .identity {
  357. display: block;
  358. font-size: 26rpx;
  359. font-weight: 600;
  360. color: #C2410C;
  361. margin-top: 10rpx;
  362. }
  363. .slogan {
  364. display: block;
  365. font-size: 24rpx;
  366. color: #9A3412;
  367. margin-top: 8rpx;
  368. }
  369. /* ===== 主动健康理念 + 闭环 ===== */
  370. .concept-card {
  371. background: #fff;
  372. border-radius: 20rpx;
  373. padding: 26rpx 28rpx;
  374. margin-bottom: 24rpx;
  375. box-shadow: 0 4rpx 16rpx rgba(249,115,22,0.08);
  376. }
  377. .concept-desc {
  378. font-size: 24rpx;
  379. color: #4B5563;
  380. line-height: 1.6;
  381. margin-bottom: 20rpx;
  382. }
  383. .concept-highlight {
  384. color: #B45309;
  385. font-weight: 600;
  386. }
  387. .loop-row {
  388. display: flex;
  389. align-items: center;
  390. gap: 6rpx;
  391. }
  392. .loop-pill {
  393. flex: 1;
  394. display: flex;
  395. align-items: center;
  396. justify-content: center;
  397. gap: 4rpx;
  398. flex-direction: column;
  399. font-size: 20rpx;
  400. padding: 10rpx 4rpx;
  401. border-radius: 12rpx;
  402. line-height: 1.3;
  403. }
  404. .loop-pill-text {
  405. font-size: 20rpx;
  406. font-weight: 500;
  407. }
  408. .loop-pill-indigo { background: #EEF2FF; color: #4F46E5; }
  409. .loop-pill-green { background: #D1FAE5; color: #047857; }
  410. .loop-pill-amber { background: #FEF3C7; color: #B45309; }
  411. .loop-pill-red { background: #FEE2E2; color: #B91C1C; }
  412. .loop-arrow {
  413. font-size: 24rpx;
  414. color: #D1D5DB;
  415. flex-shrink: 0;
  416. }
  417. /* ===== 五维标签 ===== */
  418. .dims-row {
  419. display: flex;
  420. justify-content: center;
  421. gap: 8rpx;
  422. margin-bottom: 24rpx;
  423. flex-wrap: wrap;
  424. }
  425. .dim-tag {
  426. display: flex;
  427. align-items: center;
  428. gap: 4rpx;
  429. font-size: 20rpx;
  430. color: #fff;
  431. font-weight: 500;
  432. padding: 8rpx 14rpx;
  433. border-radius: 24rpx;
  434. white-space: nowrap;
  435. }
  436. .dim-tag-body { background: #F97316; }
  437. .dim-tag-wisdom { background: #6366F1; }
  438. .dim-tag-wealth { background: #D97706; }
  439. .dim-tag-action { background: #10B981; }
  440. .dim-tag-mind { background: #EC4899; }
  441. /* ===== 登录表单 ===== */
  442. .login-form {
  443. background: #fff;
  444. border-radius: 20rpx;
  445. padding: 36rpx 36rpx 28rpx;
  446. margin-top: 48rpx;
  447. box-shadow: 0 4rpx 24rpx rgba(0,0,0,0.06);
  448. }
  449. .phone-section {
  450. margin-bottom: 24rpx;
  451. }
  452. .phone-btn {
  453. display: flex;
  454. align-items: center;
  455. justify-content: center;
  456. gap: 12rpx;
  457. width: 100%;
  458. height: 88rpx;
  459. background: #07c160;
  460. color: #fff;
  461. border-radius: 44rpx;
  462. font-size: 32rpx;
  463. font-weight: 500;
  464. border: none;
  465. }
  466. .phone-btn::after {
  467. border: none;
  468. }
  469. .phone-btn-icon {
  470. font-size: 36rpx;
  471. }
  472. /* ===== 跳过登录 ===== */
  473. .skip-section {
  474. text-align: center;
  475. margin-bottom: 20rpx;
  476. }
  477. .skip-btn {
  478. font-size: 26rpx;
  479. color: #F97316;
  480. font-weight: 500;
  481. padding: 10rpx 20rpx;
  482. background: rgba(249, 115, 22, 0.08);
  483. border-radius: 30rpx;
  484. }
  485. /* ===== 协议 ===== */
  486. .agreement {
  487. margin-top: 4rpx;
  488. text-align: center;
  489. font-size: 22rpx;
  490. line-height: 1.6;
  491. }
  492. .agreement checkbox {
  493. transform: scale(0.8);
  494. }
  495. .agreement-text {
  496. color: #9CA3AF;
  497. }
  498. .link {
  499. color: #5B9BD5;
  500. text-decoration: underline;
  501. }
  502. </style>