image.vue 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. left: 0;
  60. top: 0;
  61. opacity: 0;
  62. width: 100%;
  63. height: 100%;
  64. }
  65. }
  66. </style>