httpFunc.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { baseurl } from "../config/idnex";
  2. function httpError(err) {
  3. console.error(err);
  4. wx.hideLoading();
  5. wx.showToast({
  6. title: '请稍后再试!',
  7. })
  8. }
  9. export function httpCloud(params) {
  10. !params.noLoading && wx.showLoading();
  11. const par = params || {};
  12. wx.cloud.callFunction({
  13. // 云函数名称
  14. name: par.name,
  15. // 传给云函数的参数
  16. data: {
  17. ...(par.data || {}),
  18. type: par.type
  19. },
  20. success: function (res) {
  21. wx.hideLoading();
  22. if (!/ok/g.test(res.errMsg)) return httpError(res);
  23. if (res.result.code != 0) return wx.showToast({
  24. title: '请稍后再试!',
  25. })
  26. par.call && par.call(res.result.data);
  27. },
  28. fail: httpError
  29. })
  30. }
  31. export function httpOrther(params) {
  32. !params.noLoading && wx.showLoading();
  33. const par = params || {};
  34. wx.request({
  35. url: baseurl + par.url,
  36. data: {
  37. ...(par.data || {}),
  38. },
  39. success(res) {
  40. if (res.statusCode !== 200 || res.data.code != 0 )return wx.showToast({
  41. title: '请稍后再试!',
  42. })
  43. if (!/ok/g.test(res.errMsg)) return httpError(res);
  44. par.call && par.call(res.data.data);
  45. },
  46. complete() {
  47. wx.hideLoading();
  48. }
  49. })
  50. }