403 lines
14 KiB
JavaScript
Raw Normal View History

2024-10-25 18:09:59 +08:00
import moduleHelper from './module-helper';
import { uid, formatResponse, formatJsonStr, stringifyRes } from './utils';
const onEventLists = {};
2024-10-25 18:09:59 +08:00
let wxOnAddToFavoritesResolveConf;
let wxOnCopyUrlResolveConf;
let wxOnHandoffResolveConf;
let wxOnShareTimelineResolveConf;
let wxOnGameLiveStateChangeResolveConf;
const ClassLists = {};
const ClassOnEventLists = {};
function getClassObject(className, id) {
if (!ClassLists[className]) {
ClassLists[className] = {};
}
const obj = ClassLists[className][id];
if (!obj) {
console.error(`${className} 不存在:`, id);
}
return obj;
}
;
// eslint-disable-next-line @typescript-eslint/naming-convention
function WX_OneWayNoFunction(functionName, ...params) {
wx[functionName.replace(/^\w/, a => a.toLowerCase())](...params);
}
const onlyReadyResponse = [
'getSystemSetting',
'getAppAuthorizeSetting',
];
// eslint-disable-next-line @typescript-eslint/naming-convention
function WX_SyncFunction(functionName, ...params) {
return wx[functionName.replace(/^\w/, a => a.toLowerCase())](...params);
}
// eslint-disable-next-line @typescript-eslint/naming-convention
function WX_ClassOneWayNoFunction(className, functionName, id, ...params) {
const obj = getClassObject(className, id);
if (!obj) {
return;
}
obj[functionName.replace(/^\w/, a => a.toLowerCase())](...params);
}
export default {
WX_OneWayFunction(functionName, successType, failType, completeType, conf, callbackId) {
const lowerFunctionName = functionName.replace(/^\w/, a => a.toLowerCase());
const config = formatJsonStr(conf);
if (lowerFunctionName === 'login') {
if (!config.timeout) {
delete config.timeout;
}
2024-10-25 18:09:59 +08:00
}
else if (lowerFunctionName === 'reportScene') {
if (GameGlobal.manager && GameGlobal.manager.setGameStage) {
GameGlobal.manager.setGameStage(config.sceneId);
}
2024-10-25 18:09:59 +08:00
}
wx[lowerFunctionName]({
...config,
success(res) {
formatResponse(successType, res);
moduleHelper.send(`${functionName}Callback`, JSON.stringify({
callbackId, type: 'success', res: JSON.stringify(res),
}));
},
fail(res) {
formatResponse(failType, res);
moduleHelper.send(`${functionName}Callback`, JSON.stringify({
callbackId, type: 'fail', res: JSON.stringify(res),
}));
},
complete(res) {
formatResponse(completeType, res);
moduleHelper.send(`${functionName}Callback`, JSON.stringify({
callbackId, type: 'complete', res: JSON.stringify(res),
}));
},
2024-10-25 18:09:59 +08:00
});
},
WX_OneWayNoFunction_v(functionName) {
WX_OneWayNoFunction(functionName);
2024-10-25 18:09:59 +08:00
},
WX_OneWayNoFunction_vs(functionName, param1) {
WX_OneWayNoFunction(functionName, param1);
2024-10-25 18:09:59 +08:00
},
WX_OneWayNoFunction_vt(functionName, param1) {
const formatParam1 = formatJsonStr(param1);
WX_OneWayNoFunction(functionName, formatParam1);
2024-10-25 18:09:59 +08:00
},
WX_OneWayNoFunction_vst(functionName, param1, param2) {
const formatParam2 = formatJsonStr(param2);
WX_OneWayNoFunction(functionName, param1, formatParam2);
2024-10-25 18:09:59 +08:00
},
WX_OneWayNoFunction_vsn(functionName, param1, param2) {
WX_OneWayNoFunction(functionName, param1, param2);
2024-10-25 18:09:59 +08:00
},
WX_OneWayNoFunction_vnns(functionName, param1, param2, param3) {
WX_OneWayNoFunction(functionName, param1, param2, param3);
2024-10-25 18:09:59 +08:00
},
WX_OnEventRegister(functionName, resType) {
if (!onEventLists[functionName]) {
onEventLists[functionName] = [];
2024-10-25 18:09:59 +08:00
}
const callback = (res) => {
formatResponse(resType, res);
2024-10-25 18:09:59 +08:00
const resStr = stringifyRes(res);
moduleHelper.send(`_${functionName}Callback`, resStr);
2024-10-25 18:09:59 +08:00
};
onEventLists[functionName].push(callback);
wx[functionName.replace(/^\w/, a => a.toLowerCase())](callback);
2024-10-25 18:09:59 +08:00
},
WX_OffEventRegister(functionName) {
(onEventLists[functionName] || []).forEach((v) => {
wx[functionName.replace(/^On/, 'off')](v);
2024-10-25 18:09:59 +08:00
});
},
WX_OnAddToFavorites() {
const callback = (res) => {
const resStr = stringifyRes(res);
moduleHelper.send('_OnAddToFavoritesCallback', resStr);
return wxOnAddToFavoritesResolveConf;
};
wx.onAddToFavorites(callback);
},
WX_OnAddToFavorites_Resolve(conf) {
try {
wxOnAddToFavoritesResolveConf = formatJsonStr(conf);
return;
}
catch (e) {
}
wxOnAddToFavoritesResolveConf = {};
},
WX_OffAddToFavorites() {
wx.offAddToFavorites();
},
WX_OnCopyUrl() {
const callback = (res) => {
const resStr = stringifyRes(res);
moduleHelper.send('_OnCopyUrlCallback', resStr);
return wxOnCopyUrlResolveConf;
};
wx.onCopyUrl(callback);
},
WX_OnCopyUrl_Resolve(conf) {
try {
wxOnCopyUrlResolveConf = formatJsonStr(conf);
return;
}
catch (e) {
}
wxOnCopyUrlResolveConf = {};
},
WX_OffCopyUrl() {
wx.offCopyUrl();
},
WX_OnHandoff() {
const callback = (res) => {
const resStr = stringifyRes(res);
moduleHelper.send('_OnHandoffCallback', resStr);
return wxOnHandoffResolveConf;
};
wx.onHandoff(callback);
},
WX_OnHandoff_Resolve(conf) {
try {
wxOnHandoffResolveConf = formatJsonStr(conf);
return;
}
catch (e) {
}
wxOnHandoffResolveConf = {};
},
WX_OffHandoff() {
wx.offHandoff();
},
WX_OnShareTimeline() {
const callback = (res) => {
const resStr = stringifyRes(res);
moduleHelper.send('_OnShareTimelineCallback', resStr);
return wxOnShareTimelineResolveConf;
};
wx.onShareTimeline(callback);
},
WX_OnShareTimeline_Resolve(conf) {
try {
wxOnShareTimelineResolveConf = formatJsonStr(conf);
return;
}
catch (e) {
}
wxOnShareTimelineResolveConf = {};
},
WX_OffShareTimeline() {
wx.offShareTimeline();
},
WX_OnGameLiveStateChange() {
const callback = (res) => {
formatResponse('OnGameLiveStateChangeCallbackResult', res);
const resStr = stringifyRes(res);
moduleHelper.send('_OnGameLiveStateChangeCallback', resStr);
return wxOnGameLiveStateChangeResolveConf;
};
wx.onGameLiveStateChange(callback);
},
WX_OnGameLiveStateChange_Resolve(conf) {
try {
wxOnGameLiveStateChangeResolveConf = formatJsonStr(conf);
return;
}
catch (e) {
}
wxOnGameLiveStateChangeResolveConf = {};
},
WX_OffGameLiveStateChange() {
wx.offGameLiveStateChange();
},
WX_SyncFunction_bs(functionName, param1) {
const res = WX_SyncFunction(functionName, param1);
2024-10-25 18:09:59 +08:00
return res;
},
WX_SyncFunction_t(functionName, returnType) {
const res = WX_SyncFunction(functionName);
if (onlyReadyResponse.includes(functionName.replace(/^\w/, a => a.toLowerCase()))) {
formatResponse(returnType, JSON.parse(JSON.stringify(res)));
return JSON.stringify(res);
}
formatResponse(returnType, res);
2024-10-25 18:09:59 +08:00
return JSON.stringify(res);
},
WX_SyncFunction_tt(functionName, returnType, param1) {
const res = WX_SyncFunction(functionName, formatJsonStr(param1));
formatResponse(returnType, res);
2024-10-25 18:09:59 +08:00
return JSON.stringify(res);
},
WX_SyncFunction_b(functionName) {
const res = WX_SyncFunction(functionName);
2024-10-25 18:09:59 +08:00
return res;
},
WX_SyncFunction_bsnn(functionName, param1, param2, param3) {
const res = WX_SyncFunction(functionName, param1, param2, param3);
2024-10-25 18:09:59 +08:00
return res;
},
WX_SyncFunction_bt(functionName, param1) {
const res = WX_SyncFunction(functionName, formatJsonStr(param1));
2024-10-25 18:09:59 +08:00
return res;
},
WX_SyncFunction_nt(functionName, param1) {
const res = WX_SyncFunction(functionName, formatJsonStr(param1));
2024-10-25 18:09:59 +08:00
return res;
},
WX_SyncFunction_ss(functionName, param1) {
const res = WX_SyncFunction(functionName, param1);
2024-10-25 18:09:59 +08:00
return res;
},
WX_SyncFunction_tnn(functionName, returnType, param1, param2) {
const res = WX_SyncFunction(functionName, param1, param2);
formatResponse(returnType, res);
2024-10-25 18:09:59 +08:00
return JSON.stringify(res);
},
WX_ClassOneWayFunction(functionName, returnType, successType, failType, completeType, conf) {
2024-10-25 18:09:59 +08:00
const config = formatJsonStr(conf);
const callbackId = uid();
const obj = wx[functionName.replace(/^\w/, a => a.toLowerCase())]({
2024-10-25 18:09:59 +08:00
...config,
success(res) {
formatResponse(successType, res);
moduleHelper.send(`${functionName}Callback`, JSON.stringify({
2024-10-25 18:09:59 +08:00
callbackId, type: 'success', res: JSON.stringify(res),
}));
},
fail(res) {
formatResponse(failType, res);
moduleHelper.send(`${functionName}Callback`, JSON.stringify({
2024-10-25 18:09:59 +08:00
callbackId, type: 'fail', res: JSON.stringify(res),
}));
},
complete(res) {
formatResponse(completeType, res);
moduleHelper.send(`${functionName}Callback`, JSON.stringify({
2024-10-25 18:09:59 +08:00
callbackId, type: 'complete', res: JSON.stringify(res),
}));
},
});
if (!ClassLists[returnType]) {
ClassLists[returnType] = {};
2024-10-25 18:09:59 +08:00
}
ClassLists[returnType][callbackId] = obj;
return callbackId;
2024-10-25 18:09:59 +08:00
},
WX_ClassFunction(functionName, returnType, option) {
const obj = wx[functionName.replace(/^\w/, a => a.toLowerCase())](formatJsonStr(option));
const id = uid();
if (!ClassLists[returnType]) {
ClassLists[returnType] = {};
2024-10-25 18:09:59 +08:00
}
ClassLists[returnType][id] = obj;
return id;
2024-10-25 18:09:59 +08:00
},
WX_ClassSetProperty(className, id, key, value) {
const obj = getClassObject(className, id);
2024-10-25 18:09:59 +08:00
if (!obj) {
return;
}
if (/^\s*(\{.*\}|\[.*\])\s*$/.test(value)) {
try {
const jsonValue = JSON.parse(value);
Object.assign(obj[key], jsonValue);
}
catch (e) {
obj[key] = value;
}
}
else {
obj[key] = value;
}
},
WX_ClassOnEventFunction(className, functionName, returnType, id, eventName) {
const obj = getClassObject(className, id);
2024-10-25 18:09:59 +08:00
if (!obj) {
return;
}
if (!ClassOnEventLists[className + functionName]) {
ClassOnEventLists[className + functionName] = {};
2024-10-25 18:09:59 +08:00
}
const callback = (res) => {
if (returnType !== 'string') {
formatResponse(returnType, res);
}
if (!eventName) {
// eslint-disable-next-line no-param-reassign
eventName = '';
}
2024-10-25 18:09:59 +08:00
const resStr = JSON.stringify({
callbackId: id + eventName,
2024-10-25 18:09:59 +08:00
res: JSON.stringify(res),
});
moduleHelper.send(`_${className}${functionName}Callback`, resStr);
2024-10-25 18:09:59 +08:00
};
if (!ClassOnEventLists[className + functionName][id + eventName]) {
ClassOnEventLists[className + functionName][id + eventName] = [];
2024-10-25 18:09:59 +08:00
}
ClassOnEventLists[className + functionName][id + eventName].push(callback);
// WXVideoDecoder OnEvent 不规范 特殊处理
if (className === 'WXVideoDecoder') {
obj[functionName.replace(/^\w/, a => a.toLowerCase())](eventName, callback);
2024-10-25 18:09:59 +08:00
}
else {
obj[functionName.replace(/^\w/, a => a.toLowerCase())](callback);
2024-10-25 18:09:59 +08:00
}
},
WX_ClassOffEventFunction(className, functionName, id, eventName) {
const obj = getClassObject(className, id);
2024-10-25 18:09:59 +08:00
if (!obj) {
return;
}
if (!eventName) {
// eslint-disable-next-line no-param-reassign
eventName = '';
2024-10-25 18:09:59 +08:00
}
// eslint-disable-next-line no-param-reassign
functionName = functionName.replace(/Off/, 'On');
if (!ClassOnEventLists[className + functionName][id + eventName]) {
2024-10-25 18:09:59 +08:00
return;
}
ClassOnEventLists[className + functionName][id + eventName].forEach((v) => {
if (className === 'WXVideoDecoder') {
obj[functionName.replace(/^\w/, (a) => a.toLowerCase())](eventName, v);
}
else {
obj[functionName.replace(/^\w/, (a) => a.toLowerCase())](v);
}
});
delete ClassOnEventLists[className + functionName][id + eventName];
2024-10-25 18:09:59 +08:00
},
WX_ClassOneWayNoFunction_v(className, functionName, id) {
WX_ClassOneWayNoFunction(className, functionName, id);
2024-10-25 18:09:59 +08:00
},
WX_ClassOneWayNoFunction_vs(className, functionName, id, param1) {
WX_ClassOneWayNoFunction(className, functionName, id, param1);
2024-10-25 18:09:59 +08:00
},
WX_ClassOneWayNoFunction_t(className, functionName, returnType, id) {
const obj = getClassObject(className, id);
2024-10-25 18:09:59 +08:00
if (!obj) {
return JSON.stringify(formatResponse(returnType));
2024-10-25 18:09:59 +08:00
}
const res = obj[functionName.replace(/^\w/, a => a.toLowerCase())]();
return JSON.stringify(formatResponse(returnType, res, id));
},
WX_ClassOneWayNoFunction_vt(className, functionName, id, param1) {
const formatParam1 = formatJsonStr(param1);
WX_ClassOneWayNoFunction(className, functionName, id, formatParam1);
},
WX_ClassOneWayNoFunction_vn(className, functionName, id, param1) {
WX_ClassOneWayNoFunction(className, functionName, id, param1);
2024-10-25 18:09:59 +08:00
},
};