image.vue 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <template>
  2. <span class="upload-btn icon icon-image">
  3. <input title="图片" v-if="!isMute" type="file" data-type="image" accept="image/*" @change="sendUploadMessage" />
  4. <slot />
  5. </span>
  6. </template>
  7. <script lang="ts">
  8. import { defineComponent, reactive, toRefs, watchEffect } from "vue";
  9. import { handleErrorPrompts } from "../../../utils";
  10. const Image = defineComponent({
  11. props: {
  12. show: {
  13. type: Boolean,
  14. default: () => false,
  15. },
  16. isMute: {
  17. type: Boolean,
  18. default: () => false,
  19. },
  20. isH5: {
  21. type: Boolean,
  22. default: () => false,
  23. },
  24. },
  25. setup(props: any, ctx: any) {
  26. const data = reactive({
  27. isMute: false,
  28. });
  29. watchEffect(() => {
  30. data.isMute = props.isMute;
  31. });
  32. // 发送需要上传的消息:图片
  33. const sendUploadMessage = async (e: any) => {
  34. if (e.target.files.length > 0) {
  35. try {
  36. await Image.TUIServer.sendImageMessage(e.target);
  37. } catch (error) {
  38. handleErrorPrompts(error, props);
  39. }
  40. }
  41. e.target.value = "";
  42. };
  43. return {
  44. ...toRefs(data),
  45. sendUploadMessage,
  46. };
  47. },
  48. });
  49. export default Image;
  50. </script>
  51. <style lang="scss" scoped>
  52. @import url("../../../../styles/common.scss");
  53. @import url("../../../../styles/icon.scss");
  54. .upload-btn {
  55. position: relative;
  56. input {
  57. position: absolute;
  58. cursor: pointer;
  59. font-size: 0;
  60. left: 0;
  61. top: 0;
  62. opacity: 0;
  63. width: 100%;
  64. height: 100%;
  65. }
  66. }
  67. </style>