http.js 1.9 KB

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