12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- const BASE_URL = import.meta.env.VITE_API_BASE_URL
- const request = (url, method = 'GET', data = {}, options = {}) => {
- const {
- showLoading = true,
- loadingText = '加载中...',
- customHeaders = {}
- } = options
- const token = uni.getStorageSync('token')
- if (showLoading) {
- uni.showLoading({ title: loadingText, mask: true })
- }
- return new Promise((resolve, reject) => {
- uni.request({
- url: BASE_URL + url,
- method,
- data,
- header: {
- 'Content-Type': 'application/json',
- 'Authorization': token ? `Bearer ${token}` : '',
- ...customHeaders
- },
- success: res => {
- if (res.statusCode === 200) {
- resolve(res.data)
- } else {
- uni.showToast({
- title: res.data?.message || '请求出错',
- icon: 'none'
- })
- reject(res)
- }
- },
- fail: err => {
- uni.showToast({
- title: '网络异常,请稍后重试',
- icon: 'none'
- })
- reject(err)
- },
- complete: () => {
- if (showLoading) {
- uni.hideLoading()
- }
- }
- })
- })
- }
- export const get = (url, data = {}, options = {}) => request(url, 'GET', data, options)
- export const post = (url, data = {}, options = {}) => request(url, 'POST', data, options)
- export default request
|