更新后台

This commit is contained in:
mkm 2023-01-18 17:25:49 +08:00
parent 5b025e342b
commit 18b77ae556

View File

@ -0,0 +1,160 @@
<?php
namespace app\admin\controller\nk;
use app\admin\BaseController;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;
/**
* 文章
*
* @icon fa fa-circle-o
*/
class Article extends BaseController
{
public function __construct()
{
$this->adminInfo = get_login_admin();
}
public function index($params)
{
$where=[
'status'=>1
];
if (isset($params['keywords'])){
$where[]=['title','like','%'.$params['keywords'].'%'];
}
if($this->adminInfo['position_id'] != 1){ //不是超级管理员
$www['admin_id'] = $this->adminInfo['id'];
$user_address = Db::table('fa_szxc_information_useraddress')->where($www)->find();
if ($user_address){
if($user_address['auth_range'] == 1){
$where['village'] = $user_address['village_id'];
}elseif ($user_address['auth_range'] == 2){
$where['township'] = $user_address['street_id'];
}elseif ($user_address['auth_range'] == 3){
$where['county'] = $user_address['area_id'];
}else{
$where['village'] = $user_address['village_id'];
}
}else{
$where['village'] = '';
}
}
$category_id =$params['category_id'];
if($category_id){
$map[] = ['category_id','in',$category_id];
}else{
$map = [];
}
$total = Db::table('fa_article')
->where($where)
->where($map)
->count();
$list = Db::table('fa_article')
->withAttr('nickname',function ($value,$data){
return Db::table('fa_szxc_information_usermsg')->where('user_id',$data['user_id'])->value('name');
})
->withAttr('area',function ($value,$data){
return Db::table('fa_geo_area')->where('area_code',$data['county'])->value('area_name');
})
->withAttr('street',function ($value,$data){
return Db::table('fa_geo_street')->where('street_code',$data['township'])->value('street_name');
})
->withAttr('village',function ($value,$data){
return Db::table('fa_geo_village')->where('village_id',$data['village'])->value('village_name');
})
->where($where)
->where($map)
->page($params['page'])
->limit($params['limit'])
->field('id,title,user_id,county,township,village,image,view_time')
->select();
$result = ['total' => $total, 'data' => $list];
return table_assign(0, '', $result);
}
public function add($param){
// 检验完整性
try {
validate(\app\admin\validate\party\Article::class)->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(1, $e->getError());
}
$adds=Db::table('fa_szxc_information_useraddress')->where('admin_id',$this->adminInfo['id'])->find();
$param['view_time']=date('Y-m-d H:i:s');
$param['county']=$adds['area_id'];
$param['township']=$adds['street_id'];
$param['village']=$adds['village_id'];
$param['user_id']=$adds['user_id'];
$res=Db::table('fa_article')->strict(false)->field(true)->insertGetId($param);
if ($res){
return to_assign(0,'操作成功',['aid'=>$res]);
}
return to_assign(1, '操作失败,原因:'.$res);
}
public function edit($param){
if (request()->isAjax()) {
try {
validate(\app\admin\validate\party\Article::class)->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(1, $e->getError());
}
$res=Db::table('fa_article')->where('id',$param['id'])->strict(false)->field(true)->update($param);
if ($res){
return to_assign();
}else{
return to_assign(1, '操作失败,原因:'.$res);
}
}else{
$id = isset($param['id']) ? $param['id'] : 0;
$detail = Db::table('fa_article')->where('id',$id)->find();
View::assign('editor', get_system_config('other','editor'));
if (!empty($detail)) {
View::assign('detail', $detail);
}
else{
throw new \think\exception\HttpException(404, '找不到页面');
}
}
}
/**
* 查看信息
*/
public function read($param)
{
$id = isset($param['id']) ? $param['id'] : 0;
$detail = Db::table('fa_article')->where('id',$id)->find();
if (!empty($detail)) {
View::assign('detail', $detail);
}
else{
throw new \think\exception\HttpException(404, '找不到页面');
}
}
/**
* 删除
*/
public function del($param)
{
$id = isset($param['id']) ? $param['id'] : 0;
$type = isset($param['type']) ? $param['type'] : 0;
$res = Db::table('fa_article')->where('id',$id)->update(['status'=>$type]);
if ($res){
return to_assign();
}else{
return to_assign(1, '操作失败,原因:'.$res);
}
}
}