http.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import msg from './message';
  2. import BaseTool from '@/utils/tool';
  3. import { loginQuickly } from '@/api/upms/user';
  4. // 根据开发环境或生产环境破设置服务器根域名
  5. let ajaxTimes = 0;
  6. /**
  7. * 记录登录前的页面路径,供登录成功后回跳
  8. * 扫码场景:免登录查看设备详情 → 点操作弹登录 → 登录后应回到原页面继续操作,而不是重新扫码
  9. */
  10. const recordBackUrl = () => {
  11. try {
  12. const pages = getCurrentPages();
  13. if (!pages || pages.length === 0) {
  14. return;
  15. }
  16. const cur = pages[pages.length - 1];
  17. const opts = cur.options || (cur.$page && cur.$page.options) || {};
  18. const query = Object.keys(opts)
  19. .map(k => k + '=' + encodeURIComponent(opts[k]))
  20. .join('&');
  21. uni.setStorageSync('login_back_url', '/' + cur.route + (query ? '?' + query : ''));
  22. } catch (e) {
  23. // 取不到当前页面时忽略,登录后走默认首页
  24. }
  25. };
  26. /**
  27. * 取出并清除登录回跳地址(由登录成功逻辑调用)
  28. */
  29. export const takeBackUrl = () => {
  30. const url = uni.getStorageSync('login_back_url');
  31. uni.removeStorageSync('login_back_url');
  32. return url;
  33. };
  34. /**
  35. * 登录方式选择弹框:账号登录 / 微信快捷登录
  36. * 供两处复用:1) 接口返回 401 登录过期;2) 未登录点击操作类按钮时的前置拦截
  37. */
  38. export const goLogin = (content) => {
  39. recordBackUrl();
  40. uni.showModal({
  41. content: content || '该操作需要登录后进行,请先登录',
  42. showCancel: true,
  43. cancelText: '账号登录',
  44. confirmText: '快捷登录',
  45. success: res => {
  46. if (res.confirm) {
  47. loginQuickly().then(res => {
  48. const url = res.data
  49. if (url !== '' && url !== null && url != undefined) {
  50. // 请求用户授权
  51. window.location.href = url
  52. }
  53. })
  54. } else if (res.cancel) {
  55. uni.reLaunch({ url: '/pages/login/login' })
  56. }
  57. }
  58. })
  59. };
  60. /**
  61. * 操作类按钮统一登录校验(扫码查看设备信息无需登录,但提交类操作必须先登录)
  62. * @param {Function} next 已登录时执行的实际操作
  63. * @param {String} content 未登录时的提示文案
  64. * @return {Boolean} 是否已登录
  65. */
  66. export const requireLogin = (next, content) => {
  67. if (BaseTool.UserUtil.isLogin()) {
  68. if (typeof next === 'function') {
  69. next();
  70. }
  71. return true;
  72. }
  73. goLogin(content);
  74. return false;
  75. };
  76. export default ({ url, method, data }) => {
  77. let closeLoading = true;
  78. return new Promise((resolve, reject) => {
  79. ajaxTimes++;
  80. msg.loadingMustClose();
  81. uni.request({
  82. url: BaseTool.UserUtil.getBaseUrl() + url,
  83. header: {
  84. 'Content-Type': 'application/json;charset=UTF-8',
  85. Authorization: BaseTool.UserUtil.getToken(url)
  86. },
  87. data: data,
  88. method: method || 'GET',
  89. success (res) {
  90. // todo future 登录过期,需要单独处理
  91. if (res.data.code === 401) {
  92. // msg.hideLoading();
  93. // closeLoading = false;
  94. uni.clearStorageSync();
  95. // #ifdef MP-WEIXIN
  96. if (url.indexOf('/workplace/backlogs/user/page') >= 0) {
  97. return reject(res.data)
  98. }
  99. // #endif
  100. goLogin(res.data.message);
  101. return reject(res.data)
  102. }
  103. if (res.data.code === 403) {
  104. // uni.showModal({ content: res.data.message, showCancel: false })
  105. // msg.hideLoading();
  106. // closeLoading = false;
  107. uni.showToast({ icon: 'none', title: res.data.message });
  108. return reject(res);
  109. }
  110. // 重定向的专门处理
  111. if (res.data.code === 301) {
  112. // uni.showModal({ content: res.data.message, showCancel: false })
  113. // msg.hideLoading();
  114. // closeLoading = false;
  115. window.location.href = res.data
  116. }
  117. if (res.data.code !== 0) {
  118. // uni.showModal({ content: res.data.message, showCancel: false })
  119. // msg.hideLoading();
  120. // closeLoading = false;
  121. setTimeout(() => {
  122. uni.showToast({ icon: 'none', title: res.data.message });
  123. }, 50)
  124. return reject(res.data);
  125. }
  126. resolve(res.data);
  127. },
  128. fail (err) {
  129. // msg.hideLoading();
  130. // closeLoading = false;
  131. uni.showToast({ icon: 'none', title: '系统异常,请联系客服!' });
  132. reject(err);
  133. },
  134. complete: () => {
  135. ajaxTimes--;
  136. if (ajaxTimes === 0) {
  137. // 关闭正在等待的图标
  138. closeLoading = false;
  139. msg.hideLoading();
  140. }
  141. }
  142. });
  143. }).finally(() => {
  144. // if (closeLoading) {
  145. // msg.hideLoading();
  146. // }
  147. });
  148. };