index.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. <WxAccountSelect @change="onAccountChanged" />
  8. </el-form-item>
  9. </el-form>
  10. </ContentWrap>
  11. <!-- tab 切换 -->
  12. <ContentWrap>
  13. <el-tabs v-model="msgType" @tab-change="onTabChange">
  14. <!-- 操作工具栏 -->
  15. <el-row :gutter="10" class="mb8">
  16. <el-col :span="1.5">
  17. <el-button
  18. type="primary"
  19. plain
  20. @click="onCreate"
  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. <el-row align="middle"><Icon icon="ep:star" class="mr-2px" /> 关注时回复</el-row>
  32. </template>
  33. </el-tab-pane>
  34. <el-tab-pane :name="MsgType.Message">
  35. <template #label>
  36. <el-row align="middle"><Icon icon="ep:chat-line-round" class="mr-2px" /> 消息回复</el-row>
  37. </template>
  38. </el-tab-pane>
  39. <el-tab-pane :name="MsgType.Keyword">
  40. <template #label>
  41. <el-row align="middle"><Icon icon="fa:newspaper-o" class="mr-2px" /> 关键词回复</el-row>
  42. </template>
  43. </el-tab-pane>
  44. </el-tabs>
  45. <!-- 列表 -->
  46. <ReplyTable
  47. :loading="loading"
  48. :list="list"
  49. :msg-type="msgType"
  50. @on-update="onUpdate"
  51. @on-delete="onDelete"
  52. />
  53. <!-- 添加或修改自动回复的对话框 -->
  54. <!-- TODO @Dhb52 -->
  55. <el-dialog :title="dialogTitle" v-model="showFormDialog" width="800px" destroy-on-close>
  56. <el-form ref="formRef" :model="replyForm" :rules="rules" label-width="80px">
  57. <el-form-item label="消息类型" prop="requestMessageType" v-if="msgType === MsgType.Message">
  58. <el-select v-model="replyForm.requestMessageType" placeholder="请选择">
  59. <template v-for="dict in getDictOptions(DICT_TYPE.MP_MESSAGE_TYPE)" :key="dict.value">
  60. <el-option
  61. v-if="RequestMessageTypes.includes(dict.value)"
  62. :label="dict.label"
  63. :value="dict.value"
  64. />
  65. </template>
  66. </el-select>
  67. </el-form-item>
  68. <el-form-item label="匹配类型" prop="requestMatch" v-if="msgType === MsgType.Keyword">
  69. <el-select v-model="replyForm.requestMatch" placeholder="请选择匹配类型" clearable>
  70. <el-option
  71. v-for="dict in getIntDictOptions(DICT_TYPE.MP_AUTO_REPLY_REQUEST_MATCH)"
  72. :key="dict.value"
  73. :label="dict.label"
  74. :value="dict.value"
  75. />
  76. </el-select>
  77. </el-form-item>
  78. <el-form-item label="关键词" prop="requestKeyword" v-if="msgType === MsgType.Keyword">
  79. <el-input v-model="replyForm.requestKeyword" placeholder="请输入内容" clearable />
  80. </el-form-item>
  81. <el-form-item label="回复消息">
  82. <WxReplySelect v-model="reply" />
  83. </el-form-item>
  84. </el-form>
  85. <template #footer>
  86. <el-button @click="cancel">取 消</el-button>
  87. <el-button type="primary" @click="onSubmit">确 定</el-button>
  88. </template>
  89. </el-dialog>
  90. </ContentWrap>
  91. </template>
  92. <script setup lang="ts" name="MpAutoReply">
  93. import WxReplySelect, { type Reply, ReplyType } from '@/views/mp/components/wx-reply'
  94. import WxAccountSelect from '@/views/mp/components/wx-account-select'
  95. import * as MpAutoReplyApi from '@/api/mp/autoReply'
  96. import { DICT_TYPE, getDictOptions, getIntDictOptions } from '@/utils/dict'
  97. import { ContentWrap } from '@/components/ContentWrap'
  98. import type { FormInstance, TabPaneName } from 'element-plus'
  99. import ReplyTable from './components/ReplyTable.vue'
  100. import { MsgType } from './components/types'
  101. const message = useMessage() // 消息
  102. const accountId = ref(-1) // 公众号ID
  103. const msgType = ref<MsgType>(MsgType.Keyword) // 消息类型
  104. const RequestMessageTypes = ['text', 'image', 'voice', 'video', 'shortvideo', 'location', 'link'] // 允许选择的请求消息类型
  105. const loading = ref(true) // 遮罩层
  106. const total = ref(0) // 总条数
  107. const list = ref<any[]>([]) // 自动回复列表
  108. const formRef = ref<FormInstance | null>(null) // 表单 ref
  109. // 查询参数
  110. const queryParams = reactive({
  111. pageNo: 1,
  112. pageSize: 10,
  113. accountId: accountId
  114. })
  115. const dialogTitle = ref('') // 弹出层标题
  116. const showFormDialog = ref(false) // 是否显示弹出层
  117. const replyForm = ref<any>({}) // 表单参数
  118. // 回复消息
  119. const reply = ref<Reply>({
  120. type: ReplyType.Text,
  121. accountId: -1
  122. })
  123. // 表单校验
  124. const rules = {
  125. requestKeyword: [{ required: true, message: '请求的关键字不能为空', trigger: 'blur' }],
  126. requestMatch: [{ required: true, message: '请求的关键字的匹配不能为空', trigger: 'blur' }]
  127. }
  128. /** 侦听账号变化 */
  129. const onAccountChanged = (id: number) => {
  130. accountId.value = id
  131. reply.value.accountId = id
  132. queryParams.pageNo = 1
  133. getList()
  134. }
  135. /** 查询列表 */
  136. const getList = async () => {
  137. loading.value = true
  138. try {
  139. const data = await MpAutoReplyApi.getAutoReplyPage({
  140. ...queryParams,
  141. type: msgType.value
  142. })
  143. list.value = data.list
  144. total.value = data.total
  145. } finally {
  146. loading.value = false
  147. }
  148. }
  149. /** 搜索按钮操作 */
  150. const handleQuery = () => {
  151. queryParams.pageNo = 1
  152. getList()
  153. }
  154. const onTabChange = (tabName: TabPaneName) => {
  155. msgType.value = tabName as MsgType
  156. handleQuery()
  157. }
  158. /** 新增按钮操作 */
  159. const onCreate = () => {
  160. reset()
  161. // 打开表单,并设置初始化
  162. reply.value = {
  163. type: ReplyType.Text,
  164. accountId: queryParams.accountId
  165. }
  166. dialogTitle.value = '新增自动回复'
  167. showFormDialog.value = true
  168. }
  169. /** 修改按钮操作 */
  170. const onUpdate = async (id: number) => {
  171. reset()
  172. const data = await MpAutoReplyApi.getAutoReply(id)
  173. // 设置属性
  174. replyForm.value = { ...data }
  175. delete replyForm.value['responseMessageType']
  176. delete replyForm.value['responseContent']
  177. delete replyForm.value['responseMediaId']
  178. delete replyForm.value['responseMediaUrl']
  179. delete replyForm.value['responseDescription']
  180. delete replyForm.value['responseArticles']
  181. reply.value = {
  182. type: data.responseMessageType,
  183. accountId: queryParams.accountId,
  184. content: data.responseContent,
  185. mediaId: data.responseMediaId,
  186. url: data.responseMediaUrl,
  187. title: data.responseTitle,
  188. description: data.responseDescription,
  189. thumbMediaId: data.responseThumbMediaId,
  190. thumbMediaUrl: data.responseThumbMediaUrl,
  191. articles: data.responseArticles,
  192. musicUrl: data.responseMusicUrl,
  193. hqMusicUrl: data.responseHqMusicUrl
  194. }
  195. // 打开表单
  196. dialogTitle.value = '修改自动回复'
  197. showFormDialog.value = true
  198. }
  199. /** 删除按钮操作 */
  200. const onDelete = async (id: number) => {
  201. await message.confirm('是否确认删除此数据?')
  202. await MpAutoReplyApi.deleteAutoReply(id)
  203. await getList()
  204. message.success('删除成功')
  205. }
  206. const onSubmit = async () => {
  207. const valid = await formRef.value?.validate()
  208. if (!valid) return
  209. // 处理回复消息
  210. const submitForm: any = { ...replyForm.value }
  211. submitForm.responseMessageType = reply.value.type
  212. submitForm.responseContent = reply.value.content
  213. submitForm.responseMediaId = reply.value.mediaId
  214. submitForm.responseMediaUrl = reply.value.url
  215. submitForm.responseTitle = reply.value.title
  216. submitForm.responseDescription = reply.value.description
  217. submitForm.responseThumbMediaId = reply.value.thumbMediaId
  218. submitForm.responseThumbMediaUrl = reply.value.thumbMediaUrl
  219. submitForm.responseArticles = reply.value.articles
  220. submitForm.responseMusicUrl = reply.value.musicUrl
  221. submitForm.responseHqMusicUrl = reply.value.hqMusicUrl
  222. if (replyForm.value.id !== undefined) {
  223. await MpAutoReplyApi.updateAutoReply(submitForm)
  224. message.success('修改成功')
  225. } else {
  226. await MpAutoReplyApi.createAutoReply(submitForm)
  227. message.success('新增成功')
  228. }
  229. showFormDialog.value = false
  230. await getList()
  231. }
  232. // 表单重置
  233. const reset = () => {
  234. replyForm.value = {
  235. id: undefined,
  236. accountId: queryParams.accountId,
  237. type: msgType.value,
  238. requestKeyword: undefined,
  239. requestMatch: msgType.value === MsgType.Keyword ? 1 : undefined,
  240. requestMessageType: undefined
  241. }
  242. formRef.value?.resetFields()
  243. }
  244. // 取消按钮
  245. const cancel = () => {
  246. showFormDialog.value = false
  247. reset()
  248. }
  249. </script>