fr-wvjs.nvue 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <template>
  2. <web-view v-if="isInit" class="__flower-web-view" :ref="wv" @onPostMessage="changeMessageWv" src="/uni_modules/flower-config/hybrid/html/fr-wvjs.html" />
  3. </template>
  4. <script>
  5. import {pageStoreMap,tempStoreMap,webviewContextStoreMap} from "./store/wvStore.js";
  6. import {uuid,getCurrentPagesRoute,mixinStore} from "@/uni_modules/flower-api/uni-app"
  7. export default {
  8. mixins: [mixinStore],
  9. data() {
  10. return {
  11. wv: uuid(32),
  12. isInit: false,
  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[this.getCurrentPagesRoute] == undefined) {
  38. this.isInit = true;
  39. pageStoreMap[this.getCurrentPagesRoute] = this.wv;
  40. };
  41. if (webviewContextStoreMap[this.getCurrentPagesRoute] == undefined) {
  42. if (this.getStoreState(this.resourceId, this.isCache) == "") {
  43. tempStoreMap[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) {
  56. if (event.detail.data[0].isInitialize) {
  57. webviewContextStoreMap[this.getCurrentPagesRoute] = this.wv;
  58. for (let i in tempStoreMap) {
  59. this.getwebviewContext(i, tempStoreMap[i]);
  60. };
  61. } else {
  62. this.setStoreState(event.detail.data[0].id, event.detail.data[0].result, this.isCache);
  63. delete tempStoreMap[event.detail.data[0].id];
  64. };
  65. },
  66. getwebviewContext(resourceId, resource) {
  67. if (this.getStoreState(resourceId, this.isCache) == "") {
  68. this.$refs[webviewContextStoreMap[this.getCurrentPagesRoute]].evalJS(
  69. `onPostMessage('${this.type}','${resourceId}','${resource}')`);
  70. };
  71. }
  72. },
  73. destroyed() {
  74. delete webviewContextStoreMap[this.getCurrentPagesRoute];
  75. delete pageStoreMap[this.getCurrentPagesRoute];
  76. }
  77. }
  78. </script>
  79. <style>
  80. .__flower-web-view {
  81. width: 0rpx;
  82. height: 0rpx;
  83. opacity: 0;
  84. }
  85. </style>