FlowBL/src/views/mp/menu/index.vue

402 lines
10 KiB
Vue
Raw Normal View History

<template>
2023-04-08 21:37:54 +08:00
<doc-alert title="公众号菜单" url="https://doc.iocoder.cn/mp/menu/" />
<!-- 搜索工作栏 -->
<ContentWrap>
2023-04-12 13:29:24 +08:00
<el-form class="-mb-15px" ref="queryFormRef" :inline="true" label-width="68px">
<el-form-item label="公众号" prop="accountId">
2023-04-14 15:22:23 +08:00
<WxAccountSelect @change="onAccountChanged" />
2023-04-12 13:29:24 +08:00
</el-form-item>
</el-form>
2023-04-08 21:37:54 +08:00
</ContentWrap>
2023-04-08 10:15:11 +08:00
2023-04-08 21:37:54 +08:00
<ContentWrap>
2023-04-08 10:15:11 +08:00
<div class="public-account-management clearfix" v-loading="loading">
<!--左边配置菜单-->
<div class="left">
<div class="weixin-hd">
2023-04-12 13:29:24 +08:00
<div class="weixin-title">{{ accountName }}</div>
2023-04-08 10:15:11 +08:00
</div>
2023-04-17 23:58:58 +08:00
<div class="weixin-menu clearfix">
<MenuPreviewer
v-model="menuList"
:account-id="accountId"
:active-index="activeIndex"
:parent-index="parentIndex"
@menu-clicked="(parent, x) => menuClicked(parent, x)"
@submenu-clicked="(child, x, y) => subMenuClicked(child, x, y)"
/>
2023-04-08 10:15:11 +08:00
</div>
<div class="save_div">
2023-04-17 23:58:58 +08:00
<el-button class="save_btn" type="success" @click="onSave" v-hasPermi="['mp:menu:save']"
2023-04-08 10:15:11 +08:00
>保存并发布菜单</el-button
>
2023-04-17 23:58:58 +08:00
<el-button class="save_btn" type="danger" @click="onClear" v-hasPermi="['mp:menu:delete']"
2023-04-08 10:15:11 +08:00
>清空菜单</el-button
>
</div>
</div>
<!--右边配置-->
2023-04-17 23:58:58 +08:00
<div class="right" v-if="showRightPanel">
<MenuEditor
:account-id="accountId"
:is-parent="isParent"
v-model="activeMenu"
@delete="onDeleteMenu"
/>
2023-04-08 10:15:11 +08:00
</div>
<!-- 一进页面就显示的默认页面当点击左边按钮的时候就不显示了-->
<div v-else class="right">
<p>请选择菜单配置</p>
</div>
</div>
2023-04-08 21:37:54 +08:00
</ContentWrap>
</template>
2023-04-17 23:58:58 +08:00
2023-06-21 19:14:34 +08:00
<script lang="ts" setup>
import WxAccountSelect from '@/views/mp/components/wx-account-select'
2023-04-17 23:58:58 +08:00
import MenuEditor from './components/MenuEditor.vue'
import MenuPreviewer from './components/MenuPreviewer.vue'
import * as MpMenuApi from '@/api/mp/menu'
2023-04-17 23:58:58 +08:00
import * as UtilsTree from '@/utils/tree'
import { RawMenu, Menu } from './components/types'
2023-04-12 13:29:24 +08:00
2023-06-21 19:14:34 +08:00
defineOptions({ name: 'MpMenu' })
2023-04-08 21:37:54 +08:00
const message = useMessage() // 消息
2023-04-17 23:58:58 +08:00
const MENU_NOT_SELECTED = '__MENU_NOT_SELECTED__'
2023-04-08 10:15:11 +08:00
// ======================== 列表查询 ========================
2023-04-12 13:29:24 +08:00
const loading = ref(false) // 遮罩层
const accountId = ref(-1)
const accountName = ref<string>('')
2023-04-17 23:58:58 +08:00
const menuList = ref<Menu[]>([])
2023-04-08 10:15:11 +08:00
// ======================== 菜单操作 ========================
2023-04-17 23:58:58 +08:00
// 当前选中菜单编码:
// * 一级('x'
// * 二级('x-y'
// * 未选中MENU_NOT_SELECTED
const activeIndex = ref<string>(MENU_NOT_SELECTED)
// 二级菜单显示标志: 归属的一级菜单index
// * 未初始化:-1
// * 初始化x
const parentIndex = ref(-1)
2023-04-08 10:15:11 +08:00
// ======================== 菜单编辑 ========================
2023-04-17 23:58:58 +08:00
const showRightPanel = ref(false) // 右边配置显示默认详情还是配置详情
const isParent = ref<boolean>(true) // 是否一级菜单控制MenuEditor中name字段长度
const activeMenu = ref<Menu>({}) // 选中菜单MenuEditor的modelValue
// 一些临时值放在这里进行判断,如果放在 activeMenu由于引用关系menu 也会多了多余的参数
enum Level {
Undefined = '0',
Parent = '1',
Child = '2'
}
const tempSelfObj = ref<{
grand: Level
x: number
y: number
}>({
grand: Level.Undefined,
x: 0,
y: 0
})
2023-04-08 10:15:11 +08:00
const dialogNewsVisible = ref(false) // 跳转图文时的素材选择弹窗
/** 侦听公众号变化 **/
const onAccountChanged = (id: number, name: string) => {
2023-04-08 10:15:11 +08:00
accountId.value = id
2023-04-12 13:29:24 +08:00
accountName.value = name
getList()
2023-04-08 10:15:11 +08:00
}
/** 查询并转换菜单 **/
2023-04-08 10:15:11 +08:00
const getList = async () => {
loading.value = false
try {
const data = await MpMenuApi.getMenuList(accountId.value)
2023-04-17 23:58:58 +08:00
const menuData = menuListToFrontend(data)
menuList.value = UtilsTree.handleTree(menuData, 'id')
} finally {
loading.value = false
}
2023-04-08 10:15:11 +08:00
}
/** 搜索按钮操作 */
const handleQuery = () => {
resetForm()
getList()
}
// 将后端返回的 menuList转换成前端的 menuList
2023-04-17 23:58:58 +08:00
const menuListToFrontend = (list: any[]) => {
2023-04-08 10:15:11 +08:00
if (!list) return []
2023-04-17 23:58:58 +08:00
const result: RawMenu[] = []
list.forEach((item: RawMenu) => {
const menu: any = {
2023-04-08 10:15:11 +08:00
...item
}
2023-04-17 23:58:58 +08:00
menu.reply = {
type: item.replyMessageType,
accountId: item.accountId,
content: item.replyContent,
mediaId: item.replyMediaId,
url: item.replyMediaUrl,
title: item.replyTitle,
description: item.replyDescription,
thumbMediaId: item.replyThumbMediaId,
thumbMediaUrl: item.replyThumbMediaUrl,
articles: item.replyArticles,
musicUrl: item.replyMusicUrl,
hqMusicUrl: item.replyHqMusicUrl
2023-04-08 10:15:11 +08:00
}
2023-04-17 23:58:58 +08:00
result.push(menu as RawMenu)
2023-04-08 10:15:11 +08:00
})
2023-04-12 13:29:24 +08:00
return result
2023-04-08 10:15:11 +08:00
}
// 重置表单,清空表单数据
const resetForm = () => {
// 菜单操作
2023-04-17 23:58:58 +08:00
activeIndex.value = MENU_NOT_SELECTED
parentIndex.value = -1
2023-04-08 10:15:11 +08:00
// 菜单编辑
2023-04-17 23:58:58 +08:00
showRightPanel.value = false
activeMenu.value = {}
tempSelfObj.value = { grand: Level.Undefined, x: 0, y: 0 }
2023-04-08 10:15:11 +08:00
dialogNewsVisible.value = false
}
// ======================== 菜单操作 ========================
// 一级菜单点击事件
2023-04-17 23:58:58 +08:00
const menuClicked = (parent: Menu, x: number) => {
2023-04-08 10:15:11 +08:00
// 右侧的表单相关
2023-04-17 23:58:58 +08:00
showRightPanel.value = true // 右边菜单
activeMenu.value = parent // 这个如果放在顶部flag 会没有。因为重新赋值了。
tempSelfObj.value.grand = Level.Parent // 表示一级菜单
tempSelfObj.value.x = x // 表示一级菜单索引
isParent.value = true
2023-04-08 10:15:11 +08:00
// 左侧的选中
2023-04-17 23:58:58 +08:00
activeIndex.value = `${x}` // 菜单选中样式
parentIndex.value = x // 二级菜单显示标志
2023-04-08 10:15:11 +08:00
}
// 二级菜单点击事件
2023-04-17 23:58:58 +08:00
const subMenuClicked = (child: Menu, x: number, y: number) => {
2023-04-08 10:15:11 +08:00
// 右侧的表单相关
2023-04-17 23:58:58 +08:00
showRightPanel.value = true // 右边菜单
activeMenu.value = child // 将点击的数据放到临时变量,对象有引用作用
tempSelfObj.value.grand = Level.Child // 表示二级菜单
tempSelfObj.value.x = x // 表示一级菜单索引
tempSelfObj.value.y = y // 表示二级菜单索引
isParent.value = false
2023-04-08 10:15:11 +08:00
// 左侧的选中
2023-04-17 23:58:58 +08:00
activeIndex.value = `${x}-${y}`
2023-04-08 10:15:11 +08:00
}
// 删除当前菜单
2023-04-17 23:58:58 +08:00
const onDeleteMenu = async () => {
2023-04-08 10:15:11 +08:00
try {
await message.confirm('确定要删除吗?')
2023-04-17 23:58:58 +08:00
if (tempSelfObj.value.grand === Level.Parent) {
2023-04-08 10:15:11 +08:00
// 一级菜单的删除方法
2023-04-17 23:58:58 +08:00
menuList.value.splice(tempSelfObj.value.x, 1)
} else if (tempSelfObj.value.grand === Level.Child) {
2023-04-08 10:15:11 +08:00
// 二级菜单的删除方法
2023-04-17 23:58:58 +08:00
menuList.value[tempSelfObj.value.x].children?.splice(tempSelfObj.value.y, 1)
2023-04-08 10:15:11 +08:00
}
// 提示
message.notifySuccess('删除成功')
// 处理菜单的选中
2023-04-17 23:58:58 +08:00
activeMenu.value = {}
showRightPanel.value = false
activeIndex.value = MENU_NOT_SELECTED
2023-04-08 10:15:11 +08:00
} catch {}
}
// ======================== 菜单编辑 ========================
2023-04-17 23:58:58 +08:00
const onSave = async () => {
2023-04-08 10:15:11 +08:00
try {
2023-04-12 13:29:24 +08:00
await message.confirm('确定要保存吗?')
2023-04-08 10:15:11 +08:00
loading.value = true
2023-04-17 23:58:58 +08:00
await MpMenuApi.saveMenu(accountId.value, menuListToBackend())
2023-04-08 10:15:11 +08:00
getList()
message.notifySuccess('发布成功')
} finally {
loading.value = false
}
}
2023-04-17 23:58:58 +08:00
const onClear = async () => {
2023-04-08 10:15:11 +08:00
try {
await message.confirm('确定要删除吗?')
loading.value = true
await MpMenuApi.deleteMenu(accountId.value)
2023-04-08 10:15:11 +08:00
handleQuery()
message.notifySuccess('清空成功')
} finally {
loading.value = false
}
}
// 将前端的 menuList转换成后端接收的 menuList
2023-04-17 23:58:58 +08:00
const menuListToBackend = () => {
2023-04-12 13:29:24 +08:00
const result: any[] = []
2023-04-08 10:15:11 +08:00
menuList.value.forEach((item) => {
2023-04-17 23:58:58 +08:00
const menu = menuToBackend(item)
2023-04-08 10:15:11 +08:00
result.push(menu)
// 处理子菜单
if (!item.children || item.children.length <= 0) {
return
}
menu.children = []
item.children.forEach((subItem) => {
2023-04-17 23:58:58 +08:00
menu.children.push(menuToBackend(subItem))
2023-04-08 10:15:11 +08:00
})
})
return result
}
// 将前端的 menu转换成后端接收的 menu
2023-04-17 23:58:58 +08:00
// TODO: @芋艿需要根据后台API删除不需要的字段
const menuToBackend = (menu: any) => {
2023-04-08 10:15:11 +08:00
let result = {
...menu,
children: undefined, // 不处理子节点
reply: undefined // 稍后复制
}
2023-04-17 23:58:58 +08:00
result.replyMessageType = menu.reply.type
result.replyContent = menu.reply.content
result.replyMediaId = menu.reply.mediaId
result.replyMediaUrl = menu.reply.url
result.replyTitle = menu.reply.title
result.replyDescription = menu.reply.description
result.replyThumbMediaId = menu.reply.thumbMediaId
result.replyThumbMediaUrl = menu.reply.thumbMediaUrl
result.replyArticles = menu.reply.articles
result.replyMusicUrl = menu.reply.musicUrl
result.replyHqMusicUrl = menu.reply.hqMusicUrl
2023-04-08 10:15:11 +08:00
2023-04-17 23:58:58 +08:00
return result
2023-04-08 10:15:11 +08:00
}
</script>
2023-04-08 10:15:11 +08:00
<!--本组件样式-->
<style lang="scss" scoped="scoped">
/* 公共颜色变量 */
.clearfix {
*zoom: 1;
}
.clearfix::after {
display: table;
clear: both;
2023-04-17 23:58:58 +08:00
content: '';
2023-04-08 10:15:11 +08:00
}
div {
text-align: left;
}
.weixin-hd {
position: relative;
bottom: 426px;
2023-04-17 23:58:58 +08:00
left: 0;
2023-04-08 10:15:11 +08:00
width: 300px;
height: 64px;
2023-04-17 23:58:58 +08:00
color: #fff;
text-align: center;
2023-04-08 10:15:11 +08:00
background: transparent url('./assets/menu_head.png') no-repeat 0 0;
background-position: 0 0;
background-size: 100%;
}
.weixin-title {
position: absolute;
top: 33px;
2023-04-17 23:58:58 +08:00
left: 0;
width: 100%;
font-size: 14px;
color: #fff;
text-align: center;
2023-04-08 10:15:11 +08:00
}
.weixin-menu {
padding-left: 43px;
font-size: 12px;
2023-04-17 23:58:58 +08:00
background: transparent url('./assets/menu_foot.png') no-repeat 0 0;
2023-04-08 10:15:11 +08:00
}
.public-account-management {
width: 1200px;
2023-04-17 23:58:58 +08:00
// min-width: 1200px;
2023-04-08 10:15:11 +08:00
margin: 0 auto;
.left {
2023-04-17 23:58:58 +08:00
position: relative;
2023-04-22 20:52:11 +08:00
display: block;
2023-04-17 23:58:58 +08:00
float: left;
2023-04-08 10:15:11 +08:00
width: 350px;
height: 715px;
2023-04-17 23:58:58 +08:00
padding: 518px 25px 88px;
2023-04-08 10:15:11 +08:00
background: url('./assets/iphone_backImg.png') no-repeat;
background-size: 100% auto;
box-sizing: border-box;
.save_div {
margin-top: 15px;
text-align: center;
.save_btn {
bottom: 20px;
left: 100px;
}
}
}
2023-04-17 23:58:58 +08:00
/* 右边菜单内容 */
2023-04-08 10:15:11 +08:00
.right {
float: left;
width: 63%;
padding: 20px;
margin-left: 20px;
2023-04-17 23:58:58 +08:00
background-color: #e8e7e7;
box-sizing: border-box;
2023-04-08 10:15:11 +08:00
}
}
</style>
<!--素材样式-->
<style lang="scss" scoped>
.pagination {
margin-right: 25px;
2023-04-17 23:58:58 +08:00
text-align: right;
2023-04-08 10:15:11 +08:00
}
.select-item {
width: 280px;
padding: 10px;
2023-04-17 23:58:58 +08:00
margin: 0 auto 10px;
2023-04-08 10:15:11 +08:00
border: 1px solid #eaeaea;
}
.ope-row {
padding-top: 10px;
text-align: center;
}
.item-name {
overflow: hidden;
2023-04-17 23:58:58 +08:00
font-size: 12px;
text-align: center;
2023-04-08 10:15:11 +08:00
text-overflow: ellipsis;
white-space: nowrap;
}
</style>