login.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. :loading="loading"
  32. @click="handleWechatLogin"
  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 { wechatLogin, 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. },
  78. methods: {
  79. handleImageError(e) {
  80. console.log('Logo 加载失败,使用默认显示')
  81. },
  82. onAgreementChange(e) {
  83. this.agreed = e.detail.value.length > 0
  84. },
  85. showAgreement() {
  86. uni.navigateTo({
  87. url: '/pages/policy/agreement'
  88. })
  89. },
  90. showPrivacy() {
  91. uni.navigateTo({
  92. url: '/pages/policy/privacy'
  93. })
  94. },
  95. handleSkipLogin() {
  96. // 不登录直接进入首页
  97. uni.switchTab({ url: '/pages/index-home/index' })
  98. },
  99. handleWechatLogin() {
  100. if (!this.agreed) {
  101. uni.showToast({ title: '请先同意用户协议', icon: 'none' })
  102. return
  103. }
  104. this.loading = true
  105. var self = this
  106. // 超时保护:10秒强制结束登录流程,避免 uni.login 挂死时按钮 loading 卡死
  107. var loginTimeout = new Promise(function(_, reject) {
  108. setTimeout(function() {
  109. reject(new Error('微信登录超时'))
  110. }, 10000)
  111. })
  112. Promise.race([loginTimeout, new Promise(function(resolve, reject) {
  113. uni.login({
  114. provider: 'weixin',
  115. success: function(loginRes) { resolve(loginRes) },
  116. fail: function() { reject(new Error('微信登录失败')) }
  117. })
  118. })]).then(function(loginRes) {
  119. // 静默登录:仅获取 openid,不强制索取手机号
  120. wechatLogin(loginRes.code, {
  121. nickname: self.nickname || '用户',
  122. avatar: self.avatar || ''
  123. }, '').then(function(res) {
  124. // 保存 token 和用户信息(只写一次)
  125. uni.setStorageSync('token', res.data.token)
  126. uni.setStorageSync('userId', res.data.userId)
  127. uni.setStorageSync('role', res.data.role)
  128. uni.setStorageSync('currentRole', res.data.role)
  129. uni.setStorageSync('isSwitchedChild', false)
  130. uni.setStorageSync('isSwitchedTeacher', false)
  131. if (res.data.openid) {
  132. uni.setStorageSync('openid', res.data.openid)
  133. }
  134. uni.setStorageSync('userInfo', {
  135. nickname: res.data.nickname,
  136. userId: res.data.userId
  137. })
  138. if (res.data.teacherStatus) {
  139. uni.setStorageSync('teacherStatus', res.data.teacherStatus)
  140. }
  141. if (res.data.teacherRejectReason) {
  142. uni.setStorageSync('teacherRejectReason', res.data.teacherRejectReason)
  143. }
  144. uni.showToast({ title: '登录成功', icon: 'success' })
  145. if (res.data.needsRoleSelect) {
  146. // 首次登录:跳过角色选择,默认 parent,直接进入信息补充页
  147. uni.redirectTo({
  148. url: '/pages/user-edit/user-edit?token=' + res.data.token + '&userId=' + res.data.userId + '&autoParent=true'
  149. })
  150. } else {
  151. self.handlePostLoginRouting()
  152. }
  153. }).catch(function(e) {
  154. uni.showToast({ title: e.message || '登录失败', icon: 'none' })
  155. self.loading = false
  156. })
  157. }).catch(function(e) {
  158. uni.showToast({ title: e.message || '微信登录失败', icon: 'none' })
  159. self.loading = false
  160. })
  161. },
  162. handlePostLoginRouting() {
  163. const inviteCode = uni.getStorageSync('inviteCode')
  164. const inviteType = uni.getStorageSync('inviteType')
  165. if (inviteCode && inviteType === 'team') {
  166. this.handleTeamInviteFlow(inviteCode)
  167. } else if (inviteCode) {
  168. // 非 team 邀请码(如分享文章/商品/活动的邀请码),绑定推荐关系
  169. this.handleReferralBind(inviteCode)
  170. } else {
  171. this.navigateToHome()
  172. }
  173. },
  174. async handleReferralBind(inviteCode) {
  175. try {
  176. var res = await bindReferral(inviteCode)
  177. if (res && res.code === 200) {
  178. console.log('登录后绑定邀请码成功', inviteCode)
  179. } else if (res && res.code === 400) {
  180. console.log('用户已有邀请人,跳过绑定')
  181. }
  182. } catch (e) {
  183. console.log('邀请码绑定失败', e)
  184. }
  185. uni.removeStorageSync('inviteCode')
  186. uni.removeStorageSync('inviteType')
  187. this.navigateToHome()
  188. },
  189. async handleTeamInviteFlow(inviteCode) {
  190. try {
  191. const res = await checkTeamInviteStatus(inviteCode)
  192. const status = res.data.status
  193. if (status === 'not_planner') {
  194. uni.redirectTo({ url: `/pages/guide/register/index?inviteCode=${inviteCode}` })
  195. } else if (status === 'not_in_team') {
  196. uni.showModal({
  197. title: '加入团队',
  198. content: '是否加入该规划师团队?',
  199. success: async (confirmRes) => {
  200. if (confirmRes.confirm) {
  201. try {
  202. await joinTeamByInvite(inviteCode)
  203. uni.showToast({ title: '加入成功', icon: 'success' })
  204. uni.removeStorageSync('inviteCode')
  205. uni.removeStorageSync('inviteType')
  206. this.navigateToHome()
  207. } catch (e) {
  208. uni.showToast({ title: e.message || '加入失败', icon: 'none' })
  209. }
  210. } else {
  211. this.navigateToHome()
  212. }
  213. }
  214. })
  215. } else if (status === 'already_in_team') {
  216. uni.showModal({
  217. title: '加入新团队',
  218. content: '您已在其他团队中,离开当前团队并加入新团队?',
  219. success: async (confirmRes) => {
  220. if (confirmRes.confirm) {
  221. try {
  222. await joinTeamByInvite(inviteCode)
  223. uni.showToast({ title: '加入成功', icon: 'success' })
  224. uni.removeStorageSync('inviteCode')
  225. uni.removeStorageSync('inviteType')
  226. this.navigateToHome()
  227. } catch (e) {
  228. uni.showToast({ title: e.message || '加入失败', icon: 'none' })
  229. }
  230. } else {
  231. this.navigateToHome()
  232. }
  233. }
  234. })
  235. } else {
  236. this.navigateToHome()
  237. }
  238. } catch (e) {
  239. uni.showToast({ title: e.message || '处理邀请失败', icon: 'none' })
  240. this.navigateToHome()
  241. }
  242. },
  243. navigateToHome() {
  244. if (this.redirectUrl) {
  245. const redirect = this.redirectUrl
  246. this.redirectUrl = ''
  247. // 注意: redirectTo 不支持跳转 tabBar 页面,必须用 switchTab
  248. // 只有 pages.json tabBar.list 注册的页面才能用 switchTab
  249. var tabBarPages = ['/pages/index-home/index', '/pages/mind/index', '/pages/body/index', '/pages/wisdom/index', '/pages/wealth/index']
  250. if (tabBarPages.indexOf(redirect) !== -1) {
  251. uni.switchTab({ url: redirect })
  252. } else {
  253. uni.redirectTo({ url: redirect })
  254. }
  255. } else {
  256. uni.switchTab({ url: '/pages/index-home/index' })
  257. }
  258. }
  259. }
  260. }
  261. </script>
  262. <style scoped>
  263. .login-container {
  264. min-height: 100vh;
  265. background: linear-gradient(160deg, #FFF7ED 0%, #F97316 100%);
  266. padding: 60rpx 40rpx;
  267. }
  268. /* ===== Logo 区 ===== */
  269. .logo-section {
  270. text-align: center;
  271. margin-bottom: 48rpx;
  272. }
  273. .logo {
  274. width: 160rpx;
  275. height: 160rpx;
  276. border-radius: 80rpx;
  277. background: #fff;
  278. }
  279. .app-name {
  280. display: block;
  281. font-size: 52rpx;
  282. font-weight: bold;
  283. color: #fff;
  284. margin-top: 20rpx;
  285. }
  286. .slogan {
  287. display: block;
  288. font-size: 26rpx;
  289. color: rgba(255, 255, 255, 0.8);
  290. margin-top: 10rpx;
  291. }
  292. /* ===== 平台简介卡片 ===== */
  293. .intro-section {
  294. margin-bottom: 48rpx;
  295. }
  296. .intro-card {
  297. background: rgba(255, 255, 255, 0.15);
  298. border: 1rpx solid rgba(255, 255, 255, 0.3);
  299. border-radius: 20rpx;
  300. padding: 32rpx 28rpx;
  301. backdrop-filter: blur(10px);
  302. }
  303. .intro-title {
  304. font-size: 30rpx;
  305. font-weight: bold;
  306. color: #fff;
  307. text-align: center;
  308. margin-bottom: 16rpx;
  309. }
  310. .intro-desc {
  311. font-size: 24rpx;
  312. color: rgba(255, 255, 255, 0.9);
  313. line-height: 1.7;
  314. text-align: center;
  315. margin-bottom: 24rpx;
  316. }
  317. .intro-desc .highlight {
  318. color: #FFD700;
  319. font-weight: 600;
  320. }
  321. .dimension-tags {
  322. display: flex;
  323. flex-wrap: wrap;
  324. justify-content: center;
  325. gap: 12rpx;
  326. margin-bottom: 20rpx;
  327. }
  328. .dim-tag {
  329. font-size: 22rpx;
  330. padding: 8rpx 16rpx;
  331. border-radius: 30rpx;
  332. background: rgba(255, 255, 255, 0.2);
  333. color: #fff;
  334. font-weight: 500;
  335. }
  336. .dim-body { background: rgba(91, 155, 213, 0.4); }
  337. .dim-mind { background: rgba(236, 72, 153, 0.4); }
  338. .dim-wisdom { background: rgba(139, 92, 246, 0.4); }
  339. .dim-action { background: rgba(16, 185, 129, 0.4); }
  340. .dim-wealth { background: rgba(245, 158, 11, 0.4); }
  341. .intro-powered {
  342. text-align: center;
  343. font-size: 20rpx;
  344. color: rgba(255, 255, 255, 0.6);
  345. }
  346. /* ===== 登录表单 ===== */
  347. .login-form {
  348. background: #fff;
  349. border-radius: 20rpx;
  350. padding: 40rpx;
  351. }
  352. .phone-section {
  353. margin-bottom: 32rpx;
  354. }
  355. .phone-btn {
  356. display: flex;
  357. align-items: center;
  358. justify-content: center;
  359. gap: 10rpx;
  360. width: 100%;
  361. height: 88rpx;
  362. background: #07c160;
  363. color: #fff;
  364. border-radius: 44rpx;
  365. font-size: 32rpx;
  366. }
  367. /* ===== 跳过登录 ===== */
  368. .skip-section {
  369. text-align: center;
  370. margin-bottom: 20rpx;
  371. }
  372. .skip-btn {
  373. font-size: 28rpx;
  374. color: #999;
  375. text-decoration: underline;
  376. padding: 16rpx 0;
  377. }
  378. .agreement {
  379. margin-top: 16rpx;
  380. text-align: center;
  381. font-size: 24rpx;
  382. }
  383. .agreement-text {
  384. color: #666;
  385. }
  386. .link {
  387. color: #667eea;
  388. }
  389. .agreement-text.link {
  390. text-decoration: underline;
  391. }
  392. </style>