http.js 1.9 KB

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