refactor: 【MP自定义菜单】应用【公众号选择器】组件

This commit is contained in:
dhb52 2023-04-10 00:46:18 +08:00
parent a8b4c16a7e
commit 098b7305c9

View File

@ -2,22 +2,7 @@
<doc-alert title="公众号菜单" url="https://doc.iocoder.cn/mp/menu/" /> <doc-alert title="公众号菜单" url="https://doc.iocoder.cn/mp/menu/" />
<!-- 搜索工作栏 --> <!-- 搜索工作栏 -->
<ContentWrap> <ContentWrap>
<el-form class="-mb-15px" ref="queryFormRef" :inline="true" label-width="68px"> <WxAccountSelect @change="(accountId) => accountChanged(accountId)" />
<el-form-item label="公众号" prop="accountId">
<el-select v-model="accountId" placeholder="请选择公众号" class="!w-240px">
<el-option
v-for="item in accountList"
:key="item.id"
:label="item.name"
:value="item.id"
/>
</el-select>
</el-form-item>
<el-form-item>
<el-button @click="handleQuery"><Icon icon="ep:search" />搜索</el-button>
<el-button @click="resetQuery"><Icon icon="ep:refresh" />重置</el-button>
</el-form-item>
</el-form>
</ContentWrap> </ContentWrap>
<!-- 列表 --> <!-- 列表 -->
@ -204,17 +189,15 @@ import { handleTree } from '@/utils/tree'
import WxReplySelect from '@/views/mp/components/wx-reply/main.vue' import WxReplySelect from '@/views/mp/components/wx-reply/main.vue'
import WxNews from '@/views/mp/components/wx-news/main.vue' import WxNews from '@/views/mp/components/wx-news/main.vue'
import WxMaterialSelect from '@/views/mp/components/wx-material-select/main.vue' import WxMaterialSelect from '@/views/mp/components/wx-material-select/main.vue'
import { deleteMenu, getMenuList, saveMenu } from '@/api/mp/menu' import WxAccountSelect from '@/views/mp/components/wx-account-select/main.vue'
import * as MpAccountApi from '@/api/mp/account' import * as MpMenuApi from '@/api/mp/menu'
import menuOptions from './menuOptions' import menuOptions from './menuOptions'
const message = useMessage() // const message = useMessage() //
// ======================== ======================== // ======================== ========================
const loading = ref(true) // const loading = ref(true) //
const accountId = ref(undefined) // Id const accountId = ref(undefined) // Id
const name = ref('') //
const menuList = ref({ children: [] }) const menuList = ref({ children: [] })
const accountList = ref([]) //
// ======================== ======================== // ======================== ========================
const isActive = ref(-1) // const isActive = ref(-1) //
@ -228,60 +211,34 @@ const showConfigureContent = ref(true) // 是否展示配置内容;如果有
const hackResetWxReplySelect = ref(false) // WxReplySelect const hackResetWxReplySelect = ref(false) // WxReplySelect
const tempObj = ref({}) // const tempObj = ref({}) //
const tempSelfObj = ref({ // tempObjmenu
// tempObjmenu const tempSelfObj = ref({})
})
const dialogNewsVisible = ref(false) // const dialogNewsVisible = ref(false) //
onMounted(async () => { /** 侦听公众号变化 **/
accountList.value = await MpAccountApi.getSimpleAccountList() const accountChanged = (accountId) => {
//
if (accountList.value.length > 0) {
// @ts-ignore
setAccountId(accountList.value[0].id)
}
await getList()
})
// ======================== ========================
/** 设置账号编号 */
const setAccountId = (id) => {
accountId.value = id accountId.value = id
name.value = accountList.value.find((item) => item.id === accountId.value)?.name getList()
} }
/** 查询并转换菜单 **/
const getList = async () => { const getList = async () => {
loading.value = false loading.value = false
getMenuList(accountId.value) try {
.then((response) => { const data = await MpMenuApi.getMenuList(accountId.value)
const menuData = convertMenuList(response) const menuData = convertMenuList(data)
menuList.value = handleTree(menuData, 'id') menuList.value = handleTree(menuData, 'id')
}) } finally {
.finally(() => {
loading.value = false loading.value = false
}) }
} }
/** 搜索按钮操作 */ /** 搜索按钮操作 */
const handleQuery = () => { const handleQuery = () => {
resetForm() resetForm()
//
if (accountId.value) {
setAccountId(accountId.value)
}
getList() getList()
} }
/** 重置按钮操作 */
const resetQuery = () => {
resetForm()
//
if (accountList.value.length > 0) {
setAccountId(accountList.value[0].id)
}
handleQuery()
}
// menuList menuList // menuList menuList
const convertMenuList = (list) => { const convertMenuList = (list) => {
if (!list) return [] if (!list) return []
@ -443,7 +400,7 @@ const handleSave = async () => {
try { try {
await message.confirm('确定要删除吗?') await message.confirm('确定要删除吗?')
loading.value = true loading.value = true
await saveMenu(accountId.value, convertMenuFormList()) await MpMenuApi.saveMenu(accountId.value, convertMenuFormList())
getList() getList()
message.notifySuccess('发布成功') message.notifySuccess('发布成功')
} finally { } finally {
@ -464,7 +421,7 @@ const handleDelete = async () => {
try { try {
await message.confirm('确定要删除吗?') await message.confirm('确定要删除吗?')
loading.value = true loading.value = true
await deleteMenu(accountId.value) await MpMenuApi.deleteMenu(accountId.value)
handleQuery() handleQuery()
message.notifySuccess('清空成功') message.notifySuccess('清空成功')
} finally { } finally {
@ -546,6 +503,7 @@ const deleteMaterial = () => {
delete tempObj.value['replyArticles'] delete tempObj.value['replyArticles']
} }
</script> </script>
<!--本组件样式--> <!--本组件样式-->
<style lang="scss" scoped="scoped"> <style lang="scss" scoped="scoped">
/* 公共颜色变量 */ /* 公共颜色变量 */