main.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <!--
  2. - Copyright (C) 2018-2019
  3. - All rights reserved, Designed By www.joolun.com
  4. 芋道源码:
  5. ① 移除多余的 rep 为前缀的变量,让 message 消息更简单
  6. ② 代码优化,补充注释,提升阅读性
  7. ③ 优化消息的临时缓存策略,发送消息时,只清理被发送消息的 tab,不会强制切回到 text 输入
  8. ④ 支持发送【视频】消息时,支持新建视频
  9. -->
  10. <template>
  11. <el-tabs type="border-card" v-model="currentTab">
  12. <!-- 类型 1:文本 -->
  13. <el-tab-pane :name="ReplyType.Text">
  14. <template #label>
  15. <el-row align="middle"><Icon icon="ep:document" /> 文本</el-row>
  16. </template>
  17. <TabText v-model="reply.content" />
  18. </el-tab-pane>
  19. <!-- 类型 2:图片 -->
  20. <el-tab-pane :name="ReplyType.Image">
  21. <template #label>
  22. <el-row align="middle"><Icon icon="ep:picture" class="mr-5px" /> 图片</el-row>
  23. </template>
  24. <TabImage v-model="reply" />
  25. </el-tab-pane>
  26. <!-- 类型 3:语音 -->
  27. <el-tab-pane :name="ReplyType.Voice">
  28. <template #label>
  29. <el-row align="middle"><Icon icon="ep:phone" /> 语音</el-row>
  30. </template>
  31. <TabVoice v-model="reply" />
  32. </el-tab-pane>
  33. <!-- 类型 4:视频 -->
  34. <el-tab-pane :name="ReplyType.Video">
  35. <template #label>
  36. <el-row align="middle"><Icon icon="ep:share" /> 视频</el-row>
  37. </template>
  38. <TabVideo v-model="reply" />
  39. </el-tab-pane>
  40. <!-- 类型 5:图文 -->
  41. <el-tab-pane :name="ReplyType.News">
  42. <template #label>
  43. <el-row align="middle"><Icon icon="ep:reading" /> 图文</el-row>
  44. </template>
  45. <TabNews v-model="reply" :news-type="newsType" />
  46. </el-tab-pane>
  47. <!-- 类型 6:音乐 -->
  48. <el-tab-pane :name="ReplyType.Music">
  49. <template #label>
  50. <el-row align="middle"><Icon icon="ep:service" />音乐</el-row>
  51. </template>
  52. <TabMusic v-model="reply" />
  53. </el-tab-pane>
  54. </el-tabs>
  55. </template>
  56. <script setup lang="ts" name="WxReplySelect">
  57. import { Reply, NewsType, ReplyType, createEmptyReply } from './components/types'
  58. import TabText from './components/TabText.vue'
  59. import TabImage from './components/TabImage.vue'
  60. import TabVoice from './components/TabVoice.vue'
  61. import TabVideo from './components/TabVideo.vue'
  62. import TabNews from './components/TabNews.vue'
  63. import TabMusic from './components/TabMusic.vue'
  64. interface Props {
  65. modelValue: Reply
  66. newsType?: NewsType
  67. }
  68. const props = withDefaults(defineProps<Props>(), {
  69. newsType: () => NewsType.Published
  70. })
  71. const emit = defineEmits<{
  72. (e: 'update:modelValue', v: Reply)
  73. }>()
  74. const reply = computed<Reply>({
  75. get: () => props.modelValue,
  76. set: (val) => emit('update:modelValue', val)
  77. })
  78. // 作为多个标签保存各自Reply的缓存
  79. const tabCache = new Map<ReplyType, Reply>()
  80. // 采用独立的ref来保存当前tab,避免在watch标签变化,对reply进行赋值会产生了循环调用
  81. const currentTab = ref<ReplyType>(props.modelValue.type || ReplyType.Text)
  82. watch(
  83. currentTab,
  84. (newTab, oldTab) => {
  85. // 第一次进入:oldTab 为 undefined
  86. // 判断 newTab 是因为 Reply 为 Partial
  87. if (oldTab === undefined || newTab === undefined) {
  88. return
  89. }
  90. tabCache.set(oldTab, unref(reply))
  91. // 从缓存里面取出新tab内容,有则覆盖Reply,没有则创建空Reply
  92. const temp = tabCache.get(newTab)
  93. if (temp) {
  94. reply.value = temp
  95. } else {
  96. let newData = createEmptyReply(reply)
  97. newData.type = newTab
  98. reply.value = newData
  99. }
  100. },
  101. {
  102. immediate: true
  103. }
  104. )
  105. /** 清除除了`type`, `accountId`的字段 */
  106. const clear = () => {
  107. reply.value = createEmptyReply(reply)
  108. }
  109. defineExpose({
  110. clear
  111. })
  112. </script>
  113. <style lang="scss" scoped>
  114. .select-item {
  115. width: 280px;
  116. padding: 10px;
  117. margin: 0 auto 10px;
  118. border: 1px solid #eaeaea;
  119. }
  120. .select-item2 {
  121. padding: 10px;
  122. margin: 0 auto 10px;
  123. border: 1px solid #eaeaea;
  124. }
  125. .ope-row {
  126. padding-top: 10px;
  127. text-align: center;
  128. }
  129. .input-margin-bottom {
  130. margin-bottom: 2%;
  131. }
  132. .item-name {
  133. overflow: hidden;
  134. font-size: 12px;
  135. text-align: center;
  136. text-overflow: ellipsis;
  137. white-space: nowrap;
  138. }
  139. .el-form-item__content {
  140. line-height: unset !important;
  141. }
  142. .col-select {
  143. width: 49.5%;
  144. height: 160px;
  145. padding: 50px 0;
  146. border: 1px solid rgb(234 234 234);
  147. }
  148. .col-select2 {
  149. height: 160px;
  150. padding: 50px 0;
  151. border: 1px solid rgb(234 234 234);
  152. }
  153. .col-add {
  154. float: right;
  155. width: 49.5%;
  156. height: 160px;
  157. padding: 50px 0;
  158. border: 1px solid rgb(234 234 234);
  159. }
  160. .avatar-uploader-icon {
  161. width: 100px !important;
  162. height: 100px !important;
  163. font-size: 28px;
  164. line-height: 100px !important;
  165. color: #8c939d;
  166. text-align: center;
  167. border: 1px solid #d9d9d9;
  168. }
  169. .material-img {
  170. width: 100%;
  171. }
  172. .thumb-div {
  173. display: inline-block;
  174. text-align: center;
  175. }
  176. .item-infos {
  177. width: 30%;
  178. margin: auto;
  179. }
  180. </style>