User.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\controller;
  4. use app\BaseController;
  5. use think\facade\Db;
  6. class User extends BaseController
  7. {
  8. public function setRole(){
  9. $post =$this->request->post();
  10. $token = isset($post['token']) ? trim($post['token']) : "";
  11. if($token==""){
  12. return app_show(101,'token不能为空');
  13. }
  14. $effetc = VerifyTokens($token);
  15. if(!empty($effetc) && $effetc['code']!=0){
  16. return app_show($effetc['code'],$effetc['message']);
  17. }
  18. $uid = isset($post['id'])&&$post['id']!=='' ? intval($post['id']) :"";
  19. if($uid===''){
  20. return error_show(1004,"参数id 不能为空");
  21. }
  22. $role =isset($post['roleid']) && $post['roleid']!=="" ? intval($post['roleid']) :"";
  23. if($role===''){
  24. return error_show(1004,"参数roleid 不能为空");
  25. }
  26. $isRole = Db::name('role')->where([['id',"=",$role],['status',"=",1]])->find();
  27. if(empty($isRole)){
  28. return error_show(1004,"所选角色不存在");
  29. }
  30. $data = [
  31. 'uid'=>$uid,
  32. 'roleid'=>$role,
  33. 'status'=>1,
  34. 'addtime'=>date('Y-m-d H:i:s'),
  35. 'updatetime'=>date('Y-m-d H:i:s'),
  36. ];
  37. $insert = Db::name('user_role')->insert($data);
  38. return $insert? app_show(0,'数据新建成功'):error_show(1004,'数据新建失败');
  39. }
  40. /**
  41. * 显示资源列表
  42. *
  43. * @return \think\Response
  44. */
  45. public function list()
  46. {
  47. $post =$this->request->post();
  48. $token = isset($post['token']) ? trim($post['token']) : "";
  49. if($token==""){
  50. return error_show(101,'token不能为空');
  51. }
  52. $effetc =VerifyTokens($token);
  53. if(!empty($effetc) && $effetc['code']!=0){
  54. return error_show($effetc['code'],$effetc['message']);
  55. }
  56. $data = Getlist($token,$post);
  57. if(!empty($data) && $data['code']!=0){
  58. return error_show($data['code'],$data['message']);
  59. }
  60. $list=[];
  61. foreach($data['data']['list'] as $key=>$value){
  62. $role = Db::name("user_role")->alias('a')->leftJoin('role b',"a.roleid = b.id")->where("a.uid","=",$value['id'])
  63. ->field("roleid,role_name,a.status")->find();
  64. $value['roleid'] = isset($role['roleid']) ? $role['roleid'] :"";
  65. $value['role_name'] = isset($role['role_name']) ? $role['role_name'] :"";
  66. $value['status'] = isset($role['status']) ? $role['status'] :"0";
  67. $depart = Db::name("depart_user")->where([["uid","=",$value['id']],["is_del","=",0]])->column("itemid");
  68. $value['depart']="";
  69. if(!empty($depart)){
  70. // $deparname = Db::name("company_item")->where(["id"=>$depart,"is_del"=>0])->column("name");
  71. $deparname = Db::name("company_item")->where(["id"=>$depart,"is_del"=>0])->column("id,name,depart_link");
  72. $value['depart_code'] = [];
  73. if(!empty($deparname)){
  74. foreach ($deparname as $item) {
  75. $value['depart_code'][] = array_filter(explode("-",$item['depart_link'])) ;
  76. $value['depart_info'][] = Db::name("company_item")->where(["id"=>array_filter(explode("-",
  77. $item['depart_link'])),"is_del"=>0])->column("name");
  78. }
  79. }
  80. $value['depart'] = $deparname;
  81. }
  82. $list[]=$value;
  83. }
  84. $data['data']['list'] = $list;
  85. return app_show($data['code'],$data['message'],$data['data']);
  86. }
  87. /**
  88. * @param token
  89. * @return \think\response\Json
  90. * @throws \think\exception\DbException
  91. */
  92. public function userInfo(){
  93. $post =$this->request->post();
  94. $token = isset($post['token']) ? trim($post['token']) : "";
  95. if($token==""){
  96. return app_show(101,'token不能为空');
  97. }
  98. $effetc = VerifyTokens($token);
  99. if(!empty($effetc) && $effetc['code']!=0){
  100. return app_show($effetc['code'],$effetc['message']);
  101. }
  102. // $userinfo=GetUserInfo($token);
  103. // if(empty($userinfo)||$userinfo['code']!=0){
  104. // return app_show(1002,"员工信息不存在");
  105. // }
  106. $data = $effetc['data'];
  107. $role = Db::name("user_role")->alias('a')->leftJoin('role b',"a.roleid = b.id")->where("a.uid","=",$data['id'])
  108. ->field("roleid,role_name,a.status")->find();
  109. $data['role_name']=isset($role['role_name']) ? $role['role_name'] :"";
  110. $data['roleid']=isset($role['roleid']) ? $role['roleid'] :"";
  111. $data['status'] = isset($role['status']) ? $role['status'] :"0";
  112. $depart = Db::name("depart_user")->where([["uid","=",$data['id']],["is_del","=",0]])->column("itemid");
  113. $data['depart']="";
  114. if(!empty($depart)){
  115. $deparname = Db::name("company_item")->where(["id"=>$depart,"is_del"=>0])->field("id,name,depart_link")->select();
  116. $value['depart_code'] = [];
  117. if(!empty($deparname)){
  118. foreach ($deparname as $item) {
  119. $value['depart_code'][] = array_filter(explode("-",$item['depart_link'])) ;
  120. }
  121. }
  122. $data['depart'] = $deparname;
  123. }
  124. return app_show(0,"获取成功",$data);
  125. }
  126. public function userAll(){
  127. $post =$this->request->post();
  128. $token = isset($post['token']) ? trim($post['token']) : "";
  129. if($token==""){
  130. return app_show(101,'token不能为空');
  131. }
  132. $effetc = VerifyTokens($token);
  133. if(!empty($effetc) && $effetc['code']!=0){
  134. return app_show($effetc['code'],$effetc['message']);
  135. }
  136. $userinfo=GetAccountall($token,$post);
  137. if(empty($userinfo)||$userinfo['code']!=0){
  138. return app_show(1002,"员工信息不存在");
  139. }
  140. $data = $userinfo['data'];
  141. $list=[];
  142. foreach ($data as $value){
  143. $role = Db::name("user_role")->alias('a')->leftJoin('role b',"a.roleid = b.id")->where("a.uid","=",$value['id'])
  144. ->field("roleid,role_name,a.status")->find();
  145. $value['roleid'] = isset($role['roleid']) ? $role['roleid'] :"";
  146. $value['role_name'] = isset($role['role_name']) ? $role['role_name'] :"";
  147. $value['status'] = isset($role['status']) ? $role['status'] :"0";
  148. $depart = Db::name("depart_user")->where([["uid","=",$value['id']],["is_del","=",0]])->column("itemid");
  149. $value['depart']="";
  150. if(!empty($depart)){
  151. $deparname = Db::name("company_item")->where(["id"=>$depart,"is_del"=>0])->field("id,name,depart_link")
  152. ->select()->toArray();
  153. array_walk($deparname,function (&$v){
  154. $v = array_filter(explode("-",$v['depart_link'])) ;
  155. });
  156. $value['depart'] = $deparname;
  157. }
  158. $list[]=$value;
  159. }
  160. return app_show(0,"获取成功",$list);
  161. }
  162. /**
  163. * 保存新建的资源
  164. *
  165. * @param \think\Request $request
  166. * @return \think\Response
  167. */
  168. public function UserSave()
  169. {
  170. $post=$this->request->post();
  171. $toke= isset($post['token'])? trim($post['token']):"";
  172. if ($toke==""){
  173. return app_show(100,"token不能为空");
  174. }
  175. $verify = VerifyTokens($toke);
  176. if ($verify['code']!=0){
  177. return app_show($verify['code'],$verify['message']);
  178. }
  179. $userid = isset($post['id'])&&$post['id']!==""?intval($post['id']):"";
  180. if($userid==""){
  181. return error_show(1004,"参数id 不能为空");
  182. }
  183. $nickname= isset($post['nickname'])? trim($post['nickname']):"";
  184. if ($nickname==""){
  185. return app_show(1001,"昵称不能为空");
  186. }
  187. $mobile= isset($post['mobile'])? trim($post['mobile']):"";
  188. if ($mobile==""){
  189. return app_show(1002,"手机号不能为空");
  190. }
  191. if (checkMobile($mobile)==false){
  192. return app_show(1002,"手机号格式不正确");
  193. }
  194. $email= isset($post['email'])? trim($post['email']):"";
  195. $role =isset($post['role'])? intval($post['role']):0;
  196. if($role!=0){
  197. $roleinfo =Db::name("role")->where("id","=",$role)->find();
  198. if(empty($roleinfo)){
  199. return error_show(1002,"角色不存在");
  200. }
  201. if($roleinfo['status']==0){
  202. return error_show(1002,"角色已禁用");
  203. }
  204. }
  205. $status = isset($post['status'])&&$post['status']!=""?intval($post['status']):"0";
  206. $itemid = isset($post['itemid'])&&$post['itemid']!=="" ?$post['itemid'] :"";
  207. $item_arr = [];
  208. if($itemid!==""){
  209. foreach ($itemid as $value){
  210. $item_arr[]=end($value);
  211. }
  212. $iteminfo = Db::name("company_item")->where(["id"=>$item_arr,'is_del'=>0])->select()->toArray();
  213. if(empty($iteminfo)){
  214. return error_show(1002,"未找到部门信息");
  215. }
  216. }
  217. Db::startTrans();
  218. try{
  219. $userrole = Db::name("user_role")->where(["uid"=>$userid,"is_del"=>0])->find();
  220. if(empty($userrole)){
  221. $roledata =[
  222. "uid"=>$userid,
  223. "roleid"=>$role,
  224. "status"=>$status,
  225. "addtime"=>date("Y-m-d H:i:s"),
  226. "updatetime"=>date("Y-m-d H:i:s")
  227. ];
  228. $up= Db::name("user_role")->insert($roledata);
  229. }else{
  230. $userrole['roleid'] = $role;
  231. $userrole['status'] = $status;
  232. $userrole['updatetime'] = date("Y-m-d H:i:s");
  233. $up=Db::name("user_role")->save($userrole);
  234. }
  235. if($up){
  236. $data=[
  237. "id"=>$userid,
  238. "nickname"=>$nickname,
  239. "mobile"=>$mobile,
  240. "email"=>$email
  241. ];
  242. $save =resetinfo($toke,$data);
  243. if(!empty($save) && $save['code']!=0){
  244. Db::rollback();
  245. return error_show($save['code'],$save['message']);
  246. }
  247. Db::name("depart_user")->where(["uid"=>$userid])->save(['is_del'=>1]);
  248. if(isset($item_arr)&&!empty($item_arr)){
  249. foreach ($item_arr as $v){
  250. $depin =[
  251. "uid"=>$userid,
  252. "nickname"=>$nickname,
  253. "itemid"=>$v,
  254. "is_del"=>0,
  255. "updatetime"=>date("Y-m-d H:i:s")
  256. ];
  257. $ist = Db::name("depart_user")->where(["uid"=>$userid,"itemid"=>$v])->find();
  258. if($ist){
  259. $up = Db::name("depart_user")->where(["uid"=>$userid,"itemid"=>$v])->save($depin);
  260. }else{
  261. $depin['addtime']=date("Y-m-d H:i:s");
  262. $up = Db::name("depart_user")->insert($depin);
  263. }
  264. if($up==false){
  265. Db::rollback();
  266. return error_show(1005,'用户部门更新失败');
  267. }
  268. }
  269. }
  270. Db::commit();
  271. return app_show(0,"更新成功");
  272. }else{
  273. Db::rollback();
  274. return error_show(1005,'角色更新失败');
  275. }
  276. }catch(\Exception $e){
  277. Db::rollback();
  278. return app_show(1002,$e->getMessage());
  279. }
  280. }
  281. public function UserStatus()
  282. {
  283. $post=$this->request->post();
  284. $toke= isset($post['token'])? trim($post['token']):"";
  285. if ($toke==""){
  286. return app_show(100,"token不能为空");
  287. }
  288. $verify = VerifyTokens($toke);
  289. if ($verify['code']!=0){
  290. return app_show($verify['code'],$verify['message']);
  291. }
  292. $userid = isset($post['id'])&&$post['id']!==""?intval($post['id']):"";
  293. if($userid==""){
  294. return error_show(1004,"参数userid 不能为空");
  295. }
  296. $status = isset($post['status'])&&$post['status']!=""?intval($post['status']):"";
  297. if($status===""){
  298. return error_show(1004,"参数status 不能为空");
  299. }
  300. if(!in_array($status,[0,1])){
  301. return error_show(1004,"参数status无效");
  302. }
  303. $message = $status==1?"启用":"禁用";
  304. // $set = resetstatus($toke,["id"=>$userid,"status"=>$status]);
  305. $item = Db::name('user_role')->where(['uid'=>$userid])->find();
  306. if($item==false){
  307. $item=[
  308. "uid"=>$userid,
  309. "roleid"=>0,
  310. "is_del"=>0,
  311. "addtime"=>date("Y-m-d H:i:s")
  312. ];
  313. }
  314. $item['status']=$status;
  315. $item['updatetime'] = date("Y-m-d H:i:s");
  316. $var = Db::name('user_role')->save($item);
  317. if($var==false){
  318. return error_show(1005,"{$message}失败");
  319. }else{
  320. return app_show(0,"{$message}成功");
  321. }
  322. }
  323. }