index.vue 8.5 KB

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