index.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <template>
  2. <view class="page-login">
  3. <view class="login-card">
  4. <view class="logo-area">
  5. <text class="logo-icon">🔮</text>
  6. <text class="logo-title">数字能量学</text>
  7. <text class="logo-desc">AI 智能命理分析</text>
  8. </view>
  9. <view class="features">
  10. <view class="feature-item">
  11. <text class="feature-icon">✅</text>
  12. <text class="feature-text">一键微信登录,无需注册</text>
  13. </view>
  14. <view class="feature-item">
  15. <text class="feature-icon">🔒</text>
  16. <text class="feature-text">已通过微信安全认证</text>
  17. </view>
  18. <view class="feature-item">
  19. <text class="feature-icon">📊</text>
  20. <text class="feature-text">同步分析记录至云端</text>
  21. </view>
  22. </view>
  23. <button class="wechat-btn" @click="onWechatLogin">
  24. <text>微信一键登录</text>
  25. </button>
  26. <text class="agreement">登录即表示同意《用户协议》和《隐私政策》</text>
  27. </view>
  28. </view>
  29. </template>
  30. <script setup>
  31. import { useUserStore } from '@/stores/user'
  32. const userStore = useUserStore()
  33. async function onWechatLogin() {
  34. uni.showLoading({ title: '登录中...' })
  35. try {
  36. // Step 1: Get login code from WeChat
  37. const loginRes = await new Promise((resolve, reject) => {
  38. uni.login({
  39. provider: 'weixin',
  40. success: resolve,
  41. fail: reject
  42. })
  43. })
  44. if (!loginRes.code) {
  45. throw new Error('获取微信登录凭证失败')
  46. }
  47. // Step 2: Try to get user profile (nickname, avatar)
  48. let nickname = ''
  49. let avatarUrl = ''
  50. try {
  51. const profileRes = await new Promise((resolve, reject) => {
  52. uni.getUserProfile({
  53. desc: '用于展示用户信息',
  54. success: resolve,
  55. fail: () => reject(new Error('skip'))
  56. })
  57. })
  58. nickname = profileRes.userInfo?.nickName || ''
  59. avatarUrl = profileRes.userInfo?.avatarUrl || ''
  60. } catch (e) {
  61. // User declined profile permission - still login with code only
  62. }
  63. // Step 3: Send code to backend
  64. await userStore.wechatLogin(loginRes.code, nickname, avatarUrl)
  65. uni.hideLoading()
  66. uni.showToast({ title: '登录成功', icon: 'success' })
  67. // Check redirect_to param (US-6.6: "登录后开通" flow)
  68. const pages = getCurrentPages()
  69. const page = pages[pages.length - 1]
  70. const params = page?.options || {}
  71. const redirectTo = params.redirect_to || ''
  72. // US-5.1 §8: Redirect to registration completion if profile incomplete
  73. if (userStore.profileIncomplete) {
  74. setTimeout(() => {
  75. uni.redirectTo({ url: '/pages/register/index' })
  76. }, 500)
  77. } else if (redirectTo) {
  78. setTimeout(() => {
  79. uni.redirectTo({ url: redirectTo })
  80. }, 500)
  81. } else {
  82. setTimeout(() => {
  83. uni.switchTab({ url: '/pages/index/index' })
  84. }, 500)
  85. }
  86. } catch (e) {
  87. uni.hideLoading()
  88. uni.showToast({ title: e.message || '登录失败', icon: 'none' })
  89. }
  90. }
  91. </script>
  92. <style scoped lang="scss">
  93. .page-login {
  94. min-height: 100vh;
  95. background: linear-gradient(180deg, #F8F9FF 0%, #EEF2FF 100%);
  96. display: flex;
  97. align-items: center;
  98. justify-content: center;
  99. padding: 0 24px;
  100. }
  101. .login-card {
  102. background: #fff;
  103. border-radius: 20px;
  104. padding: 40px 28px 32px;
  105. box-shadow: 0 4px 20px rgba(0,0,0,0.06);
  106. width: 100%;
  107. max-width: 360px;
  108. }
  109. .logo-area {
  110. text-align: center;
  111. margin-bottom: 32px;
  112. }
  113. .logo-icon {
  114. font-size: 48px;
  115. display: block;
  116. margin-bottom: 8px;
  117. }
  118. .logo-title {
  119. font-size: 22px;
  120. font-weight: 700;
  121. color: #1F2937;
  122. display: block;
  123. }
  124. .logo-desc {
  125. font-size: 13px;
  126. color: #9CA3AF;
  127. display: block;
  128. margin-top: 4px;
  129. }
  130. .features {
  131. margin-bottom: 28px;
  132. }
  133. .feature-item {
  134. display: flex;
  135. align-items: center;
  136. margin-bottom: 12px;
  137. }
  138. .feature-icon {
  139. font-size: 16px;
  140. margin-right: 8px;
  141. }
  142. .feature-text {
  143. font-size: 14px;
  144. color: #6B7280;
  145. }
  146. .wechat-btn {
  147. width: 100%;
  148. height: 48px;
  149. background: linear-gradient(135deg, #4A148C, #7B2DC8);
  150. color: #fff;
  151. border: none;
  152. border-radius: 12px;
  153. font-size: 17px;
  154. font-weight: 600;
  155. line-height: 48px;
  156. text-align: center;
  157. }
  158. .agreement {
  159. display: block;
  160. text-align: center;
  161. font-size: 12px;
  162. color: #9CA3AF;
  163. margin-top: 16px;
  164. }
  165. </style>