login.vue 11 KB

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