// uni-app请求封装 export default class Request { http(router, data = {}, method,contentType,timeout=60000) { let that = this; let path ='https://api.cdwjyyh.com';//app正式环境 //let path = 'http://192.168.110.247:8113' // #ifdef H5 //path = 'http://192.168.110.245:8113' path ='https://h5api.his.cdwjyyh.com';//h5接口 // path ='http://a7ad6869.natappfree.cc';//h5接口 //path = 'http://192.168.110.247:8113' //path ='https://h5api.his.cdwjyyh.com';//h5接口 // path ='http://z2fae9e9.natappfree.cc';//直播余红奇本地 // path ='https://api.fhhx.runtzh.com';//直播余红奇线上 // #endif // 看课模块 if(router.indexOf("/app/course/getRealLink") != -1 ) { path ='https://h5api.his.cdwjyyh.com'; } // 直播模块 if(router.indexOf("/appLive") !== -1) { // router = router.replace('/appLive','') path ='https://api.fhhx.runtzh.com'; // router = router.replace('/appLive','') // path ='http://z2fae9e9.natappfree.cc' } //直播静默登录配置 // if (router.indexOf("/live") !== -1 || router.indexOf("/liveAPP") !== -1) { // if(router.indexOf("/liveAPP") !== -1){ // router = router.replace('/liveAPP', '') // } // token = uni.getStorageSync('LiveToken'); // } // 腕表模块 if(router.indexOf("/watch-api") != -1 ) { router = router.replace('/watch-api','') // path = 'http://192.168.10.155:8114' // path = 'http://192.168.10.135:8114' //path = 'http://192.168.110.61:8114' path = 'http://42.194.245.189:8114' } // doctorAi模块 if(router.indexOf("/doctorAi") != -1 ) { router = router.replace('/doctorAi','') path = 'http://doctor.ai.cdwjyyh.com' } uni.setStorageSync('requestPath',path); // uni.showLoading({ // title: '加载中' // }); return new Promise((resolve, reject) => { let token = uni.getStorageSync('AppToken'); let liveToken = uni.getStorageSync('liveToken'); let CompanyUserToken = uni.getStorageSync('CompanyUserToken'); var httpContentType='application/x-www-form-urlencoded'; if(contentType!=undefined){ httpContentType=contentType; } var routers=router; //console.log("---qxj request url:"+`${path}${router}`+" params:"+JSON.stringify(data)); // 直播模块使用liveToken let useToken = token; // 检查是否为直播模块路径 const isLiveApi = router.indexOf("/appLive") !== -1 || router.indexOf("/liveAPP") !== -1; if(isLiveApi) { // 检查liveToken是否存在 if (!liveToken || liveToken === '') { // 跳转到手机号码一键登录页面 uni.navigateTo({ url: '/pages/auth/login?quickLogin=true' }); // 拒绝请求 reject({errMsg: 'No liveToken, redirect to login'}); return; } useToken = liveToken; if(router.indexOf("/appLive") !== -1){ router = router.replace('/appLive', '') } if(router.indexOf("/liveAPP") !== -1){ router = router.replace('/liveAPP', '') } } // 请求 uni.request({ header: { // 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Type': httpContentType, 'AppToken': useToken, 'CompanyUserToken':CompanyUserToken //业务员token }, url: `${path}${router}`, data: data, method: method, timeout: timeout, success: (res) => { //收到开发者服务器成功返回的回调函数 if(res.data.code==401){ //没有权限直接退出到登录界面 let pages = getCurrentPages(); let url = pages[ pages.length - 1]; //当前页页面实例 //如果登录界面已打开,自动关闭 if(url!=undefined&&url.route=="pages/auth/login"){ resolve(res.data) return; } } if(res.token) { uni.setStorageSync('AppToken',res.token) } resolve(res.data) }, fail:(res) =>{ //接口调用失败的回调函数 reject(res); }, complete:(res) =>{ // console.log("complete==",`${path}${router}`,res) if (res.data&&res.data.code == 401) { uni.$emit('loginOut'); return false } uni.hideLoading(); } }); }) } }