ajax.js 1.6 KB

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