request.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import config from '@/common/config.js'
  2. const baseUrl =config.url
  3. const request = (url = '', type = 'GET',data = {}, header = {
  4. }) => {
  5. console.log(url.toLowerCase() +' ===='+url.toLowerCase().includes('/user/validateuser'))
  6. const token = uni.getStorageSync('token');
  7. console.log('token',token)
  8. if(!token && !url.toLowerCase().includes('/user/validateuser')){
  9. // 执行没有登录的逻辑
  10. uni.showToast({
  11. icon:'none',
  12. title:'token过期请重新登录'
  13. })
  14. setTimeout(function() {
  15. uni.navigateTo({
  16. url:'../index/login'
  17. })
  18. }, 1500);
  19. return;
  20. }
  21. if(token){
  22. header={
  23. 'Authorization':token
  24. }
  25. }
  26. return new Promise((resolve, reject) => {
  27. uni.request({
  28. method: type,
  29. url: baseUrl + url,
  30. data: data,
  31. header: header,
  32. dataType: 'json',
  33. }).then((response) => {
  34. let [error, res] = response;
  35. resolve(res.data);
  36. }).catch(error => {
  37. let [err, res] = error;
  38. reject(err)
  39. })
  40. });
  41. }
  42. export default request