vk.importObject.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /**
  2. * 导出云对象实例
  3. * @param {String} name 云对象路径,如:client/pub
  4. * @example const pubObject = uni.vk.importObject('client/pub'); // 导入云对象
  5. * 注意,只能在声明 async 的函数内使用,如:
  6. async test(){
  7. let res = await pubObject.getList({
  8. title: "请求中",
  9. data: {
  10. a: 1,
  11. b: "2"
  12. }
  13. });
  14. }
  15. */
  16. var importObject = function(name) {
  17. const newObj = new Proxy(importObject, {
  18. get: function(target, key, receiver) {
  19. /**
  20. * 导出云对象内的某个方法
  21. * @param {Object} data 请求参数,如 { a:1, b:"2" } 云对象内可通过 let { a, b } = data; 获取参数
  22. * @param {String} title 遮罩层提示语,为空或不传则代表不显示遮罩层。
  23. * @param {Boolean} needAlert 为true代表请求错误时,会有弹窗提示。默认为true
  24. * @param {Object} loading 与title二选一,格式为 { name: "loading", that: that } name是变量名,that是数据源,当发起请求时,自动that[name] = true; 请求结束后,自动that[name] = false;
  25. */
  26. return async function(options) {
  27. return uni.vk.callFunction({
  28. ...options,
  29. url: `${name}.${key}`
  30. });
  31. }
  32. },
  33. // set: function(target, key, value, receiver) {
  34. // console.log("set");
  35. // console.log("target",target);
  36. // console.log("key",key);
  37. // console.log("value",value);
  38. // console.log("receiver", receiver);
  39. // },
  40. });
  41. return newObj;
  42. };
  43. export default importObject;