login.vue 15 KB

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