64 lines
1.6 KiB
JavaScript
64 lines
1.6 KiB
JavaScript
// 代理端API配置
|
|
const BASE_URL = 'http://localhost:8888'
|
|
|
|
class Api {
|
|
constructor() {
|
|
this.baseURL = BASE_URL
|
|
}
|
|
|
|
request(options) {
|
|
return new Promise((resolve, reject) => {
|
|
const token = uni.getStorageSync('token')
|
|
|
|
uni.request({
|
|
url: this.baseURL + options.url,
|
|
method: options.method || 'GET',
|
|
data: options.data || {},
|
|
header: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': token ? `Bearer ${token}` : '',
|
|
'User-Type': 'agent', // 标识代理端
|
|
...options.header
|
|
},
|
|
success: (res) => {
|
|
if (res.statusCode === 200) {
|
|
if (res.data.code === 0) {
|
|
resolve(res.data)
|
|
} else {
|
|
reject(new Error(res.data.msg || '请求失败'))
|
|
}
|
|
} else if (res.statusCode === 401) {
|
|
uni.removeStorageSync('token')
|
|
uni.reLaunch({
|
|
url: '/pages/login/login'
|
|
})
|
|
reject(new Error('登录已过期'))
|
|
} else {
|
|
reject(new Error('网络错误'))
|
|
}
|
|
},
|
|
fail: (err) => {
|
|
reject(new Error('网络连接失败'))
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
get(url, data) {
|
|
return this.request({ url, method: 'GET', data })
|
|
}
|
|
|
|
post(url, data) {
|
|
return this.request({ url, method: 'POST', data })
|
|
}
|
|
|
|
put(url, data) {
|
|
return this.request({ url, method: 'PUT', data })
|
|
}
|
|
|
|
delete(url, data) {
|
|
return this.request({ url, method: 'DELETE', data })
|
|
}
|
|
}
|
|
|
|
module.exports = new Api() |