2020-08-07 06:39:15 +00:00
|
|
|
|
var md5 = require("../static/js/md5.min.js")
|
2020-06-15 02:00:14 +00:00
|
|
|
|
const install = (Vue, vm) => {
|
|
|
|
|
// 此为自定义配置参数,具体参数见上方说明
|
|
|
|
|
Vue.prototype.$u.http.setConfig({
|
2020-08-18 07:29:52 +00:00
|
|
|
|
baseUrl: 'https://mall.dmygkeji.com/api',
|
2020-06-15 02:00:14 +00:00
|
|
|
|
loadingText: '努力加载中~',
|
2020-08-05 13:06:29 +00:00
|
|
|
|
loadingTime: 800,
|
2020-08-07 03:40:13 +00:00
|
|
|
|
originalData: true
|
2020-06-15 02:00:14 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 请求拦截,配置Token等参数
|
|
|
|
|
Vue.prototype.$u.http.interceptor.request = (config) => {
|
2020-07-21 13:01:42 +00:00
|
|
|
|
|
2020-06-15 03:34:33 +00:00
|
|
|
|
const token = uni.getStorageSync('token');
|
2020-07-21 13:01:42 +00:00
|
|
|
|
|
2020-08-07 06:39:15 +00:00
|
|
|
|
config.header.Authorization = 'Bearer' + " " + token;
|
|
|
|
|
Date.prototype.Format = function (fmt) { // author: meizz
|
|
|
|
|
var o = {
|
|
|
|
|
"M+": this.getMonth() + 1, // 月份
|
|
|
|
|
"d+": this.getDate(), // 日
|
|
|
|
|
"h+": this.getHours(), // 小时
|
|
|
|
|
"m+": this.getMinutes(), // 分
|
|
|
|
|
"s+": this.getSeconds(), // 秒
|
|
|
|
|
"q+": Math.floor((this.getMonth() + 3) / 3), // 季度
|
|
|
|
|
"S": this.getMilliseconds() // 毫秒
|
|
|
|
|
};
|
|
|
|
|
if (/(y+)/.test(fmt))
|
|
|
|
|
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
|
|
|
|
|
for (var k in o)
|
|
|
|
|
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
|
|
|
|
|
return fmt;
|
|
|
|
|
}
|
|
|
|
|
let time = new Date().Format("yyyyMMddhhmmss")
|
|
|
|
|
config.header.Sign = md5('DBCA4F8DA7BC0BA2' + time) + '-' + time
|
2020-06-15 02:00:14 +00:00
|
|
|
|
return config;
|
2020-07-21 13:01:42 +00:00
|
|
|
|
|
2020-06-15 02:00:14 +00:00
|
|
|
|
}
|
2020-08-05 13:06:29 +00:00
|
|
|
|
|
|
|
|
|
// 响应拦截,如配置,每次请求结束都会执行本方法
|
|
|
|
|
Vue.prototype.$u.http.interceptor.response = (res) => {
|
2020-08-07 03:40:13 +00:00
|
|
|
|
if(parseInt(res.data.errCode) == 0) {
|
2020-08-05 13:06:29 +00:00
|
|
|
|
// res为服务端返回值,可能有errCode,result等字段
|
|
|
|
|
// 这里对res.result进行返回,将会在this.$u.post(url).then(res => {})的then回调中的res的到
|
|
|
|
|
// 如果配置了originalData为true,请留意这里的返回值
|
2020-08-07 03:40:13 +00:00
|
|
|
|
return res.data;
|
|
|
|
|
} else if(res.data.errCode == 401) {
|
2020-08-05 13:06:29 +00:00
|
|
|
|
// 假设201为token失效,这里跳转登录
|
2020-08-06 12:42:03 +00:00
|
|
|
|
// vm.$u.toast('您还没有登录哦,请先去登录!');
|
2020-08-11 03:12:32 +00:00
|
|
|
|
if (res.data.data.action != "memberinfo") {
|
2020-08-15 11:14:12 +00:00
|
|
|
|
uni.showToast({
|
|
|
|
|
title: "您还没有登录,请先去登录哦!",
|
|
|
|
|
icon: "none"
|
|
|
|
|
})
|
2020-08-11 14:15:58 +00:00
|
|
|
|
setTimeout(() => {
|
|
|
|
|
// 此为uView的方法,详见路由相关文档
|
|
|
|
|
uni.navigateTo({
|
|
|
|
|
url: "/pageA/login/login",
|
|
|
|
|
})
|
|
|
|
|
}, 1000)
|
|
|
|
|
// uni.showModal({
|
|
|
|
|
// title: "温馨提示",
|
|
|
|
|
// content: "您还未登录,请立即登录",
|
|
|
|
|
// cancelText: "以后再说",
|
|
|
|
|
// confirmText: "立即登录",
|
|
|
|
|
// confirmColor: "#FF780F",
|
|
|
|
|
// success(res) {
|
|
|
|
|
// // console.log(res);
|
|
|
|
|
// if (res.confirm) {
|
|
|
|
|
// setTimeout(() => {
|
|
|
|
|
// // 此为uView的方法,详见路由相关文档
|
|
|
|
|
// vm.$u.route('/pageA/login/login')
|
|
|
|
|
// }, 500)
|
|
|
|
|
// }
|
|
|
|
|
// if (res.cancel) {
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// })
|
2020-08-06 12:42:03 +00:00
|
|
|
|
}
|
2020-08-05 13:06:29 +00:00
|
|
|
|
return false;
|
2020-08-06 12:42:03 +00:00
|
|
|
|
} else if (res.errCode == 1) {
|
|
|
|
|
console.log(res.message);
|
2020-08-09 03:31:47 +00:00
|
|
|
|
return res.data;
|
2020-08-05 13:06:29 +00:00
|
|
|
|
} else {
|
|
|
|
|
// 如果返回false,则会调用Promise的reject回调,
|
|
|
|
|
// 并将进入this.$u.post(url).then().catch(res=>{})的catch回调中,res为服务端的返回值
|
2020-08-07 10:03:40 +00:00
|
|
|
|
return res.data;
|
2020-08-05 13:06:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-21 13:01:42 +00:00
|
|
|
|
|
2020-06-15 02:00:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
install
|
|
|
|
|
}
|