request.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import store from '@/store/index.js'; //vuex
  2. let server_url=' ';//请求地址
  3. // let token = ' '; //凭证
  4. // process.env.NODE_ENV === 'development' ? '192.168.0.1' : 'http://***/api' ; //环境配置
  5. function service(options = {}) {
  6. let isQueryContain = options.url.indexOf('?')>-1 ? (options.url.split('?')[1].indexOf("hotelId=") > -1 ) : false
  7. // console.log(isQueryContain);
  8. // store.state.token && (token = store.state.token); //从vuex中获取登录凭证
  9. const token = uni.getStorageSync('token') || '';
  10. options.url = `${server_url}${options.url}`;
  11. if (options.url.indexOf('?')>-1 && !isQueryContain) {
  12. options.url+= `&hotelId=${store.state.hotelId}`;
  13. }else{
  14. options.url+= `?hotelId=${store.state.hotelId}`;
  15. }
  16. //if(!isQueryContain) options.url+= `?hotelId=${store.state.hotelId}`; //从vuex中获取酒店id
  17. //配置请求头
  18. options.header = {
  19. 'content-type': 'application/json',
  20. // 'accessToken': `${token}`, //Bearer
  21. 'Auth':'APPXPLSOspYuisBnjKcYQZmGpq',
  22. 'X-Access-Token':`${token}`
  23. };
  24. if(options.method=='GET'){
  25. if(options.url.indexOf("sys/dict/getDictItems")<0){
  26. options.params = {
  27. _t: Date.parse(new Date())/1000,
  28. ...options.params
  29. }
  30. }
  31. }
  32. uni.showLoading({title: '加载中'});
  33. // console.log(options);
  34. return new Promise((resolved, rejected) => {
  35. //成功
  36. options.success = (res) => {
  37. // console.log(res);
  38. uni.hideLoading();
  39. if (res.data.code==401) {
  40. // uni.showToast({
  41. // icon: 'none',
  42. // duration: 3000,
  43. // title: `${res.data.msg}`
  44. // });
  45. uni.navigateTo({
  46. url: '/pages/login/login'
  47. });
  48. return;
  49. }
  50. if (res.data.code == 200) { //请求成功
  51. resolved(res.data);
  52. } else {
  53. // uni.showToast({
  54. // icon: 'none',
  55. // duration: 3000,
  56. // title: `${res.data.message || ''}`
  57. // });
  58. rejected('错误');//错误
  59. }
  60. }
  61. //错误
  62. options.fail = (err) => {
  63. rejected(err); //错误
  64. uni.hideLoading();
  65. }
  66. uni.request(options);
  67. });
  68. }
  69. export default service