UserInfo.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\Api\controller;
  4. use app\BaseController;
  5. use think\Request;
  6. use think\facade\Db;
  7. class UserInfo extends BaseController
  8. {
  9. /**
  10. * 显示资源列表
  11. *
  12. * @return \think\Response
  13. */
  14. public function UserList()
  15. {
  16. $post=$this->request->post();
  17. $toke= isset($post['token'])? trim($post['token']):"";
  18. if ($toke==""){
  19. return app_show(100,"token不能为空");
  20. }
  21. $verify = VerifyToken($toke);
  22. if ($verify['code']!=0){
  23. return app_show($verify['code'],$verify['message']);
  24. }
  25. $condition = [];
  26. isset($post['name'])&& $post['name']!="" ? $condition[]=["nickname","like","%{$post['name']}%"] : "";
  27. isset($post['username'])&& $post['username']!="" ? $condition[]=["username","like","%{$post['username']}%"] : "";
  28. isset($post['role'])&& $post['role']!="" ? $condition[]=["roleid","=",$post['role']] : "";
  29. isset($post['status'])&& $post['status']!=="" ? $condition[]=["status","=",$post['status']] : "";
  30. isset($post['uid'])&& $post['uid']!=="" ? $condition[]=["id","in",$post['uid']] : "";
  31. $page = isset($post['page'])&& $post['page']!=="" ? intval($post['page']) : 1;
  32. $size = isset($post['size'])&& $post['size']!=="" ? intval($post['size']) : 10;
  33. $count = Db::name("view_userinfo")->where($condition)->count();
  34. $total =intval(ceil($count/$size)) ;
  35. $page = $total>=$page? $page:$total;
  36. $list = Db::name("view_userinfo")->where($condition)->page($page,$size)->field("id,username,status,source,addtime,nickname,sex,mobile,email,portrait,post,department,roleid")->select();
  37. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  38. }
  39. /**
  40. * 显示创建资源表单页.
  41. *
  42. * @return \think\Response
  43. */
  44. public function UserStatus()
  45. {
  46. $post=$this->request->post();
  47. $toke= isset($post['token'])? trim($post['token']):"";
  48. if ($toke==""){
  49. return app_show(100,"token不能为空");
  50. }
  51. $verify = VerifyToken($toke);
  52. if ($verify['code']!=0){
  53. return app_show($verify['code'],$verify['message']);
  54. }
  55. $userid = isset($post['id'])&&$post['id']!==""?intval($post['id']):"";
  56. if($userid==""){
  57. return error_show(1004,"参数userid 不能为空");
  58. }
  59. $account=Db::name("account")->where("id","=",$userid)->find();
  60. if(empty($account)){
  61. return error_show(1003,"账户不存在");
  62. }
  63. $status = isset($post['status'])&&$post['status']!=""?intval($post['status']):"";
  64. if($status===""){
  65. return error_show(1004,"参数status 不能为空");
  66. }
  67. if(!in_array($status,[0,1])){
  68. return error_show(1004,"参数status无效");
  69. }
  70. $message = $status==1?"启用":"禁用";
  71. $result= Db::name("account")->where("id","=",$userid)->save(['status'=>$status,"updatetime"=>date("Y-m-d
  72. H:i:s")]);
  73. return $result?app_show(0,"账户{$message}") : error_show(1005,"账户{$message}失败");
  74. }
  75. /**
  76. * 保存新建的资源
  77. *
  78. * @param \think\Request $request
  79. * @return \think\Response
  80. */
  81. public function UserSave()
  82. {
  83. $post=$this->request->post();
  84. $toke= isset($post['token'])? trim($post['token']):"";
  85. if ($toke==""){
  86. return app_show(100,"token不能为空");
  87. }
  88. $verify = VerifyToken($toke);
  89. if ($verify['code']!=0){
  90. return app_show($verify['code'],$verify['message']);
  91. }
  92. $userid = isset($post['id'])&&$post['id']!==""?intval($post['id']):"";
  93. if($userid==""){
  94. return error_show(1004,"参数id 不能为空");
  95. }
  96. $account=Db::name("view_userinfo")->where("id","=",$userid)->find();
  97. if(empty($account)){
  98. return error_show(1003,"账户不存在");
  99. }
  100. $nickname= isset($post['nickname'])? trim($post['nickname']):"";
  101. if ($nickname==""){
  102. return app_show(1001,"昵称不能为空");
  103. }
  104. $mobile= isset($post['mobile'])? trim($post['mobile']):"";
  105. if ($mobile==""){
  106. return app_show(1002,"手机号不能为空");
  107. }
  108. if (checkMobile($mobile)==false){
  109. return app_show(1002,"手机号格式不正确");
  110. }
  111. $email= isset($post['email'])? trim($post['email']):"";
  112. $avatar = isset($post['portrait'])? trim($post['portrait']):"";
  113. $sex = isset($post['sex'])? trim($post['sex']):"";
  114. $postDa = isset($post['post'])? trim($post['post']):"";
  115. $department = isset($post['department'])? trim($post['department']):"";
  116. $role =isset($post['role'])&&$post['role']!==''? intval($post['role']):$account['roleid'];
  117. Db::startTrans();
  118. try{
  119. if(isset($account['user_id'])&& $account['user_id']!=""){
  120. $userinfo=[
  121. "id"=>$account['user_id'],
  122. "nickname"=>$nickname,
  123. "mobile"=>$mobile,
  124. "email"=>$email,
  125. "portrait"=>$avatar,
  126. "sex"=>$sex,
  127. "post"=> $postDa,
  128. "department"=>$department,
  129. "status"=>1,
  130. "updatetime"=>date("Y-m-d H:i:s")
  131. ];
  132. $dat=Db::table("sys_user")->save($userinfo);
  133. if(!$dat){
  134. Db::rollback();
  135. return error_show(1004,"信息修改失败");
  136. }
  137. }
  138. $relation = Db::name("user_relation")->where("accountid","=",$userid)->find();
  139. if($relation){
  140. $relation['user_id']=$account['user_id'];
  141. $relation['roleid']=$role;
  142. $relation['updatetime']=date("Y-m-d H:i:s");
  143. $rel=Db::name("user_relation")->save($relation);
  144. }else{
  145. $rela=["accountid"=>$userid,"user_id"=>$account['user_id'],"roleid"=>$role,"updatetime"=>date("Y-m-d H:i:s")];
  146. $rel= Db::name("user_relation")->insert($rela);
  147. }
  148. if(!$rel){
  149. Db::rollback();
  150. return error_show(1004,"关联信息修改失败");
  151. }
  152. $acc= [
  153. "id"=>$account['id'],
  154. "mobile"=>$mobile,
  155. "username"=>$mobile,
  156. "updatetime"=>date("Y-m-d H:i:s"),
  157. ];
  158. $nu = Db::name("account")->save($acc);
  159. if($nu){
  160. Db::commit();
  161. return app_show(0,"信息修改成功");
  162. }else{
  163. Db::rollback();
  164. return error_show(1004,"账户信息修改失败");
  165. }
  166. }catch (\Exception $e){
  167. Db::rollback();
  168. return error_show(1005,$e->getMessage());
  169. }
  170. }
  171. /**
  172. * 显示指定的资源
  173. *
  174. * @param int $id
  175. * @return \think\Response
  176. */
  177. public function all()
  178. {
  179. $post=$this->request->post();
  180. $toke= isset($post['token'])? trim($post['token']):"";
  181. if ($toke==""){
  182. return app_show(100,"token不能为空");
  183. }
  184. $verify = VerifyToken($toke);
  185. if ($verify['code']!=0){
  186. return app_show($verify['code'],$verify['message']);
  187. }
  188. $condition = [];
  189. isset($post['name'])&& $post['name']!="" ? $condition[]=["nickname","like","%{$post['name']}%"] : "";
  190. isset($post['role'])&& $post['role']!="" ? $condition[]=["roleid","=",$post['role']] : "";
  191. isset($post['status'])&& $post['status']!=="" ? $condition[]=["status","=",$post['status']] : "";
  192. $list = Db::name("view_userinfo")->where($condition)->field("id,username,status,source,addtime,nickname,sex,mobile,email,portrait,post,department,roleid")->select();
  193. return app_show(0,"获取成功",$list);
  194. }
  195. /**
  196. * 显示编辑资源表单页.
  197. *
  198. * @param int $id
  199. * @return \think\Response
  200. */
  201. public function info()
  202. {
  203. $post=$this->request->post();
  204. $toke= isset($post['token'])? trim($post['token']):"";
  205. if ($toke==""){
  206. return app_show(100,"token不能为空");
  207. }
  208. $verify = VerifyToken($toke);
  209. if ($verify['code']!=0){
  210. return app_show($verify['code'],$verify['message']);
  211. }
  212. $id = isset($post['id'])&&$post['id']!="" ? intval($post['id']) : "";
  213. if($id===""){
  214. return error_show(1003,"参数id 不能为空");
  215. }
  216. $list = Db::name("view_userinfo")->where("id","=",$id)->field("id,username,status,source,addtime,nickname,sex,mobile,email,portrait,post,department,roleid")->find();
  217. if(empty($list)){
  218. return error_show(1004,"未找到用户信息");
  219. }
  220. return app_show(0,"获取成功",$list);
  221. }
  222. /**
  223. * 保存更新的资源
  224. *
  225. * @param \think\Request $request
  226. * @param int $id
  227. * @return \think\Response
  228. */
  229. public function PassSet()
  230. {
  231. $post=$this->request->post();
  232. $toke= isset($post['token'])? trim($post['token']):"";
  233. if ($toke==""){
  234. return app_show(100,"token不能为空");
  235. }
  236. $verify = VerifyToken($toke);
  237. if ($verify['code']!=0){
  238. return app_show($verify['code'],$verify['message']);
  239. }
  240. $userid = isset($post['id'])&&$post['id']!==""?intval($post['id']):"";
  241. if($userid==""){
  242. return error_show(1004,"参数id 不能为空");
  243. }
  244. $account=Db::name("account")->where("id","=",$userid)->find();
  245. if(empty($account)){
  246. return error_show(1003,"账户不存在");
  247. }
  248. $pass = isset($post["password"]) && $post['password']!="" ? trim($post['password']) : "";
  249. if($pass==""){
  250. return error_show(1003,"账户密码不能为空");
  251. }
  252. if(mb_strlen($pass)<6){
  253. return error_show(1003,"账户密码长度不能小于6位");
  254. }
  255. $salt=makeSalt();
  256. $password = sha1($pass.$salt);
  257. $account['password']=$password;
  258. $account['salt']=$salt;
  259. $account['is_pass']=1;
  260. $account['updatetime']=date("Y-m-d H:i:s");
  261. $up = Db::name("account")->save($account);
  262. return $up?app_show(0,"密码修改成功"):error_show(1005,"密码修改失败");
  263. }
  264. /**
  265. * 删除指定资源
  266. *
  267. * @param int $id
  268. * @return \think\Response
  269. */
  270. public function delete($id)
  271. {
  272. //
  273. }
  274. }