TaoLer/app/admin/controller/User.php

171 lines
3.9 KiB
PHP
Raw Normal View History

2020-01-01 13:17:19 +08:00
<?php
namespace app\admin\controller;
use app\common\controller\AdminController;
use app\admin\validate\Admin;
use app\admin\model\Admin as adminModel;
use think\facade\View;
use think\facade\Request;
use think\facade\Config;
use think\facade\Db;
use think\facade\Session;
use think\exception\ValidateException;
use app\common\model\User as UserModel;
class User extends AdminController
{
/**
protected function initialize()
{
parent::initialize();
}
*/
//用户表
public function list()
{
if(Request::isAjax()){
2020-02-13 13:18:17 +08:00
$datas = Request::only(['id','name','email','sex']);
$map = array_filter($datas);
2020-02-13 12:15:35 +08:00
$user = Db::name('user')->where(['delete_time'=>0])->where($map)->order('id desc')->select();
2020-01-01 13:17:19 +08:00
$count = $user->count();
$res = [];
2020-02-13 12:15:35 +08:00
if($count){
2020-02-13 13:18:17 +08:00
$res = ['code'=>0,'msg'=>'','count'=>$count];
2020-01-01 13:17:19 +08:00
foreach($user as $k => $v){
2020-01-14 15:47:11 +08:00
$data = ['id'=>$v['id'],'username'=>$v['name'],'avatar'=>$v['user_img'],'phone'=>$v['phone'],'email'=>$v['email'],'sex'=>$v['sex'],'ip'=>$v['last_login_ip'],'jointime'=>date("Y-m-d",$v['create_time']),'check'=>$v['status'],'auth'=>$v['auth']];
2020-02-13 13:18:17 +08:00
$res['data'][] = $data;
2020-01-01 13:17:19 +08:00
}
2020-02-13 12:15:35 +08:00
} else {
$res = ['code'=>-1,'msg'=>'没有查询结果!'];
2020-01-01 13:17:19 +08:00
}
return json($res);
}
return View::fetch();
}
//添加用户
public function userForm()
{
//
if(Request::isAjax()){
$data = Request::param();
$result = Db::name('user')->save($data);
if($result){
2020-01-14 15:47:11 +08:00
$res = ['code'=>0,'msg'=>'添加成功'];
2020-01-01 13:17:19 +08:00
}else{
2020-01-14 15:47:11 +08:00
$res = ['code'=>-1,'msg'=>'添加失败'];
2020-01-01 13:17:19 +08:00
}
return json($res);
}
return View::fetch('userform');
}
//编辑用户
public function userEdit()
{
if(Request::isAjax()){
$data = Request::param();
$result = Db::name('user')->update($data);
if($result){
2020-01-14 15:47:11 +08:00
$res = ['code'=>0,'msg'=>'编辑成功'];
2020-01-01 13:17:19 +08:00
}else{
2020-01-14 15:47:11 +08:00
$res = ['code'=>-1,'msg'=>'编辑失败'];
2020-01-01 13:17:19 +08:00
}
return json($res);
}
$user = Db::name('user')->find(input('id'));
View::assign('user',$user);
return View::fetch('useredit');
}
//删除用户
public function delete($id)
{
if(Request::isAjax()){
$user =UserModel::find($id);
$result = $user->delete();
if($result){
2020-01-14 15:47:11 +08:00
return json(['code'=>0,'msg'=>'删除成功']);
2020-01-01 13:17:19 +08:00
}else{
2020-01-14 15:47:11 +08:00
return json(['code'=>-1,'msg'=>'删除失败']);
2020-01-01 13:17:19 +08:00
}
}
}
//上传头像
public function uploadImg()
{
$file = request()->file('file');
try {
validate(['image'=>'filesize:2048|fileExt:jpg,png,gif|image:200,200,jpg'])
->check(array($file));
$savename = \think\facade\Filesystem::disk('public')->putFile('head_pic',$file);
} catch (think\exception\ValidateException $e) {
echo $e->getMessage();
}
$upload = Config::get('filesystem.disks.public.url');
if($savename){
$name_path =str_replace('\\',"/",$upload.'/'.$savename);
$res = ['code'=>0,'msg'=>'上传头像成功','src'=>$name_path];
} else {
2020-01-14 15:47:11 +08:00
$res = ['code'=>-1,'msg'=>'上传错误'];
2020-01-01 13:17:19 +08:00
}
return json($res);
}
2020-01-14 15:47:11 +08:00
//审核用户
public function check()
{
$data = Request::param();
//获取状态
$res = Db::name('user')->where('id',$data['id'])->save(['status' => $data['status']]);
if($res){
if($data['status'] == 1){
return json(['code'=>0,'msg'=>'用户审核通过','icon'=>6]);
} else {
return json(['code'=>0,'msg'=>'禁用用户','icon'=>5]);
}
}else {
return json(['code'=>-1,'msg'=>'审核出错']);
}
}
//超级管理员
public function auth()
{
$data = Request::param();
$user = Db::name('user')->save($data);
if($user){
if($data['auth'] == 1){
return json(['code'=>0,'msg'=>'设置为超级管理员','icon'=>6]);
} else {
return json(['code'=>0,'msg'=>'取消超级管理员','icon'=>5]);
}
}else{
$res = ['code'=>-1,'msg'=>'前台管理员设置失败'];
}
return json($res);
}
2020-01-01 13:17:19 +08:00
//退出登陆
public function logout()
{
Session::clear();
$res = ['code'=>0,'msg'=>'退出成功' ];
return json($res);
}
}