| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import Vue from "vue";
- import axios from "axios";
- // import md5 from "js-md5";
- const instance = axios.create({
- baseURL: process.env.VUE_APP_BASE_API,
- timeout: 30000,
- withCredentials: true
- });
- // const appKey = "cd72c223-923f-44a3-aede-b9f07dcd56b8";
- // const plat = "steelfurniture";
- // const v = "1.0";
- instance.interceptors.request.use(
- ({ headers: { timestamp = Date.now(), token = "", ...h }, ...x }) => ({
- headers: {
- timestamp,
- // appKey,
- // plat,
- // v,
- // sign: md5(`timestamp${timestamp}plat${plat}v${v}appKey${appKey}`),
- token,
- ...h
- },
- ...x
- })
- );
- instance.interceptors.response.use(
- x => x,
- err => ({
- data: {
- code: -100,
- data: err
- }
- })
- );
- const getResult: <T>(x: IBaseResult) => IResult<T> = <T>({
- data,
- code,
- msg
- }: IBaseResult<T>) => {
- if (code === 0) return [null, data];
- // Message.error(getErrMsg(code));
- return [{ code, msg }, (data || {}) as T];
- };
- export const post: IRequest = async <T>(url: string, params: any) => {
- params.datedate= new Date().getTime();
- const { data } = await instance.post<IBaseResult<T>>(url, params);
- return getResult<T>(data);
- };
- export const get: IRequest = async <T>(url: string, params: any) => {
- params.datedate= new Date().getTime();
- const {data} = await instance.get<IBaseResult<T>>(url, {params});
- return getResult<T>(data);
- };
- export default {
- install(vue: typeof Vue) {
- Object.assign(vue.prototype, {
- $get: get,
- $post: post
- });
- }
- };
|