暫無描述

auth.ts 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import type { ILoginForm } from '@/api/login'
  2. import type { IAuthLoginRes } from '@/api/types/login'
  3. import { defineStore } from 'pinia'
  4. import { ref } from 'vue'
  5. import { authExamLogin as _authExamLogin, login as _login } from '@/api/login'
  6. import { useTokenStore } from './token'
  7. import { useUserStore } from './user'
  8. export const useAuthStore = defineStore(
  9. 'auth',
  10. () => {
  11. const tokenStore = useTokenStore()
  12. const userStore = useUserStore()
  13. // 登录加载状态
  14. const loginLoading = ref(false)
  15. /**
  16. * 登录成功后处理逻辑
  17. * @param tokenInfo 登录返回的token信息
  18. */
  19. async function _postLogin(tokenInfo: IAuthLoginRes) {
  20. // 使用现有的tokenStore来处理token存储
  21. tokenStore.setTokenInfo(tokenInfo)
  22. await userStore.fetchUserInfo()
  23. }
  24. /**
  25. * 用户登录
  26. * @param loginForm 登录参数
  27. * @returns 登录结果
  28. */
  29. const authLogin = async (loginForm: ILoginForm) => {
  30. try {
  31. loginLoading.value = true
  32. const res = await _login(loginForm)
  33. // 根据不同的token类型输出相应信息
  34. if ('token' in res) {
  35. console.log('登录-res: ', res.token)
  36. }
  37. else if ('accessToken' in res) {
  38. console.log('登录-res: ', res.accessToken)
  39. }
  40. // 直接传递整个res对象,它已经是IAuthLoginRes类型
  41. await _postLogin(res)
  42. uni.showToast({
  43. title: '登录成功',
  44. icon: 'success',
  45. })
  46. return res
  47. }
  48. catch (error) {
  49. console.error('登录失败:', error)
  50. uni.showToast({
  51. title: '登录失败,请重试',
  52. icon: 'error',
  53. })
  54. throw error
  55. }
  56. finally {
  57. loginLoading.value = false
  58. }
  59. }
  60. /**
  61. * 用户登录
  62. * @param loginForm 登录参数
  63. * @returns 登录结果
  64. */
  65. const authExamLogin = async (loginForm: ILoginForm) => {
  66. try {
  67. loginLoading.value = true
  68. const res = await _authExamLogin(loginForm)
  69. // 根据不同的token类型输出相应信息
  70. if ('token' in res) {
  71. console.log('登录-res: ', res.token)
  72. }
  73. else if ('accessToken' in res) {
  74. console.log('登录-res: ', res.accessToken)
  75. }
  76. // 直接传递整个res对象,它已经是IAuthLoginRes类型
  77. await _postLogin(res)
  78. uni.showToast({
  79. title: '登录成功',
  80. icon: 'success',
  81. })
  82. return res
  83. }
  84. catch (error) {
  85. console.error('登录失败:', error)
  86. uni.showToast({
  87. title: '登录失败,请重试',
  88. icon: 'error',
  89. })
  90. throw error
  91. }
  92. finally {
  93. loginLoading.value = false
  94. }
  95. }
  96. /**
  97. * 钉钉免登录
  98. * @param code 可选,钉钉授权码,如果不传则自动获取
  99. * @returns 登录结果
  100. */
  101. const authDingTalkLogin = async (code?: string) => {
  102. try {
  103. loginLoading.value = true
  104. const res = await tokenStore.dingTalkLogin(code)
  105. uni.showToast({
  106. title: '登录成功',
  107. icon: 'success',
  108. })
  109. return res
  110. }
  111. catch (error) {
  112. console.error('钉钉登录失败:', error)
  113. uni.showToast({
  114. title: '钉钉登录失败,请重试',
  115. icon: 'error',
  116. })
  117. throw error
  118. }
  119. finally {
  120. loginLoading.value = false
  121. }
  122. }
  123. return {
  124. loginLoading,
  125. authLogin,
  126. authExamLogin,
  127. authDingTalkLogin,
  128. }
  129. },
  130. {
  131. persist: true,
  132. },
  133. )