httpFunc.js 1.3 KB

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