'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(); } }