index.vue 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <template>
  2. <view class="page-login" :class="themeClass">
  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. <!-- US-6.2 §10: Profile completion overlay for first-time users -->
  29. <view v-if="showProfileForm" class="profile-overlay" @touchmove.prevent>
  30. <view class="profile-card">
  31. <text class="profile-card__title">完善个人资料</text>
  32. <text class="profile-card__subtitle">以下信息帮助提供更精准的分析</text>
  33. <view class="profile-card__field">
  34. <text class="profile-card__label">昵称</text>
  35. <input class="profile-card__input" v-model="profileForm.nickname" placeholder="输入昵称" maxlength="20" />
  36. </view>
  37. <view class="profile-card__field">
  38. <text class="profile-card__label">性别</text>
  39. <view class="profile-card__radio-group">
  40. <text class="profile-card__radio" :class="{ active: profileForm.gender === 1 }" @click="profileForm.gender = 1">男</text>
  41. <text class="profile-card__radio" :class="{ active: profileForm.gender === 2 }" @click="profileForm.gender = 2">女</text>
  42. </view>
  43. </view>
  44. <view class="profile-card__field">
  45. <text class="profile-card__label">出生日期</text>
  46. <text class="profile-card__prefilled">已填写:{{ profileForm.birthDate || '未填写' }}</text>
  47. </view>
  48. <view class="profile-card__actions">
  49. <text class="profile-card__btn profile-card__btn--skip" @click="onSkipProfile">跳过</text>
  50. <text class="profile-card__btn profile-card__btn--save" @click="onSaveProfile">保存</text>
  51. </view>
  52. </view>
  53. </view>
  54. </view>
  55. </template>
  56. <script setup>
  57. import { ref } from 'vue'
  58. import { onLoad } from '@dcloudio/uni-app'
  59. import { useUserStore } from '@/stores/user'
  60. import { profileApi } from '@/utils/api'
  61. import { useTheme } from '@/composables/useThemeStyle'
  62. const { themeClass } = useTheme()
  63. const userStore = useUserStore()
  64. // Params passed from index page (US-6.2 §9)
  65. const queryName = ref('')
  66. const queryBirthday = ref('')
  67. const queryQuestions = ref('')
  68. const redirectUrl = ref('')
  69. // Profile completion overlay
  70. const showProfileForm = ref(false)
  71. const profileForm = ref({
  72. nickname: '',
  73. gender: 0,
  74. birthDate: ''
  75. })
  76. onLoad((options) => {
  77. queryName.value = options.name || ''
  78. queryBirthday.value = options.birthday || ''
  79. queryQuestions.value = options.questions || ''
  80. redirectUrl.value = options.redirect_to || ''
  81. // Pre-fill profile form from consultation page
  82. profileForm.value.nickname = queryName.value
  83. profileForm.value.birthDate = queryBirthday.value
  84. })
  85. async function onWechatLogin() {
  86. uni.showLoading({ title: '登录中...' })
  87. try {
  88. // Step 1: Get login code from WeChat
  89. const loginRes = await new Promise((resolve, reject) => {
  90. uni.login({
  91. provider: 'weixin',
  92. success: resolve,
  93. fail: reject
  94. })
  95. })
  96. if (!loginRes.code) {
  97. throw new Error('获取微信登录凭证失败')
  98. }
  99. // Step 2: Try to get user profile (nickname, avatar)
  100. let nickname = ''
  101. let avatarUrl = ''
  102. try {
  103. const profileRes = await new Promise((resolve, reject) => {
  104. uni.getUserProfile({
  105. desc: '用于展示用户信息',
  106. success: resolve,
  107. fail: () => reject(new Error('skip'))
  108. })
  109. })
  110. nickname = profileRes.userInfo?.nickName || ''
  111. avatarUrl = profileRes.userInfo?.avatarUrl || ''
  112. } catch (e) {
  113. // User declined profile permission - still login with code only
  114. }
  115. // Step 2.5: Capture pending referrer from index page (US-6.2)
  116. const pendingReferrer = uni.getStorageSync('pending_referrer') || null
  117. // Step 3: Send code to backend
  118. await userStore.wechatLogin(loginRes.code, nickname, avatarUrl, pendingReferrer)
  119. // Clear pending referrer after using it (US-6.2 §6: login success → clear)
  120. if (pendingReferrer) {
  121. uni.removeStorageSync('pending_referrer')
  122. }
  123. uni.hideLoading()
  124. uni.showToast({ title: '登录成功', icon: 'success' })
  125. // US-6.2 §10: First-time user → show profile completion overlay
  126. if (userStore.profileIncomplete) {
  127. showProfileForm.value = true
  128. return // wait for user action (save/skip) before continuing
  129. }
  130. // Existing user → continue to chart
  131. continueToChart()
  132. } catch (e) {
  133. uni.hideLoading()
  134. uni.showToast({ title: e.message || '登录失败', icon: 'none' })
  135. // US-6.2 §7: Login interrupted → keep pending_referrer
  136. }
  137. }
  138. async function onSaveProfile() {
  139. uni.showLoading({ title: '保存中...' })
  140. try {
  141. await profileApi.complete({
  142. nickname: profileForm.value.nickname,
  143. gender: profileForm.value.gender,
  144. birthDate: profileForm.value.birthDate,
  145. // birthday and name are pre-filled from consultation page
  146. hasExternalBirthday: !!queryBirthday.value,
  147. externalName: queryName.value
  148. })
  149. userStore.profileIncomplete = false
  150. uni.hideLoading()
  151. uni.showToast({ title: '保存成功', icon: 'success' })
  152. } catch (e) {
  153. uni.hideLoading()
  154. uni.showToast({ title: e.message || '保存失败', icon: 'none' })
  155. return
  156. }
  157. showProfileForm.value = false
  158. continueToChart()
  159. }
  160. function onSkipProfile() {
  161. showProfileForm.value = false
  162. uni.showToast({ title: '已跳过,可随时在个人中心完善', icon: 'none' })
  163. continueToChart()
  164. }
  165. function continueToChart() {
  166. // Check redirect_to param
  167. const redirectTarget = redirectUrl.value || ''
  168. if (redirectTarget) {
  169. setTimeout(() => {
  170. uni.redirectTo({ url: redirectTarget })
  171. }, 500)
  172. } else if (queryName.value || queryBirthday.value) {
  173. // Navigate to chart with the consultation params from index
  174. setTimeout(() => {
  175. const params = new URLSearchParams({
  176. name: queryName.value,
  177. birthday: queryBirthday.value,
  178. questions: queryQuestions.value
  179. })
  180. uni.redirectTo({ url: `/pages/chart/index?${params.toString()}` })
  181. }, 500)
  182. } else {
  183. setTimeout(() => {
  184. uni.switchTab({ url: '/pages/index/index' })
  185. }, 500)
  186. }
  187. }
  188. </script>
  189. <style scoped lang="scss">
  190. .page-login {
  191. min-height: 100vh;
  192. background: linear-gradient(180deg, #F8F9FF 0%, #EEF2FF 100%);
  193. display: flex;
  194. align-items: center;
  195. justify-content: center;
  196. padding: 0 24px;
  197. }
  198. .login-card {
  199. background: #fff;
  200. border-radius: 20px;
  201. padding: 40px 28px 32px;
  202. box-shadow: 0 4px 20px rgba(0,0,0,0.06);
  203. width: 100%;
  204. max-width: 360px;
  205. }
  206. .logo-area {
  207. text-align: center;
  208. margin-bottom: 32px;
  209. }
  210. .logo-icon {
  211. font-size: 48px;
  212. display: block;
  213. margin-bottom: 8px;
  214. }
  215. .logo-title {
  216. font-size: 22px;
  217. font-weight: 700;
  218. color: #1F2937;
  219. display: block;
  220. }
  221. .logo-desc {
  222. font-size: 13px;
  223. color: #9CA3AF;
  224. display: block;
  225. margin-top: 4px;
  226. }
  227. .features {
  228. margin-bottom: 28px;
  229. }
  230. .feature-item {
  231. display: flex;
  232. align-items: center;
  233. margin-bottom: 12px;
  234. }
  235. .feature-icon {
  236. font-size: 16px;
  237. margin-right: 8px;
  238. }
  239. .feature-text {
  240. font-size: 14px;
  241. color: #6B7280;
  242. }
  243. .wechat-btn {
  244. width: 100%;
  245. height: 48px;
  246. background: linear-gradient(135deg, #4A148C, #7B2DC8);
  247. color: #fff;
  248. border: none;
  249. border-radius: 12px;
  250. font-size: 17px;
  251. font-weight: 600;
  252. line-height: 48px;
  253. text-align: center;
  254. }
  255. .agreement {
  256. display: block;
  257. text-align: center;
  258. font-size: 12px;
  259. color: #9CA3AF;
  260. margin-top: 16px;
  261. }
  262. /* US-6.2 §10: Profile completion overlay */
  263. .profile-overlay {
  264. position: fixed;
  265. inset: 0;
  266. z-index: 100;
  267. background: rgba(0, 0, 0, 0.5);
  268. display: flex;
  269. align-items: center;
  270. justify-content: center;
  271. padding: 0 24px;
  272. }
  273. .profile-card {
  274. background: #fff;
  275. border-radius: 20px;
  276. padding: 32px 24px 24px;
  277. width: 100%;
  278. max-width: 360px;
  279. }
  280. .profile-card__title {
  281. font-size: 20px;
  282. font-weight: 700;
  283. color: #1F2937;
  284. display: block;
  285. text-align: center;
  286. }
  287. .profile-card__subtitle {
  288. font-size: 13px;
  289. color: #9CA3AF;
  290. display: block;
  291. text-align: center;
  292. margin-top: 4px;
  293. margin-bottom: 24px;
  294. }
  295. .profile-card__field {
  296. margin-bottom: 16px;
  297. }
  298. .profile-card__label {
  299. font-size: 13px;
  300. color: #6B7280;
  301. display: block;
  302. margin-bottom: 6px;
  303. }
  304. .profile-card__input {
  305. height: 44px;
  306. border: 1px solid #E5E7EB;
  307. border-radius: 10px;
  308. padding: 0 14px;
  309. font-size: 16px;
  310. width: 100%;
  311. box-sizing: border-box;
  312. color: #1F2937;
  313. }
  314. .profile-card__prefilled {
  315. font-size: 14px;
  316. color: #6B7280;
  317. line-height: 44px;
  318. }
  319. .profile-card__radio-group {
  320. display: flex;
  321. gap: 12px;
  322. }
  323. .profile-card__radio {
  324. flex: 1;
  325. text-align: center;
  326. padding: 10px 0;
  327. border: 1px solid #E5E7EB;
  328. border-radius: 10px;
  329. font-size: 15px;
  330. color: #6B7280;
  331. }
  332. .profile-card__radio.active {
  333. border-color: #7C3AED;
  334. color: #7C3AED;
  335. background: #F5F3FF;
  336. }
  337. .profile-card__actions {
  338. display: flex;
  339. gap: 12px;
  340. margin-top: 24px;
  341. }
  342. .profile-card__btn {
  343. flex: 1;
  344. text-align: center;
  345. padding: 12px 0;
  346. border-radius: 10px;
  347. font-size: 15px;
  348. font-weight: 500;
  349. }
  350. .profile-card__btn--skip {
  351. background: var(--color-bg-input);
  352. color: #6B7280;
  353. }
  354. .profile-card__btn--save {
  355. background: linear-gradient(135deg, #4A148C, #7B2DC8);
  356. color: #fff;
  357. }
  358. </style>