login.vue 12 KB

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