index.vue 9.9 KB

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