http.js 5.7 KB

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