index.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. <el-dialog
  54. :title="isCreating ? '新增自动回复' : '修改自动回复'"
  55. v-model="showDialog"
  56. width="800px"
  57. destroy-on-close
  58. >
  59. <ReplyForm v-model="replyForm" v-model:reply="reply" :msg-type="msgType" ref="formRef" />
  60. <template #footer>
  61. <el-button @click="cancel">取 消</el-button>
  62. <el-button type="primary" @click="onSubmit">确 定</el-button>
  63. </template>
  64. </el-dialog>
  65. </ContentWrap>
  66. </template>
  67. <script setup lang="ts" name="MpAutoReply">
  68. import ReplyForm from '@/views/mp/autoReply/components/ReplyForm.vue'
  69. import { type Reply, ReplyType } from '@/views/mp/components/wx-reply'
  70. import WxAccountSelect from '@/views/mp/components/wx-account-select'
  71. import * as MpAutoReplyApi from '@/api/mp/autoReply'
  72. import { ContentWrap } from '@/components/ContentWrap'
  73. import type { TabPaneName } from 'element-plus'
  74. import ReplyTable from './components/ReplyTable.vue'
  75. import { MsgType } from './components/types'
  76. const message = useMessage() // 消息
  77. const accountId = ref(-1) // 公众号ID
  78. const msgType = ref<MsgType>(MsgType.Keyword) // 消息类型
  79. const loading = ref(true) // 遮罩层
  80. const total = ref(0) // 总条数
  81. const list = ref<any[]>([]) // 自动回复列表
  82. const formRef = ref<InstanceType<typeof ReplyForm> | null>(null) // 表单 ref
  83. // 查询参数
  84. const queryParams = reactive({
  85. pageNo: 1,
  86. pageSize: 10,
  87. accountId: accountId
  88. })
  89. const isCreating = ref(false) // 是否新建(否则编辑)
  90. const showDialog = ref(false) // 是否显示弹出层
  91. const replyForm = ref<any>({}) // 表单参数
  92. // 回复消息
  93. const reply = ref<Reply>({
  94. type: ReplyType.Text,
  95. accountId: -1
  96. })
  97. /** 侦听账号变化 */
  98. const onAccountChanged = (id: number) => {
  99. accountId.value = id
  100. reply.value.accountId = id
  101. queryParams.pageNo = 1
  102. getList()
  103. }
  104. /** 查询列表 */
  105. const getList = async () => {
  106. loading.value = true
  107. try {
  108. const data = await MpAutoReplyApi.getAutoReplyPage({
  109. ...queryParams,
  110. type: msgType.value
  111. })
  112. list.value = data.list
  113. total.value = data.total
  114. } finally {
  115. loading.value = false
  116. }
  117. }
  118. /** 搜索按钮操作 */
  119. const handleQuery = () => {
  120. queryParams.pageNo = 1
  121. getList()
  122. }
  123. const onTabChange = (tabName: TabPaneName) => {
  124. msgType.value = tabName as MsgType
  125. handleQuery()
  126. }
  127. /** 新增按钮操作 */
  128. const onCreate = () => {
  129. reset()
  130. // 打开表单,并设置初始化
  131. reply.value = {
  132. type: ReplyType.Text,
  133. accountId: queryParams.accountId
  134. }
  135. isCreating.value = true
  136. showDialog.value = true
  137. }
  138. /** 修改按钮操作 */
  139. const onUpdate = async (id: number) => {
  140. reset()
  141. const data = await MpAutoReplyApi.getAutoReply(id)
  142. // 设置属性
  143. replyForm.value = { ...data }
  144. delete replyForm.value['responseMessageType']
  145. delete replyForm.value['responseContent']
  146. delete replyForm.value['responseMediaId']
  147. delete replyForm.value['responseMediaUrl']
  148. delete replyForm.value['responseDescription']
  149. delete replyForm.value['responseArticles']
  150. reply.value = {
  151. type: data.responseMessageType,
  152. accountId: queryParams.accountId,
  153. content: data.responseContent,
  154. mediaId: data.responseMediaId,
  155. url: data.responseMediaUrl,
  156. title: data.responseTitle,
  157. description: data.responseDescription,
  158. thumbMediaId: data.responseThumbMediaId,
  159. thumbMediaUrl: data.responseThumbMediaUrl,
  160. articles: data.responseArticles,
  161. musicUrl: data.responseMusicUrl,
  162. hqMusicUrl: data.responseHqMusicUrl
  163. }
  164. // 打开表单
  165. isCreating.value = false
  166. showDialog.value = true
  167. }
  168. /** 删除按钮操作 */
  169. const onDelete = async (id: number) => {
  170. await message.confirm('是否确认删除此数据?')
  171. await MpAutoReplyApi.deleteAutoReply(id)
  172. await getList()
  173. message.success('删除成功')
  174. }
  175. const onSubmit = async () => {
  176. await formRef.value?.validate()
  177. // 处理回复消息
  178. const submitForm: any = { ...replyForm.value }
  179. submitForm.responseMessageType = reply.value.type
  180. submitForm.responseContent = reply.value.content
  181. submitForm.responseMediaId = reply.value.mediaId
  182. submitForm.responseMediaUrl = reply.value.url
  183. submitForm.responseTitle = reply.value.title
  184. submitForm.responseDescription = reply.value.description
  185. submitForm.responseThumbMediaId = reply.value.thumbMediaId
  186. submitForm.responseThumbMediaUrl = reply.value.thumbMediaUrl
  187. submitForm.responseArticles = reply.value.articles
  188. submitForm.responseMusicUrl = reply.value.musicUrl
  189. submitForm.responseHqMusicUrl = reply.value.hqMusicUrl
  190. if (replyForm.value.id !== undefined) {
  191. await MpAutoReplyApi.updateAutoReply(submitForm)
  192. message.success('修改成功')
  193. } else {
  194. await MpAutoReplyApi.createAutoReply(submitForm)
  195. message.success('新增成功')
  196. }
  197. showDialog.value = false
  198. await getList()
  199. }
  200. // 表单重置
  201. const reset = () => {
  202. replyForm.value = {
  203. id: undefined,
  204. accountId: queryParams.accountId,
  205. type: msgType.value,
  206. requestKeyword: undefined,
  207. requestMatch: msgType.value === MsgType.Keyword ? 1 : undefined,
  208. requestMessageType: undefined
  209. }
  210. formRef.value?.resetFields()
  211. }
  212. // 取消按钮
  213. const cancel = () => {
  214. showDialog.value = false
  215. reset()
  216. }
  217. </script>