index.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. @import "@/styles/theme.scss";
  189. .page-login {
  190. min-height: 100vh;
  191. background: linear-gradient(180deg, #F8F9FF 0%, #EEF2FF 100%);
  192. display: flex;
  193. align-items: center;
  194. justify-content: center;
  195. padding: 0 24px;
  196. }
  197. .login-card {
  198. background: #fff;
  199. border-radius: 20px;
  200. padding: 40px 28px 32px;
  201. box-shadow: 0 4px 20px rgba(0,0,0,0.06);
  202. width: 100%;
  203. max-width: 360px;
  204. }
  205. .logo-area {
  206. text-align: center;
  207. margin-bottom: 32px;
  208. }
  209. .logo-icon {
  210. font-size: 48px;
  211. display: block;
  212. margin-bottom: 8px;
  213. }
  214. .logo-title {
  215. font-size: 22px;
  216. font-weight: 700;
  217. color: #1F2937;
  218. display: block;
  219. }
  220. .logo-desc {
  221. font-size: 13px;
  222. color: #9CA3AF;
  223. display: block;
  224. margin-top: 4px;
  225. }
  226. .features {
  227. margin-bottom: 28px;
  228. }
  229. .feature-item {
  230. display: flex;
  231. align-items: center;
  232. margin-bottom: 12px;
  233. }
  234. .feature-icon {
  235. font-size: 16px;
  236. margin-right: 8px;
  237. }
  238. .feature-text {
  239. font-size: 14px;
  240. color: #6B7280;
  241. }
  242. .wechat-btn {
  243. width: 100%;
  244. height: 48px;
  245. background: linear-gradient(135deg, #4A148C, #7B2DC8);
  246. color: #fff;
  247. border: none;
  248. border-radius: 12px;
  249. font-size: 17px;
  250. font-weight: 600;
  251. line-height: 48px;
  252. text-align: center;
  253. }
  254. .agreement {
  255. display: block;
  256. text-align: center;
  257. font-size: 12px;
  258. color: #9CA3AF;
  259. margin-top: 16px;
  260. }
  261. /* US-6.2 §10: Profile completion overlay */
  262. .profile-overlay {
  263. position: fixed;
  264. inset: 0;
  265. z-index: 100;
  266. background: rgba(0, 0, 0, 0.5);
  267. display: flex;
  268. align-items: center;
  269. justify-content: center;
  270. padding: 0 24px;
  271. }
  272. .profile-card {
  273. background: #fff;
  274. border-radius: 20px;
  275. padding: 32px 24px 24px;
  276. width: 100%;
  277. max-width: 360px;
  278. }
  279. .profile-card__title {
  280. font-size: 20px;
  281. font-weight: 700;
  282. color: #1F2937;
  283. display: block;
  284. text-align: center;
  285. }
  286. .profile-card__subtitle {
  287. font-size: 13px;
  288. color: #9CA3AF;
  289. display: block;
  290. text-align: center;
  291. margin-top: 4px;
  292. margin-bottom: 24px;
  293. }
  294. .profile-card__field {
  295. margin-bottom: 16px;
  296. }
  297. .profile-card__label {
  298. font-size: 13px;
  299. color: #6B7280;
  300. display: block;
  301. margin-bottom: 6px;
  302. }
  303. .profile-card__input {
  304. height: 44px;
  305. border: 1px solid #E5E7EB;
  306. border-radius: 10px;
  307. padding: 0 14px;
  308. font-size: 16px;
  309. width: 100%;
  310. box-sizing: border-box;
  311. color: #1F2937;
  312. }
  313. .profile-card__prefilled {
  314. font-size: 14px;
  315. color: #6B7280;
  316. line-height: 44px;
  317. }
  318. .profile-card__radio-group {
  319. display: flex;
  320. gap: 12px;
  321. }
  322. .profile-card__radio {
  323. flex: 1;
  324. text-align: center;
  325. padding: 10px 0;
  326. border: 1px solid #E5E7EB;
  327. border-radius: 10px;
  328. font-size: 15px;
  329. color: #6B7280;
  330. }
  331. .profile-card__radio.active {
  332. border-color: #7C3AED;
  333. color: #7C3AED;
  334. background: #F5F3FF;
  335. }
  336. .profile-card__actions {
  337. display: flex;
  338. gap: 12px;
  339. margin-top: 24px;
  340. }
  341. .profile-card__btn {
  342. flex: 1;
  343. text-align: center;
  344. padding: 12px 0;
  345. border-radius: 10px;
  346. font-size: 15px;
  347. font-weight: 500;
  348. }
  349. .profile-card__btn--skip {
  350. background: var(--color-bg-input);
  351. color: #6B7280;
  352. }
  353. .profile-card__btn--save {
  354. background: linear-gradient(135deg, #4A148C, #7B2DC8);
  355. color: #fff;
  356. }
  357. </style>