修改了api集中化 搞定了vuex
This commit is contained in:
@@ -1,20 +1,35 @@
|
||||
import axios from '../config/axiosConfig'
|
||||
|
||||
import { AxiosRequestConfig, CustomSuccessData } from 'axios';
|
||||
import { getValue } from '@/utils/common';
|
||||
|
||||
// 泛型接口
|
||||
export interface Get {
|
||||
<T>(url: string, params?: object, config?: AxiosRequestConfig): Promise<CustomSuccessData<T>>;
|
||||
}
|
||||
|
||||
axios.interceptors.response.use((response)=>{
|
||||
return response.data;
|
||||
return response;
|
||||
},(error)=>{
|
||||
return error;
|
||||
})
|
||||
|
||||
function get(url: string,data?: object) {
|
||||
return axios.get(url,{params:data})
|
||||
const get: Get = async function (url: string,data?: object) {
|
||||
const res = await axios.get(url,{params:data});
|
||||
return res.data;
|
||||
}
|
||||
|
||||
function post(url: string,data?: object) {
|
||||
return axios.post(url,data)
|
||||
const post: Get = async function (url: string,data?: object) {
|
||||
const res = await axios.post(url,data)
|
||||
return res.data;
|
||||
}
|
||||
|
||||
function setToken(){
|
||||
axios.defaults.headers.common['Authorization'] = "Bearer " + getValue("token");
|
||||
}
|
||||
|
||||
export {
|
||||
get,
|
||||
post
|
||||
post,
|
||||
setToken
|
||||
}
|
||||
@@ -1,14 +1,10 @@
|
||||
import { get, post } from './base'
|
||||
import router from '@/router';
|
||||
import store from '@/store';
|
||||
import { LoginData, UserInfo } from '@/types';
|
||||
import { saveValue } from '@/utils/common';
|
||||
import { message } from 'ant-design-vue';
|
||||
import { get, post, setToken } from './base'
|
||||
|
||||
/**
|
||||
* 请求验证码
|
||||
* @param phone 手机号
|
||||
* @param type 类型 0国内 1国外
|
||||
*/
|
||||
|
||||
export function sendsms(phone: string, type?: number){
|
||||
return post('SendSms',{phone, type})
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户名密码登录
|
||||
@@ -16,13 +12,31 @@ export function sendsms(phone: string, type?: number){
|
||||
* @param password 密码
|
||||
*/
|
||||
|
||||
export function loginpass(phone: string, password: string){
|
||||
return post("login",{type: 2,username: phone, password: password})
|
||||
export async function loginpass(phone: string, password: string){
|
||||
const res = await post<LoginData>("login",{type: 2,username: phone, password: password});
|
||||
console.log(res.code)
|
||||
if(res.code == 1){
|
||||
message.error(res.msg)
|
||||
}else{
|
||||
console.log(res.data)
|
||||
if(!saveValue("token", res.data?.api_token) && !saveValue("memberid", res.data?.memberid) ){
|
||||
message.error("存储错误, 请允许网页使用本地存储!")
|
||||
}else{
|
||||
setToken();
|
||||
store.commit("login");
|
||||
router.push("/mine/archives")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户信息详情
|
||||
* 用户头像和用户名字
|
||||
*/
|
||||
export function userinfo(){
|
||||
return get('personalInfo')
|
||||
}
|
||||
export async function userinfo(){
|
||||
const user = await get<UserInfo>('personalInfo');
|
||||
console.log(user.data?.img)
|
||||
return {
|
||||
head: user.data?.img,
|
||||
username: user.data?.name
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user