ajax.js 1.6 KB

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