123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- <?php
- namespace App\Http\Controllers\AdminApi;
- use App\Models\UserModels\User;
- use App\Http\Controllers\AdminController;
- use App\Servers\Common\CommonServer;
- use App\Servers\Common\PassServer;
- class UserController extends AdminController
- {
- /**
- * 后台账号列表
- * @return \Illuminate\Http\JsonResponse
- */
- function getList(){
- $name = request()->input('name','');//获取搜索名称
- $phone = request()->input('phone','');//获取搜索手机号
- $status = request()->input('status',0);//获取状态
- $start = request()->input('start','');//获取开始时间
- $end = request()->input('end','');//获取结束时间
- //查询数据条件
- $where = [['is_del',0]];
- if ($name) $where[] = ['name', 'like', "%$name%"];
- if ($phone) $where[] = ['phone', 'like', "%$phone%"];
- if(in_array($status,[1,2])) $where[] = ['status','=',$status];
- if ($start) $where[] = ['created_at', '>=', $start];
- if ($end) $where[] = ['created_at', '<=', $end];
- //获取数据
- $list = User::where($where)
- ->orderBy('id','desc')
- ->select(['id','name','phone','status','created_at'])
- ->paginate(10);
- return $this->apiResponseSuccess('获取信息成功', [
- 'list' => $list->items(),
- 'total' => $list->total(),
- 'limit' => 10
- ]);
- }
- /**
- * 获取记录详情
- * @return \Illuminate\Http\JsonResponse
- */
- function getInfo()
- {
- $id = request()->input('id', '');//获取需要查询的记录id
- if (empty($id)) return $this->apiResponseError('缺少必要参数');
- //查询数据
- $where = [['id', $id], ['is_del',0]];
- $info = User::where($where)->select(['id', 'name', 'phone', 'status'])->first();
- if (empty($info)) return $this->apiResponseError('没有找到该记录');
- return $this->apiResponseSuccess('获取成功', $info);
- }
- /**
- * 更新&编辑后台账号
- * @return false|\Illuminate\Http\JsonResponse
- */
- function save(){
- $id = request()->input('id', '');//记录id,更新时必须
- $phone = request()->input('phone', '');//登录手机号
- $name = request()->input('name', '');//姓名
- $password = request()->input('password', '');//密码
- $again_password = request()->input('again_password', '');//确认密码
- if (empty($phone)) return $this->apiResponseError( '登录手机号必填');
- if(!CommonServer::creatServer()->verifyPhoneNumber($phone)){
- return $this->apiResponseError( '手机号不正确');
- }
- if(empty($id)){
- if (empty($password) || empty($again_password)) {
- return $this->apiResponseError( '请输入新密码');
- }
- if(strlen($password) < 6 || strlen($password) > 12){
- return $this->apiResponseError( '密码在6至12位');
- }
- if($password != $again_password){
- return $this->apiResponseError( '两次密码不一致');
- }
- $count = User::where('phone', '=', $phone)->where('is_del',0)->count();
- if ($count > 0 ) {
- return $this->apiResponseError( '账号已注册');
- }
- //生成密码
- $pass_server = PassServer::creatServer($password);
- $new_pass = $pass_server->creatPassword();
- $res = User::create([
- 'name' => $name,
- 'phone' => $phone,
- 'password' => $new_pass['password'],
- 'encrypt' => $new_pass['encrypt']
- ]);
- }else{
- if (empty($id)) return $this->apiResponseError( '缺少必要参数');
- $info = User::where('id',$id)->where('is_del',0)->first();
- if(!$info) return $this->apiResponseError( '记录不存在');
- $count = User::where('phone', '=', $phone)->where('is_del',0)->where('id', '<>', $id)->count();
- if ($count > 0 ) {
- return $this->apiResponseError( '账号已注册');
- }
- if (!empty($password) ) {
- if(strlen($password) < 6 || strlen($password) > 12){
- return $this->apiResponseError( '密码在6至12位');
- }
- if($password != $again_password){
- return $this->apiResponseError( '两次密码不一致');
- }
- //生成密码
- $pass_server = PassServer::creatServer($password, $info->{'encrypt'});
- $new_pass = $pass_server->creatPassword();
- $update['password'] = $new_pass['password'];
- $update['encrypt'] = $new_pass['encrypt'];
- }
- //获取当前操作用户类型,不是超级管理员不可以编辑超级管理
- $admin_id = request()->admin_user['id'];
- if($admin_id != 1 && $info['id'] == 1){
- return $this->apiResponseError( '您不可以编辑该账户');
- }
- $update['phone'] = $phone;
- $update['name'] = $name;
- $res = $info->update($update);
- }
- if ($res) {
- return $this->apiResponseSuccess('成功');
- } else {
- return $this->apiResponseError('失败');
- }
- }
- /**
- * 快速更新是否启用
- * @return \Illuminate\Http\JsonResponse
- */
- function setStatus(){
- $id = request()->input('id','');//记录id
- $status = request()->input('status', 0);//状态 1启用 2停用
- if(empty($id) || !in_array($status,[1,2])){
- return $this->apiResponseError('缺少必要参数');
- }
- $info = User::where('id',$id)->where('is_del',0)->select(['id'])->first();
- if (empty($info)) {
- return $this->apiResponseError('没有找到该记录');
- }
- if($info['id'] == 1){
- return $this->apiResponseError('该账户不能关闭');
- }
- $res = $info->update(['status'=>$status]);
- if($status == 1){
- $msg = '开启';
- }else{
- $msg = '关闭';
- }
- if ($res) {
- return $this->apiResponseSuccess($msg.'成功');
- } else {
- return $this->apiResponseError($msg.'失败');
- }
- }
- /**
- * 重置密码
- * @return \Illuminate\Http\JsonResponse
- */
- function resetPassword(){
- $id = request()->input('id', '');//重置密码的记录id
- if(empty($id)) return $this->apiResponseError('缺少必要参数');
- $info = User::where('id',$id)->where('is_del',0)->select(['id','encrypt'])->first();
- if (empty($info)) {
- return $this->apiResponseError('没有找到该记录');
- }
- $password = '123456';
- //生成密码
- $pass_server = PassServer::creatServer($password, $info->{'encrypt'});
- $new_pass = $pass_server->creatPassword();
- $update['password'] = $new_pass['password'];
- $update['encrypt'] = $new_pass['encrypt'];
- $res = $info->update($update);
- if ($res) {
- return $this->apiResponseSuccess('密码已重置,新密码为'.$password);
- } else {
- return $this->apiResponseError('密码重置失败');
- }
- }
- /**
- * 删除(批量)账号
- * @return \Illuminate\Http\JsonResponse
- */
- function destroys(){
- $ids = request()->input('ids','');//获取需要删除的id
- if(empty($ids)) return $this->apiResponseError('缺少必要参数');
- //数据条件
- if(!is_array($ids)) return $this->apiResponseError('数据格式错误');
- $res = User::whereIn('id',$ids)->where('id','<>',1)->update(['is_del' => 1,'status'=>2]);
- if ($res) {
- return $this->apiResponseSuccess('删除成功');
- } else {
- return $this->apiResponseError('删除失败');
- }
- }
- }
|