123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- import { objToUrl } from '@/utils/index';
- function request(options) {
- return new Promise((resolve, reject) => {
- let _obj = {
- url: '',
- data: {},
- method: 'GET',
- header: {
- 'Content-Type': 'application/json;charset=UTF-8'
- }
- };
- const baseUrl = getApp().globalData.baseUrl
- Object.assign(_obj, options);
- const header = {
- 'Version': '1.0.0',
- 'X-AUTH-TOKEN': uni.getStorageSync('token')
- }
- _obj.header = header
- _obj.header['X-HEADER-APPID'] = '!z2Yc.aes|+Dtf7OBN+iytt6MIem2RukRIvLjBhIS/whu4EuxvvFk=';
-
- // 其它小程序传的参数
- const apiHeader = getApp().globalData.extraData
- console.log(apiHeader,'apiHeader')
- if(apiHeader){
- _obj.header['X-MERCHANT-NO'] = apiHeader['X-MERCHANT-NO']
- _obj.header['X-SIGN-TYPE'] = apiHeader['X-SIGN-TYPE']
- _obj.header['X-SIGN-INFO'] = apiHeader['X-SIGN-INFO']
- _obj.header['X-TIMESTAMP'] = apiHeader['X-TIMESTAMP']
- }
-
- _obj.url = baseUrl + _obj.url;
- _obj.method = _obj.method.toUpperCase();
- _obj.success = function(res) {
- if (_obj.url.indexOf('checkLogin') > -1) {
- resolve(res.data);
- } else if (res.data.status === '900010') {
- setTimeout(function() {
- wx.showToast({
- title: '登录已过期,请重新登录',
- icon: 'none',
- duration: 1000
- });
- }, 10);
- setTimeout(function() {
- const currentRoute = getRoutePath();
- mpvue.removeStorage({
- key: 'token'
- });
- const url = `/pages/login/login?lanuch=${currentRoute.lanuch}&path=` + encodeURIComponent(currentRoute.url);
- mpvue.reLaunch({ url });
- }, 1000);
- } else {
- resolve(res.data);
- }
- };
- _obj.fail = function(err) {
- reject(err);
- };
- _obj.complete = function() {};
- uni.request(_obj);
- });
- }
- function getRoutePath() {
- // eslint-disable-next-line no-undef
- const pages = getCurrentPages(); // 获取加载的页面
- const currentPage = pages[pages.length - 1]; // 获取当前页面的对象
- let url = currentPage.route; // 当前页面url
- const options = objToUrl(currentPage.options); // 如果要获取url中所带的参数可以查看options
- const lanuch = [
- 'pages/personData/personData'
- ].includes(url);
- url = '/' + url;
- if (options.length) {
- url += '?' + options;
- }
- return {
- url,
- lanuch
- };
- }
- export default request;
|