UserController.php 8.3 KB

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