ajax.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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,isFinance = false,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. //#ifndef H5
  20. if(isFinance){
  21. BASE_URL = 'https://adopted.jhcs888.com/'
  22. }else{
  23. BASE_URL ='https://demoapi.jhcs888.com/'
  24. }
  25. //#endif
  26. return new Promise((resolve, reject) => {
  27. uni.request({
  28. url:BASE_URL+ url,
  29. data: data,
  30. method: method || 'post',
  31. header: {
  32. 'content-type': 'application/json',
  33. 'hversion': '1.0',
  34. 'Authorization': token
  35. },
  36. success: (res) => {
  37. if (res.statusCode === 200) {
  38. tools.hideLoading();
  39. if (res.data.code * 1 === 401) {
  40. uni.clearStorageSync()
  41. uni.reLaunch({
  42. url: '/pages/login/index'
  43. });
  44. } else {
  45. resolve(res.data)
  46. }
  47. }else if(res.statusCode === 401){
  48. uni.clearStorageSync()
  49. uni.reLaunch({
  50. url: '/pages/login/index'
  51. });
  52. } else{
  53. tools.error('网络错误!!')
  54. }
  55. },
  56. fail: (err) => {
  57. tools.error('网络错误!!')
  58. reject(err)
  59. tools.hideLoading();
  60. }
  61. })
  62. })
  63. }