request文件
import axios from 'axios'
import {
baseURL,
h5Config,
isDev
} from "@/config"
import md5 from "@/node_modules/js-md5/src/md5.js"
import {
Encrypt,
Decrypt
} from '@/commonjs/des.js';
import {
Base64
} from '@/node_modules/js-base64/base64.js'
// 适配uniapp
axios.defaults.adapter = function(config) {
return new Promise((resolve, reject) => {
var settle = require('axios/lib/core/settle');
var buildURL = require('axios/lib/helpers/buildURL');
uni.request({
method: config.method.toUpperCase(),
url: config.baseURL + buildURL(config.url, config.params, config.paramsSerializer),
header: config.headers,
data: config.data,
dataType: config.dataType,
responseType: config.responseType,
sslVerify: config.sslVerify,
complete: function complete(response) {
response = {
data: response.data,
status: response.statusCode,
errMsg: response.errMsg,
header: response.header,
config: config
};
settle(resolve, reject, response);
}
})
})
}
// 创建 axios 实例
const request = axios.create({
baseURL,
timeout: 6000, // 请求超时时间
headers: {
"Content-Type": "application/json;charset=UTF-8",
isCheck: true
}
})
// 错误处理函数
const err = (error) => {
if (error.response) {
const data = error.response.data
uni.showLoading({
title: data.msg || "系统异常,请稍后再试",
icon: "none"
})
} else {
uni.showLoading({
title: error.message,
icon: "none"
})
}
return Promise.reject(error)
}
// request interceptor(请求拦截器)
request.interceptors.request.use(config => {
// 正式请求参数
config.headers["appId"] = h5Config.key;
if (config.method == 'post') {
var setKey = md5("*****************************").substr(8, 16);
var h1 = Encrypt(JSON.stringify(config.data).toString().replace(new RegExp("\\\\\"", "gm"), "\""),
setKey,
setKey);
config.headers['sign'] = Base64.encode(h1);
config.headers["timestamp"] = new Date().getTime();
}
// 测试服务器临时参数
if(isDev){
config.headers["isDistributor"] = true;
config.headers["isUpload"] = true;
}
return config
})
// response interceptor(接收拦截器)
request.interceptors.response.use((response) => {
const res = response.data;
if (res.code !== 0) {
return Promise.reject(res)
}
return res;
}, err)
export default request;
使用:
import request from "@/utils/request";
export function getAllPosition(id) {
return request({
url: `/api/micro/boms/point/style/`,
method: "get"
})
}
export function getPointDetail(data) {
return request({
url: `/api/micro/bomsPoint/queryPointInfoById`,
method: "post",
data
})
}