useLogin.ts 4.7 KB

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