refactor(login): 简化登录逻辑并移除验证码相关字段
重构登录相关代码,移除不再需要的验证码和uuid字段 将登录逻辑集中到token store中处理 优化微信小程序登录的代码格式
This commit is contained in:
@@ -7,8 +7,6 @@ import { http } from '@/http/http'
|
||||
export interface ILoginForm {
|
||||
username: string
|
||||
password: string
|
||||
code?: string
|
||||
uuid?: string
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -32,27 +32,11 @@ async function doLogin() {
|
||||
return
|
||||
}
|
||||
try {
|
||||
// 有的时候后端会用一个接口返回token和用户信息,有的时候会分开2个接口(各有利弊,看业务场景和系统复杂度),这里使用2个接口返回的来模拟
|
||||
// 1/2 调用接口回来后设置token信息
|
||||
// 这里用单token来模拟
|
||||
tokenStore.setTokenInfo({
|
||||
token: '123456',
|
||||
expiresIn: 60 * 60 * 24 * 7,
|
||||
// 调用登录接口
|
||||
await tokenStore.login({
|
||||
username: '菲鸽',
|
||||
password: '123456',
|
||||
})
|
||||
|
||||
// 2/2 调用接口回来后设置用户信息
|
||||
// const res = await login({
|
||||
// username: '菲鸽',
|
||||
// password: '123456',
|
||||
// })
|
||||
// console.log('接口拿到的登录信息:', res)
|
||||
userStore.setUserInfo({
|
||||
userId: 123456,
|
||||
username: 'abc123456',
|
||||
nickname: '菲鸽',
|
||||
avatar: 'https://oss.laf.run/ukw0y1-site/avatar.jpg',
|
||||
})
|
||||
|
||||
console.log(redirectUrl.value)
|
||||
}
|
||||
catch (error) {
|
||||
|
||||
@@ -34,9 +34,9 @@ const { run: uploadAvatar } = useUpload<IUploadSuccessInfo>(
|
||||
// 微信小程序下登录
|
||||
async function handleLogin() {
|
||||
// #ifdef MP-WEIXIN
|
||||
|
||||
// 微信登录
|
||||
await tokenStore.wxLogin()
|
||||
|
||||
// #endif
|
||||
// #ifndef MP-WEIXIN
|
||||
uni.navigateTo({
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
import type {
|
||||
ILoginForm,
|
||||
} from '@/api/login'
|
||||
import type { IAuthLoginRes } from '@/api/types/login'
|
||||
import { defineStore } from 'pinia'
|
||||
import { computed, ref } from 'vue' // 修复:导入 computed
|
||||
@@ -93,17 +96,14 @@ export const useTokenStore = defineStore(
|
||||
|
||||
/**
|
||||
* 用户登录
|
||||
* @param credentials 登录参数
|
||||
* 有的时候后端会用一个接口返回token和用户信息,有的时候会分开2个接口,一个获取token,一个获取用户信息
|
||||
* (各有利弊,看业务场景和系统复杂度),这里使用2个接口返回的来模拟
|
||||
* @param loginForm 登录参数
|
||||
* @returns 登录结果
|
||||
*/
|
||||
const login = async (credentials: {
|
||||
username: string
|
||||
password: string
|
||||
code: string
|
||||
uuid: string
|
||||
}) => {
|
||||
const login = async (loginForm: ILoginForm) => {
|
||||
try {
|
||||
const res = await _login(credentials)
|
||||
const res = await _login(loginForm)
|
||||
console.log('普通登录-res: ', res)
|
||||
await _postLogin(res)
|
||||
uni.showToast({
|
||||
@@ -124,6 +124,8 @@ export const useTokenStore = defineStore(
|
||||
|
||||
/**
|
||||
* 微信登录
|
||||
* 有的时候后端会用一个接口返回token和用户信息,有的时候会分开2个接口,一个获取token,一个获取用户信息
|
||||
* (各有利弊,看业务场景和系统复杂度),这里使用2个接口返回的来模拟
|
||||
* @returns 登录结果
|
||||
*/
|
||||
const wxLogin = async () => {
|
||||
|
||||
Reference in New Issue
Block a user