request.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { objToUrl } from '@/utils/index';
  2. function request(options) {
  3. return new Promise((resolve, reject) => {
  4. let _obj = {
  5. url: '',
  6. data: {},
  7. method: 'GET',
  8. header: {
  9. 'Content-Type': 'application/json;charset=UTF-8'
  10. }
  11. };
  12. const baseUrl = getApp().globalData.baseUrl
  13. Object.assign(_obj, options);
  14. const header = {
  15. 'USER-TERM-TYPE': 'ipad', // 支付时需要用到 与pad一致
  16. 'App-Type': 2,
  17. 'Version': '1.0.0',
  18. 'authorization': uni.getStorageSync('token')
  19. }
  20. _obj.header = header
  21. _obj.header['X-HEADER-APPID'] = '!z2Yc.aes|STw5oReFk+wACvW4IGVHZsFe+/MgMT9cp0amuic1gqE=';
  22. _obj.url = baseUrl + _obj.url;
  23. _obj.method = _obj.method.toUpperCase();
  24. _obj.success = function(res) {
  25. if (_obj.url.indexOf('checkLogin') > -1) {
  26. resolve(res.data);
  27. } else if (res.data.status === '900010') {
  28. setTimeout(function() {
  29. wx.showToast({
  30. title: '登录已过期,请重新登录',
  31. icon: 'none',
  32. duration: 1000
  33. });
  34. }, 10);
  35. setTimeout(function() {
  36. const currentRoute = getRoutePath();
  37. mpvue.removeStorage({
  38. key: 'token'
  39. });
  40. const url = `/pages/login/login?lanuch=${currentRoute.lanuch}&path=` + encodeURIComponent(currentRoute.url);
  41. mpvue.reLaunch({ url });
  42. }, 1000);
  43. } else {
  44. resolve(res.data);
  45. }
  46. };
  47. _obj.fail = function(err) {
  48. reject(err);
  49. };
  50. _obj.complete = function() {};
  51. uni.request(_obj);
  52. });
  53. }
  54. function getRoutePath() {
  55. // eslint-disable-next-line no-undef
  56. const pages = getCurrentPages(); // 获取加载的页面
  57. const currentPage = pages[pages.length - 1]; // 获取当前页面的对象
  58. let url = currentPage.route; // 当前页面url
  59. const options = objToUrl(currentPage.options); // 如果要获取url中所带的参数可以查看options
  60. const lanuch = [
  61. 'pages/personData/personData'
  62. ].includes(url);
  63. url = '/' + url;
  64. if (options.length) {
  65. url += '?' + options;
  66. }
  67. return {
  68. url,
  69. lanuch
  70. };
  71. }
  72. export default request;