cropper.vue 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <template>
  2. <view class="container">
  3. <web-view
  4. :webview-styles="webviewStyles"
  5. @message="handleMessage"
  6. :src="webviewSrc"
  7. >
  8. </web-view>
  9. </view>
  10. </template>
  11. <script>
  12. export default {
  13. name: "buuug7-img-cropper",
  14. data() {
  15. return {
  16. webviewStyles: {},
  17. platform: "",
  18. webviewSrc:
  19. "/hybrid/html/cropper/index.html",
  20. };
  21. },
  22. mounted() {
  23. const { platform } = uni.getSystemInfoSync();
  24. this.platform = platform;
  25. console.log(platform,'获取系统对象');
  26. if (platform === "windows" || platform === "mac") {
  27. this.handleH5Message();
  28. }
  29. // #ifdef H5
  30. window.addEventListener("message", this.handleMessage, false);
  31. // #endif
  32. },
  33. methods: {
  34. handleMessage(event) {
  35. console.log('消息传输',event);
  36. // uni.navigateBack({
  37. // delta: 1,
  38. // });
  39. const platform = this.platform;
  40. // #ifdef H5
  41. if(!event.data.data.type){
  42. return;
  43. }
  44. const data = event.data.data;
  45. // #endif
  46. // #ifndef H5
  47. const data = event.detail.data[0];
  48. // #endif
  49. if (platform === "android" || platform === "ios") {
  50. const eventChannel = this.getOpenerEventChannel();
  51. eventChannel.emit("imgCropped", { data: data.dataUrl });
  52. }
  53. },
  54. handleH5Message(e) {
  55. console.log(`H5Message`);
  56. window.addEventListener("message", (event) => {
  57. const data = event.data.data;
  58. if (data && data.type === "croppedData") {
  59. const eventChannel = this.getOpenerEventChannel();
  60. eventChannel.emit("imgCropped", { data: data.dataUrl });
  61. }
  62. });
  63. },
  64. },
  65. };
  66. </script>
  67. <style></style>