request.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. // 其它小程序传的参数
  21. const apiHeader = getApp().globalData.extraData
  22. console.log(apiHeader,'apiHeader')
  23. if(apiHeader){
  24. _obj.header['X-MERCHANT-NO'] = apiHeader['X-MERCHANT-NO']
  25. _obj.header['X-SIGN-TYPE'] = apiHeader['X-SIGN-TYPE']
  26. _obj.header['X-SIGN-INFO'] = apiHeader['X-SIGN-INFO']
  27. _obj.header['X-TIMESTAMP'] = apiHeader['X-TIMESTAMP']
  28. }
  29. _obj.url = baseUrl + _obj.url;
  30. _obj.method = _obj.method.toUpperCase();
  31. _obj.success = function(res) {
  32. if (_obj.url.indexOf('checkLogin') > -1) {
  33. resolve(res.data);
  34. } else if (res.data.status === '900010') {
  35. setTimeout(function() {
  36. wx.showToast({
  37. title: '登录已过期,请重新登录',
  38. icon: 'none',
  39. duration: 1000
  40. });
  41. }, 10);
  42. setTimeout(function() {
  43. const currentRoute = getRoutePath();
  44. mpvue.removeStorage({
  45. key: 'token'
  46. });
  47. const url = `/pages/login/login?lanuch=${currentRoute.lanuch}&path=` + encodeURIComponent(currentRoute.url);
  48. mpvue.reLaunch({ url });
  49. }, 1000);
  50. } else {
  51. resolve(res.data);
  52. }
  53. };
  54. _obj.fail = function(err) {
  55. reject(err);
  56. };
  57. _obj.complete = function() {};
  58. uni.request(_obj);
  59. });
  60. }
  61. function getRoutePath() {
  62. // eslint-disable-next-line no-undef
  63. const pages = getCurrentPages(); // 获取加载的页面
  64. const currentPage = pages[pages.length - 1]; // 获取当前页面的对象
  65. let url = currentPage.route; // 当前页面url
  66. const options = objToUrl(currentPage.options); // 如果要获取url中所带的参数可以查看options
  67. const lanuch = [
  68. 'pages/personData/personData'
  69. ].includes(url);
  70. url = '/' + url;
  71. if (options.length) {
  72. url += '?' + options;
  73. }
  74. return {
  75. url,
  76. lanuch
  77. };
  78. }
  79. export default request;