User.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\UserModel;
  4. use app\admin\model\UserType;
  5. use think\Db;
  6. use think\Session;
  7. class User extends Base
  8. {
  9. /**
  10. * [index 用户列表]
  11. * @return [type] [description]
  12. * @author
  13. */
  14. public function index(){
  15. if(request()->isAjax ()){
  16. extract(input());
  17. $map = [];
  18. if(isset($role)&&$role != ""){
  19. $map['ag.id'] = $role;
  20. }
  21. if(isset($key)&&$key!="")
  22. {
  23. $map['a.username|a.real_name'] = ['like',"%" . $key . "%"];
  24. }
  25. if(isset($start)&&$start!=""&&isset($end)&&$end=="")
  26. {
  27. $map['a.last_login_time'] = ['>= time',$start];
  28. }
  29. if(isset($end)&&$end!=""&&isset($start)&&$start=="")
  30. {
  31. $map['a.last_login_time'] = ['<= time',$end];
  32. }
  33. if(isset($start)&&$start!=""&&isset($end)&&$end!="")
  34. {
  35. $map['a.last_login_time'] = ['between time',[$start,$end]];
  36. }
  37. $Nowpage = input('page') ? input('page'):1;
  38. $limits = input("limit")?input("limit"):10;// 获取总条数;
  39. $field=input('field');//字段
  40. $order=input('order');//排序方式
  41. if($field && $order){
  42. $od="a.".$field." ".$order;
  43. }else{
  44. $od="a.create_time desc";
  45. }
  46. $user = new UserModel();
  47. $count = $user->getUserCount($map);
  48. $lists = $user->getUsersByWhere($map,$od, $Nowpage, $limits);
  49. return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
  50. }
  51. $role = Db::name('auth_group')->field('id,title')->order('create_time desc')->select();
  52. $this->assign ('role',$role);
  53. return $this->fetch("user/index");
  54. }
  55. /**
  56. * [userAdd 添加用户]
  57. * @return [type] [description]
  58. * @author
  59. */
  60. public function userAdd()
  61. {
  62. if(request()->isPost()){
  63. $param = input('post.');
  64. $user = new UserModel();
  65. $param['password'] = md5(md5($param['password']) . config('auth_key'));
  66. $base64url = $param['portrait'];
  67. $res = base64_img($base64url,true);
  68. if($res['code'] == 200){
  69. $param['portrait'] = $res['msg'];
  70. }elseif($res['code'] == 100){
  71. writelog('添加管理员【'.$param['username'].'】上传头像失败',100);
  72. return json($res);
  73. }
  74. $flag = $user->insertUser($param);
  75. return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
  76. }
  77. $role = new UserType();
  78. $this->assign('role',$role->getRole());
  79. return $this->fetch('user/useradd');
  80. }
  81. /**
  82. * checkName 验证管理员名称唯一性
  83. */
  84. public function checkName(){
  85. extract(input());
  86. if(isset($id)&&$id!=""){
  87. $uid = $id;
  88. }else{
  89. $uid = '';
  90. }
  91. $user = new UserModel();
  92. $flag = $user->checkName ($username,$uid);
  93. return json(['code' => $flag['code'], 'msg' => $flag['msg']]);
  94. }
  95. /**
  96. * [userEdit 编辑用户]
  97. * @return [type] [description]
  98. * @author
  99. */
  100. public function userEdit()
  101. {
  102. $user = new UserModel();
  103. if(request()->isPost()){
  104. $param = input('post.');
  105. if(empty($param['password'])){
  106. unset($param['password']);
  107. }else{
  108. $param['password'] = md5(md5($param['password']) . config('auth_key'));
  109. }
  110. $base64url = $param['portrait'];
  111. $res = base64_img($base64url,true);
  112. $have = "";
  113. if($res['code'] == 200){
  114. $param['portrait'] = $res['msg'];
  115. //判断编辑的是不是自己的头像
  116. if(session('uid')==$param['id']){
  117. $have = "have";
  118. }
  119. }elseif($res['code'] == 100){
  120. writelog('编辑管理员【'.$param['username'].'】上传头像失败',100);
  121. return json($res);
  122. }
  123. $flag = $user->editUser($param);
  124. return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg'],'type'=>$have]);
  125. }
  126. $id = input('param.id');
  127. if($id != "1"){
  128. $role = new UserType();
  129. $this->assign([
  130. 'user' => $user->getOneUser($id),
  131. 'role' => $role->getRole()
  132. ]);
  133. //普通管理员编辑页面
  134. return $this->fetch("user/useredit");
  135. }else{
  136. $this->assign([
  137. 'user' => $user->getOneUser($id)
  138. ]);
  139. //超级管理员编辑页面
  140. return $this->fetch("user/editadmin");
  141. }
  142. }
  143. /**
  144. * [adminEdit 编辑超级管理员]
  145. * @return [type] [description]
  146. * @author
  147. */
  148. public function adminEdit(){
  149. $user = new UserModel();
  150. $oldpassword = md5(md5(input('oldpassword')).config('auth_key'));
  151. if(input('type')=="checkPassword"){
  152. $flag = $user->checkOldPassword ($oldpassword,session('uid'));
  153. return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
  154. }else{
  155. $param = input('post.');
  156. if(empty($param['password'])){
  157. unset($param['password']);
  158. }else{
  159. $param['password'] = md5(md5($param['password']) . config('auth_key'));
  160. }
  161. $base64url = $param['portrait'];
  162. $res = base64_img($base64url,true);
  163. $have = "";
  164. if($res['code'] == 200){
  165. $param['portrait'] = $res['msg'];
  166. //判断编辑的是不是自己的头像
  167. if(session('uid')==$param['id']){
  168. $have = "have";
  169. }
  170. }elseif($res['code'] == 100){
  171. writelog('编辑管理员【'.$param['username'].'】上传头像失败',100);
  172. return json($res);
  173. }
  174. $flag = $user->editUser($param);
  175. return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg'],'type'=>$have]);
  176. }
  177. }
  178. /**
  179. * [UserDel 删除用户]
  180. * @return [type] [description]
  181. * @author
  182. */
  183. public function UserDel()
  184. {
  185. $id = input('param.id');
  186. if(session('uid')==$id){
  187. return json(['code'=>100,'msg'=>'不能删除自己']);
  188. }else{
  189. $role = new UserModel();
  190. $flag = $role->delUser($id);
  191. return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
  192. }
  193. }
  194. /**
  195. * [user_state 用户状态]
  196. * @return [type] [description]
  197. * @author
  198. */
  199. public function user_state()
  200. {
  201. extract(input());
  202. $role = new UserModel();
  203. $flag = $role->userState($id,$num);
  204. return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
  205. }
  206. /**
  207. * editPwd 修改管理员密码
  208. * @return \think\response\Json
  209. */
  210. public function editPwd(){
  211. extract(input());
  212. $user = new UserModel();
  213. if(isset($type) && $type=='checkPassword'){
  214. $old_pwd = md5(md5($old_pwd).config('auth_key'));
  215. $flag = $user->checkOldPassword ($old_pwd,session('uid'));
  216. return json(['code'=>$flag['code'],'msg'=>$flag['msg']]);
  217. }else{
  218. $param['password'] = md5(md5($new_pwd).config('auth_key'));
  219. $flag = $user->editPassword($param);
  220. return json(['code'=>$flag['code'],'msg'=>$flag['msg']]);
  221. }
  222. }
  223. /**
  224. * batchDelUser 批量删除管理员
  225. * @return \think\response\Json
  226. */
  227. public function batchDelUser(){
  228. extract(input());
  229. if(empty($ids)){
  230. return json(['code'=>100,'msg'=>'请选择要删除的记录!']);
  231. }
  232. $ids = explode(',',$ids);
  233. if(in_array('1',$ids)){
  234. $key = array_search ('1',$ids);
  235. unset($ids[$key]);
  236. if(empty($ids)){
  237. return json(['code'=>100,'msg'=>'不可删除超级管理员']);
  238. die;
  239. }
  240. }
  241. if(in_array(session('uid'),$ids)){
  242. $key = array_search (session('uid'),$ids);
  243. unset($ids[$key]);
  244. if(empty($ids)){
  245. return json(['code'=>100,'msg'=>'不可删除自己']);
  246. die;
  247. }
  248. }
  249. $ids = array_merge($ids);
  250. $user = new UserModel();
  251. $flag = $user->batchDelUser($ids);
  252. return json(['code' => $flag['code'], 'msg' => $flag['msg']]);
  253. }
  254. /**
  255. * usingAdmin 批量启用管理员
  256. * @return \think\response\Json
  257. */
  258. public function usingAdmin(){
  259. extract(input());
  260. $list = [];
  261. if($ids){
  262. $ids = explode(',',$ids);
  263. for($i=0;$i<count($ids);$i++){
  264. $param = [
  265. 'id'=>$ids[$i],
  266. 'status'=>1
  267. ];
  268. $list[] = $param;
  269. }
  270. }
  271. $user = new UserModel();
  272. $flag = $user->usingAdmin($list);
  273. return json(['code' => $flag['code'], 'msg' => $flag['msg']]);
  274. }
  275. /**
  276. * forbiddenAdmin 批量禁用管理员
  277. * @return \think\response\Json
  278. */
  279. public function forbiddenAdmin(){
  280. extract(input());
  281. $list = [];
  282. if($ids){
  283. $ids = explode(',',$ids);
  284. if(in_array('1',$ids)){
  285. $key = array_search ('1',$ids);
  286. unset($ids[$key]);
  287. if(empty($ids)){
  288. return json(['code'=>100,'msg'=>'不可禁用超级管理员']);
  289. die;
  290. }
  291. }
  292. if(in_array(session('uid'),$ids)){
  293. $key = array_search (session('uid'),$ids);
  294. unset($ids[$key]);
  295. if(empty($ids)){
  296. return json(['code'=>100,'msg'=>'不可禁用自己']);
  297. die;
  298. }
  299. }
  300. $ids = array_merge($ids);
  301. for($i=0;$i<count($ids);$i++){
  302. $param = [
  303. 'id'=>$ids[$i],
  304. 'status'=>2
  305. ];
  306. $list[] = $param;
  307. }
  308. }
  309. $user = new UserModel();
  310. $flag = $user->forbiddenAdmin($list);
  311. return json(['code' => $flag['code'], 'msg' => $flag['msg']]);
  312. }
  313. /**
  314. * 导出Excel
  315. * @return \think\response\Json
  316. */
  317. public function excelAdmin(){
  318. extract(input());
  319. if($ids =="" && $key == "" && $start == "" && $end == "" && $role ==""){
  320. $data = Db::name('admin')->select();
  321. }
  322. if($ids != ""){
  323. $ids = trim($ids,',');
  324. $ids = explode(',',$ids);
  325. $data = Db::name('admin')->where('id','in',$ids)->select();
  326. }else{
  327. $map = [];
  328. if($role != ""){
  329. $map['ag.id'] = $role;
  330. }
  331. if($key!="")
  332. {
  333. $map['a.username|a.real_name'] = ['like',"%" . $key . "%"];
  334. }
  335. if($start!=""&&$end=="")
  336. {
  337. $map['a.last_login_time'] = ['>= time',$start];
  338. }
  339. if($end!=""&&$start=="")
  340. {
  341. $map['a.last_login_time'] = ['<= time',$end];
  342. }
  343. if($start!=""&&$end!="")
  344. {
  345. $map['a.last_login_time'] = ['between time',[$start,$end]];
  346. }
  347. $data = Db::name('admin')
  348. ->alias ('a')
  349. ->join('auth_group ag', 'a.groupid = ag.id','left')
  350. ->field('a.id,username,a.password,a.portrait,a.loginnum,a.last_login_ip,a.last_login_time,a.real_name,phone,a.status,a.groupid,a.create_time,a.update_time')
  351. ->where($map)
  352. ->select();
  353. }
  354. $cellname = [
  355. ['id','ID',15,'LEFT'],
  356. ['username','昵称',15,'LEFT'],
  357. ['password','密码',15,'LEFT'],
  358. ['portrait','头像',20,'LEFT'],
  359. ['loginnum','登录次数',15,'LEFT'],
  360. ['last_login_ip','上次登录ip',15,'LEFT'],
  361. ['last_login_time','上次登录时间',15,'LEFT'],
  362. ['real_name','真实姓名',15,'LEFT'],
  363. ['phone','手机号',15,'LEFT'],
  364. ['status','状态',15,'LEFT'],
  365. ['groupid','角色id',15,'LEFT'],
  366. ['create_time','创建时间',15,'LEFT'],
  367. ['update_time','修改时间',15,'LEFT']
  368. ];
  369. $res = exportExcel('管理员信息','admin',$cellname,$data);
  370. return json($res);
  371. }
  372. }