request.js 1.2 KB

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