login.vue 14 KB

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