httpUtil.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. let serverUrl = "https://broadcast.waityou24.cn";//http://127.0.0.1:15000
  2. let wechatUrl = 'https://broadcast.waityou24.cn';
  3. const getDomain = (uri) => {
  4. if (uri.startsWith('/wechat/api')) return wechatUrl;
  5. return serverUrl;
  6. }
  7. const $get = (uri, data) => {
  8. let app = getApp();
  9. let baseUrl = getDomain(uri);
  10. return new Promise((resolve, reject) => {
  11. wx.request({
  12. url: `${baseUrl}${uri}`,
  13. method: "get",
  14. data,
  15. header: {},
  16. success: (res) => toResolve(resolve, res),
  17. error: (e) => toReject(reject, e),
  18. });
  19. });
  20. };
  21. const $post = (uri, data) => {
  22. let app = getApp();
  23. let baseUrl = getDomain(uri);
  24. return new Promise((resolve, reject) => {
  25. wx.request({
  26. url: `${baseUrl}${uri}`,
  27. method: "post",
  28. data,
  29. header: {},
  30. success: (res) => toResolve(resolve, res),
  31. error: (e) => toReject(reject, e),
  32. });
  33. });
  34. };
  35. const $delete = (uri, data) => {
  36. let app = getApp();
  37. let baseUrl = getDomain(uri);
  38. return new Promise((resolve, reject) => {
  39. wx.request({
  40. url: `${baseUrl}${uri}`,
  41. method: "delete",
  42. data,
  43. success: (res) => toResolve(resolve, res),
  44. error: (e) => toReject(reject, e),
  45. });
  46. });
  47. };
  48. const toResolve = (resolve, result) => {
  49. if (result.statusCode === 200) resolve(result.data);
  50. else {
  51. console.error("请求失败");
  52. }
  53. };
  54. const toReject = (reject, result) => {
  55. console.error(result);
  56. reject(result);
  57. };
  58. module.exports = {
  59. $get,
  60. $post,
  61. $delete
  62. };