1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- const BASE_URL = import.meta.env.VITE_API_BASE_URL || ''
- export const http = (options) => {
- const {
- url,
- method = 'GET',
- data = {},
- header = {},
- showLoading = true
- } = options
- if (showLoading) {
- uni.showLoading({
- title: '加载中...',
- mask: true
- })
- }
- return new Promise((resolve, reject) => {
- uni.request({
- url: BASE_URL + url,
- method,
- data,
- header: {
- 'Content-Type': 'application/json',
- ...header
- },
- success: (res) => {
- if (res.statusCode === 200) {
- resolve(res.data)
- } else {
- uni.showToast({
- title: '请求失败',
- icon: 'none'
- })
- reject(res)
- }
- },
- fail: (err) => {
- uni.showToast({
- title: '网络错误',
- icon: 'none'
- })
- reject(err)
- },
- complete: () => {
- if (showLoading) {
- uni.hideLoading()
- }
- }
- })
- })
- }
|