request.js 828 B

123456789101112131415161718192021222324252627282930
  1. import { BASE_URL } from '../config/api.js'
  2. export function request(options = {}) {
  3. const { url, method = 'GET', data, header = {} } = options
  4. return new Promise((resolve, reject) => {
  5. uni.request({
  6. url: `${BASE_URL}${url}`,
  7. method,
  8. data,
  9. header: {
  10. 'Content-Type': 'application/json',
  11. ...header
  12. },
  13. success: (res) => {
  14. const body = res.data || {}
  15. if (res.statusCode >= 200 && res.statusCode < 300 && body.code === 200) {
  16. resolve(body)
  17. return
  18. }
  19. const msg = body.message || `请求失败(${res.statusCode})`
  20. uni.showToast({ title: msg, icon: 'none' })
  21. reject(body)
  22. },
  23. fail: (err) => {
  24. uni.showToast({ title: '网络异常', icon: 'none' })
  25. reject(err)
  26. }
  27. })
  28. })
  29. }