login.vue 13 KB

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