/**

 * 发布菜单
 * @return bool|mixed|string
 * @throws BaseException
 * @throws DbException
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 */
public function syncData()
{
    $model = new MenuModel;
    $menu = $model->where('status', 1)->select();
    $menuData = [
        "button" => []
    ];

    foreach ($menu as $v) {
        // 如果是一级菜单
        if ($v['parent_id'] == 0) {
            $menu_item = [
                'name' => $v['name']
            ];

            // 处理不同类型的菜单项
            if ($v['type'] == 'miniprogram') {
                $menu_item['type'] = 'miniprogram';
                $menu_item['url'] = $v['url'];
                $menu_item['pagepath'] = $v['pagepath'];
                $menu_item['appid'] = $this->getWxApp()['app_id'];
            } else {
                $menu_item['type'] = $v['type'];
                $menu_item['url'] = $v['url'];
            }

            $menuData["button"][] = $menu_item;
        } else {
            // 如果是二级菜单
            // 查找父级菜单名称
            $parent_menu = $model->where('id', $v['parent_id'])->find();
            $parent_name = $parent_menu ? $parent_menu['name'] : '';

            // 查找对应的一级菜单
            $parent_item_key = null;
            foreach ($menuData["button"] as $key => $item) {
                if ($item['name'] == $parent_name) {
                    $parent_item_key = $key;
                    break;
                }
            }

            // 如果找到了对应的一级菜单
            if ($parent_item_key !== null) {
                $sub_menu_item = [
                    'name' => $v['name']
                ];

                // 处理不同类型的菜单项
                if ($v['type'] == 'miniprogram') {
                    $sub_menu_item['type'] = 'miniprogram';
                    $sub_menu_item['url'] = $v['url'];
                    $sub_menu_item['pagepath'] = $v['pagepath'];
                    $sub_menu_item['appid'] = $this->getWxApp()['app_id'];
                } else {
                    $sub_menu_item['type'] = $v['type'];
                    $sub_menu_item['url'] = $v['url'];
                }

                // 添加到对应的一级菜单下
                $menuData["button"][$parent_item_key]['sub_button'][] = $sub_menu_item;
            }
        }
    }

    return $this->template_obj()->createMenu($menuData);

}`


数据表


点赞(1) 打赏

评论列表 共有 0 条评论

暂无评论
发表
评论
返回
顶部