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