UserController.php 7.9 KB

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