login.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. <template>
  2. <view class="login-container">
  3. <!-- Logo 区 -->
  4. <view class="logo-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. <text class="powered">亿图腾科技出品</text>
  9. </view>
  10. <!-- 角色价值说明 -->
  11. <view class="value-section">
  12. <view class="value-title">你能得到什么</view>
  13. <!-- 家长端 -->
  14. <view class="role-card role-parent">
  15. <view class="role-badge">
  16. <text class="role-badge-icon">👨‍👩‍👧</text>
  17. <text class="role-badge-text">家长</text>
  18. </view>
  19. <view class="role-items">
  20. <view class="role-item"><text class="role-item-icon">📋</text><text class="role-item-text">一键创建家庭成长计划</text></view>
  21. <view class="role-item"><text class="role-item-icon">📊</text><text class="role-item-text">跟踪孩子任务完成情况</text></view>
  22. <view class="role-item"><text class="role-item-icon">🧠</text><text class="role-item-text">预约专业心智测评</text></view>
  23. <view class="role-item"><text class="role-item-icon">🩺</text><text class="role-item-text">健康报告 AI 智能解析</text></view>
  24. </view>
  25. </view>
  26. <!-- 孩子端 -->
  27. <view class="role-card role-child">
  28. <view class="role-badge role-badge-child">
  29. <text class="role-badge-icon">👶</text>
  30. <text class="role-badge-text">孩子</text>
  31. </view>
  32. <view class="role-items">
  33. <view class="role-item"><text class="role-item-icon">🎯</text><text class="role-item-text">每日趣味任务挑战</text></view>
  34. <view class="role-item"><text class="role-item-icon">🏆</text><text class="role-item-text">积分兑换心愿奖励</text></view>
  35. <view class="role-item"><text class="role-item-icon">🧩</text><text class="role-item-text">专注力小游戏训练</text></view>
  36. </view>
  37. </view>
  38. </view>
  39. <!-- 五维能量 -->
  40. <view class="wuxing-section">
  41. <view class="wuxing-title">五维成长体系</view>
  42. <view class="wuxing-tags">
  43. <view class="wuxing-tag wuxing-body"><text class="wuxing-emoji">🌏</text>身·健康</view>
  44. <view class="wuxing-tag wuxing-mind"><text class="wuxing-emoji">🔥</text>心·情绪</view>
  45. <view class="wuxing-tag wuxing-wisdom"><text class="wuxing-emoji">⚡</text>智·赋能</view>
  46. <view class="wuxing-tag wuxing-action"><text class="wuxing-emoji">🌿</text>行·习惯</view>
  47. <view class="wuxing-tag wuxing-wealth"><text class="wuxing-emoji">💧</text>富·财商</view>
  48. </view>
  49. <text class="wuxing-desc">五行平衡 · 全面评估全家健康状态</text>
  50. </view>
  51. <!-- 登录表单 -->
  52. <view class="login-form">
  53. <view class="phone-section">
  54. <button
  55. class="phone-btn"
  56. open-type="getPhoneNumber"
  57. @getphonenumber="getPhoneNumber"
  58. >
  59. <text class="phone-btn-icon">📱</text>
  60. <text>手机号快捷登录</text>
  61. </button>
  62. </view>
  63. <!-- 暂不登录 -->
  64. <view class="skip-section">
  65. <text class="skip-btn" @click="handleSkipLogin">暂不登录,先逛逛</text>
  66. </view>
  67. <view class="agreement">
  68. <checkbox-group @change="onAgreementChange">
  69. <label>
  70. <checkbox value="agree" :checked="agreed" />
  71. <text class="agreement-text">我已阅读并同意</text>
  72. </label>
  73. </checkbox-group>
  74. <text class="link" @click="showAgreement">《用户协议》</text>
  75. <text class="agreement-text">和</text>
  76. <text class="link" @click="showPrivacy">《隐私政策》</text>
  77. </view>
  78. </view>
  79. </view>
  80. </template>
  81. <script>
  82. import { wechatPhoneLogin, checkTeamInviteStatus, joinTeamByInvite, bindReferral } from '../../utils/api.js'
  83. export default {
  84. data() {
  85. return {
  86. loading: false,
  87. agreed: false,
  88. nickname: '',
  89. avatar: '',
  90. redirectUrl: ''
  91. }
  92. },
  93. onLoad(options) {
  94. // 处理邀请码
  95. if (options.inviteCode) {
  96. uni.setStorageSync('inviteCode', options.inviteCode)
  97. uni.setStorageSync('inviteType', options.inviteType || 'family')
  98. }
  99. // 存储登录后跳转地址
  100. if (options.redirect) {
  101. this.redirectUrl = decodeURIComponent(options.redirect)
  102. }
  103. // 注意:不在 onLoad 预取 login code —— 微信 code 一次性有效(5分钟且用过即废),
  104. // 预取后如果用户隔几分钟再点击授权,code 已失效会报 invalid code (40029)。
  105. // 正确做法:getPhoneNumber 中用户点击授权时实时获取。
  106. },
  107. methods: {
  108. handleImageError(e) {
  109. console.log('Logo 加载失败,使用默认显示')
  110. },
  111. onAgreementChange(e) {
  112. this.agreed = e.detail.value.length > 0
  113. },
  114. showAgreement() {
  115. uni.navigateTo({
  116. url: '/pages/policy/agreement'
  117. })
  118. },
  119. showPrivacy() {
  120. uni.navigateTo({
  121. url: '/pages/policy/privacy'
  122. })
  123. },
  124. handleSkipLogin() {
  125. // 不登录直接进入首页
  126. uni.switchTab({ url: '/pages/index-home/index' })
  127. },
  128. getPhoneNumber: function(e) {
  129. if (!this.agreed) {
  130. uni.showToast({ title: '请先同意用户协议', icon: 'none' })
  131. return
  132. }
  133. if (!e.detail || !e.detail.code) {
  134. uni.showToast({ title: '授权失败,请重试', icon: 'none' })
  135. return
  136. }
  137. var self = this
  138. self.loading = true
  139. // 点击授权时实时获取 login code(微信 code 一次性有效,不能复用旧 code)
  140. uni.login({
  141. provider: 'weixin',
  142. success: function(loginRes) {
  143. if (!loginRes.code) {
  144. uni.showToast({ title: '网络异常,请重试', icon: 'none' })
  145. self.loading = false
  146. return
  147. }
  148. wechatPhoneLogin({ code: loginRes.code, phoneCode: e.detail.code }).then(function(res) {
  149. self.completeLogin(res.data)
  150. }).catch(function(err) {
  151. uni.showToast({ title: err.message || '登录失败', icon: 'none' })
  152. self.loading = false
  153. })
  154. },
  155. fail: function() {
  156. uni.showToast({ title: '网络异常,请重试', icon: 'none' })
  157. self.loading = false
  158. }
  159. })
  160. },
  161. completeLogin: function(data) {
  162. var self = this
  163. uni.setStorageSync('token', data.token)
  164. uni.setStorageSync('userId', data.userId)
  165. uni.setStorageSync('role', data.role)
  166. uni.setStorageSync('currentRole', data.role)
  167. uni.setStorageSync('isSwitchedChild', false)
  168. uni.setStorageSync('isSwitchedTeacher', false)
  169. if (data.openid) uni.setStorageSync('openid', data.openid)
  170. // familyId 必须持久化:家庭在注册时已由后端创建,缺失会导致页面误判无家庭而重复创建
  171. uni.setStorageSync('userInfo', { nickname: data.nickname, userId: data.userId, familyId: data.familyId })
  172. if (data.phone) uni.setStorageSync('phone', data.phone)
  173. if (data.teacherStatus) uni.setStorageSync('teacherStatus', data.teacherStatus)
  174. if (data.teacherRejectReason) uni.setStorageSync('teacherRejectReason', data.teacherRejectReason)
  175. self.$store.commit('setFamilyId', data.familyId)
  176. uni.showToast({ title: '登录成功', icon: 'success' })
  177. self.loading = false
  178. if (data.needsRoleSelect) {
  179. uni.redirectTo({ url: '/pages/user-edit/user-edit?token=' + data.token + '&userId=' + data.userId + '&autoParent=true' })
  180. } else {
  181. self.handlePostLoginRouting()
  182. }
  183. },
  184. handlePostLoginRouting() {
  185. const inviteCode = uni.getStorageSync('inviteCode')
  186. const inviteType = uni.getStorageSync('inviteType')
  187. if (inviteCode && inviteType === 'team') {
  188. this.handleTeamInviteFlow(inviteCode)
  189. } else if (inviteCode) {
  190. // 非 team 邀请码(如分享文章/商品/活动的邀请码),绑定推荐关系
  191. this.handleReferralBind(inviteCode)
  192. } else {
  193. this.navigateToHome()
  194. }
  195. },
  196. async handleReferralBind(inviteCode) {
  197. try {
  198. var res = await bindReferral(inviteCode)
  199. if (res && res.code === 200) {
  200. console.log('登录后绑定邀请码成功', inviteCode)
  201. } else if (res && res.code === 400) {
  202. console.log('用户已有邀请人,跳过绑定')
  203. }
  204. } catch (e) {
  205. console.log('邀请码绑定失败', e)
  206. }
  207. uni.removeStorageSync('inviteCode')
  208. uni.removeStorageSync('inviteType')
  209. this.navigateToHome()
  210. },
  211. async handleTeamInviteFlow(inviteCode) {
  212. try {
  213. const res = await checkTeamInviteStatus(inviteCode)
  214. const status = res.data.status
  215. if (status === 'not_planner') {
  216. uni.redirectTo({ url: `/pages/guide/register/index?inviteCode=${inviteCode}` })
  217. } else if (status === 'not_in_team') {
  218. uni.showModal({
  219. title: '加入团队',
  220. content: '是否加入该规划师团队?',
  221. success: async (confirmRes) => {
  222. if (confirmRes.confirm) {
  223. try {
  224. await joinTeamByInvite(inviteCode)
  225. uni.showToast({ title: '加入成功', icon: 'success' })
  226. uni.removeStorageSync('inviteCode')
  227. uni.removeStorageSync('inviteType')
  228. this.navigateToHome()
  229. } catch (e) {
  230. uni.showToast({ title: e.message || '加入失败', icon: 'none' })
  231. }
  232. } else {
  233. this.navigateToHome()
  234. }
  235. }
  236. })
  237. } else if (status === 'already_in_team') {
  238. uni.showModal({
  239. title: '加入新团队',
  240. content: '您已在其他团队中,离开当前团队并加入新团队?',
  241. success: async (confirmRes) => {
  242. if (confirmRes.confirm) {
  243. try {
  244. await joinTeamByInvite(inviteCode)
  245. uni.showToast({ title: '加入成功', icon: 'success' })
  246. uni.removeStorageSync('inviteCode')
  247. uni.removeStorageSync('inviteType')
  248. this.navigateToHome()
  249. } catch (e) {
  250. uni.showToast({ title: e.message || '加入失败', icon: 'none' })
  251. }
  252. } else {
  253. this.navigateToHome()
  254. }
  255. }
  256. })
  257. } else {
  258. this.navigateToHome()
  259. }
  260. } catch (e) {
  261. uni.showToast({ title: e.message || '处理邀请失败', icon: 'none' })
  262. this.navigateToHome()
  263. }
  264. },
  265. navigateToHome() {
  266. if (this.redirectUrl) {
  267. const redirect = this.redirectUrl
  268. this.redirectUrl = ''
  269. // 注意: redirectTo 不支持跳转 tabBar 页面,必须用 switchTab
  270. // 只有 pages.json tabBar.list 注册的页面才能用 switchTab
  271. var tabBarPages = ['/pages/index-home/index', '/pages/mind/index', '/pages/body/index', '/pages/wisdom/index', '/pages/wealth/index']
  272. if (tabBarPages.indexOf(redirect) !== -1) {
  273. uni.switchTab({ url: redirect })
  274. } else {
  275. uni.redirectTo({ url: redirect })
  276. }
  277. } else {
  278. uni.switchTab({ url: '/pages/index-home/index' })
  279. }
  280. }
  281. }
  282. }
  283. </script>
  284. <style scoped>
  285. .login-container {
  286. min-height: 100vh;
  287. background: linear-gradient(160deg, #FFF7ED 0%, #FED7AA 60%, #F97316 100%);
  288. padding: 48rpx 40rpx 40rpx;
  289. }
  290. /* ===== Logo 区 ===== */
  291. .logo-section {
  292. text-align: center;
  293. margin-bottom: 40rpx;
  294. }
  295. .logo {
  296. width: 120rpx;
  297. height: 120rpx;
  298. border-radius: 60rpx;
  299. background: #fff;
  300. box-shadow: 0 8rpx 32rpx rgba(249,115,22,0.2);
  301. }
  302. .app-name {
  303. display: block;
  304. font-size: 52rpx;
  305. font-weight: bold;
  306. color: #fff;
  307. margin-top: 16rpx;
  308. letter-spacing: 8rpx;
  309. }
  310. .slogan {
  311. display: block;
  312. font-size: 26rpx;
  313. color: rgba(255, 255, 255, 0.85);
  314. margin-top: 8rpx;
  315. }
  316. .powered {
  317. display: block;
  318. font-size: 20rpx;
  319. color: rgba(255, 255, 255, 0.55);
  320. margin-top: 12rpx;
  321. }
  322. /* ===== 角色价值说明 ===== */
  323. .value-section {
  324. margin-bottom: 32rpx;
  325. }
  326. .value-title {
  327. font-size: 28rpx;
  328. font-weight: 600;
  329. color: #fff;
  330. margin-bottom: 20rpx;
  331. padding-left: 4rpx;
  332. }
  333. .role-card {
  334. background: rgba(255, 255, 255, 0.92);
  335. border-radius: 20rpx;
  336. padding: 24rpx 28rpx;
  337. margin-bottom: 16rpx;
  338. box-shadow: 0 4rpx 20rpx rgba(0,0,0,0.08);
  339. }
  340. .role-badge {
  341. display: inline-flex;
  342. align-items: center;
  343. gap: 8rpx;
  344. background: linear-gradient(135deg, #FF8C42, #FF6B2D);
  345. border-radius: 30rpx;
  346. padding: 8rpx 20rpx;
  347. margin-bottom: 20rpx;
  348. }
  349. .role-badge-child {
  350. background: linear-gradient(135deg, #FF6B9D, #FF3D7F);
  351. }
  352. .role-badge-icon {
  353. font-size: 28rpx;
  354. }
  355. .role-badge-text {
  356. font-size: 26rpx;
  357. font-weight: 600;
  358. color: #fff;
  359. }
  360. .role-items {
  361. display: flex;
  362. flex-direction: column;
  363. gap: 14rpx;
  364. }
  365. .role-item {
  366. display: flex;
  367. align-items: center;
  368. gap: 12rpx;
  369. }
  370. .role-item-icon {
  371. font-size: 28rpx;
  372. flex-shrink: 0;
  373. }
  374. .role-item-text {
  375. font-size: 26rpx;
  376. color: #374151;
  377. line-height: 1.5;
  378. }
  379. /* ===== 五维能量 ===== */
  380. .wuxing-section {
  381. background: rgba(255, 255, 255, 0.92);
  382. border-radius: 20rpx;
  383. padding: 28rpx;
  384. margin-bottom: 32rpx;
  385. text-align: center;
  386. box-shadow: 0 4rpx 20rpx rgba(0,0,0,0.08);
  387. }
  388. .wuxing-title {
  389. font-size: 28rpx;
  390. font-weight: 600;
  391. color: #1F2937;
  392. margin-bottom: 20rpx;
  393. }
  394. .wuxing-tags {
  395. display: flex;
  396. flex-wrap: wrap;
  397. justify-content: center;
  398. gap: 12rpx;
  399. margin-bottom: 16rpx;
  400. }
  401. .wuxing-tag {
  402. font-size: 24rpx;
  403. padding: 10rpx 20rpx;
  404. border-radius: 30rpx;
  405. color: #fff;
  406. font-weight: 500;
  407. }
  408. .wuxing-body { background: rgba(255, 140, 66, 0.85); }
  409. .wuxing-mind { background: rgba(255, 107, 157, 0.85); }
  410. .wuxing-wisdom { background: rgba(99, 102, 241, 0.85); }
  411. .wuxing-action { background: rgba(16, 185, 129, 0.85); }
  412. .wuxing-wealth { background: rgba(245, 158, 11, 0.85); }
  413. .wuxing-emoji {
  414. margin-right: 4rpx;
  415. }
  416. .wuxing-desc {
  417. font-size: 22rpx;
  418. color: #9CA3AF;
  419. }
  420. /* ===== 登录表单 ===== */
  421. .login-form {
  422. background: #fff;
  423. border-radius: 20rpx;
  424. padding: 40rpx;
  425. box-shadow: 0 4rpx 24rpx rgba(0,0,0,0.06);
  426. }
  427. .phone-section {
  428. margin-bottom: 28rpx;
  429. }
  430. .phone-btn {
  431. display: flex;
  432. align-items: center;
  433. justify-content: center;
  434. gap: 12rpx;
  435. width: 100%;
  436. height: 88rpx;
  437. background: #07c160;
  438. color: #fff;
  439. border-radius: 44rpx;
  440. font-size: 32rpx;
  441. font-weight: 500;
  442. border: none;
  443. }
  444. .phone-btn::after {
  445. border: none;
  446. }
  447. .phone-btn-icon {
  448. font-size: 36rpx;
  449. }
  450. /* ===== 跳过登录 ===== */
  451. .skip-section {
  452. text-align: center;
  453. margin-bottom: 24rpx;
  454. }
  455. .skip-btn {
  456. font-size: 28rpx;
  457. color: #F97316;
  458. font-weight: 500;
  459. padding: 12rpx 24rpx;
  460. background: rgba(249, 115, 22, 0.08);
  461. border-radius: 30rpx;
  462. }
  463. /* ===== 协议 ===== */
  464. .agreement {
  465. margin-top: 8rpx;
  466. text-align: center;
  467. font-size: 24rpx;
  468. line-height: 1.6;
  469. }
  470. .agreement checkbox {
  471. transform: scale(0.8);
  472. }
  473. .agreement-text {
  474. color: #9CA3AF;
  475. }
  476. .link {
  477. color: #5B9BD5;
  478. text-decoration: underline;
  479. }
  480. </style>