useLogin.ts 4.1 KB

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