1export function mockFetch() { 2 global.sendGroupMessage = global.group.sendGroupMessage; 3 let GroupMessenger = { 4 create: function () { 5 let messenger = {}; 6 messenger.send = function (groupName, functionName, ...args) { 7 return new Promise(function (resolve, reject) { 8 let params = messenger.prepareArgs(...args); 9 sendGroupMessage(function (result) { 10 resolve(messenger.parseJsonResult(result)); 11 }, function (error) { 12 reject(messenger.parseJsonResult(error)); 13 }, groupName, functionName, ...params); 14 }) 15 }; 16 messenger.parseJsonResult = function (data) { 17 if (data && data.constructor == String) { 18 try { 19 data = JSON.parse(data); 20 } catch (jsonParseErr) { 21 console.warn("parse result exception: " + JSON.stringify(jsonParseErr)); 22 } 23 } 24 return data; 25 }; 26 messenger.prepareArgs = function (...args) { 27 let result = [...args]; 28 for (let i = 0; i < result.length; i++) { 29 if (typeof result[i] === 'function') { 30 result[i] = messenger.packageCallback(result[i]); 31 } 32 } 33 return result; 34 }; 35 messenger.packageCallback = function (func) { 36 return function (data) { 37 data = messenger.parseJsonResult(data); 38 if (!Array.isArray(data)) { 39 func(data); 40 } else { 41 func(...data); 42 } 43 }; 44 }; 45 return messenger; 46 } 47 }; 48 49 let CommonCallback = { 50 commonCallback: function commonCallback(callback, flag, data, code) { 51 if (typeof callback === 'function') { 52 switch (flag) { 53 case 'success': 54 callback(data); 55 break; 56 case 'fail': 57 callback(data, code); 58 break; 59 case 'cancel': 60 callback(data); 61 break; 62 case 'complete': 63 callback(); 64 break; 65 default: 66 break; 67 } 68 } else { 69 console.warn('callback.' + flag + ' is not function or not present'); 70 } 71 } 72 }; 73 global.commonCallback = CommonCallback.commonCallback; 74 let CommonCallbackEx = { 75 commonCallbackEx: function commonCallbackEx(callback, result, pluginError) { 76 if ((callback === undefined) || ((callback.success === undefined) && (callback.fail === undefined) && (callback.complete === undefined))) { 77 return CommonCallbackEx.promiseMethod(result, pluginError); 78 } else { 79 return CommonCallbackEx.callbackMethod(callback, result, pluginError); 80 } 81 }, 82 promiseMethod: function promiseMethod(result, pluginError) { 83 if (pluginError != undefined) { 84 throw pluginError; 85 } 86 return result; 87 }, 88 callbackMethod: function callbackMethod(callback, result, pluginError) { 89 if (pluginError != undefined) { 90 commonCallback(callback.fail, 'fail', pluginError.data, pluginError.code); 91 commonCallback(callback.complete, 'complete'); 92 throw pluginError; 93 } 94 commonCallback(callback.success, 'success', result.data); 95 commonCallback(callback.complete, 'complete'); 96 return result; 97 }, 98 catching: function catching(promise, param) { 99 return promise.then(ret => commonCallbackEx(param, ret)) 100 .catch(err => commonCallbackEx(param, null, err)); 101 } 102 }; 103 global.commonCallbackEx = CommonCallbackEx.commonCallbackEx; 104 global.systemplugin.catching = CommonCallbackEx.catching; 105 106 let FetchObject = { 107 getFetch: function () { 108 let fetch = {} 109 fetch.messenger = GroupMessenger.create(); 110 fetch.fetch = async function (param) { 111 return await CommonCallbackEx.catching(this.messenger.send("groupName", "fetch", param), param); 112 } 113 return fetch 114 } 115 } 116 117 global.systemplugin.fetch = FetchObject.getFetch(); 118}