login.vue 14 KB

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