| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- import type { ILoginForm } from '@/api/login'
- import type { IAuthLoginRes } from '@/api/types/login'
- import { defineStore } from 'pinia'
- import { ref } from 'vue'
- import { authExamLogin as _authExamLogin, login as _login } from '@/api/login'
- import { useTokenStore } from './token'
- import { useUserStore } from './user'
- export const useAuthStore = defineStore(
- 'auth',
- () => {
- const tokenStore = useTokenStore()
- const userStore = useUserStore()
- // 登录加载状态
- const loginLoading = ref(false)
- /**
- * 登录成功后处理逻辑
- * @param tokenInfo 登录返回的token信息
- */
- async function _postLogin(tokenInfo: IAuthLoginRes) {
- // 使用现有的tokenStore来处理token存储
- tokenStore.setTokenInfo(tokenInfo)
- await userStore.fetchUserInfo()
- }
- /**
- * 用户登录
- * @param loginForm 登录参数
- * @returns 登录结果
- */
- const authLogin = async (loginForm: ILoginForm) => {
- try {
- loginLoading.value = true
- const res = await _login(loginForm)
- // 根据不同的token类型输出相应信息
- if ('token' in res) {
- console.log('登录-res: ', res.token)
- }
- else if ('accessToken' in res) {
- console.log('登录-res: ', res.accessToken)
- }
- // 直接传递整个res对象,它已经是IAuthLoginRes类型
- await _postLogin(res)
- uni.showToast({
- title: '登录成功',
- icon: 'success',
- })
- return res
- }
- catch (error) {
- console.error('登录失败:', error)
- uni.showToast({
- title: '登录失败,请重试',
- icon: 'error',
- })
- throw error
- }
- finally {
- loginLoading.value = false
- }
- }
- /**
- * 用户登录
- * @param loginForm 登录参数
- * @returns 登录结果
- */
- const authExamLogin = async (loginForm: ILoginForm) => {
- try {
- loginLoading.value = true
- const res = await _authExamLogin(loginForm)
- // 根据不同的token类型输出相应信息
- if ('token' in res) {
- console.log('登录-res: ', res.token)
- }
- else if ('accessToken' in res) {
- console.log('登录-res: ', res.accessToken)
- }
- // 直接传递整个res对象,它已经是IAuthLoginRes类型
- await _postLogin(res)
- uni.showToast({
- title: '登录成功',
- icon: 'success',
- })
- return res
- }
- catch (error) {
- console.error('登录失败:', error)
- uni.showToast({
- title: '登录失败,请重试',
- icon: 'error',
- })
- throw error
- }
- finally {
- loginLoading.value = false
- }
- }
- /**
- * 钉钉免登录
- * @param code 可选,钉钉授权码,如果不传则自动获取
- * @returns 登录结果
- */
- const authDingTalkLogin = async (code?: string) => {
- try {
- loginLoading.value = true
- const res = await tokenStore.dingTalkLogin(code)
- uni.showToast({
- title: '登录成功',
- icon: 'success',
- })
- return res
- }
- catch (error) {
- console.error('钉钉登录失败:', error)
- uni.showToast({
- title: '钉钉登录失败,请重试',
- icon: 'error',
- })
- throw error
- }
- finally {
- loginLoading.value = false
- }
- }
- return {
- loginLoading,
- authLogin,
- authExamLogin,
- authDingTalkLogin,
- }
- },
- {
- persist: true,
- },
- )
|