WXApiWrap.ets 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import * as wxopensdk from '@tencent/wechat_open_sdk';
  2. import { APP_ID } from '../../constants';
  3. export type OnWXReq = (req: wxopensdk.BaseReq) => void
  4. export type OnWXResp = (resp: wxopensdk.BaseResp) => void
  5. const kTag = "WXApiEventHandlerImpl"
  6. class WXApiEventHandlerImpl implements wxopensdk.WXApiEventHandler {
  7. private onReqCallbacks: Map<OnWXReq, OnWXReq> = new Map
  8. private onRespCallbacks: Map<OnWXResp, OnWXResp> = new Map
  9. registerOnWXReqCallback(on: OnWXReq) {
  10. this.onReqCallbacks.set(on, on)
  11. }
  12. unregisterOnWXReqCallback(on: OnWXReq) {
  13. this.onReqCallbacks.delete(on)
  14. }
  15. registerOnWXRespCallback(on: OnWXResp) {
  16. this.onRespCallbacks.set(on, on)
  17. }
  18. unregisterOnWXRespCallback(on: OnWXResp) {
  19. this.onRespCallbacks.delete(on)
  20. }
  21. onReq(req: wxopensdk.BaseReq): void {
  22. wxopensdk.Log.i(kTag, "onReq:%s", JSON.stringify(req))
  23. this.onReqCallbacks.forEach((on) => {
  24. on(req)
  25. })
  26. }
  27. onResp(resp: wxopensdk.BaseResp): void {
  28. wxopensdk.Log.i(kTag, "onResp:%s", JSON.stringify(resp))
  29. this.onRespCallbacks.forEach((on) => {
  30. on(resp)
  31. })
  32. }
  33. }
  34. export const WXApi = wxopensdk.WXAPIFactory.createWXAPI(APP_ID)
  35. export const WXEventHandler = new WXApiEventHandlerImpl