|
|
@@ -0,0 +1,249 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Http\Controllers\AdminApi;
|
|
|
+
|
|
|
+use App\Models\SysModels\Role;
|
|
|
+use App\Models\SysModels\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);//获取状态
|
|
|
+ $roles_id = request()->input('roles_id','');//获取角色id
|
|
|
+ $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 ($roles_id) $where[] = ['roles_id', '=', $roles_id];
|
|
|
+ if ($start) $where[] = ['created_at', '>=', $start];
|
|
|
+ if ($end) $where[] = ['created_at', '<=', $end];
|
|
|
+
|
|
|
+ //获取数据
|
|
|
+ $list = User::where($where)
|
|
|
+ ->orderBy('id','desc')
|
|
|
+ ->select(['id','name','phone','status','roles_id','created_at'])
|
|
|
+ ->paginate(10);
|
|
|
+
|
|
|
+ foreach ($list as $value){
|
|
|
+ if($value['id'] == 1){
|
|
|
+ $value['roles_name'] = '超级管理员';
|
|
|
+ }else{
|
|
|
+ $value['roles_name'] = Role::where('id',$value['roles_id'])->where('is_del',0)->value('name');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ 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', 'roles_id', 'status'])->first();
|
|
|
+
|
|
|
+ if (empty($info)) return $this->apiResponseError('没有找到该记录');
|
|
|
+
|
|
|
+ if($info['id'] == 1){
|
|
|
+ $info['roles_name'] = '超级管理员';
|
|
|
+ }else{
|
|
|
+ $info['roles_name'] = Role::where('id',$info['roles_id'])->where('is_del',0)->value('name');
|
|
|
+ }
|
|
|
+ return $this->apiResponseSuccess('获取成功', $info);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新&编辑后台账号
|
|
|
+ * @return false|\Illuminate\Http\JsonResponse
|
|
|
+ */
|
|
|
+ function save(){
|
|
|
+ $id = request()->input('id', '');//记录id,更新时必须
|
|
|
+ $phone = request()->input('phone', '');//登录手机号
|
|
|
+ $name = request()->input('name', '');//姓名
|
|
|
+ $roles_id = request()->input('roles_id', 0);//角色id
|
|
|
+ $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 ($roles_id < 0 ) {
|
|
|
+ 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'],
|
|
|
+ 'roles_id' => $roles_id,
|
|
|
+ '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;
|
|
|
+ $update['roles_id'] = $roles_id;
|
|
|
+ $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);//是否显示
|
|
|
+ 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('数据格式错误');
|
|
|
+ if(in_array(1,$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('删除失败');
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|