Files
xingbangtuan/main.js
2020-07-12 11:20:28 +08:00

77 lines
2.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import Vue from 'vue'
import store from './store'
import App from './App'
Vue.config.productionTip = false
App.mpType = 'app'
Vue.prototype.path = 'https://xyb.wlkjwl.cn/'
// Vue.prototype.path = 'http://192.168.0.156:8091'
Vue.prototype.$store = store;
//异步请求
Vue.prototype.request = function(options){
let userInfo = uni.getStorageSync('userInfo') || '';
let that = this
,token = userInfo.token
,success = options.success
,error = options.error;
//指定、绑定options中的参数
options.data = options.data || {};
options.headers = options.headers || {};
// options.headers["content-type"] = 'application/x-www-form-urlencoded'
//有token 自动传递参数
if(token){
let sendData = typeof options.data === 'string'
? JSON.parse(options.data)
: options.data;
//自动给参数传入默认 token
options.data["Access-Token"] = token in sendData
? options.data["Access-Token"]
: (token || '');
//自动给 Request Headers 传入 token
options.headers["Access-Token"] = token in options.headers
? options.headers["Access-Token"]
: (token || '');
}
//删除成功、失败具体业务具体写
delete options.success;
delete options.error;
//返回公共请求封装一个公共的弹窗组件code码异常错误时使用
return uni.request(Object.assign({
type: 'post'
,method:'get'
,dataType: 'json'
,header:options.headers
,success: function(res){
//这里可以用于一些状态判断,登录信息失效或者其他状态码异常判断
//只要 http 状态码正常,无论 response 的 code 是否正常都执行 success
typeof success === 'function' && success(res);
}
,error: function(e, code){
uni.showToast({
title: '错误提示!<br>携带参数:'+JSON.stringify(options.data),
duration: 2000,
icon: 'none'
});
//页面公共错误提示
typeof error === 'function' && error(res);
}
},options));
}
const app = new Vue({
...App
})
app.$mount()