index.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 :objData="objData" />
  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 from '@/views/mp/components/wx-reply/main.vue'
  94. import WxAccountSelect from '@/views/mp/components/wx-account-select/main.vue'
  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 { TabPaneName } from 'element-plus'
  99. import ReplyTable from './components/ReplyTable.vue'
  100. import { MsgType, ReplyForm, ObjData } from './components/types'
  101. const message = useMessage() // 消息
  102. const msgType = ref<MsgType>(MsgType.Keyword) // 消息类型
  103. const RequestMessageTypes = ['text', 'image', 'voice', 'video', 'shortvideo', 'location', 'link'] // 允许选择的请求消息类型
  104. const loading = ref(true) // 遮罩层
  105. const total = ref(0) // 总条数
  106. const list = ref<any[]>([]) // 自动回复列表
  107. const formRef = ref() // 表单 ref
  108. // 查询参数
  109. interface QueryParams {
  110. pageNo: number
  111. pageSize: number
  112. accountId?: number
  113. }
  114. const queryParams: QueryParams = reactive({
  115. pageNo: 1,
  116. pageSize: 10,
  117. accountId: undefined
  118. })
  119. const dialogTitle = ref('') // 弹出层标题
  120. const showFormDialog = ref(false) // 是否显示弹出层
  121. const replyForm = ref<ReplyForm>({}) // 表单参数
  122. // 回复消息
  123. const objData = ref<ObjData>({
  124. type: 'text',
  125. accountId: undefined
  126. })
  127. // 表单校验
  128. const rules = {
  129. requestKeyword: [{ required: true, message: '请求的关键字不能为空', trigger: 'blur' }],
  130. requestMatch: [{ required: true, message: '请求的关键字的匹配不能为空', trigger: 'blur' }]
  131. }
  132. /** 侦听账号变化 */
  133. const onAccountChanged = (id?: number) => {
  134. queryParams.accountId = id
  135. getList()
  136. }
  137. /** 查询列表 */
  138. const getList = async () => {
  139. loading.value = true
  140. try {
  141. const data = await MpAutoReplyApi.getAutoReplyPage({
  142. ...queryParams,
  143. type: msgType.value
  144. })
  145. list.value = data.list
  146. total.value = data.total
  147. } finally {
  148. loading.value = false
  149. }
  150. }
  151. /** 搜索按钮操作 */
  152. const handleQuery = () => {
  153. queryParams.pageNo = 1
  154. getList()
  155. }
  156. const onTabChange = (tabName: TabPaneName) => {
  157. msgType.value = tabName as MsgType
  158. handleQuery()
  159. }
  160. /** 新增按钮操作 */
  161. const onCreate = () => {
  162. reset()
  163. // 打开表单,并设置初始化
  164. objData.value = {
  165. type: 'text',
  166. accountId: queryParams.accountId
  167. }
  168. dialogTitle.value = '新增自动回复'
  169. showFormDialog.value = true
  170. }
  171. /** 修改按钮操作 */
  172. const onUpdate = async (id: number) => {
  173. reset()
  174. const data = await MpAutoReplyApi.getAutoReply(id)
  175. // 设置属性
  176. replyForm.value = { ...data }
  177. delete replyForm.value['responseMessageType']
  178. delete replyForm.value['responseContent']
  179. delete replyForm.value['responseMediaId']
  180. delete replyForm.value['responseMediaUrl']
  181. delete replyForm.value['responseDescription']
  182. delete replyForm.value['responseArticles']
  183. objData.value = {
  184. type: data.responseMessageType,
  185. accountId: queryParams.accountId,
  186. content: data.responseContent,
  187. mediaId: data.responseMediaId,
  188. url: data.responseMediaUrl,
  189. title: data.responseTitle,
  190. description: data.responseDescription,
  191. thumbMediaId: data.responseThumbMediaId,
  192. thumbMediaUrl: data.responseThumbMediaUrl,
  193. articles: data.responseArticles,
  194. musicUrl: data.responseMusicUrl,
  195. hqMusicUrl: data.responseHqMusicUrl
  196. }
  197. // 打开表单
  198. dialogTitle.value = '修改自动回复'
  199. showFormDialog.value = true
  200. }
  201. /** 删除按钮操作 */
  202. const onDelete = async (id: number) => {
  203. await message.confirm('是否确认删除此数据?')
  204. await MpAutoReplyApi.deleteAutoReply(id)
  205. await getList()
  206. message.success('删除成功')
  207. }
  208. const onSubmit = async () => {
  209. const valid = await formRef.value?.validate()
  210. if (!valid) return
  211. // 处理回复消息
  212. const submitForm: any = { ...replyForm.value }
  213. submitForm.responseMessageType = objData.value.type
  214. submitForm.responseContent = objData.value.content
  215. submitForm.responseMediaId = objData.value.mediaId
  216. submitForm.responseMediaUrl = objData.value.url
  217. submitForm.responseTitle = objData.value.title
  218. submitForm.responseDescription = objData.value.description
  219. submitForm.responseThumbMediaId = objData.value.thumbMediaId
  220. submitForm.responseThumbMediaUrl = objData.value.thumbMediaUrl
  221. submitForm.responseArticles = objData.value.articles
  222. submitForm.responseMusicUrl = objData.value.musicUrl
  223. submitForm.responseHqMusicUrl = objData.value.hqMusicUrl
  224. if (replyForm.value.id !== undefined) {
  225. await MpAutoReplyApi.updateAutoReply(submitForm)
  226. message.success('修改成功')
  227. } else {
  228. await MpAutoReplyApi.createAutoReply(submitForm)
  229. message.success('新增成功')
  230. }
  231. showFormDialog.value = false
  232. await getList()
  233. }
  234. // 表单重置
  235. const reset = () => {
  236. replyForm.value = {
  237. id: undefined,
  238. accountId: queryParams.accountId,
  239. type: msgType.value,
  240. requestKeyword: undefined,
  241. requestMatch: msgType.value === MsgType.Keyword ? 1 : undefined,
  242. requestMessageType: undefined
  243. }
  244. formRef.value?.resetFields()
  245. }
  246. // 取消按钮
  247. const cancel = () => {
  248. showFormDialog.value = false
  249. reset()
  250. }
  251. </script>