User.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------------------------------
  4. * GouGuOPEN [ 左手研发,右手开源,未来可期!]
  5. +-----------------------------------------------------------------------------------------------
  6. * @Copyright (c) 2021~2024 http://www.gouguoa.com All rights reserved.
  7. +-----------------------------------------------------------------------------------------------
  8. * @Licensed 勾股OA,开源且可免费使用,但并不是自由软件,未经授权许可不能去除勾股OA的相关版权信息
  9. +-----------------------------------------------------------------------------------------------
  10. * @Author 勾股工作室 <hdm58@qq.com>
  11. +-----------------------------------------------------------------------------------------------
  12. */
  13. declare (strict_types = 1);
  14. namespace app\user\controller;
  15. use app\base\BaseController;
  16. use app\user\model\Admin as AdminList;
  17. use app\user\model\Department as DepartmentModel;
  18. use app\user\validate\AdminCheck;
  19. use avatars\MDAvatars;
  20. use Overtrue\Pinyin\Pinyin;
  21. use think\exception\ValidateException;
  22. use think\facade\Db;
  23. use think\facade\View;
  24. class User extends BaseController
  25. {
  26. public function index()
  27. {
  28. if (request()->isAjax()) {
  29. $param = get_params();
  30. $where = array();
  31. $whereOr = array();
  32. if (!empty($param['keywords'])) {
  33. $where[] = ['a.id|a.username|a.name|a.nickname|a.mobile|a.desc', 'like', '%' . $param['keywords'] . '%'];
  34. }
  35. if (isset($param['status']) && $param['status']!='') {
  36. $where[] = ['a.status', '=', $param['status']];
  37. }
  38. else{
  39. $where[] = ['a.status', '=', 1];
  40. }
  41. if (!empty($param['type'])) {
  42. $where[] = ['a.type', '=', $param['type']];
  43. }
  44. if (!empty($param['did'])) {
  45. $admin_array = Db::name('DepartmentAdmin')->where('department_id',$param['did'])->column('admin_id');
  46. $map1=[
  47. ['a.id','in',$admin_array],
  48. ];
  49. $map2=[
  50. ['a.did', '=', $param['did']],
  51. ];
  52. $whereOr =[$map1,$map2];
  53. }
  54. $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
  55. $admin = AdminList::alias('a')
  56. ->with('departments')
  57. ->field('a.*,p.title as position,d.title as department')
  58. ->join('Department d', 'd.id = a.did','left')
  59. ->join('Position p', 'p.id = a.position_id','left')
  60. ->where($where)
  61. ->where(function ($query) use($whereOr) {
  62. if (!empty($whereOr)){
  63. $query->whereOr($whereOr);
  64. }
  65. })
  66. ->paginate(['list_rows'=> $rows])
  67. ->order('a.id desc')
  68. ->each(function ($item, $key) {
  69. //遍历次要部门数据
  70. $departments = $item['departments']->toArray();
  71. if(empty($departments)){
  72. $item['departments'] = '-';
  73. }
  74. else{
  75. $item['departments'] = split_array_field($departments,'title');
  76. }
  77. $item['entry_time'] = to_date($item['entry_time'],'Y-m-d');
  78. $item['last_login_time'] = to_date($item['last_login_time'],'Y-m-d H:i');
  79. $item['last_login_ip'] = empty($item['last_login_ip']) ? '-' : $item['last_login_ip'];
  80. });
  81. return table_assign(0, '', $admin);
  82. } else {
  83. return view();
  84. }
  85. }
  86. //生成登录名
  87. public function create_name($name,$id=0,$total=0,$old='')
  88. {
  89. $count = Db::name('Admin')->where([['username','=',$name],['id','<>',$id]])->count();
  90. if($total==0){
  91. $old = $name;
  92. }
  93. $total++;
  94. if($count>0){
  95. $newname = $old.$total;
  96. $name = $this->create_name($newname,$id,$total,$old);
  97. }
  98. return $name;
  99. }
  100. //添加
  101. public function add()
  102. {
  103. $param = get_params();
  104. if (request()->isAjax()) {
  105. $id = isset($param['id'])?$param['id']:0;
  106. $param['entry_time'] = strtotime($param['entry_time']);
  107. $param['nickname'] = $param['name'];
  108. if ($id > 0) {
  109. if($id == 1){
  110. return to_assign(1, '超级管理员信息不支持编辑');
  111. }
  112. try {
  113. validate(AdminCheck::class)->scene('edit')->check($param);
  114. } catch (ValidateException $e) {
  115. // 验证失败 输出错误信息
  116. return to_assign(1, $e->getError());
  117. }
  118. $detail = get_admin($param['id']);
  119. $department_ids = Db::name('DepartmentAdmin')->where('admin_id',$param['id'])->column('department_id');
  120. $detail['department_ids'] = implode(',',$department_ids);
  121. // 启动事务
  122. Db::startTrans();
  123. try {
  124. Db::name('Admin')->where(['id' => $id])->strict(false)->field(true)->update($param);
  125. if($detail['department_ids'] != $param['department_ids']){
  126. Db::name('DepartmentAdmin')->where('admin_id',$id)->whereIn('department_id', $detail['department_ids'])->delete();
  127. if(!empty($param['department_ids'])){
  128. $dids = explode(',',$param['department_ids']);
  129. foreach ($dids as $did) {
  130. Db::name('DepartmentAdmin')->insert(['admin_id'=>$param['id'],'department_id'=>$did,'create_time' => time()]);
  131. }
  132. }
  133. }
  134. if(empty($param['thumb'])){
  135. $char = mb_substr($param['name'], 0, 1, 'utf-8');
  136. Db::name('Admin')->where('id', $id)->update(['thumb' => $this->to_avatars($char)]);
  137. }
  138. $info = Db::name('Admin')->where('id', $id)->find();
  139. $model = new DepartmentModel();
  140. $auth_dids = $model->get_auth_departments($info);
  141. $son_dids = $model->get_son_departments($info);
  142. Db::name('Admin')->where('id',$id)->update(['auth_dids'=>$auth_dids,'son_dids'=>$son_dids]);
  143. add_log('edit', $id, $param);
  144. //清除菜单\权限缓存
  145. clear_cache('adminMenu');
  146. // 提交事务
  147. Db::commit();
  148. } catch (\Exception $e) {
  149. // 回滚事务
  150. Db::rollback();
  151. return to_assign(1, '提交失败:' . $e->getMessage());
  152. }
  153. } else {
  154. $username = Pinyin::name($param['name'],'none')->join('');
  155. $param['username'] = $this->create_name($username,$id);
  156. try {
  157. validate(AdminCheck::class)->scene('add')->check($param);
  158. } catch (ValidateException $e) {
  159. // 验证失败 输出错误信息
  160. return to_assign(1, $e->getError());
  161. }
  162. $param['create_time'] = time();
  163. $param['salt'] = set_salt(20);
  164. $param['pwd'] = set_password($param['reg_pwd'], $param['salt']);
  165. // 启动事务
  166. Db::startTrans();
  167. try {
  168. $uid = Db::name('Admin')->strict(false)->field(true)->insertGetId($param);
  169. if(!empty($param['department_ids'])){
  170. $dids = explode(',',$param['department_ids']);
  171. foreach ($dids as $did) {
  172. Db::name('DepartmentAdmin')->insert(['admin_id'=>$uid,'department_id'=>$did,'create_time' => time()]);
  173. }
  174. }
  175. if(empty($param['thumb'])){
  176. $char = mb_substr($param['name'], 0, 1, 'utf-8');
  177. Db::name('Admin')->where('id', $uid)->update(['thumb' => $this->to_avatars($char)]);
  178. }
  179. $info = Db::name('Admin')->where('id', $uid)->find();
  180. $model = new DepartmentModel();
  181. $auth_dids = $model->get_auth_departments($info);
  182. $son_dids = $model->get_son_departments($info);
  183. Db::name('Admin')->where('id',$uid)->update(['auth_dids'=>$auth_dids,'son_dids'=>$son_dids]);
  184. add_log('add', $uid, $param);
  185. // 提交事务
  186. Db::commit();
  187. } catch (\Exception $e) {
  188. // 回滚事务
  189. Db::rollback();
  190. return to_assign(1, '提交失败:' . $e->getMessage());
  191. }
  192. }
  193. return to_assign();
  194. } else {
  195. $id = isset($param['id']) ? $param['id'] : 0;
  196. $department = set_recursion(get_department());
  197. $position = Db::name('Position')->where('status', '>=', 0)->order('create_time asc')->select();
  198. if ($id > 0) {
  199. $detail = get_admin($id);
  200. $detail['pname'] = Db::name('Admin')->where('id',$detail['pid'])->value('name');
  201. $department_ids = Db::name('DepartmentAdmin')->where('admin_id',$param['id'])->column('department_id');
  202. $detail['department_ids'] = implode(',',$department_ids);
  203. View::assign('detail', $detail);
  204. } else {
  205. //初始化密码
  206. $reg_pwd = set_salt(6);
  207. View::assign('reg_pwd', $reg_pwd);
  208. }
  209. View::assign('department', $department);
  210. View::assign('position', $position);
  211. View::assign('id', $id);
  212. return view();
  213. }
  214. }
  215. //生成头像
  216. public function to_avatars($char)
  217. {
  218. $defaultData = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
  219. 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'S', 'Y', 'Z',
  220. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  221. '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖', '拾',
  222. '一', '二', '三', '四', '五', '六', '七', '八', '九', '十');
  223. if (isset($char)) {
  224. $Char = $char;
  225. } else {
  226. $Char = $defaultData[mt_rand(0, count($defaultData) - 1)];
  227. }
  228. $OutputSize = min(512, empty($_GET['size']) ? 36 : intval($_GET['size']));
  229. $Avatar = new MDAvatars($Char, 256, 1);
  230. $avatar_name = '/avatars/avatar_256_' . set_salt(10) . time() . '.png';
  231. $path = get_config('filesystem.disks.public.url') . $avatar_name;
  232. $res = $Avatar->Save('.' . $path, 256);
  233. $Avatar->Free();
  234. return $path;
  235. }
  236. //查看
  237. public function view()
  238. {
  239. $id = get_params('id');
  240. $detail = get_admin($id);
  241. $department_ids = Db::name('DepartmentAdmin')->where('admin_id',$id)->column('department_id');
  242. $department_names = Db::name('Department')->whereIn('id',$department_ids)->column('title');
  243. $detail['department_names'] = implode(',',$department_names);
  244. $detail['pname'] = Db::name('Admin')->where('id',$detail['pid'])->value('name');
  245. //查询所有菜单和权限节点
  246. $menu = Db::name('AdminRule')->where(['menu' => 1])->order('sort asc,id asc')->select()->toArray();
  247. $rule = Db::name('AdminRule')->order('sort asc,id asc')->select()->toArray();
  248. //查询用户拥有的菜单和节点
  249. $user_groups = Db::name('PositionGroup')
  250. ->alias('a')
  251. ->join("AdminGroup g", "a.group_id=g.id", 'LEFT')
  252. ->where([['a.pid', '=', $detail["position_id"]], ['g.status', '=', 1]])
  253. ->select()
  254. ->toArray();
  255. $groups = $user_groups ?: [];
  256. $rules = [];
  257. foreach ($groups as $g) {
  258. $rules = array_merge($rules, explode(',', trim($g['rules'], ',')));
  259. }
  260. $rules = array_unique($rules);
  261. //数据嵌套
  262. $role_rule = create_tree_list(0, $rule, $rules);
  263. View::assign('role_rule', $role_rule);
  264. View::assign('detail', $detail);
  265. add_log('view', get_params('id'));
  266. return view();
  267. }
  268. //禁用,恢复
  269. public function set()
  270. {
  271. $type = get_params("type");
  272. $ids = get_params("ids");
  273. $idArray = explode(',', $ids);
  274. $list = [];
  275. foreach ($idArray as $key => $val) {
  276. if ($val == 1) {
  277. continue;
  278. }
  279. $list[] = [
  280. 'status' => $type,
  281. 'id' => $val,
  282. 'update_time' => time(),
  283. ];
  284. }
  285. foreach ($list as $key => $v) {
  286. if (Db::name('Admin')->update($v) !== false) {
  287. if ($type == 0) {
  288. add_log('disable', $v['id']);
  289. } else if ($type == 1) {
  290. add_log('recovery', $v['id']);
  291. }
  292. }
  293. }
  294. return to_assign(0, '操作成功');
  295. }
  296. //重置密码
  297. public function reset_psw()
  298. {
  299. $id = get_params("id");
  300. if($id == 1){
  301. return to_assign(1, '该账号是超级管理员,不允许重置');
  302. }
  303. $new_pwd = set_salt(6);
  304. $salt = set_salt(20);
  305. $data = [
  306. 'reg_pwd' => $new_pwd,
  307. 'salt' => $salt,
  308. 'pwd' => set_password($new_pwd, $salt),
  309. 'id' => $id,
  310. 'update_time' => time(),
  311. ];
  312. if (Db::name('Admin')->update($data) !== false) {
  313. add_log('reset', $id);
  314. return to_assign(0, '操作成功');
  315. } else {
  316. return to_assign(1, '操作失败');
  317. }
  318. }
  319. //删除员工
  320. public function del()
  321. {
  322. $id = get_params("id");
  323. if($id == 1){
  324. return to_assign(1, '该账号是超级管理员,不允许删除');
  325. }
  326. $data = [
  327. 'id' => $id,
  328. 'delete_time' => time(),
  329. ];
  330. if (Db::name('Admin')->update($data) !== false) {
  331. add_log('delete', $id);
  332. return to_assign(0, '操作成功');
  333. } else {
  334. return to_assign(1, '操作失败');
  335. }
  336. }
  337. //管理员操作日志
  338. public function log()
  339. {
  340. if (request()->isAjax()) {
  341. $param = get_params();
  342. $where = array();
  343. if (!empty($param['keywords'])) {
  344. $where[] = ['name|rule_menu|param_id', 'like', '%' . $param['keywords'] . '%'];
  345. }
  346. if (!empty($param['title_cate'])) {
  347. $where['title'] = $param['title_cate'];
  348. }
  349. if (!empty($param['rule_menu'])) {
  350. $where['rule_menu'] = $param['rule_menu'];
  351. }
  352. $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
  353. $content = DB::name('AdminLog')
  354. ->field("id,uid,name,action,title,content,rule_menu,ip,param_id,param,FROM_UNIXTIME(create_time,'%Y-%m-%d %H:%i:%s') create_time")
  355. ->order('create_time desc')
  356. ->where($where)
  357. ->paginate(['list_rows'=> $rows]);
  358. $content->toArray();
  359. foreach ($content as $k => $v) {
  360. $data = $v;
  361. $param_array = json_decode($v['param'], true);
  362. $param_value = '';
  363. foreach ($param_array as $key => $value) {
  364. if (is_array($value)) {
  365. $value = implode(',', $value);
  366. }
  367. $param_value .= $key . ':' . $value . '&nbsp;&nbsp;|&nbsp;&nbsp;';
  368. }
  369. $data['param'] = $param_value;
  370. $content->offsetSet($k, $data);
  371. }
  372. return table_assign(0, '', $content);
  373. } else {
  374. return view();
  375. }
  376. }
  377. }