login.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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">Care Family · 家庭健康成长俱乐部</text>
  8. </view>
  9. <!-- 平台简介 -->
  10. <view class="intro-section">
  11. <view class="intro-card">
  12. <view class="intro-title">🌏 给全家的一站式幸福提案</view>
  13. <view class="intro-desc">
  14. 围绕<span class="highlight">身、心、智、行、富</span>五个维度,用科技、智慧与温情,为您定制全生命周期解决方案。
  15. </view>
  16. <view class="dimension-tags">
  17. <view class="dim-tag dim-body">身 · 主动健康</view>
  18. <view class="dim-tag dim-mind">心 · 情绪丰盈</view>
  19. <view class="dim-tag dim-wisdom">智 · 赋能未来</view>
  20. <view class="dim-tag dim-action">行 · 知行合一</view>
  21. <view class="dim-tag dim-wealth">富 · 物质精神</view>
  22. </view>
  23. <view class="intro-powered">亿图腾科技出品</view>
  24. </view>
  25. </view>
  26. <!-- 手机号快捷登录 -->
  27. <view class="login-form">
  28. <view class="phone-section">
  29. <button
  30. class="phone-btn"
  31. open-type="getPhoneNumber"
  32. @getphonenumber="getPhoneNumber"
  33. >
  34. <text>手机号快捷登录</text>
  35. </button>
  36. </view>
  37. <!-- 暂不登录 -->
  38. <view class="skip-section">
  39. <text class="skip-btn" @click="handleSkipLogin">暂不登录,先逛逛</text>
  40. </view>
  41. <view class="agreement">
  42. <checkbox-group @change="onAgreementChange">
  43. <label>
  44. <checkbox value="agree" :checked="agreed" />
  45. <text class="agreement-text">我已阅读并同意</text>
  46. </label>
  47. </checkbox-group>
  48. <text class="link" @click="showAgreement">《用户协议》</text>
  49. <text class="agreement-text">和</text>
  50. <text class="link" @click="showPrivacy">《隐私政策》</text>
  51. </view>
  52. </view>
  53. </view>
  54. </template>
  55. <script>
  56. import { wechatPhoneLogin, checkTeamInviteStatus, joinTeamByInvite, bindReferral } from '../../utils/api.js'
  57. export default {
  58. data() {
  59. return {
  60. loading: false,
  61. agreed: false,
  62. nickname: '',
  63. avatar: '',
  64. redirectUrl: ''
  65. }
  66. },
  67. onLoad(options) {
  68. // 处理邀请码
  69. if (options.inviteCode) {
  70. uni.setStorageSync('inviteCode', options.inviteCode)
  71. uni.setStorageSync('inviteType', options.inviteType || 'family')
  72. }
  73. // 存储登录后跳转地址
  74. if (options.redirect) {
  75. this.redirectUrl = decodeURIComponent(options.redirect)
  76. }
  77. // 注意:不在 onLoad 预取 login code —— 微信 code 一次性有效(5分钟且用过即废),
  78. // 预取后如果用户隔几分钟再点击授权,code 已失效会报 invalid code (40029)。
  79. // 正确做法:getPhoneNumber 中用户点击授权时实时获取。
  80. },
  81. methods: {
  82. handleImageError(e) {
  83. console.log('Logo 加载失败,使用默认显示')
  84. },
  85. onAgreementChange(e) {
  86. this.agreed = e.detail.value.length > 0
  87. },
  88. showAgreement() {
  89. uni.navigateTo({
  90. url: '/pages/policy/agreement'
  91. })
  92. },
  93. showPrivacy() {
  94. uni.navigateTo({
  95. url: '/pages/policy/privacy'
  96. })
  97. },
  98. handleSkipLogin() {
  99. // 不登录直接进入首页
  100. uni.switchTab({ url: '/pages/index-home/index' })
  101. },
  102. getPhoneNumber: function(e) {
  103. if (!this.agreed) {
  104. uni.showToast({ title: '请先同意用户协议', icon: 'none' })
  105. return
  106. }
  107. if (!e.detail || !e.detail.code) {
  108. uni.showToast({ title: '授权失败,请重试', icon: 'none' })
  109. return
  110. }
  111. var self = this
  112. self.loading = true
  113. // 点击授权时实时获取 login code(微信 code 一次性有效,不能复用旧 code)
  114. uni.login({
  115. provider: 'weixin',
  116. success: function(loginRes) {
  117. if (!loginRes.code) {
  118. uni.showToast({ title: '网络异常,请重试', icon: 'none' })
  119. self.loading = false
  120. return
  121. }
  122. wechatPhoneLogin({ code: loginRes.code, phoneCode: e.detail.code }).then(function(res) {
  123. self.completeLogin(res.data)
  124. }).catch(function(err) {
  125. uni.showToast({ title: err.message || '登录失败', icon: 'none' })
  126. self.loading = false
  127. })
  128. },
  129. fail: function() {
  130. uni.showToast({ title: '网络异常,请重试', icon: 'none' })
  131. self.loading = false
  132. }
  133. })
  134. },
  135. completeLogin: function(data) {
  136. var self = this
  137. uni.setStorageSync('token', data.token)
  138. uni.setStorageSync('userId', data.userId)
  139. uni.setStorageSync('role', data.role)
  140. uni.setStorageSync('currentRole', data.role)
  141. uni.setStorageSync('isSwitchedChild', false)
  142. uni.setStorageSync('isSwitchedTeacher', false)
  143. if (data.openid) uni.setStorageSync('openid', data.openid)
  144. // familyId 必须持久化:家庭在注册时已由后端创建,缺失会导致页面误判无家庭而重复创建
  145. uni.setStorageSync('userInfo', { nickname: data.nickname, userId: data.userId, familyId: data.familyId })
  146. if (data.phone) uni.setStorageSync('phone', data.phone)
  147. if (data.teacherStatus) uni.setStorageSync('teacherStatus', data.teacherStatus)
  148. if (data.teacherRejectReason) uni.setStorageSync('teacherRejectReason', data.teacherRejectReason)
  149. self.$store.commit('setFamilyId', data.familyId)
  150. uni.showToast({ title: '登录成功', icon: 'success' })
  151. self.loading = false
  152. if (data.needsRoleSelect) {
  153. uni.redirectTo({ url: '/pages/user-edit/user-edit?token=' + data.token + '&userId=' + data.userId + '&autoParent=true' })
  154. } else {
  155. self.handlePostLoginRouting()
  156. }
  157. },
  158. handlePostLoginRouting() {
  159. const inviteCode = uni.getStorageSync('inviteCode')
  160. const inviteType = uni.getStorageSync('inviteType')
  161. if (inviteCode && inviteType === 'team') {
  162. this.handleTeamInviteFlow(inviteCode)
  163. } else if (inviteCode) {
  164. // 非 team 邀请码(如分享文章/商品/活动的邀请码),绑定推荐关系
  165. this.handleReferralBind(inviteCode)
  166. } else {
  167. this.navigateToHome()
  168. }
  169. },
  170. async handleReferralBind(inviteCode) {
  171. try {
  172. var res = await bindReferral(inviteCode)
  173. if (res && res.code === 200) {
  174. console.log('登录后绑定邀请码成功', inviteCode)
  175. } else if (res && res.code === 400) {
  176. console.log('用户已有邀请人,跳过绑定')
  177. }
  178. } catch (e) {
  179. console.log('邀请码绑定失败', e)
  180. }
  181. uni.removeStorageSync('inviteCode')
  182. uni.removeStorageSync('inviteType')
  183. this.navigateToHome()
  184. },
  185. async handleTeamInviteFlow(inviteCode) {
  186. try {
  187. const res = await checkTeamInviteStatus(inviteCode)
  188. const status = res.data.status
  189. if (status === 'not_planner') {
  190. uni.redirectTo({ url: `/pages/guide/register/index?inviteCode=${inviteCode}` })
  191. } else if (status === 'not_in_team') {
  192. uni.showModal({
  193. title: '加入团队',
  194. content: '是否加入该规划师团队?',
  195. success: async (confirmRes) => {
  196. if (confirmRes.confirm) {
  197. try {
  198. await joinTeamByInvite(inviteCode)
  199. uni.showToast({ title: '加入成功', icon: 'success' })
  200. uni.removeStorageSync('inviteCode')
  201. uni.removeStorageSync('inviteType')
  202. this.navigateToHome()
  203. } catch (e) {
  204. uni.showToast({ title: e.message || '加入失败', icon: 'none' })
  205. }
  206. } else {
  207. this.navigateToHome()
  208. }
  209. }
  210. })
  211. } else if (status === 'already_in_team') {
  212. uni.showModal({
  213. title: '加入新团队',
  214. content: '您已在其他团队中,离开当前团队并加入新团队?',
  215. success: async (confirmRes) => {
  216. if (confirmRes.confirm) {
  217. try {
  218. await joinTeamByInvite(inviteCode)
  219. uni.showToast({ title: '加入成功', icon: 'success' })
  220. uni.removeStorageSync('inviteCode')
  221. uni.removeStorageSync('inviteType')
  222. this.navigateToHome()
  223. } catch (e) {
  224. uni.showToast({ title: e.message || '加入失败', icon: 'none' })
  225. }
  226. } else {
  227. this.navigateToHome()
  228. }
  229. }
  230. })
  231. } else {
  232. this.navigateToHome()
  233. }
  234. } catch (e) {
  235. uni.showToast({ title: e.message || '处理邀请失败', icon: 'none' })
  236. this.navigateToHome()
  237. }
  238. },
  239. navigateToHome() {
  240. if (this.redirectUrl) {
  241. const redirect = this.redirectUrl
  242. this.redirectUrl = ''
  243. // 注意: redirectTo 不支持跳转 tabBar 页面,必须用 switchTab
  244. // 只有 pages.json tabBar.list 注册的页面才能用 switchTab
  245. var tabBarPages = ['/pages/index-home/index', '/pages/mind/index', '/pages/body/index', '/pages/wisdom/index', '/pages/wealth/index']
  246. if (tabBarPages.indexOf(redirect) !== -1) {
  247. uni.switchTab({ url: redirect })
  248. } else {
  249. uni.redirectTo({ url: redirect })
  250. }
  251. } else {
  252. uni.switchTab({ url: '/pages/index-home/index' })
  253. }
  254. }
  255. }
  256. }
  257. </script>
  258. <style scoped>
  259. .login-container {
  260. min-height: 100vh;
  261. background: linear-gradient(160deg, #FFF7ED 0%, #F97316 100%);
  262. padding: 60rpx 40rpx;
  263. }
  264. /* ===== Logo 区 ===== */
  265. .logo-section {
  266. text-align: center;
  267. margin-bottom: 48rpx;
  268. }
  269. .logo {
  270. width: 160rpx;
  271. height: 160rpx;
  272. border-radius: 80rpx;
  273. background: #fff;
  274. }
  275. .app-name {
  276. display: block;
  277. font-size: 52rpx;
  278. font-weight: bold;
  279. color: #fff;
  280. margin-top: 20rpx;
  281. }
  282. .slogan {
  283. display: block;
  284. font-size: 26rpx;
  285. color: rgba(255, 255, 255, 0.8);
  286. margin-top: 10rpx;
  287. }
  288. /* ===== 平台简介卡片 ===== */
  289. .intro-section {
  290. margin-bottom: 48rpx;
  291. }
  292. .intro-card {
  293. background: rgba(255, 255, 255, 0.15);
  294. border: 1rpx solid rgba(255, 255, 255, 0.3);
  295. border-radius: 20rpx;
  296. padding: 32rpx 28rpx;
  297. backdrop-filter: blur(10px);
  298. }
  299. .intro-title {
  300. font-size: 30rpx;
  301. font-weight: bold;
  302. color: #fff;
  303. text-align: center;
  304. margin-bottom: 16rpx;
  305. }
  306. .intro-desc {
  307. font-size: 24rpx;
  308. color: rgba(255, 255, 255, 0.9);
  309. line-height: 1.7;
  310. text-align: center;
  311. margin-bottom: 24rpx;
  312. }
  313. .intro-desc .highlight {
  314. color: #FFD700;
  315. font-weight: 600;
  316. }
  317. .dimension-tags {
  318. display: flex;
  319. flex-wrap: wrap;
  320. justify-content: center;
  321. gap: 12rpx;
  322. margin-bottom: 20rpx;
  323. }
  324. .dim-tag {
  325. font-size: 22rpx;
  326. padding: 8rpx 16rpx;
  327. border-radius: 30rpx;
  328. background: rgba(255, 255, 255, 0.2);
  329. color: #fff;
  330. font-weight: 500;
  331. }
  332. .dim-body { background: rgba(91, 155, 213, 0.4); }
  333. .dim-mind { background: rgba(236, 72, 153, 0.4); }
  334. .dim-wisdom { background: rgba(139, 92, 246, 0.4); }
  335. .dim-action { background: rgba(16, 185, 129, 0.4); }
  336. .dim-wealth { background: rgba(245, 158, 11, 0.4); }
  337. .intro-powered {
  338. text-align: center;
  339. font-size: 20rpx;
  340. color: rgba(255, 255, 255, 0.6);
  341. }
  342. /* ===== 登录表单 ===== */
  343. .login-form {
  344. background: #fff;
  345. border-radius: 20rpx;
  346. padding: 40rpx;
  347. }
  348. .phone-section {
  349. margin-bottom: 32rpx;
  350. }
  351. .phone-btn {
  352. display: flex;
  353. align-items: center;
  354. justify-content: center;
  355. gap: 10rpx;
  356. width: 100%;
  357. height: 88rpx;
  358. background: #07c160;
  359. color: #fff;
  360. border-radius: 44rpx;
  361. font-size: 32rpx;
  362. }
  363. /* ===== 跳过登录 ===== */
  364. .skip-section {
  365. text-align: center;
  366. margin-bottom: 20rpx;
  367. }
  368. .skip-btn {
  369. font-size: 28rpx;
  370. color: #999;
  371. text-decoration: underline;
  372. padding: 16rpx 0;
  373. }
  374. .agreement {
  375. margin-top: 16rpx;
  376. text-align: center;
  377. font-size: 24rpx;
  378. }
  379. .agreement-text {
  380. color: #666;
  381. }
  382. .link {
  383. color: #667eea;
  384. }
  385. .agreement-text.link {
  386. text-decoration: underline;
  387. }
  388. </style>