login.vue 12 KB

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