http.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import $ from "../utils/Tool.js";
  2. import errorRequest from "./errorRequest.js";
  3. const test = "http://192.168.0.47:52015"; //192.168.12.20 172.16.36.245
  4. const API_BASE_URL = "https://duoduoenv.sagacloud.cn";
  5. function get(url, data = {}, testenv) {
  6. let _url;
  7. if (testenv) {
  8. _url = test + url.replace("/server", "");
  9. } else {
  10. _url = API_BASE_URL + url;
  11. }
  12. data = Object.assign(data, {
  13. pubname: "sagacare",
  14. openid: $.storage.get("openId"),
  15. });
  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. if (request.statusCode >= 400) {
  26. console.log(
  27. 'systemLog/save接口请求失败',request
  28. );
  29. errorRequest({
  30. name: _url, // 类型:String 必有字段 备注:接口名字
  31. input: JSON.stringify(data) || '', // 类型:String 必有字段 备注:请求输入参数
  32. exception: JSON.stringify(request.errMsg) // 类型:String 必有字段 备注:异常信息
  33. });
  34. reject(request.errMsg);
  35. } else{
  36. resolve(request.data);
  37. }
  38. },
  39. fail: function fail(error) {
  40. wx.showModal({
  41. title: "网络错误",
  42. content: "网络出错,请刷新重试",
  43. showCancel: false,
  44. });
  45. errorRequest({
  46. name: _url, // 类型:String 必有字段 备注:接口名字
  47. input: JSON.stringify(data) || '', // 类型:String 必有字段 备注:请求输入参数
  48. exception: JSON.stringify(error), // 类型:String 必有字段 备注:异常信息
  49. });
  50. reject(error);
  51. },
  52. complete: function complete() {},
  53. });
  54. });
  55. }
  56. function post(url, data = {}, testenv) {
  57. let _url;
  58. if (testenv) {
  59. _url = test + url.replace("/server", "");
  60. } else {
  61. _url = API_BASE_URL + url;
  62. }
  63. data = Object.assign(data, {
  64. openid: $.storage.get("openId"),
  65. });
  66. _url = _url + `?pubname=sagacare&openid=${$.storage.get("openId")}`;
  67. if (data.flag) {
  68. _url = _url + `&flag=${data.flag}`;
  69. }
  70. return new Promise(function (resolve, reject) {
  71. wx.request({
  72. url: _url,
  73. method: "POST",
  74. data: data,
  75. header: {
  76. "content-type": "application/json",
  77. },
  78. success: function success(request) {
  79. if (request.statusCode >= 400) {
  80. console.log(
  81. 'systemLog/save接口请求失败',request
  82. );
  83. errorRequest({
  84. name: _url, // 类型:String 必有字段 备注:接口名字
  85. input: JSON.stringify(data) || '', // 类型:String 必有字段 备注:请求输入参数
  86. exception: JSON.stringify(request.errMsg) // 类型:String 必有字段 备注:异常信息
  87. });
  88. reject(request.errMsg);
  89. } else{
  90. resolve(request.data);
  91. }
  92. },
  93. fail: function fail(error) {
  94. wx.showModal({
  95. title: "网络错误",
  96. content: "网络出错,请刷新重试",
  97. showCancel: false,
  98. });
  99. errorRequest({
  100. name: _url, // 类型:String 必有字段 备注:接口名字
  101. input: JSON.stringify(data) || '', // 类型:String 必有字段 备注:请求输入参数
  102. exception: JSON.stringify(error) // 类型:String 必有字段 备注:异常信息
  103. });
  104. reject(error);
  105. },
  106. complete: function complete() {},
  107. });
  108. });
  109. }
  110. module.exports = {
  111. http: { get, post },
  112. };