uniapp-h5/src/utils/http.js

65 lines
1.5 KiB
JavaScript

//邮储银行专用的
import useCode from "../pages/ycnc/hooks/useCode";
import {login} from '../api/ycnc'
function http(options) {
uni.showLoading({
title: "加载中",
mask: true,
});
return new Promise((resolve, reject) => {
uni.request({
...options,
header: {
// 'content-type': 'application/json',
'content-type': 'application/x-www-form-urlencoded',
},
responseType: 'json',
success: async (res) => {
if (res.statusCode && res.statusCode != 200) {
uni.hideLoading().then(() => {
uni.showToast({
title: "api错误" + res.errMsg,
icon: 'error'
});
})
reject()
} else {
if(res.data?.code >= 200 && res.data?.code < 300){
uni.hideLoading()
resolve(res.data.data)
}else if(res.data?.code === 401){
//邮储银行专用的
const authCode = await useCode();
const params = {code:authCode};
const {token} = await login({params});
uni.setStorageSync('token',token);
http(options).then(ress => {
resolve(ress)
})
}else{
uni.hideLoading().then(() => {
uni.showToast({
title: res.data.message,
icon: 'error'
});
})
reject(res.data.message)
}
}
},
// 这里的接口请求,如果出现问题就输出接口请求失败
fail: (err) => {
uni.hideLoading().then(() => {
uni.showToast({
title: err,
icon: 'error'
});
})
reject(err)
}
})
})
}
export default http