You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
149 lines
3.3 KiB
149 lines
3.3 KiB
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' |
|
// import { |
|
// useUserStore |
|
// } from "@/store/uaeUserStore"; |
|
|
|
// import { |
|
// storeToRefs |
|
// } from 'pinia'; |
|
|
|
// const userStore = useUserStore() |
|
|
|
// const { |
|
// HANDLE_ACCTOKEN, |
|
// HANDLE_ISLOGIN, |
|
// HANDLE_REFTOKEN, |
|
// HANDLE_USERINFO |
|
// } = userStore |
|
|
|
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 && params.url.indexOf('oauth/token') == -1) { |
|
uni.reLaunch({ |
|
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 |
|
// }) |
|
let urlhd = '' |
|
if (uni.getStorageSync('setip')) { |
|
urlhd = uni.getStorageSync('setip') |
|
} else { |
|
urlhd = host |
|
} |
|
|
|
function req() { |
|
uni.request({ |
|
url: urlhd + 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 |
|
if (data.code != 200 && data?.msg) { |
|
uni.showToast({ |
|
title: data?.msg || '', |
|
icon: 'none' |
|
}) |
|
} else if (data.audio) { |
|
uni.showToast({ |
|
title: data.msg, |
|
icon: 'none' |
|
}) |
|
} |
|
|
|
switch (data.code) { |
|
case 200: |
|
relove(data); |
|
break; |
|
case 203: |
|
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.reLaunch({ |
|
url: '/pages/login/login' |
|
}) |
|
uni.removeStorage({ |
|
key: 'access_token' |
|
}) |
|
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; |