'integer', 'pid' => 'integer', 'account_type' => 'integer', 'type' => 'integer', 'rank' => 'integer', 'hidden' => 'integer']; /** * 根据账号类型获取权限(主账号) * Author: cfn * @param int $account_type * @return array */ public static function getAuth1(int $account_type) { return self::where("account_type", $account_type) ->where("type", 2) ->select([Db::raw("concat(method,':',flag) as auth")]) ->groupBy(["auth"]) ->pluck("auth") ->toArray(); } /** * 根据账号获取权限(子账号+角色) * Author: cfn * @param int $account_id * @param int $account_type * @return array */ public static function getAuth2(int $account_id, int $account_type) { return Db::table("account_role") ->leftJoin("role_menu", "account_role.role_id", "=", "role_menu.role_id") ->leftJoin("menu", "menu.menu_id", "=", "role_menu.menu_id") ->where("account_role.account_id", $account_id) ->where("menu.account_type", $account_type) ->where("menu.type", 2) ->select([Db::raw("concat(method,':',flag) as auth")]) ->pluck("auth") ->toArray(); } /** * Author: cfn * @param $method * @param $flag * @param $account_type * @return string */ public static function getTitleByCache($method, $flag, $account_type) { // 先从缓存取数据,缓存中没则从数据库中取数据 $container = ApplicationContext::getContainer(); $redis = $container->get(\Hyperf\Redis\Redis::class); $menus = $redis->get("AUTH:" . $account_type); if (!empty($menus) && isset($menus[$method . ":" . $flag])) { // 存在则从缓存中读取 return $menus[$method . ":" . $flag]; } else { $title = self::where('type', 2) ->where('method', $method) ->where('flag', $flag) ->where('account_type', $account_type) ->value("title"); if ($title) { // 代表缓存数据已过期需更新 self::setCache($account_type); return $title; } return "未知操作"; } } /** * Author: cfn * @param $account_type * @return bool|\Redis */ private static function setCache($account_type) { $arr = self::where('type', 2) ->where('account_type', $account_type) ->select(["title", "concat(method,':',flag) as flag"]); // 数组形式转换 $newArr = []; foreach ($arr as $v) { $newArr[$v['title']] = $v['flag']; } $container = ApplicationContext::getContainer(); $redis = $container->get(\Hyperf\Redis\Redis::class); return $redis->set("AUTH:" . $account_type, json_encode($newArr), 72 * 60 * 60); } /** * Author: cfn * @param int $account_type * @param array $field * @return array */ public static function getMenu(int $account_type, array $field = ['*']) { return self::where("account_type", $account_type) ->where("type", 0) ->orderByDesc("rank") ->select($field) ->get() ->toArray(); } /** * Author: cfn * @param int $account_type * @return array */ public static function getButton(int $account_type) { return self::where("account_type", $account_type) ->where("type", 1) ->orderByDesc("rank") ->pluck('flag') ->toArray(); } /** * Author: cfn * @param int $account_id * @param int $account_type * @param array $field * @return array */ public static function getMenu2(int $account_id, int $account_type, array $field = ['*']) { return Db::table("account_role as ar") ->leftJoin("role_menu as rm", "ar.role_id", "=", "rm.role_id") ->leftJoin("menu as m", "m.menu_id", "=", "rm.menu_id") ->where("ar.account_id", $account_id) ->where("m.account_type", $account_type) ->where("m.type", 0) ->orderByDesc("m.rank") ->select($field) ->get() ->toArray(); } /** * Author: cfn * @param int $account_id * @param int $account_type * @return array */ public static function getButton2(int $account_id, int $account_type) { return Db::table("account_role as ar") ->leftJoin("role_menu as rm", "ar.role_id", "=", "rm.role_id") ->leftJoin("menu as m", "m.menu_id", "=", "rm.menu_id") ->where("ar.account_id", $account_id) ->where("m.account_type", $account_type) ->where("m.type", 1) ->pluck('m.flag') ->toArray(); } public static function getMenus(int $account_type) { return self::where('account_type', $account_type) ->orderByDesc("rank") ->select(["menu_id", "title", "flag", "pid", "type", "method", "name", "path", "icon", "rank", "hidden", "account_type"]) ->groupBy(["menu_id"]) ->get() ->toArray(); } public static function quickAdd(array $data): bool { Db::beginTransaction(); // 1.菜单 $data1 = ['pid' => $data['pid'], 'title' => $data['title'] . "管理", 'account_type' => $data['account_type'], 'type' => 0, 'name' => $data['name'], 'path' => $data['path'], 'icon' => $data['icon']]; $pid = self::add($data1); // 2.添加按钮 $data2 = ['pid' => $pid, 'title' => $data['title'] . '添加', 'account_type' => $data1['account_type'], 'type' => 1, 'flag' => $data['flag'] . ":add"]; $pid2 = self::add($data2); // 3.添加接口 $data3 = ['pid' => $pid2, 'title' => $data['title'] . '添加', 'account_type' => $data1['account_type'], 'type' => 2, 'flag' => $data['flag'] . ":add", 'method' => 'post']; $pid3 = self::add($data3); // 4.修改按钮 $data2 = ['pid' => $pid, 'title' => $data['title'] . '修改', 'account_type' => $data1['account_type'], 'type' => 1, 'flag' => $data['flag'] . ":edit"]; $pid2 = self::add($data2); // 5.修改接口 $data3 = ['pid' => $pid2, 'title' => $data['title'] . '修改', 'account_type' => $data1['account_type'], 'type' => 2, 'flag' => $data['flag'] . ":edit", 'method' => 'put']; $pid3 = self::add($data3); // 6.删除按钮 $data2 = ['pid' => $pid, 'title' => $data['title'] . '删除', 'account_type' => $data1['account_type'], 'type' => 1, 'flag' => $data['flag'] . ":del"]; $pid2 = self::add($data2); // 7.删除接口 $data3 = ['pid' => $pid2, 'title' => $data['title'] . '删除', 'account_type' => $data1['account_type'], 'type' => 2, 'flag' => $data['flag'] . ":del", 'method' => 'delete']; $pid3 = self::add($data3); // 8.列表接口 $data3 = ['pid' => $pid, 'title' => $data['title'] . '列表', 'account_type' => $data1['account_type'], 'type' => 2, 'flag' => $data['flag'] . ":list", 'method' => 'get']; $pid3 = self::add($data3); // 9.选择接口 $data3 = ['pid' => $pid, 'title' => $data['title'] . '选项', 'account_type' => $data1['account_type'], 'type' => 2, 'flag' => $data['flag'] . ":option", 'method' => 'get']; $pid3 = self::add($data3); Db::commit(); return true; } }