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.
280 lines
7.4 KiB
280 lines
7.4 KiB
import { apiUrl } from './config.js';
|
|
import { exitLogin } from './common.js'
|
|
import md5Libs from './md5Lib.js'; //引入MD5加密文件
|
|
|
|
// 这里的vm,就是我们在vue文件里面的this,所以我们能在这里获取vuex的变量,比如存放在里面的token
|
|
// 同时,我们也可以在此使用getApp().globalData,如果你把token放在getApp().globalData的话,也是可以使用的
|
|
const install = (Vue, vm) => {
|
|
Vue.prototype.$u.http.setConfig({
|
|
baseUrl: apiUrl,
|
|
showLoading: false,
|
|
originalData: true,
|
|
// 如果将此值设置为true,拦截回调中将会返回服务端返回的所有数据response,而不是response.data
|
|
// 设置为true后,就需要在this.$u.install.response进行多一次的判断,请打印查看具体值
|
|
// originalData: true,
|
|
});
|
|
// 请求拦截,配置Token等参数
|
|
Vue.prototype.$u.http.interceptor.request = (config) => {
|
|
const systemInfo = uni.getSystemInfoSync();//获取系统信息
|
|
config.header.system = systemInfo.system;
|
|
|
|
config.header.platform = systemInfo.platform;
|
|
// #ifdef H5
|
|
config.header.platform = systemInfo.platform + ' H5';
|
|
// #endif
|
|
// #ifdef MP
|
|
config.header.platform = systemInfo.platform + ' MP';
|
|
// #endif
|
|
// #ifdef APP-VUE
|
|
config.header.platform = systemInfo.platform + ' APP';
|
|
// #endif
|
|
|
|
config.header.model = systemInfo.model;
|
|
var token = '';
|
|
if(uni.getStorageSync('token')){
|
|
token = uni.getStorageSync('token');
|
|
}
|
|
config.header.token = token; //验证用户登录token
|
|
var timestamp = Date.parse(new Date()); //当前时间戳(单位ms);
|
|
timestamp = timestamp / 1000; //(转为单位s)
|
|
|
|
// 接口加密,生成接口认证sign
|
|
// sign= MD5 (接口地址+data+timestamp)
|
|
const httpApi = config.url;//获取请求的接口
|
|
const httpData = config.data;//获取请求接口传的参数
|
|
var httpSign = getSign(httpApi,httpData,timestamp);
|
|
config.header.sign = httpSign;//在不验证登录时API接口验证
|
|
config.header.timestamp = timestamp;//把时间戳也传过去,30秒内有效
|
|
config.header.version = vm.vuex_base.version;
|
|
return config;
|
|
}
|
|
|
|
// 响应拦截,判断状态码是否通过
|
|
Vue.prototype.$u.http.interceptor.response = (res) => {
|
|
var resdata = res.data;
|
|
if(res.statusCode) {
|
|
switch(res.statusCode) {
|
|
case 200:
|
|
if(res.data){
|
|
switch(res.data.code){
|
|
case 1:
|
|
let more = {
|
|
code: res.data.code,
|
|
msg: res.data.msg,
|
|
};
|
|
// 合并并且返回收到的数据
|
|
return deepMerge(more,res.data.data);
|
|
break;
|
|
case 0:
|
|
let errormore = {
|
|
code: res.data.code,
|
|
msg: res.data.msg
|
|
};
|
|
// 合并并且返回收到的数据
|
|
return deepMerge(errormore,res.data.data);
|
|
break;
|
|
}
|
|
}
|
|
break;
|
|
|
|
case 404:
|
|
uni.showToast({
|
|
title: '请求接口不存在',
|
|
icon: 'none'
|
|
})
|
|
break;
|
|
|
|
case 417:
|
|
uni.showToast({
|
|
title: 'abnormal operation!',
|
|
icon: 'none'
|
|
})
|
|
break;
|
|
|
|
case 408:
|
|
uni.showToast({
|
|
title: 'request timeout!',
|
|
icon: 'none'
|
|
})
|
|
break;
|
|
|
|
case 410:
|
|
//token过期登录超时-by_HCH
|
|
uni.hideLoading();//登录信息过期隐藏数据加载提示
|
|
uni.showModal({
|
|
title:'提示',
|
|
content:'登录信息过期,请重新授权登录!',
|
|
showCancel:false,
|
|
success: function(res) {
|
|
if (res.confirm) {
|
|
uni.clearStorageSync();//清空缓存
|
|
uni.redirectTo({
|
|
url: '/pages/user/login'
|
|
})
|
|
}
|
|
}
|
|
})
|
|
return false;
|
|
break;
|
|
|
|
case 401:
|
|
exitLogin();
|
|
uni.hideLoading();
|
|
uni.showModal({
|
|
title: '提示',
|
|
content: res.data.msg,
|
|
confirmText: '登录',
|
|
success: function(res) {
|
|
if (res.confirm) {
|
|
uni.navigateTo({
|
|
url: '/pages/user/login'
|
|
})
|
|
} else if (res.cancel) {}
|
|
}
|
|
});
|
|
break;
|
|
|
|
default:
|
|
uni.showToast({
|
|
title: '操作失败!',
|
|
icon: 'none'
|
|
})
|
|
break;
|
|
|
|
}
|
|
return 0;
|
|
|
|
}else{
|
|
|
|
switch (res.errMsg) {
|
|
case 'request:fail':
|
|
uni.showToast({
|
|
title: 'X﹏X!糟糕,迷路了',
|
|
icon: 'none'
|
|
})
|
|
break;
|
|
case 'request:fail url not in domain list':
|
|
uni.showToast({
|
|
title: '请求失败',
|
|
icon: 'none'
|
|
})
|
|
break;
|
|
default:
|
|
uni.showToast({
|
|
title: '网络错误',
|
|
icon: 'none'
|
|
})
|
|
break;
|
|
}
|
|
return 0;
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取签名 将请求的接口地址拼接上所有参数(参数按字母排序)后拼接加上当前时间戳进行md5
|
|
* url 接口地址
|
|
* date 参数{参数名1 : 值1, 参数名2 : 值2}
|
|
* timestamp 时间戳
|
|
* dev_by HCH 2021-10-15
|
|
*/
|
|
function getSign(url, data, timestamp) {
|
|
//var _ = require('./underscore.js');
|
|
var querystring = [];
|
|
if (data && data.sign) {
|
|
//参数中不能有sign
|
|
return false;
|
|
}else{
|
|
if(data) {
|
|
for(let param in data) {
|
|
// data[param]==0 这个条件加上,不然会判为空,导致拼接的参数缺失
|
|
if(param && (data[param] || data[param]==0)){
|
|
querystring = querystring.concat({
|
|
'name': param,
|
|
'value': data[param]
|
|
})
|
|
}
|
|
}
|
|
}
|
|
//排序(这里去掉了,有需要可以用)
|
|
// querystring = _.sortBy(querystring, 'name');
|
|
//去重(这里去掉了,有需要可以用)
|
|
// querystring = _.uniq(querystring, true, 'name');
|
|
var urlData = url;
|
|
urlData = urlData.toLowerCase();//这里情况特殊,因为后端api接口中有转小写这步操作
|
|
if(querystring.length > 0){
|
|
urlData = urlData + '?';
|
|
}
|
|
for(let i = 0; i < querystring.length; i++) {
|
|
// data[param]==0 这个条件加上,不然会判为空,导致拼接的参数缺失
|
|
if (querystring[i] && querystring[i].name && (querystring[i].value || querystring[i].value==0)) {
|
|
urlData += querystring[i].name + '=' + querystring[i].value;
|
|
if (i < (querystring.length - 1)) {
|
|
urlData += '&';
|
|
}
|
|
}
|
|
}
|
|
var sign = md5Libs.md5(urlData + timestamp);
|
|
return sign;
|
|
}
|
|
}
|
|
|
|
|
|
// 判断arr是否为一个数组,返回一个bool值
|
|
function isArray(arr) {
|
|
return Object.prototype.toString.call(arr) === '[object Array]';
|
|
}
|
|
|
|
|
|
// 深度克隆
|
|
function deepClone(obj) {
|
|
// 对常见的“非”值,直接返回原来值
|
|
if ([null, undefined, NaN, false].includes(obj)) return obj;
|
|
if (typeof obj !== "object" && typeof obj !== 'function') {
|
|
//原始类型直接返回
|
|
return obj;
|
|
}
|
|
var o = isArray(obj) ? [] : {};
|
|
for (let i in obj) {
|
|
if (obj.hasOwnProperty(i)) {
|
|
o[i] = typeof obj[i] === "object" ? deepClone(obj[i]) : obj[i];
|
|
}
|
|
}
|
|
return o;
|
|
}
|
|
|
|
|
|
// JS对象深度合并
|
|
function deepMerge(target = {}, source = {}) {
|
|
target = deepClone(target);
|
|
if(typeof target !== 'object' || typeof source !== 'object') return false;
|
|
for (var prop in source) {
|
|
if(!source.hasOwnProperty(prop)) continue;
|
|
if(prop in target) {
|
|
if(typeof target[prop] !== 'object') {
|
|
target[prop] = source[prop];
|
|
}else{
|
|
if(typeof source[prop] !== 'object') {
|
|
target[prop] = source[prop];
|
|
}else{
|
|
if(target[prop].concat && source[prop].concat) {
|
|
target[prop] = target[prop].concat(source[prop]);
|
|
}else{
|
|
target[prop] = deepMerge(target[prop], source[prop]);
|
|
}
|
|
}
|
|
}
|
|
}else{
|
|
target[prop] = source[prop];
|
|
}
|
|
}
|
|
return target;
|
|
}
|
|
|
|
export default {
|
|
install
|
|
}
|
|
export {
|
|
getSign
|
|
} |