request.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. export default function request(config = {}) {
  2. let {
  3. url,
  4. data = {},
  5. method = "GET",
  6. header = {},
  7. loading = false,
  8. loadingText = "请求中..."
  9. } = config
  10. // 拼接url TODO 这里要修改,改成不同环境取不同的url
  11. url = process.env.BASE_API_URL + url;
  12. // 添加token
  13. header['Authorization'] = uni.getStorageSync('token');
  14. // 是否显示loading
  15. if (loading) {
  16. uni.showLoading({
  17. title: loadingText,
  18. mask: true
  19. });
  20. }
  21. return new Promise((resolve, reject) => {
  22. uni.request({
  23. url,
  24. data,
  25. method,
  26. header,
  27. withCredentials: true,
  28. success: res => {
  29. if (res.data.code === '0') {
  30. resolve(res.data.data)
  31. } else if (res.data.code === '999') {
  32. uni.showModal({
  33. title: "错误提示",
  34. content: res.data.msg,
  35. showCancel: false
  36. })
  37. reject(res.data)
  38. } else {
  39. uni.showToast({
  40. title: res.data.msg,
  41. icon: "none"
  42. })
  43. reject(res.data)
  44. }
  45. },
  46. fail: err => {
  47. reject(err)
  48. },
  49. complete: () => {
  50. if (loading) {
  51. uni.hideLoading();
  52. }
  53. }
  54. })
  55. })
  56. }