ajax.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import tools from "./tools";
  2. let BASE_URL =''
  3. //#ifndef H5
  4. BASE_URL ='https://demoapi.jhcs888.com/'
  5. //#endif
  6. export const request = (url, method, data, show = false) => {
  7. let token = uni.getStorageSync('token')
  8. let u_id = uni.getStorageSync('u_id')
  9. if(token&&u_id){
  10. data.token = token;
  11. data.u_id = u_id
  12. }
  13. if (show) {
  14. tools.showLoading();
  15. }
  16. if (data === undefined) {
  17. data = {};
  18. }
  19. return new Promise((resolve, reject) => {
  20. uni.request({
  21. url:BASE_URL+ url,
  22. data: data,
  23. method: method || 'post',
  24. header: {
  25. 'content-type': 'application/json',
  26. 'hversion': '1.0',
  27. 'Authorization': token
  28. },
  29. success: (res) => {
  30. if (res.statusCode === 200) {
  31. tools.hideLoading();
  32. if (res.data.code * 1 === 401) {
  33. uni.clearStorageSync()
  34. uni.reLaunch({
  35. url: '/pages/login/index'
  36. });
  37. } else {
  38. resolve(res.data)
  39. }
  40. }else if(res.statusCode === 401){
  41. window.localStorage.clear()
  42. uni.clearStorageSync()
  43. uni.reLaunch({
  44. url: '/pages/login/index'
  45. });
  46. } else{
  47. tools.error('网络错误!!')
  48. }
  49. },
  50. fail: (err) => {
  51. tools.error('网络错误!!')
  52. reject(err)
  53. tools.hideLoading();
  54. }
  55. })
  56. })
  57. }