http.js 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import msg from './message';
  2. import BaseTool from '@/utils/tool';
  3. import { loginQuickly } from '@/api/upms/user';
  4. // 根据开发环境或生产环境破设置服务器根域名
  5. let ajaxTimes = 0;
  6. export default ({ url, method, data }) => {
  7. let closeLoading = true;
  8. return new Promise((resolve, reject) => {
  9. ajaxTimes++;
  10. msg.loadingMustClose();
  11. uni.request({
  12. url: BaseTool.UserUtil.getBaseUrl() + url,
  13. header: {
  14. 'Content-Type': 'application/json;charset=UTF-8',
  15. Authorization: BaseTool.UserUtil.getToken(url)
  16. },
  17. data: data,
  18. method: method || 'GET',
  19. success (res) {
  20. // todo future 登录过期,需要单独处理
  21. if (res.data.code === 401) {
  22. // msg.hideLoading();
  23. // closeLoading = false;
  24. uni.clearStorageSync();
  25. // #ifdef MP-WEIXIN
  26. if (url.indexOf('/workplace/backlogs/user/page') >= 0) {
  27. return reject(res.data)
  28. }
  29. // #endif
  30. uni.showModal({
  31. content: res.data.message,
  32. showCancel: true,
  33. cancelText: '账号登录',
  34. confirmText: '快捷登录',
  35. success: res => {
  36. if (res.confirm) {
  37. loginQuickly().then(res => {
  38. const url = res.data
  39. if (url !== '' && url !== null && url != undefined) {
  40. // 请求用户授权
  41. window.location.href = url
  42. }
  43. })
  44. } else if (res.cancel) {
  45. uni.reLaunch({ url: '/pages/login/login' })
  46. }
  47. }
  48. })
  49. return reject(res.data)
  50. }
  51. if (res.data.code === 403) {
  52. // uni.showModal({ content: res.data.message, showCancel: false })
  53. // msg.hideLoading();
  54. // closeLoading = false;
  55. uni.showToast({ icon: 'none', title: res.data.message });
  56. return reject(res);
  57. }
  58. // 重定向的专门处理
  59. if (res.data.code === 301) {
  60. // uni.showModal({ content: res.data.message, showCancel: false })
  61. // msg.hideLoading();
  62. // closeLoading = false;
  63. window.location.href = res.data
  64. }
  65. if (res.data.code !== 0) {
  66. // uni.showModal({ content: res.data.message, showCancel: false })
  67. // msg.hideLoading();
  68. // closeLoading = false;
  69. setTimeout(() => {
  70. uni.showToast({ icon: 'none', title: res.data.message });
  71. }, 50)
  72. return reject(res.data);
  73. }
  74. resolve(res.data);
  75. },
  76. fail (err) {
  77. // msg.hideLoading();
  78. // closeLoading = false;
  79. uni.showToast({ icon: 'none', title: '系统异常,请联系客服!' });
  80. reject(err);
  81. },
  82. complete: () => {
  83. ajaxTimes--;
  84. if (ajaxTimes === 0) {
  85. // 关闭正在等待的图标
  86. closeLoading = false;
  87. msg.hideLoading();
  88. }
  89. }
  90. });
  91. }).finally(() => {
  92. // if (closeLoading) {
  93. // msg.hideLoading();
  94. // }
  95. });
  96. };