|
|
@@ -0,0 +1,95 @@
|
|
|
+import { IBestToast } from '@ibestservices/ibest-ui';
|
|
|
+import { BaseReq, BaseResp, LaunchFromWXReq, SendAuthResp, WXApiEventHandler } from '@tencent/wechat_open_sdk';
|
|
|
+import { WechatApi } from '../../apis/WechatApi';
|
|
|
+import { AccessTokenResponse, WeChatExtData } from '../../model/WechatModel';
|
|
|
+import { YTLog } from '../YTLog';
|
|
|
+import { WechatUtil } from './WechatUtil';
|
|
|
+
|
|
|
+export type OnWXReq = (req: BaseReq) => void;
|
|
|
+
|
|
|
+export type OnWXResp = (resp: BaseResp) => void;
|
|
|
+
|
|
|
+const TAG: string = 'WeChatLoginUtils';
|
|
|
+
|
|
|
+export class WXApiEventHandlerImpl implements WXApiEventHandler {
|
|
|
+ private onReqCallbacks: Map<OnWXReq, OnWXReq> = new Map();
|
|
|
+ private onRespCallbacks: Map<OnWXResp, OnWXResp> = new Map();
|
|
|
+
|
|
|
+ registerOnWXReqCallback(on: OnWXReq) {
|
|
|
+ this.onReqCallbacks.set(on, on);
|
|
|
+ }
|
|
|
+
|
|
|
+ unregisterOnWXReqCallback(on: OnWXReq) {
|
|
|
+ this.onReqCallbacks.delete(on);
|
|
|
+ }
|
|
|
+
|
|
|
+ registerOnWXRespCallback(on: OnWXResp) {
|
|
|
+ this.onRespCallbacks.set(on, on);
|
|
|
+ }
|
|
|
+
|
|
|
+ unregisterOnWXRespCallback(on: OnWXResp) {
|
|
|
+ this.onRespCallbacks.delete(on);
|
|
|
+ }
|
|
|
+
|
|
|
+ onReq(req: BaseReq): void {
|
|
|
+ YTLog.info(TAG, `onReq: ${JSON.stringify(req)}`);
|
|
|
+ if (req instanceof LaunchFromWXReq) {
|
|
|
+ const messageExt = req.message?.messageExt;
|
|
|
+ YTLog.info(TAG, `Received LaunchFromWXReq with messageExt: ${messageExt}`);
|
|
|
+ if (messageExt) {
|
|
|
+ let extData: WeChatExtData;
|
|
|
+ try {
|
|
|
+ extData = JSON.parse(messageExt) as WeChatExtData;
|
|
|
+ YTLog.info(TAG, `Parsed extData: ${JSON.stringify(extData)}`);
|
|
|
+ if (extData.openId) {
|
|
|
+ YTLog.info(TAG, `Received openId: ${extData.openId}`);
|
|
|
+ }
|
|
|
+ } catch (e) {
|
|
|
+ YTLog.error(TAG, `Failed to parse messageExt: ${e}`);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ YTLog.warn(TAG, 'No messageExt data received');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ onResp(resp: BaseResp): void {
|
|
|
+ YTLog.info(TAG, `onResp: ${JSON.stringify(resp)}`);
|
|
|
+ if (resp instanceof SendAuthResp) {
|
|
|
+ if (resp.errCode === 0 && resp.code) {
|
|
|
+ YTLog.info(TAG, `WeChat login success, code: ${resp.code}`);
|
|
|
+ this.handleLoginSuccess(resp.code);
|
|
|
+ } else {
|
|
|
+ YTLog.error(TAG, `WeChat login failed, errCode: ${resp.errCode}, errStr: ${resp.errStr}`);
|
|
|
+ // CommonUtils.showToastContent($r('app.string.wechat_login_failed'));
|
|
|
+ IBestToast.show({ type: 'fail', message: ` ${resp.errCode}, errStr: ${resp.errStr}` })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async fetchAccessToken(code: string, appSecret: string): Promise<AccessTokenResponse | null> {
|
|
|
+ try {
|
|
|
+ return await WechatApi.getAccessToken(code, appSecret)
|
|
|
+ } catch (error) {
|
|
|
+ return null
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private async handleLoginSuccess(code: string) {
|
|
|
+ try {
|
|
|
+
|
|
|
+ let response = await this.fetchAccessToken(code, WechatUtil.appSecret);
|
|
|
+ if (!response) {
|
|
|
+ IBestToast.show({ type: 'fail', message: '获取微信授权码失败' })
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //TODO 写微信调用后端接口逻辑
|
|
|
+
|
|
|
+ } catch (error) {
|
|
|
+ IBestToast.show({ type: 'fail', message: '获取微信授权码失败' })
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export const wechatEventHandler = new WXApiEventHandlerImpl();
|