import store from '@/store/index.js'; //vuex let server_url=' ';//请求地址 // let token = ' '; //凭证 // process.env.NODE_ENV === 'development' ? '192.168.0.1' : 'http://***/api' ; //环境配置 function service(options = {}) { let isQueryContain = options.url.indexOf('?')>-1 ? (options.url.split('?')[1].indexOf("hotelId=") > -1 ) : false // console.log(isQueryContain); // store.state.token && (token = store.state.token); //从vuex中获取登录凭证 const token = uni.getStorageSync('token') || ''; options.url = `${server_url}${options.url}`; if (options.url.indexOf('?')>-1 && !isQueryContain) { options.url+= `&hotelId=${store.state.hotelId}`; }else{ options.url+= `?hotelId=${store.state.hotelId}`; } //if(!isQueryContain) options.url+= `?hotelId=${store.state.hotelId}`; //从vuex中获取酒店id //配置请求头 options.header = { 'content-type': 'application/json', // 'accessToken': `${token}`, //Bearer 'Auth':'APPXPLSOspYuisBnjKcYQZmGpq', 'X-Access-Token':`${token}` }; if(options.method=='GET'){ if(options.url.indexOf("sys/dict/getDictItems")<0){ options.params = { _t: Date.parse(new Date())/1000, ...options.params } } } uni.showLoading({title: '加载中'}); // console.log(options); return new Promise((resolved, rejected) => { //成功 options.success = (res) => { // console.log(res); uni.hideLoading(); if (res.data.code==401) { // uni.showToast({ // icon: 'none', // duration: 3000, // title: `${res.data.msg}` // }); uni.clearStorageSync() uni.navigateTo({ url: '/pages/login/login' }); return; } if (res.data.code == 200) { //请求成功 resolved(res.data); } else { // uni.showToast({ // icon: 'none', // duration: 3000, // title: `${res.data.message || ''}` // }); rejected('错误');//错误 } } //错误 options.fail = (err) => { rejected(err); //错误 uni.hideLoading(); } uni.request(options); }); } export default service