http.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import $ from "../utils/Tool.js"
  2. // const test = "http://172.16.36.245:50009";
  3. const API_BASE_URL = "https://duoduoenv.sagacloud.cn";
  4. function get(url, data = {},testenv){
  5. let _url;
  6. if(testenv){
  7. _url = test + url.replace('/server','');
  8. }else{
  9. _url = API_BASE_URL + url;
  10. }
  11. data = Object.assign(data, {
  12. pubname: "sagacare",
  13. openid:$.storage.get('openId')
  14. });
  15. return new Promise(function (resolve, reject) {
  16. wx.request({
  17. url: _url,
  18. method: 'GET',
  19. data: data,
  20. header: {
  21. 'content-type': 'application/json'
  22. },
  23. success: function success(request) {
  24. console.log(request)
  25. resolve(request.data);
  26. },
  27. fail: function fail(error) {
  28. wx.showModal({
  29. title: '网络错误',
  30. content: '网络出错,请刷新重试',
  31. showCancel: false
  32. })
  33. reject(error);
  34. },
  35. complete: function complete() {
  36. }
  37. });
  38. });
  39. }
  40. function post(url, data = {},testenv) {
  41. let _url;
  42. if(testenv){
  43. _url = test + url.replace('/server','');
  44. }else{
  45. _url = API_BASE_URL + url;
  46. }
  47. console.log(_url,"_url")
  48. data = Object.assign(data, {
  49. openid:$.storage.get('openId')
  50. })
  51. _url = _url + `?pubname=sagacare&openid=${$.storage.get('openId')}`
  52. if (data.flag) {
  53. _url = _url + `&flag=${data.flag}`
  54. }
  55. return new Promise(function (resolve, reject) {
  56. wx.request({
  57. url: _url,
  58. method: 'POST',
  59. data: data,
  60. header: {
  61. 'content-type': 'application/json'
  62. },
  63. success: function success(request) {
  64. resolve(request.data);
  65. },
  66. fail: function fail(error) {
  67. wx.showModal({
  68. title: '网络错误',
  69. content: '网络出错,请刷新重试',
  70. showCancel: false
  71. })
  72. reject(error);
  73. },
  74. complete: function complete() {
  75. }
  76. });
  77. });
  78. };
  79. module.exports = {
  80. http:{get,post}
  81. }