main.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 lang="ts" setup>
  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. defineOptions({ name: 'WxReplySelect' })
  65. interface Props {
  66. modelValue: Reply
  67. newsType?: NewsType
  68. }
  69. const props = withDefaults(defineProps<Props>(), {
  70. newsType: () => NewsType.Published
  71. })
  72. const emit = defineEmits<{
  73. (e: 'update:modelValue', v: Reply)
  74. }>()
  75. const reply = computed<Reply>({
  76. get: () => props.modelValue,
  77. set: (val) => emit('update:modelValue', val)
  78. })
  79. // 作为多个标签保存各自Reply的缓存
  80. const tabCache = new Map<ReplyType, Reply>()
  81. // 采用独立的ref来保存当前tab,避免在watch标签变化,对reply进行赋值会产生了循环调用
  82. const currentTab = ref<ReplyType>(props.modelValue.type || ReplyType.Text)
  83. watch(
  84. currentTab,
  85. (newTab, oldTab) => {
  86. // 第一次进入:oldTab 为 undefined
  87. // 判断 newTab 是因为 Reply 为 Partial
  88. if (oldTab === undefined || newTab === undefined) {
  89. return
  90. }
  91. tabCache.set(oldTab, unref(reply))
  92. // 从缓存里面取出新tab内容,有则覆盖Reply,没有则创建空Reply
  93. const temp = tabCache.get(newTab)
  94. if (temp) {
  95. reply.value = temp
  96. } else {
  97. let newData = createEmptyReply(reply)
  98. newData.type = newTab
  99. reply.value = newData
  100. }
  101. },
  102. {
  103. immediate: true
  104. }
  105. )
  106. /** 清除除了`type`, `accountId`的字段 */
  107. const clear = () => {
  108. reply.value = createEmptyReply(reply)
  109. }
  110. defineExpose({
  111. clear
  112. })
  113. </script>
  114. <style lang="scss" scoped>
  115. .select-item {
  116. width: 280px;
  117. padding: 10px;
  118. margin: 0 auto 10px;
  119. border: 1px solid #eaeaea;
  120. }
  121. .select-item2 {
  122. padding: 10px;
  123. margin: 0 auto 10px;
  124. border: 1px solid #eaeaea;
  125. }
  126. .ope-row {
  127. padding-top: 10px;
  128. text-align: center;
  129. }
  130. .input-margin-bottom {
  131. margin-bottom: 2%;
  132. }
  133. .item-name {
  134. overflow: hidden;
  135. font-size: 12px;
  136. text-align: center;
  137. text-overflow: ellipsis;
  138. white-space: nowrap;
  139. }
  140. .el-form-item__content {
  141. line-height: unset !important;
  142. }
  143. .col-select {
  144. width: 49.5%;
  145. height: 160px;
  146. padding: 50px 0;
  147. border: 1px solid rgb(234 234 234);
  148. }
  149. .col-select2 {
  150. height: 160px;
  151. padding: 50px 0;
  152. border: 1px solid rgb(234 234 234);
  153. }
  154. .col-add {
  155. float: right;
  156. width: 49.5%;
  157. height: 160px;
  158. padding: 50px 0;
  159. border: 1px solid rgb(234 234 234);
  160. }
  161. .avatar-uploader-icon {
  162. width: 100px !important;
  163. height: 100px !important;
  164. font-size: 28px;
  165. line-height: 100px !important;
  166. color: #8c939d;
  167. text-align: center;
  168. border: 1px solid #d9d9d9;
  169. }
  170. .material-img {
  171. width: 100%;
  172. }
  173. .thumb-div {
  174. display: inline-block;
  175. text-align: center;
  176. }
  177. .item-infos {
  178. width: 30%;
  179. margin: auto;
  180. }
  181. </style>