examTeamApp/toolJs/https.js

60 lines
1.3 KiB
JavaScript
Raw Normal View History

import tools from '@/toolJs/tools.js'
2024-05-02 15:59:36 +08:00
import store from '../store'
2024-09-26 09:18:27 +08:00
// let baseUrl = "http://tc.pcxbc.com"
2024-10-11 10:04:19 +08:00
let baseUrl = "https://tc.pcxbc.com/testedition"
2024-05-02 15:59:36 +08:00
const httpRequest = (url, method = "get", data) => {
2024-05-29 16:35:45 +08:00
data.token = uni.getStorageSync('token')
data.aan_id = uni.getStorageSync('aan_id')
2024-05-02 15:59:36 +08:00
let httpDefaultOpts = {
url: baseUrl + url,
data: data,
method: method,
header: {
'X-Requested-With': 'XMLHttpRequest',
'content-type': 'application/json; charset=UTF-8',
2024-05-02 15:59:36 +08:00
},
}
let promise = new Promise(function(resolve, reject) {
uni.request(httpDefaultOpts).then(
(res) => {
uni.hideLoading()
2024-05-29 16:35:45 +08:00
console.log("request", url, res, data)
if (res[1].data.code == 20001) {
2024-05-02 15:59:36 +08:00
uni.clearStorageSync()
uni.setStorageSync('token', null)
2024-05-29 16:35:45 +08:00
uni.setStorageSync('aan_id', null)
2024-05-02 15:59:36 +08:00
setTimeout(function() {
uni.reLaunch({
2024-10-11 10:04:19 +08:00
url: "/pageTwo/login/login"
2024-05-02 15:59:36 +08:00
})
}, 2000)
return
}
if (res[1].statusCode != 200) {
2024-05-29 16:35:45 +08:00
tools.msg(res[1].data.msg)
2024-05-02 15:59:36 +08:00
return
}
resolve(res[1].data)
}
).catch(
(response) => {
uni.hideLoading()
reject(response)
}
)
})
return promise
};
const get = (url, data) => {
return httpRequest(url, 'get', data)
}
const post = (url, data) => {
return httpRequest(url, 'post', data)
}
export default {
baseUrl,
get,
post
}