| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 |
- <?php
- /**
- * 新闻列表控制器
- */
- namespace app\admin\controller;
- use think\Db;
- use app\common\library\FileUpload;
- use think\Config;
- class Article extends Admin
- {
- protected function _initialize()
- {
- parent::_initialize();
- $this->articleModel = Db::name('cy_article');
- $this->contentModel = Db::name('cy_content');
- }
- public function index()
- {
- $where = [];
- $start_time = input('request.start_time');
-
- // 类型
- if (input('request.type') != '') {
- $where['a.type'] = ['=', input('request.type')];
- }
- // 标题
- if (input('request.title') != '') {
- $where['a.title'] = ['like', '%' . input('request.title') .'%'];
- }
- // 显示位置
- if (input('request.position') != '') {
- $where['a.position'] = ['like', '%' . input('request.position') .'%'];
- }
-
-
- // 游戏
- if (input('request.gameid') != '') {
- $where['a.gameid'] = input('request.gameid');
- }
-
- //开始时间和结束时间不为空时
- if ($start_time != '' && input('request.end_time') != '') {
- $where['a.create_time'] = [
- ['>=', strtotime($start_time)],
- ['<=', strtotime(input('request.end_time').' 23:59:59')],
- ];
- } //开始时间不为空时
- elseif ($start_time!= '') {
- $where['a.create_time'] = ['>=', strtotime($start_time)];
- } //结束时间不为空时
- elseif (input('request.end_time') != '') {
- $where['a.create_time'] = ['<=', strtotime(input('request.end_time').' 23:59:59')];
- }
- $where['a.isdelete'] = 0;
- $param = input('get.');
- $article= $this->articleModel
- ->alias('a')
- ->join('cy_game g', 'a.gameid=g.id', 'left')
- ->field('a.*,g.id as gameid2,g.name as game_name')
- ->where($where)
- ->order('a.id desc')->paginate(10, false, array('query' => $param));
- $gameList = Db::name('cy_game')->field('id,name')->order('name asc')->select();
- $this->assign('game_list', $gameList);
- $this->assign('article', $article);
- $this->assign('page', $article->render());
- return $this->fetch();
- }
- public function add()
- {
- if (request()->isPost()) {
-
- $fileUpload = new FileUpload();
-
- //新闻图片
- $image_url = $fileUpload->set('allowExt', 'jpg,jpeg,png,gif')->set('maxsize', 1024000)->set('dir','image/news/pic/')->upload(request()->file('image'));
- $image_error = $fileUpload->getError();
-
- $data = [
- 'title' => input('post.title'),
- 'gameid' => input('post.gameid',0,'int'),
- 'zhiding' => input('post.top'),
- 'type' => input('post.type'),
- 'content' => input('post.content'),
- 'create_time' => NOW_TIMESTAMP
- ];
- $result = $this->validate($data, [
- ['title', 'require|max:100', '标题不能为空|标题不能超过100个字符'],
- ['gameid', 'require', '游戏不能为空'],
- ['zhiding', 'require', '是否置顶不能为空'],
- ['type', 'require|integer', '类型不能为空|类型必须为整型'],
- ['content', 'require', '内容不能为空'],
- ]);
- if (true !== $result) {
- $this->error($result);
- }
- //新闻图片上传出错时
- elseif(!empty(request()->file('image')) && empty($image_url))
- {
- $this->error('新闻图片'.$image_error);
- }
- else {
- unset($data['content']);
-
- if ( !empty(input('post.position/a'))) {
- $data['position'] = implode('',input('post.position/a'));
- }
-
- //有上传新闻图片时
- if(!empty($image_url))
- $data['image'] = $image_url;
- if(in_array($data['type'],[1,2,3,4,5]) && empty($image_url)){
- $this->error('请选择新闻图片');
- }
-
- $id = $this->articleModel->insertGetId($data);
- if (empty($id)) {
- $this->error('添加失败!');
- }
-
- $this->contentModel->insert(['id'=>$id,'content'=>input('post.content')]);
- $this->success('新闻添加成功!', 'article/index');
- }
- }
- $gameList = Db::name('cy_game')->field('id,name')->order('name asc')->select();
- $this->assign('game_list', $gameList);
- return $this->fetch();
- }
- public function edit($id)
- {
- $id = (int)$id;
- if (empty($id)) {
- $this->error('资讯ID不能为空');
- }
- $articleInfo = $this->articleModel->where(['id' => $id])->find();
- if (empty($articleInfo)) {
- $this->error('资讯信息不能为空! ');
- }
- if (request()->isPost()) {
-
- $fileUpload = new FileUpload();
-
- //新闻图片
- $image_url = $fileUpload->set('allowExt', 'jpg,jpeg,png,gif')->set('maxsize', 1024000)->set('dir','image/news/pic')->upload(request()->file('image'));
- $image_error = $fileUpload->getError();
-
- $data = [
- 'title' => input('post.title'),
- 'gameid' => input('post.gameid',0,'int'),
- 'zhiding' => input('post.top'),
- 'type' => input('post.type'),
- 'content' => input('post.content'),
- ];
- $result = $this->validate($data, [
- ['title', 'require|max:100', '标题不能为空|标题不能超过100个字符'],
- ['gameid', 'require', '游戏不能为空'],
- ['zhiding', 'require', '是否置顶不能为空'],
- ['type', 'require|integer', '类型不能为空|类型必须为整型'],
- ['content', 'require', '内容不能为空'],
- ]);
- if (true !== $result) {
- $this->error($result);
- }
- //新闻图片上传出错时
- elseif(!empty(request()->file('image')) && empty($image_url))
- {
- $this->error('新闻图片'.$image_error);
- }
- else {
- unset($data['content']);
-
- if ( !empty(input('post.position/a'))) {
- $data['position'] = implode('',input('post.position/a'));
- }else{
- $data['position'] = null;
- }
-
- //有上传新闻图片时
- if(!empty($image_url))
- $data['image'] = $image_url;
-
- if(in_array($data['type'],[1,2,3,4,5]) && empty($image_url) && !$articleInfo['image']){
- $this->error('请选择新闻图片');
- }
- $this->articleModel->where(['id'=>$id])->update($data);
-
- $this->contentModel->where(['id'=>$id])->update(['content'=>input('post.content')]);
- model('GameActivityRead')->where('game_article_id',$id)->delete();
- $this->success('新闻编辑成功!', 'article/index');
- }
- }
-
- $content = $this->contentModel->where(['id' => $id])->value('content');
- $gameList = Db::name('cy_game')->field('id,name')->order('name asc')->select();
- $this->assign('articleInfo', $articleInfo);
- $this->assign('content', $content);
- $this->assign('game_list', $gameList);
- return $this->fetch();
- }
- public function del()
- {
- $id = input('id', 0, 'intval');
- if(empty($id)) $this->error('记录ID不能为空');
- $ret = $this->articleModel->where([
- 'id' => $id,
- ])->setField('isdelete', 1);
- if (!$ret) {
- $this->error('删除错误!');
- }
- $this->success('删除成功', url('index'));
- }
- /**
- * 微信公众号-文章列表
- */
- public function wxArticleList(){
- $where = [];
- // 标题
- if (input('request.title') != '') {
- $where['title'] = ['like', '%' . input('request.title') .'%'];
- }
- $start = $this->request->param('start_time');
- $end = $this->request->param('end_time');
- //开始时间和结束时间不为空时
- if ($start != '' && $end != '') {
- $where['create_time'] = [
- ['>=', strtotime($start)],
- ['<=', strtotime($end . ' 23:59:59')],
- ];
- } //开始时间不为空时
- elseif ($start != '') {
- $where['create_time'] = ['>=', strtotime($start)];
- } //结束时间不为空时
- elseif ($end != '') {
- $where['create_time'] = ['<=', strtotime($end . ' 23:59:59')];
- }
- $wechatDb = Config::get('db_config_wechat.database'); // 微信公众号数据库
- $where['is_del'] = 0;
- $infoList = Db::table($wechatDb . '.nw_wx_article')
- ->field('*')
- ->where($where)
- ->order('create_time', 'desc')
- ->paginate(10, false, ['query' => input('get.')]);
- $this->assign('article', $infoList);
- $this->assign('page', $infoList->render());
- $this->assign('wxlink',Config::get('WX_ARTICLE_LINK'));
- return $this->fetch('wx_article_list');
- }
- /**
- * 微信公众号-文章 添加
- */
- public function wxArticleAdd(){
- if (request()->isPost()){
- $fileUpload = new FileUpload();
- //新闻图片
- $image_url = $fileUpload->set('allowExt', 'jpg,jpeg,png,gif')->set('maxsize', 1024000 *3)->set('dir','image/wechat/pic/')->upload(request()->file('image'));
- $image_error = $fileUpload->getError();
- $data = [
- 'title' => input('post.title'),
- 'releaser' => input('post.releaser') ? input('post.releaser') : '祈盟网络游戏',
- 'content' => input('post.content'),
- 'create_time' => NOW_TIMESTAMP
- ];
- $result = $this->validate($data, [
- ['title', 'require', '标题不能为空'],
- ['releaser', 'require', '发布人不能为空'],
- ['content', 'require', '内容不能为空'],
- ]);
- if (true !== $result) {
- $this->error($result);
- }
- //新闻图片上传出错时
- elseif(!empty(request()->file('image')) && empty($image_url))
- {
- $this->error('新闻图片'.$image_error);
- }
- //有上传新闻图片时
- if(!empty($image_url))
- $data['image'] = $image_url;
- $wechatDb = Config::get('db_config_wechat.database'); // 微信公众号数据库
- $shortLink = strtolower(random(8));
- while (Db::table($wechatDb.'.nw_wx_article')->field('id')->where(['short_link' => $shortLink])->find()) {
- $shortLink = strtolower(random(8));
- }
- $data['short_link'] = $shortLink;
- Db::table($wechatDb.'.nw_wx_article')->insert($data);
- $this->success('文章添加成功!', 'article/wxArticleList');
- }
- return $this->fetch('wx_article_add');
- }
- /**
- * 微信公众号-文章 删除
- */
- public function wxArticleDel()
- {
- $id = input('id' , 0 , 'intval');
- $isRelation = input('check' , '' , 'trim');
- if(empty($id)) $this->error('记录ID不能为空');
- $wechatDb = Config::get('db_config_wechat.database'); // 微信公众号数据库
- // 检查是否有关联分享礼包
- if ($isRelation == 'isRelation'){
- $isExsit = Db::table($wechatDb . '.nw_wx_sharelibao')->where(['article_id' => $id,'is_del'=>0])->find();
- if (!empty($isExsit)) {
- $msg = '该文章已关联分享礼包“'.$isExsit['name'].'”,删除后该礼包也将被自动删除,是否确认删除?';
- }else{
- $msg = '该文章未关联分享礼包,是否确认删除?';
- }
- $this->success($msg);
- }
- if ($isRelation != 'sureDel') $this->error('参数错误!');
- Db::startTrans();
- try{
- Db::table($wechatDb . '.nw_wx_article')->where(['id' => $id])->setField('is_del', 1);
- Db::table($wechatDb . '.nw_wx_sharelibao')->where(['article_id' => $id])->setField('is_del', 1);
- Db::commit();
- }catch (\Exception $e) {
- // 回滚事务
- Db::rollback();
- $this->error('操作失败:'.$e->getMessage());
- }
- $this->success('删除成功', url('wxArticleList'));
-
- }
- /**
- * 微信公众号-文章 编辑
- */
- public function wxArticleEdit()
- {
- $id = input('id', 0, 'intval');
- if (empty($id)) {
- $this->error('ID不能为空');
- }
- $wechatDb = Config::get('db_config_wechat.database'); // 微信公众号数据库
- $articleInfo = Db::table($wechatDb . '.nw_wx_article')->where(['id' => $id])->find();
- if (empty($articleInfo)) {
- $this->error('资讯信息不能为空! ');
- }
- if (request()->isPost()) {
- $fileUpload = new FileUpload();
- //新闻图片
- $image_url = $fileUpload->set('allowExt', 'jpg,jpeg,png,gif')->set('maxsize', 1024000 *3)->set('dir','image/news/pic/')->upload(request()->file('image'));
- $image_error = $fileUpload->getError();
- $data = [
- 'title' => input('post.title'),
- 'releaser' => input('post.releaser'),
- 'content' => input('post.content'),
- 'update_time' => time(),
- 'update_id' => session('ADMIN_ID'),
- ];
- $result = $this->validate($data, [
- ['title', 'require', '标题不能为空'],
- ['content', 'require', '内容不能为空']
- ]);
- if (true !== $result) {
- $this->error($result);
- }//新闻图片上传出错时
- elseif(!empty(request()->file('image')) && empty($image_url))
- {
- $this->error('新闻图片'.$image_error);
- }
- $addIMG = input('post.addIMG');
- if ($addIMG == 2){
- $data['image'] = '';
- }
- //有上传新闻图片时
- if(!empty($image_url) && $addIMG == 1)
- $data['image'] = $image_url;
- Db::table($wechatDb . '.nw_wx_article')->where(['id'=>$id])->update($data);
- $this->success('编辑成功!', 'article/wxArticleList');
- }
- $this->assign('info', $articleInfo);
- return $this->fetch('wx_article_edit');
- }
- }
|