poster.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <template>
  2. <view>
  3. <view @tap="onCreate">
  4. <slot/>
  5. </view>
  6. <we-canvas ref="poster" @success="onCreateSuccess" @fail="onCreateFail"/>
  7. </view>
  8. </template>
  9. <script>
  10. import WeCanvas from "./index.vue";
  11. export default {
  12. components: {
  13. WeCanvas
  14. },
  15. props: {
  16. config: {
  17. type: Object,
  18. default: {}
  19. },
  20. preload: {
  21. // 是否预下载图片资源
  22. type: Boolean,
  23. default: false
  24. },
  25. hideLoading: {
  26. // 是否隐藏loading
  27. type: Boolean,
  28. default: false
  29. }
  30. },
  31. data() {
  32. return {};
  33. },
  34. methods: {
  35. trigger(event, data) {
  36. if (this.listener && typeof this.listener[event] === "function") {
  37. this.listener[event](data);
  38. }
  39. },
  40. once(event, fun) {
  41. if (typeof this.listener === "undefined") {
  42. this.listener = {};
  43. }
  44. this.listener[event] = fun;
  45. },
  46. downloadResource(reset) {
  47. return new Promise((resolve, reject) => {
  48. if (reset) {
  49. this.downloadStatus = null;
  50. }
  51. const poster = this.$refs.poster
  52. if (this.downloadStatus && this.downloadStatus !== "fail") {
  53. if (this.downloadStatus === "success") {
  54. resolve();
  55. } else {
  56. this.once("downloadSuccess", () => resolve());
  57. this.once("downloadFail", e => reject(e));
  58. }
  59. } else {
  60. poster
  61. .downloadResource(this.config.images)
  62. .then(() => {
  63. this.downloadStatus = "success";
  64. resolve();
  65. })
  66. .catch(e => reject(e));
  67. }
  68. });
  69. },
  70. onCreate(reset = false) {
  71. !this.hideLoading && uni.showLoading({ mask: true, title: "生成中" });
  72. return this.downloadResource(typeof reset === "boolean" && reset)
  73. .then(() => {
  74. !this.hideLoading && uni.hideLoading();
  75. const poster = this.$refs.poster;
  76. poster.create(this.config);
  77. })
  78. .catch(err => {
  79. !this.hideLoading && uni.hideLoading();
  80. uni.showToast({ icon: "none", title: err.errMsg || "生成失败" });
  81. console.error(err);
  82. this.$emit("fail", err);
  83. });
  84. },
  85. onCreateSuccess(path) {
  86. this.$emit("success", path);
  87. },
  88. onCreateFail(err) {
  89. console.error(err);
  90. this.$emit("fail", err);
  91. },
  92. },
  93. onReady() {
  94. if (this.preload) {
  95. const poster = this.$refs.poster;
  96. this.downloadStatus = 'doing';
  97. poster.downloadResource(this.config.images).then(() => {
  98. this.downloadStatus = 'success';
  99. this.trigger('downloadSuccess');
  100. }).catch((e) => {
  101. this.downloadStatus = 'fail';
  102. this.trigger('downloadFail', e);
  103. });
  104. }
  105. },
  106. };
  107. </script>
  108. <style lang="scss">
  109. </style>