refactor: mp模块ts重构

This commit is contained in:
dhb52
2023-04-12 13:29:24 +08:00
parent ebbf47f509
commit df3b381d6f
12 changed files with 943 additions and 603 deletions

View File

@ -3,13 +3,16 @@
<!-- 搜索工作栏 -->
<ContentWrap>
<!-- TODO @芋艿调整成 el-form WxAccountSelect -->
<WxAccountSelect @change="accountChanged" />
<el-form class="-mb-15px" :model="queryParams" :inline="true" label-width="68px">
<el-form-item label="公众号" prop="accountId">
<WxMpSelect @change="onAccountChanged" />
</el-form-item>
</el-form>
</ContentWrap>
<!-- tab 切换 -->
<ContentWrap>
<el-tabs v-model="type" @tab-change="handleTabChange">
<el-tabs v-model="msgType" @tab-change="handleTabChange">
<!-- 操作工具栏 -->
<el-row :gutter="10" class="mb8">
<el-col :span="1.5">
@ -18,26 +21,26 @@
plain
@click="handleAdd"
v-hasPermi="['mp:auto-reply:create']"
v-if="type !== '1' || list.length <= 0"
v-if="msgType !== MsgType.Follow || list.length <= 0"
>
<Icon icon="ep:plus" />新增
</el-button>
</el-col>
</el-row>
<!-- tab -->
<el-tab-pane name="1">
<el-tab-pane :name="MsgType.Follow">
<template #label>
<span><Icon icon="ep:star-off" /> 关注时回复</span>
<span><Icon icon="ep:star" /> 关注时回复</span>
</template>
</el-tab-pane>
<el-tab-pane name="2">
<el-tab-pane :name="MsgType.Message">
<template #label>
<span><Icon icon="ep:chat-line-round" /> 消息回复</span>
</template>
</el-tab-pane>
<el-tab-pane name="3">
<el-tab-pane :name="MsgType.Keyword">
<template #label>
<span><Icon icon="ep:news" /> 关键词回复</span>
<span><Icon icon="fa:newspaper-o" /> 关键词回复</span>
</template>
</el-tab-pane>
</el-tabs>
@ -47,10 +50,20 @@
label="请求消息类型"
align="center"
prop="requestMessageType"
v-if="type === '2'"
v-if="msgType === MsgType.Message"
/>
<el-table-column label="关键词" align="center" prop="requestKeyword" v-if="type === '3'" />
<el-table-column label="匹配类型" align="center" prop="requestMatch" v-if="type === '3'">
<el-table-column
label="关键词"
align="center"
prop="requestKeyword"
v-if="msgType === MsgType.Keyword"
/>
<el-table-column
label="匹配类型"
align="center"
prop="requestMatch"
v-if="msgType === MsgType.Keyword"
>
<template #default="scope">
<dict-tag :type="DICT_TYPE.MP_AUTO_REPLY_REQUEST_MATCH" :value="scope.row.requestMatch" />
</template>
@ -64,7 +77,7 @@
<template #default="scope">
<div v-if="scope.row.responseMessageType === 'text'">{{ scope.row.responseContent }}</div>
<div v-else-if="scope.row.responseMessageType === 'voice'">
<WxVoicePlayer :url="scope.row.responseMediaUrl" />
<WxVoicePlayer v-if="scope.row.responseMediaUrl" :url="scope.row.responseMediaUrl" />
</div>
<div v-else-if="scope.row.responseMessageType === 'image'">
<a target="_blank" :href="scope.row.responseMediaUrl">
@ -77,7 +90,11 @@
scope.row.responseMessageType === 'shortvideo'
"
>
<WxVideoPlayer :url="scope.row.responseMediaUrl" style="margin-top: 10px" />
<WxVideoPlayer
v-if="scope.row.responseMediaUrl"
:url="scope.row.responseMediaUrl"
style="margin-top: 10px"
/>
</div>
<div v-else-if="scope.row.responseMessageType === 'news'">
<WxNews :articles="scope.row.responseArticles" />
@ -123,21 +140,21 @@
</el-table>
<!-- 添加或修改自动回复的对话框 -->
<el-dialog :title="title" v-model="open" width="800px" append-to-body>
<el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
<el-form-item label="消息类型" prop="requestMessageType" v-if="type === '2'">
<el-select v-model="form.requestMessageType" placeholder="请选择">
<el-dialog :title="title" v-model="showReplyFormDialog" width="800px" append-to-body>
<el-form ref="formRef" :model="replyForm" :rules="rules" label-width="80px">
<el-form-item label="消息类型" prop="requestMessageType" v-if="msgType === MsgType.Message">
<el-select v-model="replyForm.requestMessageType" placeholder="请选择">
<template v-for="dict in getDictOptions(DICT_TYPE.MP_MESSAGE_TYPE)" :key="dict.value">
<el-option
v-if="requestMessageTypes.includes(dict.value)"
v-if="RequestMessageTypes.includes(dict.value)"
:label="dict.label"
:value="dict.value"
/>
</template>
</el-select>
</el-form-item>
<el-form-item label="匹配类型" prop="requestMatch" v-if="type === '3'">
<el-select v-model="form.requestMatch" placeholder="请选择匹配类型" clearable>
<el-form-item label="匹配类型" prop="requestMatch" v-if="msgType === MsgType.Keyword">
<el-select v-model="replyForm.requestMatch" placeholder="请选择匹配类型" clearable>
<el-option
v-for="dict in getDictOptions(DICT_TYPE.MP_AUTO_REPLY_REQUEST_MATCH)"
:key="dict.value"
@ -146,8 +163,8 @@
/>
</el-select>
</el-form-item>
<el-form-item label="关键词" prop="requestKeyword" v-if="type === '3'">
<el-input v-model="form.requestKeyword" placeholder="请输入内容" clearable />
<el-form-item label="关键词" prop="requestKeyword" v-if="msgType === MsgType.Keyword">
<el-input v-model="replyForm.requestKeyword" placeholder="请输入内容" clearable />
</el-form-item>
<el-form-item label="回复消息">
<WxReplySelect :objData="objData" v-if="hackResetWxReplySelect" />
@ -160,38 +177,47 @@
</el-dialog>
</ContentWrap>
</template>
<script setup name="MpAutoReply">
<script setup lang="ts" name="MpAutoReply">
import WxVideoPlayer from '@/views/mp/components/wx-video-play/main.vue'
import WxVoicePlayer from '@/views/mp/components/wx-voice-play/main.vue'
import WxMusic from '@/views/mp/components/wx-music/main.vue'
import WxNews from '@/views/mp/components/wx-news/main.vue'
import WxReplySelect from '@/views/mp/components/wx-reply/main.vue'
import WxAccountSelect from '@/views/mp/components/wx-account-select/main.vue'
import WxMpSelect from '@/views/mp/components/WxMpSelect.vue'
import * as MpAutoReplyApi from '@/api/mp/autoReply'
import { DICT_TYPE, getDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { ContentWrap } from '@/components/ContentWrap'
import { TabPaneName } from 'element-plus'
const message = useMessage()
// const queryFormRef = ref()
const formRef = ref()
// tab 类型1关注时回复2消息回复3关键词回复
const type = ref('3')
// 消息类型Follow: 关注时回复Message: 消息回复Keyword: 关键词回复
// 作为tab.name
enum MsgType {
Follow = 1,
Message = 2,
Keyword = 3
}
const msgType = ref<MsgType>(MsgType.Keyword)
// 允许选择的请求消息类型
const requestMessageTypes = ['text', 'image', 'voice', 'video', 'shortvideo', 'location', 'link']
const RequestMessageTypes = ['text', 'image', 'voice', 'video', 'shortvideo', 'location', 'link']
// 遮罩层
const loading = ref(true)
// 显示搜索条件
// const showSearch = ref(true)
// 总条数
const total = ref(0)
// 自动回复列表
const list = ref([])
const list = ref<any[]>([])
// 查询参数
const queryParams = reactive({
interface QueryParams {
pageNo: number
pageSize: number
accountId?: number
}
const queryParams: QueryParams = reactive({
pageNo: 1,
pageSize: 10,
accountId: undefined
@ -200,12 +226,50 @@ const queryParams = reactive({
// 弹出层标题
const title = ref('')
// 是否显示弹出层
const open = ref(false)
const showReplyFormDialog = ref(false)
// 表单参数
const form = ref({})
type ReplyType = 'text' | 'image' | 'voice' | 'video' | 'shortvideo' | 'location' | 'link'
interface ReplyForm {
// relation:
id?: number
accountId?: number
type?: MsgType
// request:
requestMessageType?: ReplyType
requestMatch?: number
requestKeyword?: string
// response:
responseMessageType?: ReplyType
responseContent?: string
responseMediaId?: number
responseMediaUrl?: string
responseTitle?: string
responseDescription?: number
responseThumbMediaId?: string
responseThumbMediaUrl?: string
responseArticles?: any[]
responseMusicUrl?: string
responseHqMusicUrl?: string
}
interface ObjData {
type: ReplyType
accountId?: number
content?: string
mediaId?: number
url?: string
title?: string
description?: string
thumbMediaId?: number
thumbMediaUrl?: string
articles?: any[]
musicUrl?: string
hqMusicUrl?: string
}
const replyForm = ref<ReplyForm>({})
// 回复消息
const objData = ref({
type: 'text'
const objData = ref<ObjData>({
type: 'text',
accountId: undefined
})
// 表单校验
const rules = {
@ -216,8 +280,8 @@ const rules = {
// 重置 WxReplySelect 组件,解决无法清除的问题
const hackResetWxReplySelect = ref(false)
const accountChanged = (accountId) => {
queryParams.accountId = accountId
const onAccountChanged = (id?: number) => {
queryParams.accountId = id
getList()
}
@ -227,7 +291,7 @@ const getList = async () => {
try {
const data = await MpAutoReplyApi.getAutoReplyPage({
...queryParams,
type: type.value
type: msgType.value
})
list.value = data.list
total.value = data.total
@ -242,8 +306,8 @@ const handleQuery = () => {
getList()
}
const handleTabChange = (tabName) => {
type.value = tabName
const handleTabChange = (tabName: TabPaneName) => {
msgType.value = tabName as MsgType
handleQuery()
}
@ -252,94 +316,87 @@ const handleAdd = () => {
reset()
resetEditor()
// 打开表单,并设置初始化
open.value = true
title.value = '新增自动回复'
objData.value = {
type: 'text',
accountId: queryParams.accountId
}
title.value = '新增自动回复'
showReplyFormDialog.value = true
}
/** 修改按钮操作 */
const handleUpdate = (row) => {
const handleUpdate = async (row: any) => {
reset()
resetEditor()
console.log(row)
MpAutoReplyApi.getAutoReply(row.id).then((data) => {
// 设置属性
form.value = { ...data }
delete form.value['responseMessageType']
delete form.value['responseContent']
delete form.value['responseMediaId']
delete form.value['responseMediaUrl']
delete form.value['responseDescription']
delete form.value['responseArticles']
objData.value = {
type: data.responseMessageType,
accountId: queryParams.accountId,
content: data.responseContent,
mediaId: data.responseMediaId,
url: data.responseMediaUrl,
title: data.responseTitle,
description: data.responseDescription,
thumbMediaId: data.responseThumbMediaId,
thumbMediaUrl: data.responseThumbMediaUrl,
articles: data.responseArticles,
musicUrl: data.responseMusicUrl,
hqMusicUrl: data.responseHqMusicUrl
}
const data = await MpAutoReplyApi.getAutoReply(row.id)
// 设置属性
replyForm.value = { ...data }
delete replyForm.value['responseMessageType']
delete replyForm.value['responseContent']
delete replyForm.value['responseMediaId']
delete replyForm.value['responseMediaUrl']
delete replyForm.value['responseDescription']
delete replyForm.value['responseArticles']
objData.value = {
type: data.responseMessageType,
accountId: queryParams.accountId,
content: data.responseContent,
mediaId: data.responseMediaId,
url: data.responseMediaUrl,
title: data.responseTitle,
description: data.responseDescription,
thumbMediaId: data.responseThumbMediaId,
thumbMediaUrl: data.responseThumbMediaUrl,
articles: data.responseArticles,
musicUrl: data.responseMusicUrl,
hqMusicUrl: data.responseHqMusicUrl
}
// 打开表单
open.value = true
title.value = '修改自动回复'
})
// 打开表单
title.value = '修改自动回复'
showReplyFormDialog.value = true
}
const handleSubmit = () => {
formRef.value?.validate((valid) => {
if (!valid) {
return
}
const handleSubmit = async () => {
const valid = await formRef.value?.validate()
if (!valid) return
// 处理回复消息
const form = { ...form.value }
form.responseMessageType = objData.value.type
form.responseContent = objData.value.content
form.responseMediaId = objData.value.mediaId
form.responseMediaUrl = objData.value.url
form.responseTitle = objData.value.title
form.responseDescription = objData.value.description
form.responseThumbMediaId = objData.value.thumbMediaId
form.responseThumbMediaUrl = objData.value.thumbMediaUrl
form.responseArticles = objData.value.articles
form.responseMusicUrl = objData.value.musicUrl
form.responseHqMusicUrl = objData.value.hqMusicUrl
// 处理回复消息
const submitForm: any = { ...replyForm.value }
submitForm.responseMessageType = objData.value.type
submitForm.responseContent = objData.value.content
submitForm.responseMediaId = objData.value.mediaId
submitForm.responseMediaUrl = objData.value.url
submitForm.responseTitle = objData.value.title
submitForm.responseDescription = objData.value.description
submitForm.responseThumbMediaId = objData.value.thumbMediaId
submitForm.responseThumbMediaUrl = objData.value.thumbMediaUrl
submitForm.responseArticles = objData.value.articles
submitForm.responseMusicUrl = objData.value.musicUrl
submitForm.responseHqMusicUrl = objData.value.hqMusicUrl
if (form.value.id !== undefined) {
MpAutoReplyApi.updateAutoReply(form).then(() => {
message.success('修改成功')
open.value = false
getList()
})
} else {
MpAutoReplyApi.createAutoReply(form).then(() => {
message.success('新增成功')
open.value = false
getList()
})
}
})
if (replyForm.value.id !== undefined) {
await MpAutoReplyApi.updateAutoReply(submitForm)
message.success('修改成功')
} else {
await MpAutoReplyApi.createAutoReply(submitForm)
message.success('新增成功')
}
showReplyFormDialog.value = false
getList()
}
// 表单重置
const reset = () => {
form.value = {
replyForm.value = {
id: undefined,
accountId: queryParams.accountId,
type: type.value,
type: msgType.value,
requestKeyword: undefined,
requestMatch: type.value === '3' ? 1 : undefined,
requestMatch: msgType.value === MsgType.Keyword ? 1 : undefined,
requestMessageType: undefined
}
formRef.value?.resetFields()
@ -347,7 +404,7 @@ const reset = () => {
// 取消按钮
const cancel = () => {
open.value = false
showReplyFormDialog.value = false
reset()
}