fr-wvjs.uvue 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <template>
  2. <web-view v-if="isInit" class="__flower-web-view" :id="wv" @message="changeMessageWv" src="/uni_modules/flower-config/hybrid/html/fr-wvjs.html" />
  3. </template>
  4. <script>
  5. import { pageStoreMap, tempStoreMap, webviewContextStoreMap } from "./store/wvStore.uts";
  6. import { uuid, getCurrentPagesRoute, mixinStore } from "@/uni_modules/flower-api/uni-app-x";
  7. export default {
  8. mixins: [mixinStore],
  9. data() {
  10. return {
  11. wv: uuid(32) as string,
  12. isInit: false as Boolean,
  13. getCurrentPagesRoute: getCurrentPagesRoute()
  14. }
  15. },
  16. props: {
  17. type: {
  18. type: String,
  19. default: "svg"
  20. },
  21. isCache: {
  22. type: Boolean,
  23. default: false
  24. },
  25. resourceId: {
  26. type: String,
  27. default: "",
  28. required: true
  29. },
  30. resource: {
  31. type: String,
  32. default: "",
  33. required: true
  34. }
  35. },
  36. created() {
  37. if (pageStoreMap.get(this.getCurrentPagesRoute) == null) {
  38. this.isInit = true;
  39. pageStoreMap.set(this.getCurrentPagesRoute as string, this.wv);
  40. };
  41. if (webviewContextStoreMap.get(this.getCurrentPagesRoute) == null) {
  42. if (this.getStoreState(this.resourceId, this.isCache) == "") {
  43. tempStoreMap.set(this.resourceId, this.resource);
  44. };
  45. } else {
  46. this.getwebviewContext(this.resourceId, this.resource);
  47. };
  48. },
  49. watch: {
  50. resource() {
  51. this.getwebviewContext(this.resourceId, this.resource);
  52. }
  53. },
  54. methods: {
  55. changeMessageWv(event : UniWebViewMessageEvent) {
  56. const _this = this;
  57. if (event.detail.data[0]['isInitialize'] ?? false == true) {
  58. webviewContextStoreMap.set(_this.getCurrentPagesRoute as string, uni.createWebviewContext(pageStoreMap.get(_this.getCurrentPagesRoute) as string, _this) as WebviewContext);
  59. tempStoreMap.forEach(function (value : string, key : string) {
  60. _this.getwebviewContext(key, value);
  61. });
  62. } else {
  63. _this.setStoreState(event.detail.data[0]['id'] as string, event.detail.data[0]['result'] as string, _this.isCache);
  64. tempStoreMap.delete(event.detail.data[0]['id'] as string);
  65. };
  66. },
  67. getwebviewContext(resourceId : string, resource : string) {
  68. if (this.getStoreState(resourceId, this.isCache) == "") {
  69. webviewContextStoreMap.get(this.getCurrentPagesRoute)?.evalJS(`onPostMessage('${this.type}','${resourceId}','${resource}')`);
  70. };
  71. }
  72. },
  73. unmounted() {
  74. webviewContextStoreMap.delete(this.getCurrentPagesRoute as string);
  75. pageStoreMap.delete(this.getCurrentPagesRoute as string);
  76. }
  77. }
  78. </script>
  79. <style>
  80. .__flower-web-view {
  81. display: none;
  82. width: 0rpx;
  83. height: 0rpx;
  84. }
  85. </style>