request.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.clearStorageSync()
  46. uni.navigateTo({
  47. url: '/pages/login/login'
  48. });
  49. return;
  50. }
  51. if (res.data.code == 200) { //请求成功
  52. resolved(res.data);
  53. } else {
  54. // uni.showToast({
  55. // icon: 'none',
  56. // duration: 3000,
  57. // title: `${res.data.message || ''}`
  58. // });
  59. rejected('错误');//错误
  60. }
  61. }
  62. //错误
  63. options.fail = (err) => {
  64. rejected(err); //错误
  65. uni.hideLoading();
  66. }
  67. uni.request(options);
  68. });
  69. }
  70. export default service