ajax.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. uni.clearStorageSync()
  42. uni.reLaunch({
  43. url: '/pages/login/index'
  44. });
  45. } else{
  46. tools.error('网络错误!!')
  47. }
  48. },
  49. fail: (err) => {
  50. tools.error('网络错误!!')
  51. reject(err)
  52. tools.hideLoading();
  53. }
  54. })
  55. })
  56. }