2023-09-13 10:36:12 +08:00

135 lines
3.8 KiB
JavaScript

// call native method
function callNative(data) {
let dataStr = JSON.stringify(data);
console.log('call native: ' + dataStr);
if (window.cfwallet_JuEd8Ql5over8kneww && window.cfwallet_JuEd8Ql5over8kneww.pageCall) {
cfwallet_JuEd8Ql5over8kneww.pageCall(dataStr);
} else if (window.webkit && window.webkit.messageHandlers&& window.webkit.messageHandlers.pageCall) {
webkit.messageHandlers.pageCall.postMessage(dataStr);
}
}
// for native call js
function nativeCall(funid, val) {
console.log('from native: ' + val);
if (!val) {
return;
}
jc.wallet.nativeSvr.handleNativeCallback(funid, val);
}
// for proxy method callback
function proxyCallback(funid, data) {
window.dispatchEvent(
new CustomEvent("walletcb", {
detail: { funid, data}
}));
}
window.jsb = {
jcCallback: function(funid, data) {
window.dispatchEvent(
new CustomEvent("walletcb", {
detail: { funid, data}
}));
},
// BEGIN:: only for web
signWithApple: function(funid) {
callNative({action: 'signWithApple',funid});
},
signWithGoogle: function(funid) {
callNative({action: 'signWithGoogle',funid});
},
signWithTikTok: function(funid) {
callNative({action: 'signWithTikTok',funid});
},
signWithFacebook: function(funid) {
callNative({action: 'signWithFacebook',funid});
},
// method that returns immediately
prepareWallet: function(funid) {
callNative({action: 'prepareWallet',funid, key, pass});
},
walletSecKey: function(funid) {
callNative({action: 'walletSecKey',funid, key, pass});
},
hashSvrPass: function(funid) {
callNative({action: 'walletSecKey',funid, key, pass});
},
walletSign: function() {
callNative({action: 'walletSecKey',funid, key, pass});
},
// END:: only for web
// BEGIN:: native method
scanQRCode: function(funid, title) {
callNative({action: 'scanQRCode',funid,title});
},
buyProduct: function(funid, productId, orderId) {
callNative({action: 'buyProduct', funid, productId, orderId});
},
queryPurchase: function(funid) {
callNative({action: 'queryPurchase',funid});
},
queryProducts: function(funid, productIds) {
callNative({action: 'queryProducts',funid, productIds});
},
finishTransaction: function(funid, transactionId) {
callNative({action: 'finishTransaction',funid, transactionId});
},
authGetStoragePass: function(funid, key) {
callNative({action: 'authGetStoragePass',funid, key});
},
passStorageState: function(funid, key) {
callNative({action: 'passStorageState',funid, key});
},
storagePass: function(funid, key, pass) {
callNative({action: 'storagePass',funid, key, pass});
},
// END:: native method
}
function callProxyMethod(...args) {
var methodname = args.shift();
var funid = args.shift();
// var params = JSON.stringify(args);
callNative({action: 'proxyMethod', methodname, funid, params: args})
}
class Deferred{
constructor() {
this.promise = new Promise((resolve, reject) => {
this._resolve = resolve;
this._reject = reject;
});
}
resolve(value){
this._resolve(value);
}
reject(reason){
this._reject(reason);
}
then(onfulfilled,onrejected){
return this.promise.then(onfulfilled, onrejected);
}
catch(onrejected){
return this.promise.catch(onrejected);
}
}
var cbmap = new Map();
window.addEventListener("walletcb", e => {
console.log('receive walletcb: ' + JSON.stringify(e.detail))
var data = e.detail;
if (cbmap.has(data.funid)) {
cbmap.get(data.funid).resolve(data.data);
cbmap.delete(data.funid);
}
});
function callMethod(...args) {
var methodName = args.shift()
let funid = ((Math.random() * 100000 + Math.random() * 1000) | 0) + ''
args.unshift(funid);
let deferred = new Deferred();
cbmap.set(funid, deferred)
window[methodName].apply(this, args);
return deferred.promise
}