53 lines
1.1 KiB
JavaScript
53 lines
1.1 KiB
JavaScript
import { getBaseUrl } from './config'
|
|
import { getToken } from './auth'
|
|
|
|
export function request(options) {
|
|
const baseUrl = getBaseUrl()
|
|
const token = getToken()
|
|
const headers = Object.assign({}, options.header || {})
|
|
if (token) {
|
|
const normalized = token.startsWith('Bearer ') ? token : ('Bearer ' + token)
|
|
headers['Authorization'] = normalized
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
uni.request({
|
|
...options,
|
|
url: baseUrl + options.url,
|
|
header: headers,
|
|
success: (res) => {
|
|
resolve(res)
|
|
},
|
|
fail: (err) => {
|
|
reject(err)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
export function uploadFile({ url, filePath, name = 'file', formData = {} }) {
|
|
const baseUrl = getBaseUrl()
|
|
const token = getToken()
|
|
const header = {}
|
|
if (token) {
|
|
const normalized = token.startsWith('Bearer ') ? token : ('Bearer ' + token)
|
|
header['Authorization'] = normalized
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
uni.uploadFile({
|
|
url: baseUrl + url,
|
|
filePath,
|
|
name,
|
|
formData,
|
|
header,
|
|
success: (res) => {
|
|
resolve(res)
|
|
},
|
|
fail: (err) => {
|
|
reject(err)
|
|
}
|
|
})
|
|
})
|
|
}
|