This commit is contained in:
Gdpao
2020-08-07 19:59:51 +08:00
22 changed files with 332 additions and 145 deletions

View File

@@ -1,10 +1,11 @@
var md5 = require("../static/js/md5.min.js")
const install = (Vue, vm) => {
// 此为自定义配置参数,具体参数见上方说明
Vue.prototype.$u.http.setConfig({
baseUrl: 'https://dmmall.sdbairui.com/api',
loadingText: '努力加载中~',
loadingTime: 800,
// originalData: true
originalData: true
});
// 请求拦截配置Token等参数
@@ -12,20 +13,37 @@ const install = (Vue, vm) => {
const token = uni.getStorageSync('token');
config.header.Authorization = 'Bearer' + " " + token;
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
return config;
}
// 响应拦截,如配置,每次请求结束都会执行本方法
Vue.prototype.$u.http.interceptor.response = (res) => {
if(parseInt(res.errCode) == 0) {
if(parseInt(res.data.errCode) == 0) {
// res为服务端返回值可能有errCoderesult等字段
// 这里对res.result进行返回将会在this.$u.post(url).then(res => {})的then回调中的res的到
// 如果配置了originalData为true请留意这里的返回值
return res;
} else if(res.errCode == 401) {
return res.data;
} else if(res.data.errCode == 401) {
// 假设201为token失效这里跳转登录
// vm.$u.toast('您还没有登录哦,请先去登录!');
if (res.data.action != "memberinfo") {
@@ -55,7 +73,7 @@ const install = (Vue, vm) => {
} else {
// 如果返回false则会调用Promise的reject回调
// 并将进入this.$u.post(url).then().catch(res=>{})的catch回调中res为服务端的返回值
return res;
return res.data;
}
}