login.vue 12 KB

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