request.js 853 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /**
  2. * 网络请求
  3. * @param {Object} req
  4. */
  5. export function request(req) {
  6. let header = {
  7. 'Content-type': 'application/x-www-form-urlencoded'
  8. }
  9. if (req.header) {
  10. header = Object.assign(header, req.header)
  11. }
  12. uni.request({
  13. url: req.url,
  14. data: req.data || {},
  15. header: header,
  16. method: req.method || "GET",
  17. dataType: req.dataType || "json",
  18. success: function(res) {
  19. console.info('--- request success >>>', res)
  20. if (req.success) {
  21. req.success(res.data)
  22. }
  23. },
  24. fail: function(res) {
  25. console.warn('--- request fail >>>', res)
  26. uni.showToast({
  27. title: '网络异常~',
  28. icon: 'none'
  29. })
  30. if (req.fail) {
  31. req.fail(res)
  32. }
  33. },
  34. complete: function(res) {
  35. if (res.statusCode !== 200) {
  36. console.log('--- request http error >>>', res)
  37. }
  38. if (req.complete) {
  39. req.complete(res)
  40. }
  41. }
  42. })
  43. }