login.vue 14 KB

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