login.vue 12 KB

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