request.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. 'Version': '1.0.0',
  16. 'X-AUTH-TOKEN': uni.getStorageSync('token')
  17. }
  18. _obj.header = header
  19. _obj.header['X-HEADER-APPID'] = '!z2Yc.aes|+Dtf7OBN+iytt6MIem2RukRIvLjBhIS/whu4EuxvvFk=';
  20. _obj.url = baseUrl + _obj.url;
  21. _obj.method = _obj.method.toUpperCase();
  22. _obj.success = function(res) {
  23. if (_obj.url.indexOf('checkLogin') > -1) {
  24. resolve(res.data);
  25. } else if (res.data.status === '900010') {
  26. setTimeout(function() {
  27. wx.showToast({
  28. title: '登录已过期,请重新登录',
  29. icon: 'none',
  30. duration: 1000
  31. });
  32. }, 10);
  33. setTimeout(function() {
  34. const currentRoute = getRoutePath();
  35. mpvue.removeStorage({
  36. key: 'token'
  37. });
  38. const url = `/pages/login/login?lanuch=${currentRoute.lanuch}&path=` + encodeURIComponent(currentRoute.url);
  39. mpvue.reLaunch({ url });
  40. }, 1000);
  41. } else {
  42. resolve(res.data);
  43. }
  44. };
  45. _obj.fail = function(err) {
  46. reject(err);
  47. };
  48. _obj.complete = function() {};
  49. uni.request(_obj);
  50. });
  51. }
  52. function getRoutePath() {
  53. // eslint-disable-next-line no-undef
  54. const pages = getCurrentPages(); // 获取加载的页面
  55. const currentPage = pages[pages.length - 1]; // 获取当前页面的对象
  56. let url = currentPage.route; // 当前页面url
  57. const options = objToUrl(currentPage.options); // 如果要获取url中所带的参数可以查看options
  58. const lanuch = [
  59. 'pages/personData/personData'
  60. ].includes(url);
  61. url = '/' + url;
  62. if (options.length) {
  63. url += '?' + options;
  64. }
  65. return {
  66. url,
  67. lanuch
  68. };
  69. }
  70. export default request;