TaoLer/app/admin/controller/Admin.php

235 lines
6.4 KiB
PHP
Raw Normal View History

2020-01-01 13:17:19 +08:00
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019-10-15
* Time: 15:40
*/
namespace app\admin\controller;
use app\common\controller\AdminController;
use app\admin\validate\Admin as AdminValidate;
use app\admin\model\Admin as AdminModel;
use think\facade\View;
use think\facade\Request;
use think\facade\Db;
use think\facade\Session;
2021-03-17 18:19:32 +08:00
use think\facade\Cookie;
2020-01-01 13:17:19 +08:00
use think\exception\ValidateException;
use app\common\model\User as UserModel;
2020-03-05 20:15:01 +08:00
use taoler\com\Files;
2020-01-01 13:17:19 +08:00
class Admin extends AdminController
{
//管理员
2020-01-09 18:03:33 +08:00
public function index()
2020-01-01 13:17:19 +08:00
{
if(Request::isAjax()){
2020-04-02 23:41:48 +08:00
$data = Request::only(['id','username','mobile','email']);
2020-02-21 19:39:08 +08:00
$map = array_filter($data);
2020-01-01 13:17:19 +08:00
$admins = Db::name('admin')
2020-04-02 23:41:48 +08:00
->field('id,username,mobile,email,last_login_ip,status,last_login_time')
->where('delete_time',0)
2020-02-21 19:39:08 +08:00
->where($map)
2020-01-01 13:17:19 +08:00
->select();
$count = $admins->count();
2020-02-21 19:39:08 +08:00
if($count){
2020-01-01 13:17:19 +08:00
$res = ['code'=>0,'msg'=>'','count'=>$count];
foreach($admins as $k => $v){
2020-04-02 23:41:48 +08:00
$data = ['id'=>$v['id'],'loginname'=>$v['username'],'telphone'=>$v['mobile'],'email'=>$v['email'],'ip'=>$v['last_login_ip'],'check'=>$v['status'],'logintime'=>date("Y-m-d",$v['last_login_time'])];
2020-01-01 13:17:19 +08:00
$res['data'][] = $data;
}
2020-02-21 19:39:08 +08:00
} else {
$res = ['code'=>-1,'msg'=>'没有查询结果!'];
2020-01-01 13:17:19 +08:00
}
return json($res);
}
2020-01-09 18:03:33 +08:00
return View::fetch();
2020-01-01 13:17:19 +08:00
}
//管理员审核
2020-01-09 18:03:33 +08:00
public function check()
2020-01-01 13:17:19 +08:00
{
$data = Request::param();
//获取状态
$res = Db::name('admin')->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 {
2020-01-14 15:47:11 +08:00
return json(['code'=>-1,'msg'=>'审核出错']);
2020-01-01 13:17:19 +08:00
}
}
//添加管理员
2020-01-09 18:03:33 +08:00
public function add()
2020-01-01 13:17:19 +08:00
{
if(Request::isAjax()){
$data = Request::param();
$data['create_time'] = time();
$salt = substr(md5($data['create_time']),-6);
2020-09-23 12:28:49 +08:00
$data['password'] = md5(substr_replace(md5($data['password']),$salt,0,6));
$data['status'] = 1;
2020-04-02 23:41:48 +08:00
//$adminId = Db::name('admin')->insertGetId($data);
$admin = Db::name('admin')->save($data);
//Db::name('auth_group_access')->insert(['uid'=>$adminId,'group_id'=>$data['auth_group_id']]);
if($admin){
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);
}
2020-04-02 23:41:48 +08:00
//$auth_group = Db::name('auth_group')->select();
//View::assign(['auth_group'=>$auth_group]);
2020-01-09 18:03:33 +08:00
return View::fetch();
2020-01-01 13:17:19 +08:00
}
//管理员编辑
2020-02-21 13:14:55 +08:00
public function edit($id)
2020-01-01 13:17:19 +08:00
{
2020-02-21 13:14:55 +08:00
$admin = AdminModel::find($id);
2020-01-01 13:17:19 +08:00
if(Request::isAjax()){
$data = Request::param();
if(empty($data['password'])){
unset($data['password']);
} else {
$t = strtotime($admin['create_time']);
$salt = substr(md5($t),-6);
$data['password'] = md5(substr_replace(md5($data['password']),$salt,0,6));
}
$data['update_time'] = time();
$result = $admin->update($data);
2020-04-02 23:41:48 +08:00
//Db::name('auth_group_access')->where('uid',$data['id'])->update(['group_id'=>$data['auth_group_id']]);
2020-01-01 13:17:19 +08:00
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);
}
2020-04-02 23:41:48 +08:00
//$auth_group = Db::name('auth_group')->select();,'auth_group'=>$auth_group
View::assign(['admin'=>$admin]);
2020-01-01 13:17:19 +08:00
return View::fetch();
}
//删除管理员
public function delete($id)
{
if(Request::isAjax()){
$user =AdminModel::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
}
}
}
2020-04-07 17:11:12 +08:00
//基本资料显示
2020-01-01 13:17:19 +08:00
public function info()
2020-04-07 17:11:12 +08:00
{
$admin = AdminModel::find(Session::get('admin_id'));
$auths = $admin->adminGroup;
$authname = [];
foreach($auths as $v){
$authname[] = $v->title;
}
$authGroupTitle = implode('|', $authname);
View::assign(['admin'=>$admin,'authGroupTitle'=>$authGroupTitle]);
2020-04-07 17:11:12 +08:00
return View::fetch('set/user/info');
}
//管理员资料更新
public function infoSet()
2020-01-01 13:17:19 +08:00
{
$admin = AdminModel::find(Session::get('admin_id'));
if(Request::isAjax()){
2020-01-14 15:47:11 +08:00
$data = Request::only(['nickname','sex','mobile','email','remarks']);
$result = $admin->save($data);
2020-01-01 13:17:19 +08:00
if($result){
$res = ['code'=>0,'msg'=>'更新成功'];
} 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-04-07 17:11:12 +08:00
//显示改密码页面
2020-01-01 13:17:19 +08:00
public function repass()
{
2020-04-07 17:11:12 +08:00
$admin = AdminModel::find(Session::get('admin_id'));
View::assign('admin',$admin);
return View::fetch('set/user/repass');
}
//密码重设
public function repassSet()
{
2020-01-01 13:17:19 +08:00
$admin = AdminModel::find(Session::get('admin_id'));
if(Request::isAjax()){
$data = Request::param();
$salt = substr(md5(strtotime($admin['create_time'])),-6);
$pwd = substr_replace(md5($data['oldPassword']),$salt,0,6);
$data['oldPassword'] = md5($pwd);
if($admin['password'] != $data['oldPassword']){
2020-01-14 15:47:11 +08:00
return json(['code'=>-1,'msg'=>'当前密码错误']);
2020-01-01 13:17:19 +08:00
} elseif($data['password'] != $data['repassword']){
2020-01-14 15:47:11 +08:00
return json(['code'=>-1,'msg'=>'两次密码不一致']);
2020-01-01 13:17:19 +08:00
} else {
$password = md5(substr_replace(md5($data['password']),$salt,0,6));
$result = $admin->update([
'id' => $admin['id'],
'password' => $password
]);
if($result){
$res = ['code'=>0,'msg'=>'更新成功'];
} else {
2020-01-14 15:47:11 +08:00
$res = ['code'=>-1,'msg'=>'更新失败'];
2020-01-01 13:17:19 +08:00
}
2020-01-14 15:47:11 +08:00
return json($res);
2020-01-01 13:17:19 +08:00
}
2020-04-07 17:11:12 +08:00
}
2020-01-01 13:17:19 +08:00
}
2020-03-05 20:15:01 +08:00
//清除缓存Cache
public function clearCache(){
2020-04-25 16:48:59 +08:00
//$atemp = app()->getRootPath().'runtime/admin/temp/';
//$itemp = app()->getRootPath().'runtime/index/temp/';
//$cache = app()->getRootPath().'runtime/cache/';
$atemp = str_replace('\\',"/",app()->getRootPath().'runtime/admin/temp/');
$itemp = str_replace('\\',"/",app()->getRootPath().'runtime/index/temp/');
$cache = str_replace('\\',"/",app()->getRootPath().'runtime/cache/');
2020-03-05 20:15:01 +08:00
Files::delDirAndFile($atemp);
Files::delDirAndFile($itemp);
if(is_dir($cache) && Files::delDirAndFile($cache)){
return json(['code'=>0,'msg'=>'清除缓存成功']);
} else {
return json(['code'=>-1,'msg'=>'清除缓存失败']);
}
}
2020-04-25 16:48:59 +08:00
//退出登陆
public function logout()
{
2021-03-17 18:19:32 +08:00
//清空缓存
Cookie::delete('adminAuth');
2020-04-25 16:48:59 +08:00
Session::clear();
2021-03-17 18:19:32 +08:00
return json(['code'=>0,'msg'=>'退出成功' ]);
2020-04-25 16:48:59 +08:00
}
2020-01-01 13:17:19 +08:00
}