request.js 1018 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import {
  2. msg
  3. } from './util'
  4. /**
  5. * @param {String} cloudFnName
  6. * @param {String} operation 操作类型(增删改查)
  7. * @param {Object} data 请求参数
  8. * @param {Object} ext 附加参数
  9. */
  10. export const request = (cloudFnName, operation, data = {}, ext = {}) => {
  11. return new Promise((resolve, reject) => {
  12. if (ext.showLoading !== false) {
  13. uni.showLoading()
  14. }
  15. uniCloud.callFunction({
  16. name: cloudFnName,
  17. data: {
  18. operation,
  19. data
  20. }
  21. }).then(res => {
  22. if (ext.showLoading !== false) {
  23. uni.hideLoading()
  24. }
  25. if(res.result.code!=200){
  26. uni.showToast({
  27. icon:"none",
  28. title: res.result.msg,
  29. duration: 2000
  30. });
  31. reject(res.result.msg);
  32. }else{
  33. console.log("requestResult",res)
  34. resolve(res.result.data);
  35. }
  36. }).catch((err) => {
  37. if (ext.showLoading !== false) {
  38. uni.hideLoading()
  39. }
  40. uni.showToast({
  41. icon:"none",
  42. title: err.toString().replace("Error:",""),
  43. duration: 2000
  44. });
  45. reject(err);
  46. })
  47. })
  48. }