qa-education-exam-weapp/utils/request.js

94 lines
2.9 KiB
JavaScript

// let requestPath = 'https://qynx.qhdsafety.com/qa-education/'; // 后台请求地址
// let requestPath = 'http://192.168.0.30:7451/'; // 后台请求地址
let requestPath = 'http://192.168.0.31:7452/qa-education-exam-ent'; // 测试请求地址
const app = getApp()
function post(url, data) {
return new Promise((resolve, reject) => {
if (data && data.loading !== false) {
wx.showLoading({
title: '加载中',
mask: true
});
}
wx.request({
url: requestPath + url,
data: {
USER_ID:app.globalData?.userInfo?.USER_ID,
...data
},
method: 'POST',
header: {
'Content-type': 'application/x-www-form-urlencoded'
},
success: (res) => {
if (data && data.loading !== false) {
wx.hideLoading();
}
if (res.data.result === 'success') {
resolve(res.data)
} else {
wx.showToast({
title: res.data.msg || '系统开小差了',
icon: 'error',
});
reject(res.data)
}
},
fail: (err) => {
if (data && data.loading !== false) {
wx.hideLoading();
}
wx.showToast({
title: '网络错误请重试',
icon: 'error',
});
reject(err)
}
});
})
}
function upload(url, data) {
return new Promise((resolve, reject) => {
if (data && data.loading !== false) {
wx.showLoading({
title: '加载中',
mask: true
});
}
wx.uploadFile({
url: requestPath + url,
filePath: data.filePath,
name: data.name || 'FFILE',
formData: {
USER_ID:app.globalData?.userInfo?.USER_ID,
...data.formData} || {},
success: (res) => {
if (data && data.loading !== false) {
wx.hideLoading();
}
if (JSON.parse(res.data).result === 'success') {
resolve(JSON.parse(res.data))
} else {
wx.showToast({
title: JSON.parse(res.data).msg || '系统开小差了',
icon: 'error',
});
reject(JSON.parse(res.data))
}
},
fail: (err) => {
wx.hideLoading();
wx.showToast({
title: '网络错误请重试',
icon: 'error',
});
reject(err)
}
});
})
}
export {post, upload}