main.vue 4.9 KB

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