2023-01-30 17:45:55 +08:00

175 lines
6.2 KiB
PHP

<?php
namespace app\admin\controller\nk\party;
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();
$this->url=[
'/admin/nk.party.article/index',
'/admin/nk.party.article/add',
'/admin/nk.party.article/edit',
'/admin/nk.party.article/del',
'/admin/nk.party.article/read',
];
}
public function index()
{
if (request()->isAjax()) {
$params= get_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'] = '';
}
}
$total = Db::table('fa_szxc_party_article')
->where($where)
->count();
$list = Db::table('fa_szxc_party_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)
->page($params['page'])
->limit($params['limit'])
->order('id desc')
->field('id,title,user_id,county,township,village,image,view_time')
->select();
$result = ['total' => $total, 'data' => $list];
return table_assign(0, '', $result);
}
else{
return view('nk/party/article/index',['url'=>$this->url]);
}
}
public function add(){
if (request()->isAjax()) {
$param= get_params();
// 检验完整性
try {
validate(\app\admin\validate\nk\ArticleValidate::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_szxc_party_article')->strict(false)->field(true)->insertGetId($param);
if ($res){
return to_assign(0,'操作成功',['aid'=>$res]);
}
return to_assign(1, '操作失败,原因:'.$res);
}else{
View::assign('editor', get_system_config('other','editor'));
return view();
}
}
public function edit(){
$param= get_params();
if (request()->isAjax()) {
try {
validate(\app\admin\validate\nk\ArticleValidate::class)->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(1, $e->getError());
}
$res=Db::table('fa_szxc_party_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_szxc_party_article')->where('id',$id)->find();
View::assign('editor', get_system_config('other','editor'));
if (!empty($detail)) {
View::assign('detail', $detail);
return view();
}
else{
throw new \think\exception\HttpException(404, '找不到页面');
}
}
}
/**
* 查看信息
*/
public function read()
{
$param= get_params();
$id = isset($param['id']) ? $param['id'] : 0;
$detail = Db::table('fa_szxc_party_article')->where('id',$id)->find();
if (!empty($detail)) {
View::assign('detail', $detail);
return view();
}
else{
throw new \think\exception\HttpException(404, '找不到页面');
}
}
/**
* 删除
*/
public function del()
{
$param= get_params();
$id = isset($param['id']) ? $param['id'] : 0;
$type = isset($param['type']) ? $param['type'] : 0;
$res = Db::table('fa_szxc_party_article')->where('id',$id)->update(['status'=>$type]);
if ($res){
return to_assign();
}else{
return to_assign(1, '操作失败,原因:'.$res);
}
}
}