index.vue 9.8 KB

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