2023-02-27 16:47:42 +08:00

232 lines
9.1 KiB
PHP

<?php
namespace app\admin\controller\nk;
use app\admin\BaseController;
use think\exception\ValidateException;
use think\facade\Db;
use app\admin\controller\nk\Article;
use think\facade\View;
/**
* 文章
*
* @icon fa fa-circle-o
*/
class Life extends BaseController
{
public function __construct()
{
$this->adminInfo = get_login_admin();
$this->category_id=360;
$this->url=[
'/admin/nk.life/index?category_id='.$this->category_id,
'/admin/nk.life/add',
'/admin/nk.life/edit',
'/admin/nk.life/del',
'/admin/nk.life/read',
];
}
/**
* 查看
*/
public function index()
{
if (request()->isAjax()) {
$params= get_params();
$params['category_id'] = Db::table('fa_category')->where(['status' => 'normal', 'pid' => '360'])->column('id');
(new Article())->index($params);
}
return view('nk/life/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();
if($this->adminInfo['position_id'] != 1) { //不是超级管理员
$param['county']=$adds['area_id'];
$param['township']=$adds['street_id'];
$param['village']=$adds['village_id'];
$param['user_id']=$adds['user_id'];
}else{
if(empty($param['county'])){
$param['county']=$adds['area_id'];
}
if(empty($param['township'])){
$param['township']=$adds['street_id'];
}
if(empty($param['village'])){
$param['village']=$adds['village_id'];
}
if(empty($param['user_id'])){
$param['user_id']=$adds['user_id'];
}
}
$param['view_time']=date('Y-m-d H:i:s');
if(empty($param['category_type'])){
$param['category_type'] = 0;
}
if(empty($param['end_time'])){
$param['end_time'] = date('Y-m-d H:i:s');
}
$res=Db::table('fa_article')->strict(false)->field(true)->insertGetId($param);
if ($res){
if(!empty($param['is_vote']) && $param['is_vote']==1){
$data=['start_time'=>date('Y-m-d H:i:s'),'end_time'=>$param['end_time'],'article_id'=>$res,'county'=>$adds['area_id'],'township'=>$adds['street_id'],'village'=>$adds['village_id']];
Db::table('fa_article_vote_side_tables')->insert($data);
}
// 写入关联表
$life_data['article_id'] = $res;
$life_data['price'] = $param['price'];
$life_data['star'] = rtrim($param['star'],'星');
$life_data['lat'] = $param['lat'];
$life_data['lng'] = $param['lng'];
Db::table('fa_article_life')->strict(false)->field(true)->insert($life_data);
return to_assign(0,'操作成功',['aid'=>$res]);
}
return to_assign(1, '操作失败,原因:'.$res);
// (new Article())->add($params);
}else{
View::assign('editor', get_system_config('other','editor'));
View::assign('url', $this->url);
// 获取用户信息
$this->users = Db::table('fa_szxc_information_usermsg')->where('status',1)->field('user_id,name')->select();
View::assign('users', $this->users);
$street = Db::table('fa_geo_area')->where(['switch' => 1, 'city_code' => '510500'])
->field('area_id id,area_code code,area_name name')
->select();
View::assign('street', $street);
// 分类
$category = Db::table('fa_category')->where(['status' => 'normal', 'pid' => '360'])->select();
View::assign('category', $category);
return view('nk/life/add');
}
}
/**
* 修改
*/
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());
}
$adds=Db::table('fa_szxc_information_useraddress')->where('admin_id',$this->adminInfo['id'])->find();
if($this->adminInfo['position_id'] == 1) { //是超级管理员
if(empty($param['county'])){
$param['county']=$adds['area_id'];
}
if(empty($param['township'])){
$param['township']=$adds['street_id'];
}
if(empty($param['village'])){
$param['village']=$adds['village_id'];
}
if(empty($param['user_id'])){
$param['user_id']=$adds['user_id'];
}
}
$res=Db::table('fa_article')->where('id',$param['id'])->strict(false)->field(true)->update($param);
if(!empty($param['is_vote']) && $param['is_vote']==1){
Db::table('fa_article_vote_side_tables')->where('article_id',$param['id'])->update(['end_time'=>$param['end_time']]);
}
// 写入关联表
$life_data['price'] = $param['price'];
$life_data['star'] = rtrim($param['star'],'星');
$life_data['lat'] = $param['lat'];
$life_data['lng'] = $param['lng'];
Db::table('fa_article_life')->where('article_id',$param['id'])->update($life_data);
return to_assign();
}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)) {
$ress = Db::table('fa_article_life')->where('article_id',$param['id'])->find();
$detail['price'] = $ress['price'];
$detail['star'] = $ress['star'];
$detail['lat'] = $ress['lat'];
$detail['lng'] = $ress['lng'];
View::assign('detail', $detail);
// 获取用户信息
$this->users = Db::table('fa_szxc_information_usermsg')->where('status',1)->field('user_id,name')->select();
View::assign('users', $this->users);
$street = Db::table('fa_geo_area')->where(['switch' => 1, 'city_code' => '510500'])
->field('area_id id,area_code code,area_name name')
->select();
View::assign('street', $street);
// 分类
$category = Db::table('fa_category')->where(['status' => 'normal', 'pid' => '360'])->select();
View::assign('category', $category);
return view('nk/life/edit',['url'=>$this->url]);
}
else{
throw new \think\exception\HttpException(404, '找不到页面');
}
}
}
/**
* 查看信息
*/
public function read()
{
$param = get_params();
$id = isset($param['id']) ? $param['id'] : 0;
$detail = Db::table('fa_article')->where('id',$id)->find();
if (!empty($detail)) {
$detail['comment'] = Db::table('fa_article_comment')
->where('vote_id',$id)
->withAttr('user_info',function ($value,$data){
return Db::table('fa_szxc_information_usermsg')->where('user_id',$data['user_id'])->value('name');
})
->select();
$ress = Db::table('fa_article_life')->where('article_id',$param['id'])->find();
$detail['price'] = $ress['price'];
$detail['star'] = $ress['star'];
$detail['lat'] = $ress['lat'];
$detail['lng'] = $ress['lng'];
View::assign('detail', $detail);
// 分类
$category = Db::table('fa_category')->where(['status' => 'normal', 'pid' => '360'])->select();
View::assign('category', $category);
View::assign('admin_id', $this->adminInfo['id']);
}
else{
throw new \think\exception\HttpException(404, '找不到页面');
}
return view('nk/life/read',['url'=>$this->url]);
}
/**
* 修改
*/
public function del()
{
$params= get_params();
(new Article())->del($params);
}
}