http.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. data = Object.assign(data, {
  47. openid:$.storage.get('openId')
  48. })
  49. _url = _url + `?pubname=sagacare&openid=${$.storage.get('openId')}`
  50. if (data.flag) {
  51. _url = _url + `&flag=${data.flag}`
  52. }
  53. return new Promise(function (resolve, reject) {
  54. wx.request({
  55. url: _url,
  56. method: 'POST',
  57. data: data,
  58. header: {
  59. 'content-type': 'application/json'
  60. },
  61. success: function success(request) {
  62. resolve(request.data);
  63. },
  64. fail: function fail(error) {
  65. wx.showModal({
  66. title: '网络错误',
  67. content: '网络出错,请刷新重试',
  68. showCancel: false
  69. })
  70. reject(error);
  71. },
  72. complete: function complete() {
  73. }
  74. });
  75. });
  76. };
  77. module.exports = {
  78. http:{get,post}
  79. }