login.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. // 超时保护:10秒强制结束登录流程,避免 uni.login 挂死时按钮 loading 卡死
  100. var loginTimeout = new Promise(function(_, reject) {
  101. setTimeout(function() {
  102. reject(new Error('微信登录超时'))
  103. }, 10000)
  104. })
  105. Promise.race([loginTimeout, new Promise(function(resolve, reject) {
  106. uni.login({
  107. provider: 'weixin',
  108. success: function(loginRes) { resolve(loginRes) },
  109. fail: function() { reject(new Error('微信登录失败')) }
  110. })
  111. })]).then(function(loginRes) {
  112. wechatLogin(loginRes.code, {
  113. nickname: self.nickname || '用户',
  114. avatar: self.avatar || ''
  115. }).then(function(res) {
  116. // 保存 token 和用户信息(只写一次)
  117. uni.setStorageSync('token', res.data.token)
  118. uni.setStorageSync('userId', res.data.userId)
  119. uni.setStorageSync('role', res.data.role)
  120. uni.setStorageSync('familyId', res.data.familyId)
  121. uni.setStorageSync('currentRole', res.data.role)
  122. uni.setStorageSync('isSwitchedChild', false)
  123. uni.setStorageSync('isSwitchedTeacher', false)
  124. if (res.data.openid) {
  125. uni.setStorageSync('openid', res.data.openid)
  126. }
  127. uni.setStorageSync('userInfo', {
  128. nickname: res.data.nickname,
  129. userId: res.data.userId
  130. })
  131. if (res.data.teacherStatus) {
  132. uni.setStorageSync('teacherStatus', res.data.teacherStatus)
  133. }
  134. if (res.data.teacherRejectReason) {
  135. uni.setStorageSync('teacherRejectReason', res.data.teacherRejectReason)
  136. }
  137. uni.showToast({ title: '登录成功', icon: 'success' })
  138. if (res.data.needsRoleSelect) {
  139. uni.redirectTo({
  140. url: '/pages/user-edit/user-edit?token=' + res.data.token + '&userId=' + res.data.userId + '&needsRoleSelect=true'
  141. })
  142. } else {
  143. self.handlePostLoginRouting()
  144. }
  145. }).catch(function(e) {
  146. uni.showToast({ title: e.message || '登录失败', icon: 'none' })
  147. self.loading = false
  148. })
  149. }).catch(function(e) {
  150. uni.showToast({ title: e.message || '微信登录失败', icon: 'none' })
  151. self.loading = false
  152. })
  153. },
  154. handlePostLoginRouting() {
  155. const inviteCode = uni.getStorageSync('inviteCode')
  156. const inviteType = uni.getStorageSync('inviteType')
  157. if (inviteCode && inviteType === 'team') {
  158. this.handleTeamInviteFlow(inviteCode)
  159. } else {
  160. this.navigateToHome()
  161. }
  162. },
  163. async handleTeamInviteFlow(inviteCode) {
  164. try {
  165. const res = await checkTeamInviteStatus(inviteCode)
  166. const status = res.data.status
  167. if (status === 'not_planner') {
  168. uni.redirectTo({ url: `/pages/guide/register/index?inviteCode=${inviteCode}` })
  169. } else if (status === 'not_in_team') {
  170. uni.showModal({
  171. title: '加入团队',
  172. content: '是否加入该规划师团队?',
  173. success: async (confirmRes) => {
  174. if (confirmRes.confirm) {
  175. try {
  176. await joinTeamByInvite(inviteCode)
  177. uni.showToast({ title: '加入成功', icon: 'success' })
  178. uni.removeStorageSync('inviteCode')
  179. uni.removeStorageSync('inviteType')
  180. this.navigateToHome()
  181. } catch (e) {
  182. uni.showToast({ title: e.message || '加入失败', icon: 'none' })
  183. }
  184. } else {
  185. this.navigateToHome()
  186. }
  187. }
  188. })
  189. } else if (status === 'already_in_team') {
  190. uni.showModal({
  191. title: '加入新团队',
  192. content: '您已在其他团队中,离开当前团队并加入新团队?',
  193. success: async (confirmRes) => {
  194. if (confirmRes.confirm) {
  195. try {
  196. await joinTeamByInvite(inviteCode)
  197. uni.showToast({ title: '加入成功', icon: 'success' })
  198. uni.removeStorageSync('inviteCode')
  199. uni.removeStorageSync('inviteType')
  200. this.navigateToHome()
  201. } catch (e) {
  202. uni.showToast({ title: e.message || '加入失败', icon: 'none' })
  203. }
  204. } else {
  205. this.navigateToHome()
  206. }
  207. }
  208. })
  209. } else {
  210. this.navigateToHome()
  211. }
  212. } catch (e) {
  213. uni.showToast({ title: e.message || '处理邀请失败', icon: 'none' })
  214. this.navigateToHome()
  215. }
  216. },
  217. navigateToHome() {
  218. if (this.redirectUrl) {
  219. const redirect = this.redirectUrl
  220. this.redirectUrl = ''
  221. // 使用 redirectTo 支持非 tabBar 页面,避免 switchTab 无法跳转到 discover 等非 tabBar 页
  222. uni.redirectTo({ url: redirect })
  223. } else {
  224. uni.switchTab({ url: '/pages/index/index' })
  225. }
  226. }
  227. }
  228. }
  229. </script>
  230. <style scoped>
  231. .login-container {
  232. min-height: 100vh;
  233. background: linear-gradient(160deg, #FFF7ED 0%, #F97316 100%);
  234. padding: 60rpx 40rpx;
  235. }
  236. /* ===== Logo 区 ===== */
  237. .logo-section {
  238. text-align: center;
  239. margin-bottom: 48rpx;
  240. }
  241. .logo {
  242. width: 160rpx;
  243. height: 160rpx;
  244. border-radius: 80rpx;
  245. background: #fff;
  246. }
  247. .app-name {
  248. display: block;
  249. font-size: 52rpx;
  250. font-weight: bold;
  251. color: #fff;
  252. margin-top: 20rpx;
  253. }
  254. .slogan {
  255. display: block;
  256. font-size: 26rpx;
  257. color: rgba(255, 255, 255, 0.8);
  258. margin-top: 10rpx;
  259. }
  260. /* ===== 平台简介卡片 ===== */
  261. .intro-section {
  262. margin-bottom: 48rpx;
  263. }
  264. .intro-card {
  265. background: rgba(255, 255, 255, 0.15);
  266. border: 1rpx solid rgba(255, 255, 255, 0.3);
  267. border-radius: 20rpx;
  268. padding: 32rpx 28rpx;
  269. backdrop-filter: blur(10px);
  270. }
  271. .intro-title {
  272. font-size: 30rpx;
  273. font-weight: bold;
  274. color: #fff;
  275. text-align: center;
  276. margin-bottom: 16rpx;
  277. }
  278. .intro-desc {
  279. font-size: 24rpx;
  280. color: rgba(255, 255, 255, 0.9);
  281. line-height: 1.7;
  282. text-align: center;
  283. margin-bottom: 24rpx;
  284. }
  285. .intro-desc .highlight {
  286. color: #FFD700;
  287. font-weight: 600;
  288. }
  289. .dimension-tags {
  290. display: flex;
  291. flex-wrap: wrap;
  292. justify-content: center;
  293. gap: 12rpx;
  294. margin-bottom: 20rpx;
  295. }
  296. .dim-tag {
  297. font-size: 22rpx;
  298. padding: 8rpx 16rpx;
  299. border-radius: 30rpx;
  300. background: rgba(255, 255, 255, 0.2);
  301. color: #fff;
  302. font-weight: 500;
  303. }
  304. .dim-body { background: rgba(91, 155, 213, 0.4); }
  305. .dim-mind { background: rgba(236, 72, 153, 0.4); }
  306. .dim-wisdom { background: rgba(139, 92, 246, 0.4); }
  307. .dim-action { background: rgba(16, 185, 129, 0.4); }
  308. .dim-wealth { background: rgba(245, 158, 11, 0.4); }
  309. .intro-powered {
  310. text-align: center;
  311. font-size: 20rpx;
  312. color: rgba(255, 255, 255, 0.6);
  313. }
  314. /* ===== 登录表单 ===== */
  315. .login-form {
  316. background: #fff;
  317. border-radius: 20rpx;
  318. padding: 40rpx;
  319. }
  320. .wechat-section {
  321. margin-bottom: 32rpx;
  322. }
  323. .wechat-btn {
  324. display: flex;
  325. align-items: center;
  326. justify-content: center;
  327. gap: 10rpx;
  328. width: 100%;
  329. height: 88rpx;
  330. background: #07c160;
  331. color: #fff;
  332. border-radius: 44rpx;
  333. font-size: 32rpx;
  334. }
  335. .wechat-icon {
  336. font-size: 36rpx;
  337. }
  338. .agreement {
  339. margin-top: 16rpx;
  340. text-align: center;
  341. font-size: 24rpx;
  342. }
  343. .agreement-text {
  344. color: #666;
  345. }
  346. .link {
  347. color: #667eea;
  348. }
  349. .agreement-text.link {
  350. text-decoration: underline;
  351. }
  352. </style>