index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <template>
  2. <doc-alert title="自动回复" url="https://doc.iocoder.cn/mp/auto-reply/" />
  3. <!-- 搜索工作栏 -->
  4. <ContentWrap>
  5. <el-form class="-mb-15px" :model="queryParams" :inline="true" label-width="68px">
  6. <el-form-item label="公众号" prop="accountId">
  7. <WxMpSelect @change="onAccountChanged" />
  8. </el-form-item>
  9. </el-form>
  10. </ContentWrap>
  11. <!-- tab 切换 -->
  12. <ContentWrap>
  13. <el-tabs v-model="msgType" @tab-change="handleTabChange">
  14. <!-- 操作工具栏 -->
  15. <el-row :gutter="10" class="mb8">
  16. <el-col :span="1.5">
  17. <el-button
  18. type="primary"
  19. plain
  20. @click="handleAdd"
  21. v-hasPermi="['mp:auto-reply:create']"
  22. v-if="msgType !== MsgType.Follow || list.length <= 0"
  23. >
  24. <Icon icon="ep:plus" />新增
  25. </el-button>
  26. </el-col>
  27. </el-row>
  28. <!-- tab 项 -->
  29. <el-tab-pane :name="MsgType.Follow">
  30. <template #label>
  31. <span><Icon icon="ep:star" /> 关注时回复</span>
  32. </template>
  33. </el-tab-pane>
  34. <el-tab-pane :name="MsgType.Message">
  35. <template #label>
  36. <span><Icon icon="ep:chat-line-round" /> 消息回复</span>
  37. </template>
  38. </el-tab-pane>
  39. <el-tab-pane :name="MsgType.Keyword">
  40. <template #label>
  41. <span><Icon icon="fa:newspaper-o" /> 关键词回复</span>
  42. </template>
  43. </el-tab-pane>
  44. </el-tabs>
  45. <!-- 列表 -->
  46. <el-table v-loading="loading" :data="list">
  47. <el-table-column
  48. label="请求消息类型"
  49. align="center"
  50. prop="requestMessageType"
  51. v-if="msgType === MsgType.Message"
  52. />
  53. <el-table-column
  54. label="关键词"
  55. align="center"
  56. prop="requestKeyword"
  57. v-if="msgType === MsgType.Keyword"
  58. />
  59. <el-table-column
  60. label="匹配类型"
  61. align="center"
  62. prop="requestMatch"
  63. v-if="msgType === MsgType.Keyword"
  64. >
  65. <template #default="scope">
  66. <dict-tag :type="DICT_TYPE.MP_AUTO_REPLY_REQUEST_MATCH" :value="scope.row.requestMatch" />
  67. </template>
  68. </el-table-column>
  69. <el-table-column label="回复消息类型" align="center">
  70. <template #default="scope">
  71. <dict-tag :type="DICT_TYPE.MP_MESSAGE_TYPE" :value="scope.row.responseMessageType" />
  72. </template>
  73. </el-table-column>
  74. <el-table-column label="回复内容" align="center">
  75. <template #default="scope">
  76. <div v-if="scope.row.responseMessageType === 'text'">{{ scope.row.responseContent }}</div>
  77. <div v-else-if="scope.row.responseMessageType === 'voice'">
  78. <WxVoicePlayer v-if="scope.row.responseMediaUrl" :url="scope.row.responseMediaUrl" />
  79. </div>
  80. <div v-else-if="scope.row.responseMessageType === 'image'">
  81. <a target="_blank" :href="scope.row.responseMediaUrl">
  82. <img :src="scope.row.responseMediaUrl" style="width: 100px" />
  83. </a>
  84. </div>
  85. <div
  86. v-else-if="
  87. scope.row.responseMessageType === 'video' ||
  88. scope.row.responseMessageType === 'shortvideo'
  89. "
  90. >
  91. <WxVideoPlayer
  92. v-if="scope.row.responseMediaUrl"
  93. :url="scope.row.responseMediaUrl"
  94. style="margin-top: 10px"
  95. />
  96. </div>
  97. <div v-else-if="scope.row.responseMessageType === 'news'">
  98. <WxNews :articles="scope.row.responseArticles" />
  99. </div>
  100. <div v-else-if="scope.row.responseMessageType === 'music'">
  101. <WxMusic
  102. :title="scope.row.responseTitle"
  103. :description="scope.row.responseDescription"
  104. :thumb-media-url="scope.row.responseThumbMediaUrl"
  105. :music-url="scope.row.responseMusicUrl"
  106. :hq-music-url="scope.row.responseHqMusicUrl"
  107. />
  108. </div>
  109. </template>
  110. </el-table-column>
  111. <el-table-column
  112. label="创建时间"
  113. align="center"
  114. prop="createTime"
  115. :formatter="dateFormatter"
  116. width="180"
  117. />
  118. <el-table-column label="操作" align="center">
  119. <template #default="scope">
  120. <el-button
  121. type="primary"
  122. link
  123. @click="handleUpdate(scope.row)"
  124. v-hasPermi="['mp:auto-reply:update']"
  125. >
  126. 修改
  127. </el-button>
  128. <el-button
  129. type="danger"
  130. link
  131. @click="handleDelete(scope.row)"
  132. v-hasPermi="['mp:auto-reply:delete']"
  133. >
  134. 删除
  135. </el-button>
  136. </template>
  137. </el-table-column>
  138. </el-table>
  139. <!-- 添加或修改自动回复的对话框 -->
  140. <el-dialog :title="title" v-model="showReplyFormDialog" width="800px" destroy-on-close>
  141. <el-form ref="formRef" :model="replyForm" :rules="rules" label-width="80px">
  142. <el-form-item label="消息类型" prop="requestMessageType" v-if="msgType === MsgType.Message">
  143. <el-select v-model="replyForm.requestMessageType" placeholder="请选择">
  144. <template v-for="dict in getDictOptions(DICT_TYPE.MP_MESSAGE_TYPE)" :key="dict.value">
  145. <el-option
  146. v-if="RequestMessageTypes.includes(dict.value)"
  147. :label="dict.label"
  148. :value="dict.value"
  149. />
  150. </template>
  151. </el-select>
  152. </el-form-item>
  153. <el-form-item label="匹配类型" prop="requestMatch" v-if="msgType === MsgType.Keyword">
  154. <el-select v-model="replyForm.requestMatch" placeholder="请选择匹配类型" clearable>
  155. <el-option
  156. v-for="dict in getDictOptions(DICT_TYPE.MP_AUTO_REPLY_REQUEST_MATCH)"
  157. :key="dict.value"
  158. :label="dict.label"
  159. :value="dict.value"
  160. />
  161. </el-select>
  162. </el-form-item>
  163. <el-form-item label="关键词" prop="requestKeyword" v-if="msgType === MsgType.Keyword">
  164. <el-input v-model="replyForm.requestKeyword" placeholder="请输入内容" clearable />
  165. </el-form-item>
  166. <el-form-item label="回复消息">
  167. <WxReplySelect :objData="objData" />
  168. </el-form-item>
  169. </el-form>
  170. <template #footer>
  171. <el-button @click="cancel">取 消</el-button>
  172. <el-button type="primary" @click="handleSubmit">确 定</el-button>
  173. </template>
  174. </el-dialog>
  175. </ContentWrap>
  176. </template>
  177. <script setup lang="ts" name="MpAutoReply">
  178. import WxVideoPlayer from '@/views/mp/components/wx-video-play/main.vue'
  179. import WxVoicePlayer from '@/views/mp/components/wx-voice-play/main.vue'
  180. import WxMusic from '@/views/mp/components/wx-music/main.vue'
  181. import WxNews from '@/views/mp/components/wx-news/main.vue'
  182. import WxReplySelect from '@/views/mp/components/wx-reply/main.vue'
  183. import WxMpSelect from '@/views/mp/components/WxMpSelect.vue'
  184. import * as MpAutoReplyApi from '@/api/mp/autoReply'
  185. import { DICT_TYPE, getDictOptions } from '@/utils/dict'
  186. import { dateFormatter } from '@/utils/formatTime'
  187. import { ContentWrap } from '@/components/ContentWrap'
  188. import type { TabPaneName } from 'element-plus'
  189. const message = useMessage()
  190. const formRef = ref()
  191. // 消息类型(Follow: 关注时回复;Message: 消息回复;Keyword: 关键词回复)
  192. // 作为tab.name,enum的数字不能随意修改,与api参数相关
  193. enum MsgType {
  194. Follow = 1,
  195. Message = 2,
  196. Keyword = 3
  197. }
  198. const msgType = ref<MsgType>(MsgType.Keyword)
  199. // 允许选择的请求消息类型
  200. const RequestMessageTypes = ['text', 'image', 'voice', 'video', 'shortvideo', 'location', 'link']
  201. // 遮罩层
  202. const loading = ref(true)
  203. // 总条数
  204. const total = ref(0)
  205. // 自动回复列表
  206. const list = ref<any[]>([])
  207. // 查询参数
  208. interface QueryParams {
  209. pageNo: number
  210. pageSize: number
  211. accountId?: number
  212. }
  213. const queryParams: QueryParams = reactive({
  214. pageNo: 1,
  215. pageSize: 10,
  216. accountId: undefined
  217. })
  218. // 弹出层标题
  219. const title = ref('')
  220. // 是否显示弹出层
  221. const showReplyFormDialog = ref(false)
  222. // 表单参数
  223. type ReplyType = 'text' | 'image' | 'voice' | 'video' | 'shortvideo' | 'location' | 'link'
  224. interface ReplyForm {
  225. // relation:
  226. id?: number
  227. accountId?: number
  228. type?: MsgType
  229. // request:
  230. requestMessageType?: ReplyType
  231. requestMatch?: number
  232. requestKeyword?: string
  233. // response:
  234. responseMessageType?: ReplyType
  235. responseContent?: string
  236. responseMediaId?: number
  237. responseMediaUrl?: string
  238. responseTitle?: string
  239. responseDescription?: number
  240. responseThumbMediaId?: string
  241. responseThumbMediaUrl?: string
  242. responseArticles?: any[]
  243. responseMusicUrl?: string
  244. responseHqMusicUrl?: string
  245. }
  246. interface ObjData {
  247. type: ReplyType
  248. accountId?: number
  249. content?: string
  250. mediaId?: number
  251. url?: string
  252. title?: string
  253. description?: string
  254. thumbMediaId?: number
  255. thumbMediaUrl?: string
  256. articles?: any[]
  257. musicUrl?: string
  258. hqMusicUrl?: string
  259. }
  260. const replyForm = ref<ReplyForm>({})
  261. // 回复消息
  262. const objData = ref<ObjData>({
  263. type: 'text',
  264. accountId: undefined
  265. })
  266. // 表单校验
  267. const rules = {
  268. requestKeyword: [{ required: true, message: '请求的关键字不能为空', trigger: 'blur' }],
  269. requestMatch: [{ required: true, message: '请求的关键字的匹配不能为空', trigger: 'blur' }]
  270. }
  271. const onAccountChanged = (id?: number) => {
  272. queryParams.accountId = id
  273. getList()
  274. }
  275. /** 查询列表 */
  276. const getList = async () => {
  277. loading.value = false
  278. try {
  279. const data = await MpAutoReplyApi.getAutoReplyPage({
  280. ...queryParams,
  281. type: msgType.value
  282. })
  283. list.value = data.list
  284. total.value = data.total
  285. } finally {
  286. loading.value = false
  287. }
  288. }
  289. /** 搜索按钮操作 */
  290. const handleQuery = () => {
  291. queryParams.pageNo = 1
  292. getList()
  293. }
  294. const handleTabChange = (tabName: TabPaneName) => {
  295. msgType.value = tabName as MsgType
  296. handleQuery()
  297. }
  298. /** 新增按钮操作 */
  299. const handleAdd = () => {
  300. reset()
  301. // 打开表单,并设置初始化
  302. objData.value = {
  303. type: 'text',
  304. accountId: queryParams.accountId
  305. }
  306. title.value = '新增自动回复'
  307. showReplyFormDialog.value = true
  308. }
  309. /** 修改按钮操作 */
  310. const handleUpdate = async (row: any) => {
  311. reset()
  312. const data = await MpAutoReplyApi.getAutoReply(row.id)
  313. // 设置属性
  314. replyForm.value = { ...data }
  315. delete replyForm.value['responseMessageType']
  316. delete replyForm.value['responseContent']
  317. delete replyForm.value['responseMediaId']
  318. delete replyForm.value['responseMediaUrl']
  319. delete replyForm.value['responseDescription']
  320. delete replyForm.value['responseArticles']
  321. objData.value = {
  322. type: data.responseMessageType,
  323. accountId: queryParams.accountId,
  324. content: data.responseContent,
  325. mediaId: data.responseMediaId,
  326. url: data.responseMediaUrl,
  327. title: data.responseTitle,
  328. description: data.responseDescription,
  329. thumbMediaId: data.responseThumbMediaId,
  330. thumbMediaUrl: data.responseThumbMediaUrl,
  331. articles: data.responseArticles,
  332. musicUrl: data.responseMusicUrl,
  333. hqMusicUrl: data.responseHqMusicUrl
  334. }
  335. // 打开表单
  336. title.value = '修改自动回复'
  337. showReplyFormDialog.value = true
  338. }
  339. const handleSubmit = async () => {
  340. const valid = await formRef.value?.validate()
  341. if (!valid) return
  342. // 处理回复消息
  343. const submitForm: any = { ...replyForm.value }
  344. submitForm.responseMessageType = objData.value.type
  345. submitForm.responseContent = objData.value.content
  346. submitForm.responseMediaId = objData.value.mediaId
  347. submitForm.responseMediaUrl = objData.value.url
  348. submitForm.responseTitle = objData.value.title
  349. submitForm.responseDescription = objData.value.description
  350. submitForm.responseThumbMediaId = objData.value.thumbMediaId
  351. submitForm.responseThumbMediaUrl = objData.value.thumbMediaUrl
  352. submitForm.responseArticles = objData.value.articles
  353. submitForm.responseMusicUrl = objData.value.musicUrl
  354. submitForm.responseHqMusicUrl = objData.value.hqMusicUrl
  355. if (replyForm.value.id !== undefined) {
  356. await MpAutoReplyApi.updateAutoReply(submitForm)
  357. message.success('修改成功')
  358. } else {
  359. await MpAutoReplyApi.createAutoReply(submitForm)
  360. message.success('新增成功')
  361. }
  362. showReplyFormDialog.value = false
  363. await getList()
  364. }
  365. // 表单重置
  366. const reset = () => {
  367. replyForm.value = {
  368. id: undefined,
  369. accountId: queryParams.accountId,
  370. type: msgType.value,
  371. requestKeyword: undefined,
  372. requestMatch: msgType.value === MsgType.Keyword ? 1 : undefined,
  373. requestMessageType: undefined
  374. }
  375. formRef.value?.resetFields()
  376. }
  377. // 取消按钮
  378. const cancel = () => {
  379. showReplyFormDialog.value = false
  380. reset()
  381. }
  382. const handleDelete = async (row) => {
  383. await message.confirm('是否确认删除此数据?')
  384. await MpAutoReplyApi.deleteAutoReply(row.id)
  385. await getList()
  386. message.success('删除成功')
  387. }
  388. </script>