UserController.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace App\Http\Controllers\AdminApi;
  3. use App\Models\UserModels\Role;
  4. use App\Models\UserModels\User;
  5. use App\Http\Controllers\AdminController;
  6. use App\Servers\Common\CommonServer;
  7. use App\Servers\Common\PassServer;
  8. class UserController extends AdminController
  9. {
  10. /**
  11. * 后台账号列表
  12. * @return \Illuminate\Http\JsonResponse
  13. */
  14. function getList(){
  15. $name = request()->input('name','');//获取搜索名称
  16. $phone = request()->input('phone','');//获取搜索手机号
  17. $status = request()->input('status',0);//获取状态
  18. $roles_id = request()->input('roles_id','');//获取角色id
  19. $start = request()->input('start','');//获取开始时间
  20. $end = request()->input('end','');//获取结束时间
  21. //查询数据条件
  22. $where = [['is_del',0]];
  23. if ($name) $where[] = ['name', 'like', "%$name%"];
  24. if ($phone) $where[] = ['phone', 'like', "%$phone%"];
  25. if(in_array($status,[1,2])) $where[] = ['status','=',$status];
  26. if ($roles_id) $where[] = ['roles_id', '=', $roles_id];
  27. if ($start) $where[] = ['created_at', '>=', $start];
  28. if ($end) $where[] = ['created_at', '<=', $end];
  29. //获取数据
  30. $list = User::where($where)
  31. ->orderBy('id','desc')
  32. ->select(['id','name','phone','status','roles_id','created_at'])
  33. ->paginate(10);
  34. foreach ($list as $value){
  35. if($value['id'] == 1){
  36. $value['roles_name'] = '超级管理员';
  37. }else{
  38. $value['roles_name'] = Role::where('id',$value['roles_id'])->where('is_del',0)->value('name');
  39. }
  40. }
  41. return $this->apiResponseSuccess('获取信息成功', [
  42. 'list' => $list->items(),
  43. 'total' => $list->total(),
  44. 'limit' => 10
  45. ]);
  46. }
  47. /**
  48. * 获取记录详情
  49. * @return \Illuminate\Http\JsonResponse
  50. */
  51. function getInfo()
  52. {
  53. $id = request()->input('id', '');//获取需要查询的记录id
  54. if (empty($id)) return $this->apiResponseError('缺少必要参数');
  55. //查询数据
  56. $where = [['id', $id], ['is_del',0]];
  57. $info = User::where($where)->select(['id', 'name', 'phone', 'roles_id', 'status'])->first();
  58. if (empty($info)) return $this->apiResponseError('没有找到该记录');
  59. if($info['id'] == 1){
  60. $info['roles_name'] = '超级管理员';
  61. }else{
  62. $info['roles_name'] = Role::where('id',$info['roles_id'])->where('is_del',0)->value('name');
  63. }
  64. return $this->apiResponseSuccess('获取成功', $info);
  65. }
  66. /**
  67. * 更新&编辑后台账号
  68. * @return false|\Illuminate\Http\JsonResponse
  69. */
  70. function save(){
  71. $id = request()->input('id', '');//记录id,更新时必须
  72. $phone = request()->input('phone', '');//登录手机号
  73. $name = request()->input('name', '');//姓名
  74. $roles_id = request()->input('roles_id', 0);//角色id
  75. $password = request()->input('password', '');//密码
  76. $again_password = request()->input('again_password', '');//确认密码
  77. if (empty($phone)) return $this->apiResponseError( '登录手机号必填');
  78. if(!CommonServer::creatServer()->verifyPhoneNumber($phone)){
  79. return $this->apiResponseError( '手机号不正确');
  80. }
  81. if ($roles_id < 0 ) {
  82. return $this->apiResponseError( '请选择角色');
  83. }
  84. if(empty($id)){
  85. if (empty($password) || empty($again_password)) {
  86. return $this->apiResponseError( '请输入新密码');
  87. }
  88. if(strlen($password) < 6 || strlen($password) > 12){
  89. return $this->apiResponseError( '密码在6至12位');
  90. }
  91. if($password != $again_password){
  92. return $this->apiResponseError( '两次密码不一致');
  93. }
  94. $count = User::where('phone', '=', $phone)->where('is_del',0)->count();
  95. if ($count > 0 ) {
  96. return $this->apiResponseError( '账号已注册');
  97. }
  98. //生成密码
  99. $pass_server = PassServer::creatServer($password);
  100. $new_pass = $pass_server->creatPassword();
  101. $res = User::create([
  102. 'name' => $name,
  103. 'phone' => $phone,
  104. 'password' => $new_pass['password'],
  105. 'roles_id' => $roles_id,
  106. 'encrypt' => $new_pass['encrypt']
  107. ]);
  108. }else{
  109. if (empty($id)) return $this->apiResponseError( '缺少必要参数');
  110. $info = User::where('id',$id)->where('is_del',0)->first();
  111. if(!$info) return $this->apiResponseError( '记录不存在');
  112. $count = User::where('phone', '=', $phone)->where('is_del',0)->where('id', '<>', $id)->count();
  113. if ($count > 0 ) {
  114. return $this->apiResponseError( '账号已注册');
  115. }
  116. if (!empty($password) ) {
  117. if(strlen($password) < 6 || strlen($password) > 12){
  118. return $this->apiResponseError( '密码在6至12位');
  119. }
  120. if($password != $again_password){
  121. return $this->apiResponseError( '两次密码不一致');
  122. }
  123. //生成密码
  124. $pass_server = PassServer::creatServer($password, $info->{'encrypt'});
  125. $new_pass = $pass_server->creatPassword();
  126. $update['password'] = $new_pass['password'];
  127. $update['encrypt'] = $new_pass['encrypt'];
  128. }
  129. //获取当前操作用户类型,不是超级管理员不可以编辑超级管理
  130. $admin_id = request()->admin_user['id'];
  131. if($admin_id != 1 && $info['id'] == 1){
  132. return $this->apiResponseError( '您不可以编辑超级管理员');
  133. }
  134. $update['phone'] = $phone;
  135. $update['name'] = $name;
  136. $update['roles_id'] = $roles_id;
  137. $res = $info->update($update);
  138. }
  139. if ($res) {
  140. return $this->apiResponseSuccess('成功');
  141. } else {
  142. return $this->apiResponseError('失败');
  143. }
  144. }
  145. /**
  146. * 快速更新是否启用
  147. * @return \Illuminate\Http\JsonResponse
  148. */
  149. function setStatus(){
  150. $id = request()->input('id','');//记录id
  151. $status = request()->input('status', 0);//是否显示
  152. if(empty($id) || !in_array($status,[1,2])){
  153. return $this->apiResponseError('缺少必要参数');
  154. }
  155. $info = User::where('id',$id)->where('is_del',0)->select(['id'])->first();
  156. if (empty($info)) {
  157. return $this->apiResponseError('没有找到该记录');
  158. }
  159. if($info['id'] == 1){
  160. return $this->apiResponseError('超级管理员不能关闭');
  161. }
  162. $res = $info->update(['status'=>$status]);
  163. if($status == 1){
  164. $msg = '开启';
  165. }else{
  166. $msg = '关闭';
  167. }
  168. if ($res) {
  169. return $this->apiResponseSuccess($msg.'成功');
  170. } else {
  171. return $this->apiResponseError($msg.'失败');
  172. }
  173. }
  174. /**
  175. * 重置密码
  176. * @return \Illuminate\Http\JsonResponse
  177. */
  178. function resetPassword(){
  179. $id = request()->input('id', '');//重置密码的记录id
  180. if(empty($id)) return $this->apiResponseError('缺少必要参数');
  181. $info = User::where('id',$id)->where('is_del',0)->select(['id','encrypt'])->first();
  182. if (empty($info)) {
  183. return $this->apiResponseError('没有找到该记录');
  184. }
  185. $password = '123456';
  186. //生成密码
  187. $pass_server = PassServer::creatServer($password, $info->{'encrypt'});
  188. $new_pass = $pass_server->creatPassword();
  189. $update['password'] = $new_pass['password'];
  190. $update['encrypt'] = $new_pass['encrypt'];
  191. $res = $info->update($update);
  192. if ($res) {
  193. return $this->apiResponseSuccess('密码已重置,新密码为'.$password);
  194. } else {
  195. return $this->apiResponseError('密码重置失败');
  196. }
  197. }
  198. /**
  199. * 删除(批量)账号
  200. * @return \Illuminate\Http\JsonResponse
  201. */
  202. function destroys(){
  203. $ids = request()->input('ids','');//获取需要删除的id
  204. if(empty($ids)) return $this->apiResponseError('缺少必要参数');
  205. //数据条件
  206. if(!is_array($ids)) return $this->apiResponseError('数据格式错误');
  207. if(in_array(1,$ids)){
  208. return $this->apiResponseError('超级管理员不能删除');
  209. }
  210. $res = User::whereIn('id',$ids)->where('id','<>',1)->update(['is_del' => 1,'status'=>2]);
  211. if ($res) {
  212. return $this->apiResponseSuccess('删除成功');
  213. } else {
  214. return $this->apiResponseError('删除失败');
  215. }
  216. }
  217. }