adminInfo = get_login_admin(); // $this->pid = 361; $this->category_id = 362; $this->url=[ '/admin/nk.culturatour/index?category_id='.$this->category_id, '/admin/nk.culturatour/add', '/admin/nk.culturatour/edit', '/admin/nk.culturatour/del', '/admin/nk.culturatour/read', ]; } /** * 文章列表 */ function Index(){ if (request()->isAjax()) { $params= get_params(); $params['category_id']=$this->category_id; $where = ['category_id'=>$this->category_id]; $list = Db::table('fa_article')->field('id')->where($where) ->page(1) ->limit(1) ->order('id desc') ->select(); (new Article())->index($params); } return view('nk/article/index',['url'=>$this->url]); } /** * 查看文章 【无关联表】 */ function Read(){ $params = get_params(); (new Article())->read($params); return view('nk/article/read',['url'=>$this->url]); } /** * 添加文章【无关联表】 */ function Add(){ if (request()->isAjax()) { $params= get_params(); $params['category_id']=$this->category_id; // unset($params['amp;']); (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); return view('nk/article/add'); } } /** * 编辑文章 【无关联表】 */ function Edit(){ $params= get_params(); (new Article())->edit($params); return view('nk/article/edit',['url'=>$this->url]); } /** * 删除文章【无关联表】 */ function Del(){ $params= get_params(); (new Article())->del($params); } /** * 添加文章与关联表数据. * 【有关联表】 */ function addArticle($param, $callback){ // 检验完整性 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'); } Db::startTrans(); try{ // 新建文章 $aid = Db::table('fa_article')->strict(false)->field(true)->insertGetId($param); // 新建文章相关表的信息 $pid = $callback($aid); }catch(ExceptionDbException $e){ Db::rollback(); return to_assign(1, '操作失败,原因:'.$e->getMessage()); } if (!empty($aid) && !empty($pid)) { Db::commit(); 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'=>$aid,'county'=>$adds['area_id'],'township'=>$adds['street_id'],'village'=>$adds['village_id']]; Db::table('fa_article_vote_side_tables')->insert($data); } return to_assign(0,'操作成功',['aid'=>$aid]); } return to_assign(1, '操作失败,原因:'.$aid); } /** * 1. 获取编辑页展示文章与关联表数据. * 【有关联表】 */ function getEditData($param, $id, $callback){ // 获取指定文章 $detail = Db::table('fa_article')->where('id',$id)->withAttr('lesson', $callback )->find(); View::assign('editor', get_system_config('other','editor')); // 获取此文章的用户信息 if (!empty($detail)) { 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); } else{ throw new \think\exception\HttpException(404, '找不到页面'); } } /** * 2. ajax提交编辑更新文章与关联表数据. * 【有关联表】 */ function updateEditData($param, $id, $callback){ 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',$id)->strict(false)->field(true)->update($param); // 更新文章相关的扩展信息 $callback($param); if ($res){ // 更新文章相关的投票信息 if(!empty($param['is_vote']) && $param['is_vote']==1){ Db::table('fa_article_vote_side_tables')->where('article_id',$id)->update(['end_time'=>$param['end_time']]); } return to_assign(); }else{ return to_assign(1, '操作失败,原因:'.$res); } } /** * 文章与关联表删除 * 【有关联表】 */ function delArticle($id, $callback){ // 此处同时删除两个表 Db::startTrans(); try{ $res1 = Db::table('fa_article')->where('id',$id)->delete(); $pid = $callback($id); }catch (ExceptionDbException $e){ Db::rollback(); $res1 = $pid = ''; return ["code"=>1, "msg"=>'操作失败,原因:'.$e->getMessage()]; } if ($res1 && $pid){ Db::commit(); return ["code"=>0, "msg"=>'操作成功']; }else{ return ["code"=>1, "msg"=>'操作失败,原因:两表一起删除失败[主表:'.$res1.'][联表:'.$pid.']']; } } }