import { API } from './constants'
|
|
|
|
function getBaseURL() {
|
|
return API.BASE
|
|
}
|
|
|
|
function request(method, path, params = {}) {
|
|
return new Promise((resolve, reject) => {
|
|
const token = uni.getStorageSync('staffToken') || ''
|
|
|
|
uni.request({
|
|
url: getBaseURL() + path,
|
|
method: method,
|
|
data: method === 'GET' ? undefined : params,
|
|
dataType: 'json',
|
|
header: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': token ? 'Bearer ' + token : '',
|
|
},
|
|
timeout: 10000,
|
|
success(res) {
|
|
const data = res.data
|
|
if (data.code === 0) {
|
|
resolve(data.data)
|
|
} else if (data.code === -1) {
|
|
if (res.statusCode === 401) {
|
|
uni.removeStorageSync('staffToken')
|
|
uni.reLaunch({ url: '/pages/staff/login' })
|
|
}
|
|
uni.showToast({ title: data.msg || '请求失败', icon: 'none' })
|
|
reject(new Error(data.msg))
|
|
} else {
|
|
resolve(data)
|
|
}
|
|
},
|
|
fail(err) {
|
|
uni.showToast({ title: '网络错误', icon: 'none' })
|
|
reject(err)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
export function get(path, params = {}) {
|
|
const qs = Object.keys(params).map(k => k + '=' + encodeURIComponent(params[k])).join('&')
|
|
return request('GET', qs ? path + '?' + qs : path)
|
|
}
|
|
|
|
export function post(path, data = {}) {
|
|
return request('POST', path, data)
|
|
}
|