UNPKG

@react-native-ohos/react-native-wechat-lib

Version:

react native ohos react native wechat lib

495 lines (467 loc) 14.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.authByScan = exports.addListener = exports.WechatError = void 0; exports.chooseInvoice = chooseInvoice; exports.isWXAppSupportApi = exports.isWXAppInstalled = exports.getApiVersion = void 0; exports.launchMiniProgram = launchMiniProgram; exports.openWXApp = exports.once = void 0; exports.pay = pay; exports.removeAllListeners = exports.registerApp = void 0; exports.sendAuthRequest = sendAuthRequest; exports.shareFile = shareFile; exports.shareImage = shareImage; exports.shareLocalImage = shareLocalImage; exports.shareMiniProgram = shareMiniProgram; exports.shareMusic = shareMusic; exports.shareText = shareText; exports.shareVideo = shareVideo; exports.shareWebpage = shareWebpage; exports.subscribeMessage = subscribeMessage; var _events = require("events"); var _reactNative = require("react-native"); var _NativeRNWechatLibModule = _interopRequireDefault(require("./specs/NativeRNWechatLibModule")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } let isAppRegistered = false; // Event emitter to dispatch request and response from WechatLib. const emitter = new _events.EventEmitter(); _reactNative.DeviceEventEmitter.addListener("WeChat_Resp", resp => { emitter.emit(resp.type, resp); }); _reactNative.DeviceEventEmitter.addListener("WeChat_Req", resp => { emitter.emit(resp.type, resp); }); function wrapRegisterApp(nativeFunc) { if (!nativeFunc) { return undefined; } return (...args) => { if (isAppRegistered) { return Promise.resolve(true); } isAppRegistered = true; // 注册状态修改 return new Promise((resolve, reject) => { nativeFunc.apply(null, [...args, (error, result) => { if (!error) { return resolve(result); } if (typeof error === "string") { return reject(new Error(error)); } reject(error); }]); }); }; } function wrapApi(nativeFunc) { if (!nativeFunc) { return undefined; } return (...args) => { if (!isAppRegistered) { return Promise.reject(new Error("registerApp required.")); } return new Promise((resolve, reject) => { nativeFunc.apply(null, [...args, (error, result) => { if (!error) { return resolve(result); } if (typeof error === "string") { return reject(new Error(error)); } reject(error); }]); }); }; } /** * `addListener` inherits from `events` module * @method addListener * @param {String} eventName - the event name * @param {Function} trigger - the function when event is fired */ const addListener = exports.addListener = emitter.addListener.bind(emitter); /** * `once` inherits from `events` module * @method once * @param {String} eventName - the event name * @param {Function} trigger - the function when event is fired */ const once = exports.once = emitter.once.bind(emitter); /** * `removeAllListeners` inherits from `events` module * @method removeAllListeners * @param {String} eventName - the event name */ const removeAllListeners = exports.removeAllListeners = emitter.removeAllListeners.bind(emitter); /** * @method registerApp * @param {String} appid - the app id * @return {Promise} */ const registerApp = exports.registerApp = wrapRegisterApp(_NativeRNWechatLibModule.default.registerApp); // /** // * @method registerAppWithDescription // * @param {String} appid - the app id // * @param {String} appdesc - the app description // * @return {Promise} // */ // export const registerAppWithDescription = wrapRegisterApp( // WechatLib.registerAppWithDescription, // ); /** * Return if the wechat app is installed in the device. * @method isWXAppInstalled * @return {Promise} */ const isWXAppInstalled = exports.isWXAppInstalled = wrapApi(_NativeRNWechatLibModule.default.isWXAppInstalled); /** * Return if the wechat application supports the api * @method isWXAppSupportApi * @return {Promise} */ const isWXAppSupportApi = exports.isWXAppSupportApi = wrapApi(_NativeRNWechatLibModule.default.isWXAppSupportApi); /** * Get the wechat app installed url * @method getWXAppInstallUrl * @return {String} the wechat app installed url */ // export const getWXAppInstallUrl = wrapApi(WechatLib.getWXAppInstallUrl); /** * Get the wechat api version * @method getApiVersion * @return {String} the api version string */ const getApiVersion = exports.getApiVersion = wrapApi(_NativeRNWechatLibModule.default.getApiVersion); /** * Open wechat app * @method openWXApp * @return {Promise} */ const openWXApp = exports.openWXApp = wrapApi(_NativeRNWechatLibModule.default.openWXApp); // wrap the APIs const nativeLaunchMiniProgram = wrapApi(_NativeRNWechatLibModule.default.launchMiniProgram); const nativeShareText = wrapApi(_NativeRNWechatLibModule.default.shareText); const nativeShareImage = wrapApi(_NativeRNWechatLibModule.default.shareImage); const nativeShareLocalImage = wrapApi(_NativeRNWechatLibModule.default.shareLocalImage); const nativeShareMusic = wrapApi(_NativeRNWechatLibModule.default.shareMusic); const nativeShareVideo = wrapApi(_NativeRNWechatLibModule.default.shareVideo); const nativeShareWebpage = wrapApi(_NativeRNWechatLibModule.default.shareWebpage); const nativeShareMiniProgram = wrapApi(_NativeRNWechatLibModule.default.shareMiniProgram); const nativeSubscribeMessage = wrapApi(_NativeRNWechatLibModule.default.subscribeMessage); const nativeChooseInvoice = wrapApi(_NativeRNWechatLibModule.default.chooseInvoice); const nativeShareFile = wrapApi(_NativeRNWechatLibModule.default.shareFile); const authByScan = exports.authByScan = wrapApi(_NativeRNWechatLibModule.default.authByScan); const NormalRes = { errCode: 0, errStr: '' }; /** * @method sendAuthRequest * @param {Array} scopes - the scopes for authentication. * @return {Promise} */ function sendAuthRequest(scopes, state) { return new Promise((resolve, reject) => { const onSendAuthRes = resp => { _NativeRNWechatLibModule.default.unregisterCallback("SendAuth.Resp"); if (resp.errCode === 0) { resolve(resp); } else { reject(new WechatError(resp)); } }; _NativeRNWechatLibModule.default.registerCallback("SendAuth.Resp", onSendAuthRes); _NativeRNWechatLibModule.default.sendAuthRequest(scopes, state, () => {}); }); } /** * Share text * @method shareText * @param {Object} data */ function shareText(data) { if (data && data.scene == null) { data.scene = 0; } return new Promise(async (resolve, reject) => { try { await (nativeShareText === null || nativeShareText === void 0 ? void 0 : nativeShareText(data)); resolve(NormalRes); } catch (error) { reject({ errCode: -1, errStr: error.message }); } }); } /** * Choose Invoice * @method chooseInvoice * @param {Object} data */ function chooseInvoice(data = {}) { return new Promise((resolve, reject) => { nativeChooseInvoice === null || nativeChooseInvoice === void 0 || nativeChooseInvoice(data); emitter.once("WXChooseInvoiceResp.Resp", resp => { if (resp.errCode === 0) { resolve(resp); } else { reject(new WechatError(resp)); } }); }); } /** * Share File * @method shareFile * @param {Object} data */ function shareFile(data) { return new Promise((resolve, reject) => { nativeShareFile === null || nativeShareFile === void 0 || nativeShareFile(data); emitter.once("SendMessageToWX.Resp", resp => { if (resp.errCode === 0) { resolve(resp); } else { reject(new WechatError(resp)); } }); }); } /** * Share image * @method shareImage * @param {Object} data */ function shareImage(data) { if (data && data.scene == null) { data.scene = 0; } return new Promise(async (resolve, reject) => { try { await (nativeShareImage === null || nativeShareImage === void 0 ? void 0 : nativeShareImage(data)); resolve(NormalRes); } catch (error) { reject({ errCode: -1, errStr: error.message }); } }); } /** * Share local image * @method shareLocalImage * @param {Object} data */ function shareLocalImage(data) { if (data && data.scene == null) { data.scene = 0; } return new Promise(async (resolve, reject) => { try { await (nativeShareLocalImage === null || nativeShareLocalImage === void 0 ? void 0 : nativeShareLocalImage(data)); resolve(NormalRes); } catch (error) { reject({ errCode: -1, errStr: error.message }); } }); } /** * Share music * @method shareMusic * @param {Object} data */ function shareMusic(data) { if (data && data.scene == null) { data.scene = 0; } return new Promise((resolve, reject) => { nativeShareMusic === null || nativeShareMusic === void 0 || nativeShareMusic(data); emitter.once("SendMessageToWX.Resp", resp => { if (resp.errCode === 0) { resolve(resp); } else { reject(new WechatError(resp)); } }); }); } /** * Share video * @method shareVideo * @param {Object} data */ function shareVideo(data) { if (data && data.scene == null) { data.scene = 0; } return new Promise((resolve, reject) => { nativeShareVideo === null || nativeShareVideo === void 0 || nativeShareVideo(data); emitter.once("SendMessageToWX.Resp", resp => { if (resp.errCode === 0) { resolve(resp); } else { reject(new WechatError(resp)); } }); }); } /** * Share webpage * @method shareWebpage * @param {Object} data */ function shareWebpage(data) { if (data && data.scene == null) { data.scene = 0; } return new Promise((resolve, reject) => { nativeShareWebpage === null || nativeShareWebpage === void 0 || nativeShareWebpage(data); emitter.once("SendMessageToWX.Resp", resp => { if (resp.errCode === 0) { resolve(resp); } else { reject(new WechatError(resp)); } }); }); } /** * Share miniProgram * @method shareMiniProgram * @param {Object} data */ function shareMiniProgram(data) { if (data && data.scene == null) { data.scene = 0; } if (data && data.miniProgramType == null) { data.miniProgramType = 0; } return new Promise((resolve, reject) => { nativeShareMiniProgram === null || nativeShareMiniProgram === void 0 || nativeShareMiniProgram(data); emitter.once("SendMessageToWX.Resp", resp => { if (resp.errCode === 0) { resolve(resp); } else { reject(new WechatError(resp)); } }); }); } /** * 打开小程序 * @method launchMini * @param * @param {String} userName - 拉起的小程序的username * @param {Integer} miniProgramType - 拉起小程序的类型. 0-正式版 1-开发版 2-体验版 * @param {String} path - 拉起小程序页面的可带参路径,不填默认拉起小程序首页 */ function launchMiniProgram({ userName, miniProgramType = 0, path = "" }) { return new Promise((resolve, reject) => { if (miniProgramType !== 0 && miniProgramType !== 1 && miniProgramType !== 2) { reject(new WechatError({ errStr: "拉起小程序的类型不对,0-正式版 1-开发版 2-体验版", errCode: -1 })); return; } const onLaunchMiniRes = resp => { _NativeRNWechatLibModule.default.unregisterCallback("WXLaunchMiniProgramReq.Resp"); if (resp.errCode === 0) { resolve(resp); } else { reject(new WechatError(resp)); } }; _NativeRNWechatLibModule.default.registerCallback("WXLaunchMiniProgramReq.Resp", onLaunchMiniRes); nativeLaunchMiniProgram === null || nativeLaunchMiniProgram === void 0 || nativeLaunchMiniProgram({ userName, miniProgramType, path }); }); } /** * 一次性订阅消息 * @method shareVideo * @param {Object} data */ function subscribeMessage(data) { if (data && data.scene == null) { data.scene = 0; } return new Promise((resolve, reject) => { nativeSubscribeMessage === null || nativeSubscribeMessage === void 0 || nativeSubscribeMessage(data); emitter.once("WXSubscribeMsgReq.Resp", resp => { if (resp.errCode === 0) { resolve(resp); } else { reject(new WechatError(resp)); } }); }); } /** * 支付 * @param data PaymentLoad * @returns Promise<PayResponse> */ function pay(data) { function correct(actual, fixed) { if (!data[fixed] && data[actual]) { data[fixed] = data[actual]; delete data[actual]; } } correct("prepayid", "prepayId"); correct("noncestr", "nonceStr"); correct("partnerid", "partnerId"); correct("timestamp", "timeStamp"); data.timeStamp = String(data.timeStamp); return new Promise((resolve, reject) => { const onPayRes = resp => { _NativeRNWechatLibModule.default.unregisterCallback("Pay.Resp"); if (resp.errCode === 0) { resolve(resp); } else { reject(new WechatError(resp)); } }; _NativeRNWechatLibModule.default.registerCallback("Pay.Resp", onPayRes); _NativeRNWechatLibModule.default.pay(data, result => { if (result) reject(result); }); }); } /** * promises will reject with this error when API call finish with an errCode other than zero. */ class WechatError extends Error { constructor(resp) { const message = resp.errStr || resp.errCode.toString(); super(message); this.name = "WechatError"; this.code = resp.errCode; // 处理 Babel 对 Error 类继承的限制 if (typeof Object.setPrototypeOf === "function") { Object.setPrototypeOf(this, WechatError.prototype); } else { // @ts-ignore this.__proto__ = WechatError.prototype; } } } exports.WechatError = WechatError; //# sourceMappingURL=index.js.map