SsqChatH5Native.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { Canvas, director, Node, WebView, Widget } from "cc";
  2. import { handleUserData, ISsqChatImpl, SsqUser } from "./ISsqChatImpl";
  3. import { ANDROID } from "cc/env";
  4. export class SsqChatNative implements ISsqChatImpl {
  5. private _webView: WebView;
  6. private _bondData: any;
  7. ensureReady(): Promise<void> {
  8. if (!this._webView) {
  9. const node = new Node("SsqChat");
  10. const scene = director.getScene();
  11. const canvas = scene.getComponentInChildren(Canvas);
  12. if (canvas) node.setParent(canvas.node);
  13. const widget = node.addComponent(Widget);
  14. widget.isAlignLeft = true;
  15. widget.isAlignRight = true;
  16. widget.isAlignBottom = true;
  17. widget.isAlignTop = true;
  18. widget.left = 0;
  19. widget.right = 0;
  20. widget.top = 0;
  21. widget.bottom = 0;
  22. widget.updateAlignment();
  23. this._webView = node.addComponent(WebView);
  24. this._webView.setJavascriptInterfaceScheme("ssqchat");
  25. //@ts-ignore
  26. this._webView.setOnJSCallback((_view, url) => {
  27. if (url === 'ssqchat://close') {
  28. this._webView.node.active = false;
  29. }
  30. });
  31. }
  32. return null;
  33. }
  34. bindUser(user: SsqUser | null): void {
  35. const hostname = ANDROID ? "app_android" : "app_ios";
  36. this._bondData = handleUserData(user, hostname);
  37. }
  38. openChat(): void {
  39. if (!this._webView) return;
  40. if (!this._webView.node.active) this._webView.node.active = true;
  41. let link = "http://192.168.1.248:8080/ssqchat.html";//AppKernel.stores.mainStore.onlineChat;
  42. if (this._bondData) {
  43. let params = JSON.stringify(this._bondData);
  44. params = encodeURIComponent(params);
  45. link = `${link}?${params}`;
  46. }
  47. console.log(link);
  48. this._webView.url = link;
  49. }
  50. closeChat(): void {
  51. if (!this._webView) return;
  52. this._webView.node.active = false;
  53. }
  54. }