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. selectedMethod.value = 'email'
  43. loginForm.value.email = item.email
  44. emailStep.value = 1
  45. }
  46. }
  47. /**
  48. * dao: 倒计时60秒
  49. */
  50. const countSeconds = () => {
  51. seconds.value = 60
  52. const timer = setInterval(() => {
  53. seconds.value--
  54. if (seconds.value === 0)
  55. clearInterval(timer)
  56. }, 1000)
  57. }
  58. /**
  59. * 邮箱--发送验证码
  60. */
  61. const getMailCode = async () => {
  62. try {
  63. if (seconds.value > 0)
  64. return
  65. // 获取验证码
  66. await getEmailCodeApi({ account: loginForm.value.email, type: 's003_login' })
  67. countSeconds()
  68. }
  69. catch (error) {
  70. console.error('Error sending email code:', error)
  71. }
  72. }
  73. /**
  74. * 邮箱--验证是否存在--是否与已存在google的邮箱--下一步
  75. * @returns
  76. */
  77. async function sendEmail(formEl: FormInstance | undefined) {
  78. if (!formEl)
  79. return
  80. await formEl.validate(async (valid, fields) => {
  81. if (valid) {
  82. // 验证已存在google的邮箱
  83. const res: any = await validateEmailIsExistApi({ email: loginForm.value.email })
  84. if (!res || res !== 'google')
  85. // 如果没有google的邮箱,直接下一步,或者没有注册过邮箱
  86. isEmailGoogle.value = false
  87. if (res === 'google') {
  88. // 显示google登陆按钮
  89. isEmailGoogle.value = true
  90. loginForm.value.email = ''
  91. }
  92. nextStep()
  93. }
  94. else { console.log('error submit!', fields) }
  95. })
  96. }
  97. /**
  98. * 邮箱--验证码--完成登陆 (后台默认为注册)
  99. * @returns
  100. */
  101. async function finishCode(formEl: FormInstance | undefined) {
  102. errorCodeTxt.value = ''
  103. if (!formEl)
  104. return
  105. await formEl.validate(async (valid, fields) => {
  106. if (valid) {
  107. await handleLogin({
  108. email: loginForm.value.email,
  109. captcha: loginForm.value.captcha,
  110. type: 'email',
  111. })
  112. }
  113. else { console.log('error submit!', fields) }
  114. })
  115. }
  116. /**
  117. * 登录--提交
  118. */
  119. async function handleLogin(params: any) {
  120. try {
  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. }