login.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. if (data.needsRoleSelect) {
  160. uni.redirectTo({ url: '/pages/user-edit/user-edit?token=' + data.token + '&userId=' + data.userId + '&autoParent=true' })
  161. } else {
  162. self.handlePostLoginRouting()
  163. }
  164. },
  165. handlePostLoginRouting() {
  166. const inviteCode = uni.getStorageSync('inviteCode')
  167. const inviteType = uni.getStorageSync('inviteType')
  168. if (inviteCode && inviteType === 'team') {
  169. this.handleTeamInviteFlow(inviteCode)
  170. } else if (inviteCode) {
  171. // 非 team 邀请码(如分享文章/商品/活动的邀请码),绑定推荐关系
  172. this.handleReferralBind(inviteCode)
  173. } else {
  174. this.navigateToHome()
  175. }
  176. },
  177. async handleReferralBind(inviteCode) {
  178. try {
  179. var res = await bindReferral(inviteCode)
  180. if (res && res.code === 200) {
  181. console.log('登录后绑定邀请码成功', inviteCode)
  182. } else if (res && res.code === 400) {
  183. console.log('用户已有邀请人,跳过绑定')
  184. }
  185. } catch (e) {
  186. console.log('邀请码绑定失败', e)
  187. }
  188. uni.removeStorageSync('inviteCode')
  189. uni.removeStorageSync('inviteType')
  190. this.navigateToHome()
  191. },
  192. async handleTeamInviteFlow(inviteCode) {
  193. try {
  194. const res = await checkTeamInviteStatus(inviteCode)
  195. const status = res.data.status
  196. if (status === 'not_planner') {
  197. uni.redirectTo({ url: `/pages/guide/register/index?inviteCode=${inviteCode}` })
  198. } else if (status === 'not_in_team') {
  199. uni.showModal({
  200. title: '加入团队',
  201. content: '是否加入该规划师团队?',
  202. success: async (confirmRes) => {
  203. if (confirmRes.confirm) {
  204. try {
  205. await joinTeamByInvite(inviteCode)
  206. uni.showToast({ title: '加入成功', icon: 'success' })
  207. uni.removeStorageSync('inviteCode')
  208. uni.removeStorageSync('inviteType')
  209. this.navigateToHome()
  210. } catch (e) {
  211. uni.showToast({ title: e.message || '加入失败', icon: 'none' })
  212. }
  213. } else {
  214. this.navigateToHome()
  215. }
  216. }
  217. })
  218. } else if (status === 'already_in_team') {
  219. uni.showModal({
  220. title: '加入新团队',
  221. content: '您已在其他团队中,离开当前团队并加入新团队?',
  222. success: async (confirmRes) => {
  223. if (confirmRes.confirm) {
  224. try {
  225. await joinTeamByInvite(inviteCode)
  226. uni.showToast({ title: '加入成功', icon: 'success' })
  227. uni.removeStorageSync('inviteCode')
  228. uni.removeStorageSync('inviteType')
  229. this.navigateToHome()
  230. } catch (e) {
  231. uni.showToast({ title: e.message || '加入失败', icon: 'none' })
  232. }
  233. } else {
  234. this.navigateToHome()
  235. }
  236. }
  237. })
  238. } else {
  239. this.navigateToHome()
  240. }
  241. } catch (e) {
  242. uni.showToast({ title: e.message || '处理邀请失败', icon: 'none' })
  243. this.navigateToHome()
  244. }
  245. },
  246. navigateToHome() {
  247. if (this.redirectUrl) {
  248. const redirect = this.redirectUrl
  249. this.redirectUrl = ''
  250. // 注意: redirectTo 不支持跳转 tabBar 页面,必须用 switchTab
  251. // 只有 pages.json tabBar.list 注册的页面才能用 switchTab
  252. var tabBarPages = ['/pages/index-home/index', '/pages/mind/index', '/pages/body/index', '/pages/wisdom/index', '/pages/wealth/index']
  253. if (tabBarPages.indexOf(redirect) !== -1) {
  254. uni.switchTab({ url: redirect })
  255. } else {
  256. uni.redirectTo({ url: redirect })
  257. }
  258. } else {
  259. uni.switchTab({ url: '/pages/index-home/index' })
  260. }
  261. }
  262. }
  263. }
  264. </script>
  265. <style scoped>
  266. .login-container {
  267. min-height: 100vh;
  268. background: linear-gradient(180deg, #FFF9F0 0%, #FFEEDB 100%);
  269. padding: 40rpx 36rpx 32rpx;
  270. display: flex;
  271. flex-direction: column;
  272. }
  273. /* ===== 顶部品牌区 ===== */
  274. .brand-section {
  275. text-align: center;
  276. margin-bottom: 32rpx;
  277. }
  278. .logo {
  279. width: 104rpx;
  280. height: 104rpx;
  281. border-radius: 52rpx;
  282. background: #fff;
  283. box-shadow: 0 8rpx 24rpx rgba(249,115,22,0.2);
  284. }
  285. .app-name {
  286. display: block;
  287. font-size: 44rpx;
  288. font-weight: bold;
  289. color: #7C2D12;
  290. margin-top: 14rpx;
  291. letter-spacing: 6rpx;
  292. }
  293. .slogan {
  294. display: block;
  295. font-size: 24rpx;
  296. color: #9A3412;
  297. margin-top: 8rpx;
  298. }
  299. /* ===== 主动健康理念 + 闭环 ===== */
  300. .concept-card {
  301. background: #fff;
  302. border-radius: 20rpx;
  303. padding: 26rpx 28rpx;
  304. margin-bottom: 24rpx;
  305. box-shadow: 0 4rpx 16rpx rgba(249,115,22,0.08);
  306. }
  307. .concept-desc {
  308. font-size: 24rpx;
  309. color: #4B5563;
  310. line-height: 1.6;
  311. margin-bottom: 20rpx;
  312. }
  313. .concept-highlight {
  314. color: #B45309;
  315. font-weight: 600;
  316. }
  317. .loop-row {
  318. display: flex;
  319. align-items: center;
  320. gap: 6rpx;
  321. }
  322. .loop-pill {
  323. flex: 1;
  324. display: flex;
  325. align-items: center;
  326. justify-content: center;
  327. gap: 4rpx;
  328. flex-direction: column;
  329. font-size: 20rpx;
  330. padding: 10rpx 4rpx;
  331. border-radius: 12rpx;
  332. line-height: 1.3;
  333. }
  334. .loop-pill-text {
  335. font-size: 20rpx;
  336. font-weight: 500;
  337. }
  338. .loop-pill-indigo { background: #EEF2FF; color: #4F46E5; }
  339. .loop-pill-green { background: #D1FAE5; color: #047857; }
  340. .loop-pill-amber { background: #FEF3C7; color: #B45309; }
  341. .loop-pill-red { background: #FEE2E2; color: #B91C1C; }
  342. .loop-arrow {
  343. font-size: 24rpx;
  344. color: #D1D5DB;
  345. flex-shrink: 0;
  346. }
  347. /* ===== 五维标签 ===== */
  348. .dims-row {
  349. display: flex;
  350. justify-content: center;
  351. gap: 8rpx;
  352. margin-bottom: 24rpx;
  353. flex-wrap: wrap;
  354. }
  355. .dim-tag {
  356. display: flex;
  357. align-items: center;
  358. gap: 4rpx;
  359. font-size: 20rpx;
  360. color: #fff;
  361. font-weight: 500;
  362. padding: 8rpx 14rpx;
  363. border-radius: 24rpx;
  364. white-space: nowrap;
  365. }
  366. .dim-tag-body { background: #F97316; }
  367. .dim-tag-wisdom { background: #6366F1; }
  368. .dim-tag-wealth { background: #D97706; }
  369. .dim-tag-action { background: #10B981; }
  370. .dim-tag-mind { background: #EC4899; }
  371. /* ===== 登录表单 ===== */
  372. .login-form {
  373. background: #fff;
  374. border-radius: 20rpx;
  375. padding: 36rpx 36rpx 28rpx;
  376. margin-top: auto;
  377. box-shadow: 0 4rpx 24rpx rgba(0,0,0,0.06);
  378. }
  379. .phone-section {
  380. margin-bottom: 24rpx;
  381. }
  382. .phone-btn {
  383. display: flex;
  384. align-items: center;
  385. justify-content: center;
  386. gap: 12rpx;
  387. width: 100%;
  388. height: 88rpx;
  389. background: #07c160;
  390. color: #fff;
  391. border-radius: 44rpx;
  392. font-size: 32rpx;
  393. font-weight: 500;
  394. border: none;
  395. }
  396. .phone-btn::after {
  397. border: none;
  398. }
  399. .phone-btn-icon {
  400. font-size: 36rpx;
  401. }
  402. /* ===== 跳过登录 ===== */
  403. .skip-section {
  404. text-align: center;
  405. margin-bottom: 20rpx;
  406. }
  407. .skip-btn {
  408. font-size: 26rpx;
  409. color: #F97316;
  410. font-weight: 500;
  411. padding: 10rpx 20rpx;
  412. background: rgba(249, 115, 22, 0.08);
  413. border-radius: 30rpx;
  414. }
  415. /* ===== 协议 ===== */
  416. .agreement {
  417. margin-top: 4rpx;
  418. text-align: center;
  419. font-size: 22rpx;
  420. line-height: 1.6;
  421. }
  422. .agreement checkbox {
  423. transform: scale(0.8);
  424. }
  425. .agreement-text {
  426. color: #9CA3AF;
  427. }
  428. .link {
  429. color: #5B9BD5;
  430. text-decoration: underline;
  431. }
  432. </style>