import store from "@/store/index.js"; import utils from '@/utils/utils.js' import functions from '@/utils/functions.js' import {host,APPKEY} from "@/config/host"; import base64 from '@/utils/base64.js'; import { hexMD5 } from '@/utils/md5.js' const service = (params) => { return new Promise((relove, reject) => { let refresh_token=uni.getStorageSync('refresh_token') let token=uni.getStorageSync('access_token') let user=uni.getStorageSync('userinfo') let header={}; if(user&&token&&refresh_token){ store.commit('updateislog',true) store.commit('updaterftk',refresh_token) store.commit('updatetk',token) store.commit('updatauser',user) header['Blade-Auth']=`bearer ${store.state.access_token}` }else{ if(!token&¶ms.url.indexOf('oauth/token')==-1){ uni.navigateTo({ url:'/pages/login/login' }) functions.error('请登录') } } header.Authorization=`Basic ${base64.encode(APPKEY)}` // return if(params.url.indexOf('blade-auth/oauth/token')!=-1){ header['Content-Type']='application/x-www-form-urlencoded' header['Tenant-Id']=params.data.tenantId params.data.password=hexMD5(params.data.password) } // uni.showLoading({ // title:'请求中', // mask:true // }) function req(){ uni.request({ url: host+params.url, data: params.data, method: params.method, // dataType: "json", header }).then(res => { // console.log(res); uni.hideLoading() if (res.statusCode == 200) { let data = res.data; // #ifdef APP utils.ttsspke(data.audio||'') // #endif switch (data.code) { case 200: relove(data); break; case 203: relove(data); break; case 1001: store.commit('updatetk',data.data.token) uni.setStorageSync('token',data.data) req() break; case 1002: uni.navigateTo({ url:'/pages/login/login' }) functions.error('请登录') relove({data:{}}); break; default: relove(data); break; } } else { if(res.statusCode == 400){ relove({}); return }else if(res.statusCode == 500){ functions.error('接口出错,请联系后端开发人员') // #ifdef APP utils.ttsspke('接口出错,请联系后端开发人员') // #endif return }else if(res.statusCode == 401){ uni.navigateTo({ url:'/pages/login/login' }) functions.error('请登录') return } functions.error('连接服务器失败') // #ifdef APP utils.ttsspke('连接服务器失败') // #endif } }).catch(err=>{ console.log(err); functions.error('请检查是否连接到无线网络') // #ifdef APP utils.ttsspke('请检查是否连接到无线网络') // #endif uni.hideLoading() }) } req() }) } export default service;