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