request.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { objToUrl } from '@/utils/index';
  2. function request(options) {
  3. // wx.showLoading({
  4. // mask: true,
  5. // title: '加载中' // 数据请求前loading,提高用户体验
  6. // });
  7. return new Promise((resolve, reject) => {
  8. const baseUrl = 'https://zyyc.chelingzhu.com/zyyc-shop/'; // 预发布
  9. // const baseUrl = 'https://shop.zy-yc.cn/zyyc-shop/'; // 生产
  10. // const baseUrl = 'http://192.168.16.103:7101/zyyc-shop/';
  11. // const baseUrl = process.env.NODE_ENV === 'production' ? proUrl : devUrl;
  12. let _obj = {
  13. url: '',
  14. data: {},
  15. method: 'GET',
  16. header: {
  17. 'Content-Type': 'application/json;charset=UTF-8'
  18. }
  19. };
  20. Object.assign(_obj, options);
  21. _obj.header['X-AUTH-TOKEN'] = mpvue.getStorageSync('token');
  22. _obj.header['X-HEADER-APPID'] = 'vb/TIFOLtHqF5qWlwFKVie3SAD9Ef1ec8ERVkvn5brk='; // 章鱼养车
  23. _obj.url = baseUrl + _obj.url;
  24. _obj.method = _obj.method.toUpperCase();
  25. _obj.success = function(res) {
  26. if (_obj.url.indexOf('checkLogin') > -1) {
  27. resolve(res);
  28. } else if (res.data.status === '900010') {
  29. setTimeout(function() {
  30. wx.showToast({
  31. title: '登录已过期,请重新登录',
  32. icon: 'none',
  33. duration: 1000
  34. });
  35. }, 10);
  36. setTimeout(function() {
  37. const currentRoute = getRoutePath();
  38. mpvue.removeStorage({
  39. key: 'token'
  40. });
  41. const url = `/pages/login/main?lanuch=${currentRoute.lanuch}&path=` + encodeURIComponent(currentRoute.url);
  42. mpvue.reLaunch({ url });
  43. }, 1000);
  44. } else {
  45. resolve(res);
  46. }
  47. };
  48. _obj.fail = function(err) {
  49. reject(err);
  50. };
  51. _obj.complete = function() {
  52. wx.hideLoading();
  53. };
  54. wx.request(_obj);
  55. });
  56. }
  57. function getRoutePath() {
  58. // eslint-disable-next-line no-undef
  59. const pages = getCurrentPages(); // 获取加载的页面
  60. const currentPage = pages[pages.length - 1]; // 获取当前页面的对象
  61. let url = currentPage.route; // 当前页面url
  62. const options = objToUrl(currentPage.options); // 如果要获取url中所带的参数可以查看options
  63. const lanuch = [
  64. 'pages/index/main',
  65. 'pages/bundle/main',
  66. 'pages/store/main',
  67. 'pages/member/main'
  68. ].includes(url);
  69. url = '/' + url;
  70. if (options.length) {
  71. url += '?' + options;
  72. }
  73. return {
  74. url,
  75. lanuch
  76. };
  77. }
  78. export default request;