123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500 |
- <?php
- namespace App\Services\Sys;
- use App\Models\Sys\SysGroup;
- use App\Models\Sys\SysUsers;
- use App\Models\Sys\SysPermission;
- use App\Models\Sys\SysRolePermission;
- use App\Models\Sys\SysRole;
- use App\Models\Sys\SysUserRole;
- use App\Models\Sys\SysUserAdver;
- use App\Models\Sys\SysUserCusts;
- use App\Models\Sys\SysCustomer;
- use App\Models\JuxingAdAccount;
- use App\Support\RedisModel;
- use Illuminate\Support\Facades\Hash;
- class PermissionService
- {
- public static function createPermission($parent_id,$title,$route,$weight,$icon)
- {
- $per = new SysPermission();
- $per->parent_id = $parent_id;
- $per->title = $title;
- $per->route = $route;
- $per->weight = $weight;
- $per->icon = $icon;
- return [$per->save(), 0];
- }
- public static function editPermission($id,$title,$route,$icon)
- {
- $per = SysPermission::where('id', $id)->first();
- if(!isset($per->id)){
- return ['参数有误', 1102];
- }
- $per->title = $title;
- $per->route = $route;
- $per->icon = $icon;
- return [$per->save(), 0];
- }
- /***
- * 对权限进行排序设置
- * @param $parent_id
- * @param $permission_id_arr
- */
- public static function be_sort($parent_id, $permission_id_arr){
- foreach ($permission_id_arr as $num => $p_id){
- SysPermission::query()->where("id",$p_id)
- ->where("parent_id",$parent_id)
- ->update(['weight'=>$num]);
- }
- return true;
- }
- public static function permissionList($parent_id)
- {
- $res = SysPermission::where('parent_id', $parent_id)
- ->where('enable', 1)
- ->orderBy('weight')
- ->get();
- if($res->isEmpty()){
- return $res;
- }
- foreach($res as $item){
- $item->child_count = SysPermission::where('parent_id', $item->id)->where('enable', 1)->count();
- }
- return $res;
- }
- public static function delPermission($id)
- {
- $res = SysPermission::where('id', $id)->update([
- 'enable' => 0
- ]);
- #删除子权限
- $res = SysPermission::where('parent_id', $id)->update([
- 'enable' => 0
- ]);
- return true;
- }
- /**
- * 获取权限树
- * @param int $parent_id
- * @param int $level
- * @param false $only_permission_id_arr [false:查看全部权限。权限集合数组:只查看特定的权限]
- * @return array
- */
- public static function permissionTree($parent_id = 0,$level = 1,$only_permission_id_arr = false, $is_tab = false){
- $admin_id = \Auth::id();
- $query = SysPermission::query()->where("enable",1)
- ->where("parent_id",$parent_id);
- if ($is_tab) {
- $query->where('is_view', 1);
- }
- if($admin_id != 1){
- $query->where('id', '!=', 5); //只有管理员能操作菜单权限
- }
- //是否只查看特定权限
- if($only_permission_id_arr!==false){
- $query->whereIn("id",$only_permission_id_arr);
- }
- $list = $query->select("id","title","route","icon")
- ->orderBy("weight")
- ->get()
- ->toArray();
- if(empty($list)) return [];
- foreach ($list as $k => $item){
- $list[$k]['level'] = $level;
- $list[$k]['sub'] = self::permissionTree($item['id'],$level+1,$only_permission_id_arr, $is_tab);
- }
- return $list;
- }
- public static function userPermission($admin_id)
- {
- # 获取用户权限集
- $role_ids = SysUserRole::where('sys_user_id', $admin_id)->where('enable', 1)->pluck('role_id')->all();
- if(empty($role_ids)){
- return null;
- }
- if(in_array(1, $role_ids)){
- //超级管理员拥有所有权限
- return self::permissionTree(0, 1, false, true);
- }
- $permissionIds = SysRolePermission::whereIn('role_id', $role_ids)->where('enable', 1)->pluck('permission_id')->all();
- $res = self::permissionTree(0, 1, $permissionIds, true);
- return $res;
- }
- public static function addRole($name,$desc,$per_ids)
- {
- $role = new SysRole();
- $role->name = $name;
- $role->desc = $desc;
- $role->save();
- #角色权限
- $data = array();
- foreach($per_ids as $permission_id){
- $data[] = [
- 'role_id' => $role->id,
- 'permission_id' => $permission_id
- ];
- }
- SysRolePermission::insert($data);
- return ['添加成功', 0];
- }
- public static function editRole($id,$name,$desc,$per_ids)
- {
- $role = SysRole::where('id', $id)->first();
- if(!isset($role->id)){
- return ['参数有误', 1102];
- }
- $role->name = $name;
- $role->desc = $desc;
- $role->save();
- #查出现有权限
- $al_per_ids = SysRolePermission::where('role_id', $id)
- ->where('enable', 1)
- ->pluck('permission_id')
- ->all();
- $new_per_ids = array_diff($per_ids, $al_per_ids);
- $del_per_ids = array_diff($al_per_ids, $per_ids);
- if(!empty($new_per_ids)){
- $data = [];
- foreach($new_per_ids as $permission_id){
- $data[] = [
- 'role_id' => $id,
- 'permission_id' => $permission_id
- ];
- }
- SysRolePermission::insert($data);
- }
- if(!empty($del_per_ids)){
- SysRolePermission::whereIn('permission_id', $del_per_ids)
- ->where('role_id', $id)
- ->update(['enable'=>0]);
- }
- return ['操作成功', 0];
- }
- public static function delRole($id)
- {
- $role = SysRole::where('id', $id)->where('enable', 1)->first();
- if(!isset($role->id)){
- return ['参数有误', 1102];
- }
- $role->enable = 0;
- return [$role->save(), 0];
- }
- public static function roleList()
- {
- $listQ = SysRole::where('enable', 1)->where('id','>',1);
- $list = $listQ->orderBy('id')->get();
- return $list;
- }
- public static function roleDetail($role_id)
- {
- $role = SysRole::where('id', $role_id)->first();
- if(!isset($role->id)){
- return null;
- }
- #查角色权限
- if($role_id == 1){
- $per_ids = SysRolePermission::where('enable', 1)->pluck('permission_id')->all();
- } else {
- $per_ids = SysRolePermission::where('enable', 1)->where('role_id', $role_id)->pluck('permission_id')->all();
- }
- $role->permissionIds = $per_ids;
- return $role;
- }
- public static function addUser($name,$password,$role_ids,$advertiser_ids,$desc,$cust_ids,$group_id)
- {
- if(strlen($password)<6){
- return ['', 2001];
- }
- $user = new SysUsers;
- $user->name = $name;
- $user->desc = $desc;
- $user->password = Hash::make($password);
- $user->group_id = $group_id;
- $user->save();
- #角色
- $data = array();
- foreach($role_ids as $role_id){
- $data[] = [
- 'sys_user_id' => $user->id,
- 'role_id' => $role_id
- ];
- }
- SysUserRole::insert($data);
- #广告主
- $data = array();
- foreach($cust_ids as $customer_id){
- $data[] = [
- 'sys_user_id' => $user->id,
- 'customer_id' => $customer_id
- ];
- }
- SysUserCusts::insert($data);
- return ['添加成功', 0];
- }
- public static function editUser($id,$name,$password,$role_ids,$advertiser_ids,$desc,$cust_ids,$group_id)
- {
- if($password && strlen($password)<6){
- return ['', 2001];
- }
- $user = SysUsers::where('id', $id)->first();
- if(!isset($user->id)){
- return ['参数有误', 1102];
- }
- $user->name = $name;
- $user->desc = $desc;
- $user->group_id = $group_id;
- if($password) $user->password = Hash::make($password);
- $user->save();
- #查出现有角色
- $al_role_ids = SysUserRole::where('sys_user_id', $id)
- ->where('enable', 1)
- ->pluck('role_id')
- ->all();
- $new_role_ids = array_diff($role_ids, $al_role_ids);
- $del_role_ids = array_diff($al_role_ids, $role_ids);
- if(!empty($new_role_ids)){
- $data = [];
- foreach($new_role_ids as $role_id){
- $data[] = [
- 'sys_user_id' => $id,
- 'role_id' => $role_id
- ];
- }
- SysUserRole::insert($data);
- }
- if(!empty($del_role_ids)){
- SysUserRole::whereIn('role_id', $del_role_ids)
- ->where('sys_user_id', $id)
- ->update(['enable'=>0]);
- }
- #客户绑定
- $al_cust_ids = SysUserCusts::where('sys_user_id', $id)
- ->where('enable', 1)
- ->pluck('customer_id')
- ->all();
- $new_cust_ids = array_diff($cust_ids, $al_cust_ids);
- $del_cust_ids = array_diff($al_cust_ids, $cust_ids);
- if(!empty($new_cust_ids)){
- $data = [];
- foreach($new_cust_ids as $customer_id){
- $data[] = [
- 'sys_user_id' => $id,
- 'customer_id' => $customer_id
- ];
- }
- SysUserCusts::insert($data);
- }
- if(!empty($del_cust_ids)){
- SysUserCusts::whereIn('customer_id', $del_cust_ids)
- ->where('sys_user_id', $id)
- ->update(['enable'=>0]);
- }
- return ['操作成功', 0];
- }
- public static function delUser($id)
- {
- $user = SysUsers::where('id', $id)->where('enable', 1)->first();
- if(!isset($user->id)){
- return ['参数有误', 1102];
- }
- $user->enable = 0;
- return [$user->save(), 0];
- }
- public static function userList($page, $pageSize)
- {
- $offset = ($page-1) * $pageSize;
- $listQ = SysUsers::where('enable', 1)->where('id','>',1);
- $count = $listQ->count();
- $list = $listQ->select('name', 'id', 'desc', 'group_id')
- ->orderBy('id')
- ->offset($offset)
- ->limit($pageSize)
- ->get();
- $groupListMap = SysGroup::query()
- ->whereIn('id', $list->pluck('group_id'))
- ->where('enable', 1)
- ->pluck('name', 'id');
- foreach($list as $item){
- $role_ids = SysUserRole::where('sys_user_id', $item->id)
- ->where('enable', 1)
- ->pluck('role_id')
- ->all();
- $item->roles = SysRole::whereIn('id', $role_ids)->where('enable', 1)->select('id', 'name')->get();
- /*$adver_ids = SysUserAdver::where('sys_user_id', $item->id)
- ->where('enable', 1)
- ->pluck('advertiser_id')
- ->all();
- $item->advers = JuxingAdAccount::select('nick_name', 'advertiser_id')->where('enable', 1)->whereIn('advertiser_id', $adver_ids)->get();*/
- $cust_ids = SysUserCusts::where('sys_user_id', $item->id)
- ->where('enable', 1)
- ->pluck('customer_id')
- ->all();
- $item->cust = SysCustomer::select('name', 'remarks')->where('enable', 1)->whereIn('id', $cust_ids)->get();
- $item->group_name = $groupListMap->get($item->group_id) ?? null;
- }
- return [$list, $count];
- }
- public static function userDetail($id)
- {
- $user = SysUsers::select('id', 'name', 'desc', 'group_id')->where('id', $id)->first();
- if(!isset($user->id)){
- return null;
- }
- #角色
- $user->role_ids = SysUserRole::where('sys_user_id', $id)
- ->where('enable', 1)
- ->pluck('role_id')
- ->all();
- #广告主
- $user->adver_ids = SysUserAdver::where('sys_user_id', $id)
- ->where('enable', 1)
- ->pluck('advertiser_id')
- ->all();
- #客户
- $user->cust_ids = SysUserCusts::where('sys_user_id', $id)
- ->where('enable', 1)
- ->pluck('customer_id')
- ->all();
-
- #用户组
- $user->group_name = SysGroup::where('id', $user->group_id)->where('enable', 1)->value('name') ?? null;
- return $user;
- }
- public static function adverList()
- {
- $list = JuxingAdAccount::select('nick_name', 'advertiser_id')
- ->where('enable', 1)
- ->orderBy('id')
- ->get();
- return $list;
- }
- public static function doLogin($username, $password)
- {
- $user = SysUsers::select(['name', 'password', 'id'])
- ->where('name', $username)
- ->where('enable', 1)
- ->first();
- if (empty($user)) {
- return ['账户不存在', 2002];
- }
- if (Hash::check($password, $user->password)) {
- $redisKey = env('LOGIN_REDIS_KEY') . '-'. $user->id;
- $randomKey = RedisModel::get($redisKey);
- if(!$randomKey){
- $randomKey = SysUsers::random(20);
- RedisModel::set($redisKey, $randomKey);
- RedisModel::expire($redisKey, 86400*30);
- }
- $data = array(
- 'admin_id' => $user->id,
- 'username' => $user->name,
- 'random' => $randomKey,
- 'ttl' => time()
- );
- return [$data, 0];
- } else {
- return ['登录账号或密码不正确', 2003];
- }
- }
- }
|