| 123456789101112131415161718192021222324252627282930 |
- import { BASE_URL } from '../config/api.js'
- export function request(options = {}) {
- const { url, method = 'GET', data, header = {} } = options
- return new Promise((resolve, reject) => {
- uni.request({
- url: `${BASE_URL}${url}`,
- method,
- data,
- header: {
- 'Content-Type': 'application/json',
- ...header
- },
- success: (res) => {
- const body = res.data || {}
- if (res.statusCode >= 200 && res.statusCode < 300 && body.code === 200) {
- resolve(body)
- return
- }
- const msg = body.message || `请求失败(${res.statusCode})`
- uni.showToast({ title: msg, icon: 'none' })
- reject(body)
- },
- fail: (err) => {
- uni.showToast({ title: '网络异常', icon: 'none' })
- reject(err)
- }
- })
- })
- }
|