You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

244 lines
5.9 KiB

/**
* 通用uni-app网络请求
* 基于 Promise 对象实现更简单的 request 使用方式,支持请求和响应拦截
*/
import config from '@/utils/config.js';
import md5 from '@/utils/md5.js';
import constant from "@/utils/constant"; //常量
/*
// 开放的接口
import http from './interface'
http.config.baseUrl = "http://localhost:8080/api/"
http.request(url:'user/list',method:'GET').then((res)=>{
console.log(JSON.stringify(res))
})
http.get('user/list').then((res)=>{
console.log(JSON.stringify(res))
})
http.get('user/list', {status: 1}).then((res)=>{
console.log(JSON.stringify(res))
})
http.post('user', {id:1, status: 1}).then((res)=>{
console.log(JSON.stringify(res))
})
http.put('user/1', {status: 2}).then((res)=>{
console.log(JSON.stringify(res))
})
http.delete('user/1').then((res)=>{
console.log(JSON.stringify(res))
})
*/
let requestList = {} //api请求记录
// 将当前请求的api记录起来
function addRequestKey(key) {
requestList[key] = true
}
// 将请求完成的api从记录中移除
function removeRequestKey(key) {
delete requestList[key]
}
//当前请求的api是否已有记录
function hitRequestKey(key) {
return requestList[key];
}
// 获取串行请求的key,方便记录
function getLockRequestKey(url, data) {
let lockKey = url;
try {
if (data) {
lockKey += JSON.stringify(data)
}
} catch (e) {}
return lockKey;
}
//签名函数处理
function getSign(jsondata) {
let arr = [];
for (let key in jsondata) {
if (key !== "sign") {
arr.push(key + "=" + jsondata[key]);
}
}
arr.sort();
let sign = decodeURIComponent(arr.join("") + "guagua");
return md5.md5(sign);
}
let that = null,lockKeyData = null;
export default {
config: {
isWaiting: true,
baseUrl:config.baseUrl,
header: {
"Content-Type":"application/x-www-form-urlencoded"
},
data: {},
method: "GET",
dataType: "json",
/* 如设为json,会对返回的数据做一次 JSON.parse */
responseType: "text",
success(res) {
that.config.isWaiting && uni.hideLoading();
},
fail(res) {
that.config.isWaiting && uni.hideLoading();
},
complete() {
}
},
interceptor: {
request: null,
response: null
},
request(options) {
that = this;
if (!options) {
options = {}
}
options.baseUrl = options.baseUrl || this.config.baseUrl;
options.dataType = options.dataType || this.config.dataType;
options.url = options.baseUrl + options.url;
options.data = options.data || {};
options.method = options.method || this.config.method;
//添加参数自定义参数
lockKeyData = getLockRequestKey(options.url, options.data);
if (hitRequestKey(lockKeyData)) {
console.log(requestList)
return false;
}
addRequestKey(lockKeyData);
let isWaiting = true;
if (options.data && options.data.isWaiting === false) {
isWaiting = false;
delete options.data.isWaiting;
}
isWaiting && uni.showLoading({
title: '加载中',
mask: true
});
//TODO 加密数据
//TODO 数据签名
let loginInfo = uni.getStorageSync(constant.LOGIN_INFO) || {};
options.data.time = parseInt(new Date().getTime() / 1000);
if(loginInfo && loginInfo.UserId){
options.data.UserId = loginInfo.UserId;//用户id
}
this.config.header.loginTicket = loginInfo.loginTicket || ""; //防止数据串改
options.data.sign = getSign(options.data);
return new Promise((resolve, reject) => {
let _config = null;
options.complete = (response) => {
let statusCode = response.statusCode;
response.config = _config;
if (process.env.NODE_ENV === 'development') {
if (statusCode === 200) {
//console.log("【" + _config.requestId + "】 结果:" + JSON.stringify(response.data))
}
}
if (this.interceptor.response) {
let newResponse = this.interceptor.response(response);
if (newResponse) {
response = newResponse;
}
}
// 统一的响应日志记录
_reslog(response);
if (statusCode === 200) { //成功
resolve(response);
} else {
reject(response);
}
}
_config = Object.assign({}, this.config, options);
_config.requestId = new Date().getTime();
if (this.interceptor.request) {
this.interceptor.request(_config);
}
// 统一的请求日志记录
_reqlog(_config);
uni.request(_config);
});
},
get(url, data, options) {
if (!options) {
options = {}
}
options.url = url
options.data = data
options.method = 'GET'
return this.request(options)
},
post(url, data, options) {
if (!options) {
options = {}
}
options.url = url
options.data = data
options.method = 'POST'
return this.request(options)
},
put(url, data, options) {
if (!options) {
options = {}
}
options.url = url
options.data = data
options.method = 'PUT'
return this.request(options)
},
delete(url, data, options) {
if (!options) {
options = {}
}
options.url = url
options.data = data
options.method = 'DELETE'
return this.request(options)
}
}
/**
* 请求接口日志记录
*/
function _reqlog(req) {
removeRequestKey(lockKeyData); //删除请求的key
if (process.env.NODE_ENV === 'development') {
//console.log("【" + req.requestId + "】 地址:" + req.url)
if (req.data) {
console.log("【" + req.requestId + "】 请求参数:" + JSON.stringify(req.data))
}
}
//TODO 调接口异步写入日志数据库
}
/**
* 响应接口日志记录
*/
function _reslog(res) {
let _statusCode = res.statusCode;
if (process.env.NODE_ENV === 'development') {
//console.log("【" + res.config.requestId + "】 地址:" + res.config.url)
if (res.config.data) {
console.log("【" + res.config.requestId + "】 响应参数:" + JSON.stringify(res.config.data))
}
//console.log("【" + res.config.requestId + "】 响应结果:" + JSON.stringify(res))
}
//TODO 除了接口服务错误外,其他日志调接口异步写入日志数据库
switch (_statusCode) {
case 200:
break;
case 401:
break;
case 404:
break;
default:
break;
}
}