useLogin.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import type { FormInstance, FormRules } from 'element-plus'
  2. import { getEmailCodeApi, validateEmailIsExistApi } from '@/api/model/user'
  3. import { useUserStore } from '@/stores/modules/user'
  4. const { closeLoginModal } = useLoginModal()
  5. const { loading, onGoogleLogin } = useGoogleLogin()
  6. const loginForm = ref<any>({
  7. email: '',
  8. captcha: '',
  9. })
  10. const emailStep = ref<number>(0)
  11. const seconds = ref<number>(0)
  12. const selectedMethod = ref<string>('google')
  13. const errorCodeTxt = ref<string>('')
  14. const isEmailGoogle = ref<boolean>(false)
  15. export default function useRegister() {
  16. const nextStep = () => {
  17. emailStep.value++
  18. }
  19. const backStep = () => {
  20. if (emailStep.value > 0)
  21. emailStep.value--
  22. }
  23. // const register = async () => {
  24. // try {
  25. // const form = {
  26. // ...params.value,
  27. // purchaseCategory: params.value.purchaseCategory.join(','),
  28. // }
  29. // await registerApi(form)
  30. // nextStep()
  31. // }
  32. // catch (error) {
  33. // console.log(error)
  34. // }
  35. // }
  36. /**
  37. * 二选一选择邮箱--下一步
  38. */
  39. const onSelectEmail = () => {
  40. selectedMethod.value = 'email'
  41. loginForm.value.email = ''
  42. nextStep()
  43. }
  44. /**
  45. * 选择曾经登陆过的账号--下一步
  46. */
  47. const onSelectAccount = async (item: any) => {
  48. console.log('onSelectAccount', item, loading.value)
  49. // type: 'google' | 'email'
  50. if (item.type === 'google') {
  51. const result = await onGoogleLogin()
  52. await onGoogleLoginSuccess(result)
  53. }
  54. if (item.type === 'email') {
  55. loginForm.value.email = item.email
  56. emailStep.value = 1
  57. }
  58. }
  59. /**
  60. * dao: 倒计时60秒
  61. */
  62. const countSeconds = () => {
  63. seconds.value = 60
  64. const timer = setInterval(() => {
  65. seconds.value--
  66. if (seconds.value === 0)
  67. clearInterval(timer)
  68. }, 1000)
  69. }
  70. /**
  71. * 邮箱--发送验证码
  72. */
  73. const getMailCode = async () => {
  74. try {
  75. if (seconds.value > 0)
  76. return
  77. // 获取验证码
  78. await getEmailCodeApi({ account: loginForm.value.email, type: 's003_login' })
  79. countSeconds()
  80. }
  81. catch (error) {
  82. console.error('Error sending email code:', error)
  83. }
  84. }
  85. /**
  86. * 邮箱--验证是否存在--是否与已存在google的邮箱--下一步
  87. * @returns
  88. */
  89. async function sendEmail(formEl: FormInstance | undefined) {
  90. if (!formEl)
  91. return
  92. await formEl.validate(async (valid, fields) => {
  93. if (valid) {
  94. // 验证已存在google的邮箱
  95. const res: any = await validateEmailIsExistApi({ email: loginForm.value.email })
  96. console.log('loginedAccountList', res)
  97. if (!res || res.last_login_type !== 'google')
  98. // 如果没有google的邮箱,直接下一步,或者没有注册过邮箱
  99. isEmailGoogle.value = false
  100. if (res.last_login_type === 'google') {
  101. // 显示google登陆按钮
  102. isEmailGoogle.value = true
  103. loginForm.value.email = ''
  104. }
  105. nextStep()
  106. }
  107. else { console.log('error submit!', fields) }
  108. })
  109. }
  110. /**
  111. * 邮箱--验证码--完成登陆 (后台默认为注册)
  112. * @returns
  113. */
  114. async function finishCode(formEl: FormInstance | undefined) {
  115. errorCodeTxt.value = ''
  116. if (!formEl)
  117. return
  118. await formEl.validate(async (valid, fields) => {
  119. if (valid) {
  120. await handleLogin({
  121. email: loginForm.value.email,
  122. captcha: loginForm.value.captcha,
  123. type: 'email',
  124. })
  125. }
  126. else { console.log('error submit!', fields) }
  127. })
  128. }
  129. /**
  130. * 登录--提交
  131. */
  132. async function handleLogin(params: any) {
  133. try {
  134. console.log('登陆成功后------000')
  135. const { login } = useUserStore()
  136. await login(params)
  137. console.log('登陆成功后------1111')
  138. closeLoginModal({ status: true, isFirstLogin: true })
  139. }
  140. catch (err: any) {
  141. console.log('Login error:', err, selectedMethod.value)
  142. if (selectedMethod.value === 'email')
  143. errorCodeTxt.value = 'The code is wrong or invalid.'
  144. else
  145. ElMessage.error(err.message || 'Login failed, please try again later.')
  146. }
  147. }
  148. /**
  149. * google登陆成功
  150. */
  151. async function onGoogleLoginSuccess(info: any) {
  152. await handleLogin({
  153. googleKey: info.token,
  154. email: info.userInfo.email,
  155. type: 'google',
  156. })
  157. }
  158. return { emailStep, loginForm, onSelectAccount, errorCodeTxt, seconds, isEmailGoogle, sendEmail, nextStep, onSelectEmail, finishCode, onGoogleLoginSuccess, backStep, getMailCode }
  159. }