84 lines
2.3 KiB
TypeScript
84 lines
2.3 KiB
TypeScript
import {MockMethod} from "vite-plugin-mock";
|
|
import {Random} from "mockjs";
|
|
|
|
type UserModel = {
|
|
username: string;
|
|
account: string;
|
|
avatar: string;
|
|
vip: number;
|
|
}
|
|
type LoginUserModel = {
|
|
token: string;
|
|
} & UserModel
|
|
|
|
const loginUserList: LoginUserModel[] = [
|
|
{
|
|
token: "123123123123",
|
|
username: '张三',
|
|
account: '123123',
|
|
avatar: 'https://www.cdnjson.com/images/2023/01/31/qwrgFf.jpg',
|
|
vip: Date.now() + 3600 * 24 * 30
|
|
},
|
|
{
|
|
token: "asd2314asdf12",
|
|
username: '李四',
|
|
account: 'admin',
|
|
avatar: 'https://www.cdnjson.com/images/2023/01/31/qwrgFf.jpg',
|
|
vip: 0
|
|
}
|
|
]
|
|
const codeSessions: any = {};
|
|
|
|
function success(data: any = null) {
|
|
return {code: 0, message: 'success', data}
|
|
}
|
|
|
|
function fail(message: string, data: any = null) {
|
|
return {code: 1, message, data}
|
|
}
|
|
|
|
const apiMock: MockMethod[] = [
|
|
{
|
|
url: '/api/user/login',
|
|
method: 'post',
|
|
timeout: 300,
|
|
response: ({body}: { body: { phone: string, code: string } }) => {
|
|
const {phone, code} = body
|
|
if (!code || !codeSessions[phone] || (codeSessions[phone] != code && code != '123')) {
|
|
return fail('验证码不正确')
|
|
}
|
|
const user = loginUserList.filter(s => s.account === phone)
|
|
return user ? success(user) : fail('用户不存在');
|
|
}
|
|
},
|
|
{
|
|
url: '/api/user/info',
|
|
response: ({body}: { body: { phone: string, code: string } }) => {
|
|
console.log(body)
|
|
return success(loginUserList[0])
|
|
}
|
|
},
|
|
{
|
|
url: '/api/sendCode',
|
|
method: 'post',
|
|
timeout: 1500,
|
|
response: ({body}: { body: { phone: string } }) => {
|
|
const code = codeSessions[body.phone] = String(Random.natural(100000, 999999));
|
|
return success(code);
|
|
// return fail('短信发送网关异常', body)
|
|
}
|
|
},
|
|
{
|
|
url: '/api/chat',
|
|
method: 'post',
|
|
timeout: 200,
|
|
response: ({body}: { body: { message: string } }) => {
|
|
if (!body.message) {
|
|
return fail('消息不能为空')
|
|
}
|
|
return success({body, content: Random.cparagraph()});
|
|
}
|
|
}
|
|
]
|
|
|
|
export default apiMock |