SystemAdmin.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\model\system;
  12. use traits\ModelTrait;
  13. use basic\ModelBasic;
  14. use behavior\system\SystemBehavior;
  15. use service\HookService;
  16. use think\Session;
  17. /**
  18. * Class SystemAdmin
  19. * @package app\admin\model\system
  20. */
  21. class SystemAdmin extends ModelBasic
  22. {
  23. use ModelTrait;
  24. protected $insert = ['add_time'];
  25. public static function setAddTimeAttr($value)
  26. {
  27. return time();
  28. }
  29. public static function setRolesAttr($value)
  30. {
  31. return is_array($value) ? implode(',', $value) : $value;
  32. }
  33. /**
  34. * 用户登陆
  35. * @param string $account 账号
  36. * @param string $pwd 密码
  37. * @param string $verify 验证码
  38. * @return bool 登陆成功失败
  39. */
  40. public static function login($account,$pwd)
  41. {
  42. $adminInfo = self::get(compact('account'));
  43. if(!$adminInfo) return self::setErrorInfo('登陆的账号不存在!');
  44. if($adminInfo['pwd'] != md5($pwd)) return self::setErrorInfo('账号或密码错误,请重新输入');
  45. if(!$adminInfo['status']) return self::setErrorInfo('该账号已被关闭!');
  46. self::setLoginInfo($adminInfo->toarray());
  47. HookService::afterListen('system_admin_login',$adminInfo,null,false,SystemBehavior::class);
  48. return true;
  49. }
  50. /**
  51. * 保存当前登陆用户信息
  52. */
  53. public static function setLoginInfo($adminInfo)
  54. {
  55. //补充角色信息
  56. $roleSign = SystemRole::where(['id' => $adminInfo['roles']])->value('sign');
  57. $adminInfo['role_sign'] = $roleSign ? $roleSign : "";
  58. Session::set('adminId',$adminInfo['id']);
  59. Session::set('adminInfo',$adminInfo);
  60. }
  61. /**
  62. * 清空当前登陆用户信息
  63. */
  64. public static function clearLoginInfo()
  65. {
  66. Session::delete('adminInfo');
  67. Session::delete('adminId');
  68. Session::clear();
  69. }
  70. /**
  71. * 检查用户登陆状态
  72. * @return bool
  73. */
  74. public static function hasActiveAdmin()
  75. {
  76. return Session::has('adminId') && Session::has('adminInfo');
  77. }
  78. /**
  79. * 获得登陆用户信息
  80. * @return mixed
  81. */
  82. public static function activeAdminInfoOrFail()
  83. {
  84. $adminInfo = Session::get('adminInfo');
  85. if(!$adminInfo) exception('请登陆');
  86. if(!$adminInfo['status']) exception('该账号已被关闭!');
  87. return $adminInfo;
  88. }
  89. /**
  90. * 获得登陆用户Id 如果没有直接抛出错误
  91. * @return mixed
  92. */
  93. public static function activeAdminIdOrFail()
  94. {
  95. $adminId = Session::get('adminId');
  96. if(!$adminId) exception('访问用户为登陆登陆!');
  97. return $adminId;
  98. }
  99. /**
  100. * @return array
  101. */
  102. public static function activeAdminAuthOrFail()
  103. {
  104. $adminInfo = self::activeAdminInfoOrFail();
  105. return $adminInfo->level === 0 ? SystemRole::getAllAuth() : SystemRole::rolesByAuth($adminInfo->roles);
  106. }
  107. /**
  108. * 获得有效管理员信息
  109. * @param $id
  110. * @return static
  111. */
  112. public static function getValidAdminInfoOrFail($id)
  113. {
  114. $adminInfo = self::get($id);
  115. if(!$adminInfo) exception('用户不能存在!');
  116. if(!$adminInfo['status']) exception('该账号已被关闭!');
  117. return $adminInfo;
  118. }
  119. /**
  120. * @param $field
  121. * @return false|\PDOStatement|string|\think\Collection
  122. */
  123. public static function getOrdAdmin($field = 'real_name,id',$level = 0){
  124. return self::where('level','>=',$level)->field($field)->select();
  125. }
  126. public static function getTopAdmin($field = 'real_name,id')
  127. {
  128. return self::where('level',0)->field($field)->select();
  129. }
  130. /**
  131. * @param $where
  132. * @return array
  133. */
  134. public static function systemPage($where){
  135. $model = new self;
  136. if($where['name'] != ''){
  137. $model = $model->where('account','LIKE',"%$where[name]%");
  138. $model = $model->where('real_name','LIKE',"%$where[name]%");
  139. }
  140. if($where['roles'] != '')
  141. $model = $model->where("CONCAT(',',roles,',') LIKE '%,$where[roles],%'");
  142. $model = $model->where('level','=',$where['level'])->where('is_del',0);
  143. return self::page($model,function($admin,$key){
  144. $admin->roles = SystemRole::where('id','IN',$admin->roles)->column('role_name');
  145. },$where);
  146. }
  147. /**根据身份获取后台账号列表
  148. * @param $sign 角色身份标识
  149. * @return false|\PDOStatement|string|\think\Collection
  150. * @throws \think\db\exception\DataNotFoundException
  151. * @throws \think\db\exception\ModelNotFoundException
  152. * @throws \think\exception\DbException
  153. */
  154. public static function getRoleAdmin($sign = false) {
  155. $where['a.status'] = 1;
  156. if ((is_array($sign) && !$sign) || !$sign){
  157. $role_sign = SystemRole::where('status',1)
  158. ->field('sign')
  159. ->select()
  160. ->toArray();
  161. $sign = array_column($role_sign, 'sign');
  162. }
  163. if (!is_array($sign) && $sign) {
  164. $where['r.sign'] = $sign;
  165. }
  166. return self::where($where)
  167. ->whereIn('sign',$sign)
  168. ->alias('a')
  169. ->join('__SYSTEM_ROLE__ r', 'a.roles = r.id', 'LEFT')
  170. ->field('a.id as admin_id, a.account as admin_name, a.roles as role_id, a.level as admin_level, a.status as admin_status')
  171. ->select()
  172. ->toArray();
  173. }
  174. /**
  175. * 检测用户权限身份
  176. */
  177. public static function testUserLevel($user){
  178. $role=SystemRole::where('sign','verification')->where('status',1)->find();
  179. if(!$role) return false;
  180. $level=self::where('roles',$role['id'])->where('phone',$user['phone'])->find();
  181. if($level) return true;
  182. else return false;
  183. }
  184. }