index.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. setTimeout(() => {
  68. // 使用 switchTab 跳转到首页(reLaunch 清空了页面栈)
  69. uni.switchTab({ url: '/pages/index/index' })
  70. }, 500)
  71. } catch (e) {
  72. uni.hideLoading()
  73. uni.showToast({ title: e.message || '登录失败', icon: 'none' })
  74. }
  75. }
  76. </script>
  77. <style scoped lang="scss">
  78. .page-login {
  79. min-height: 100vh;
  80. background: linear-gradient(180deg, #F8F9FF 0%, #EEF2FF 100%);
  81. display: flex;
  82. align-items: center;
  83. justify-content: center;
  84. padding: 0 24px;
  85. }
  86. .login-card {
  87. background: #fff;
  88. border-radius: 20px;
  89. padding: 40px 28px 32px;
  90. box-shadow: 0 4px 20px rgba(0,0,0,0.06);
  91. width: 100%;
  92. max-width: 360px;
  93. }
  94. .logo-area {
  95. text-align: center;
  96. margin-bottom: 32px;
  97. }
  98. .logo-icon {
  99. font-size: 48px;
  100. display: block;
  101. margin-bottom: 8px;
  102. }
  103. .logo-title {
  104. font-size: 22px;
  105. font-weight: 700;
  106. color: #1F2937;
  107. display: block;
  108. }
  109. .logo-desc {
  110. font-size: 13px;
  111. color: #9CA3AF;
  112. display: block;
  113. margin-top: 4px;
  114. }
  115. .features {
  116. margin-bottom: 28px;
  117. }
  118. .feature-item {
  119. display: flex;
  120. align-items: center;
  121. margin-bottom: 12px;
  122. }
  123. .feature-icon {
  124. font-size: 16px;
  125. margin-right: 8px;
  126. }
  127. .feature-text {
  128. font-size: 14px;
  129. color: #6B7280;
  130. }
  131. .wechat-btn {
  132. width: 100%;
  133. height: 48px;
  134. background: linear-gradient(135deg, #4A148C, #7B2DC8);
  135. color: #fff;
  136. border: none;
  137. border-radius: 12px;
  138. font-size: 17px;
  139. font-weight: 600;
  140. line-height: 48px;
  141. text-align: center;
  142. }
  143. .agreement {
  144. display: block;
  145. text-align: center;
  146. font-size: 12px;
  147. color: #9CA3AF;
  148. margin-top: 16px;
  149. }
  150. </style>