Article.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. <?php
  2. /**
  3. * 新闻列表控制器
  4. */
  5. namespace app\admin\controller;
  6. use think\Db;
  7. use app\common\library\FileUpload;
  8. use think\Config;
  9. class Article extends Admin
  10. {
  11. protected function _initialize()
  12. {
  13. parent::_initialize();
  14. $this->articleModel = Db::name('cy_article');
  15. $this->contentModel = Db::name('cy_content');
  16. }
  17. public function index()
  18. {
  19. $where = [];
  20. $start_time = input('request.start_time');
  21. // 类型
  22. if (input('request.type') != '') {
  23. $where['a.type'] = ['=', input('request.type')];
  24. }
  25. // 标题
  26. if (input('request.title') != '') {
  27. $where['a.title'] = ['like', '%' . input('request.title') .'%'];
  28. }
  29. // 显示位置
  30. if (input('request.position') != '') {
  31. $where['a.position'] = ['like', '%' . input('request.position') .'%'];
  32. }
  33. // 游戏
  34. if (input('request.gameid') != '') {
  35. $where['a.gameid'] = input('request.gameid');
  36. }
  37. //开始时间和结束时间不为空时
  38. if ($start_time != '' && input('request.end_time') != '') {
  39. $where['a.create_time'] = [
  40. ['>=', strtotime($start_time)],
  41. ['<=', strtotime(input('request.end_time').' 23:59:59')],
  42. ];
  43. } //开始时间不为空时
  44. elseif ($start_time!= '') {
  45. $where['a.create_time'] = ['>=', strtotime($start_time)];
  46. } //结束时间不为空时
  47. elseif (input('request.end_time') != '') {
  48. $where['a.create_time'] = ['<=', strtotime(input('request.end_time').' 23:59:59')];
  49. }
  50. $where['a.isdelete'] = 0;
  51. $param = input('get.');
  52. $article= $this->articleModel
  53. ->alias('a')
  54. ->join('cy_game g', 'a.gameid=g.id', 'left')
  55. ->field('a.*,g.id as gameid2,g.name as game_name')
  56. ->where($where)
  57. ->order('a.id desc')->paginate(10, false, array('query' => $param));
  58. $gameList = Db::name('cy_game')->field('id,name')->order('name asc')->select();
  59. $this->assign('game_list', $gameList);
  60. $this->assign('article', $article);
  61. $this->assign('page', $article->render());
  62. return $this->fetch();
  63. }
  64. public function add()
  65. {
  66. if (request()->isPost()) {
  67. $fileUpload = new FileUpload();
  68. //新闻图片
  69. $image_url = $fileUpload->set('allowExt', 'jpg,jpeg,png,gif')->set('maxsize', 1024000)->set('dir','image/news/pic/')->upload(request()->file('image'));
  70. $image_error = $fileUpload->getError();
  71. $data = [
  72. 'title' => input('post.title'),
  73. 'gameid' => input('post.gameid',0,'int'),
  74. 'zhiding' => input('post.top'),
  75. 'type' => input('post.type'),
  76. 'content' => input('post.content'),
  77. 'create_time' => NOW_TIMESTAMP
  78. ];
  79. $result = $this->validate($data, [
  80. ['title', 'require|max:100', '标题不能为空|标题不能超过100个字符'],
  81. ['gameid', 'require', '游戏不能为空'],
  82. ['zhiding', 'require', '是否置顶不能为空'],
  83. ['type', 'require|integer', '类型不能为空|类型必须为整型'],
  84. ['content', 'require', '内容不能为空'],
  85. ]);
  86. if (true !== $result) {
  87. $this->error($result);
  88. }
  89. //新闻图片上传出错时
  90. elseif(!empty(request()->file('image')) && empty($image_url))
  91. {
  92. $this->error('新闻图片'.$image_error);
  93. }
  94. else {
  95. unset($data['content']);
  96. if ( !empty(input('post.position/a'))) {
  97. $data['position'] = implode('',input('post.position/a'));
  98. }
  99. //有上传新闻图片时
  100. if(!empty($image_url))
  101. $data['image'] = $image_url;
  102. if(in_array($data['type'],[1,2,3,4,5]) && empty($image_url)){
  103. $this->error('请选择新闻图片');
  104. }
  105. $id = $this->articleModel->insertGetId($data);
  106. if (empty($id)) {
  107. $this->error('添加失败!');
  108. }
  109. $this->contentModel->insert(['id'=>$id,'content'=>input('post.content')]);
  110. $this->success('新闻添加成功!', 'article/index');
  111. }
  112. }
  113. $gameList = Db::name('cy_game')->field('id,name')->order('name asc')->select();
  114. $this->assign('game_list', $gameList);
  115. return $this->fetch();
  116. }
  117. public function edit($id)
  118. {
  119. $id = (int)$id;
  120. if (empty($id)) {
  121. $this->error('资讯ID不能为空');
  122. }
  123. $articleInfo = $this->articleModel->where(['id' => $id])->find();
  124. if (empty($articleInfo)) {
  125. $this->error('资讯信息不能为空! ');
  126. }
  127. if (request()->isPost()) {
  128. $fileUpload = new FileUpload();
  129. //新闻图片
  130. $image_url = $fileUpload->set('allowExt', 'jpg,jpeg,png,gif')->set('maxsize', 1024000)->set('dir','image/news/pic')->upload(request()->file('image'));
  131. $image_error = $fileUpload->getError();
  132. $data = [
  133. 'title' => input('post.title'),
  134. 'gameid' => input('post.gameid',0,'int'),
  135. 'zhiding' => input('post.top'),
  136. 'type' => input('post.type'),
  137. 'content' => input('post.content'),
  138. ];
  139. $result = $this->validate($data, [
  140. ['title', 'require|max:100', '标题不能为空|标题不能超过100个字符'],
  141. ['gameid', 'require', '游戏不能为空'],
  142. ['zhiding', 'require', '是否置顶不能为空'],
  143. ['type', 'require|integer', '类型不能为空|类型必须为整型'],
  144. ['content', 'require', '内容不能为空'],
  145. ]);
  146. if (true !== $result) {
  147. $this->error($result);
  148. }
  149. //新闻图片上传出错时
  150. elseif(!empty(request()->file('image')) && empty($image_url))
  151. {
  152. $this->error('新闻图片'.$image_error);
  153. }
  154. else {
  155. unset($data['content']);
  156. if ( !empty(input('post.position/a'))) {
  157. $data['position'] = implode('',input('post.position/a'));
  158. }else{
  159. $data['position'] = null;
  160. }
  161. //有上传新闻图片时
  162. if(!empty($image_url))
  163. $data['image'] = $image_url;
  164. if(in_array($data['type'],[1,2,3,4,5]) && empty($image_url) && !$articleInfo['image']){
  165. $this->error('请选择新闻图片');
  166. }
  167. $this->articleModel->where(['id'=>$id])->update($data);
  168. $this->contentModel->where(['id'=>$id])->update(['content'=>input('post.content')]);
  169. model('GameActivityRead')->where('game_article_id',$id)->delete();
  170. $this->success('新闻编辑成功!', 'article/index');
  171. }
  172. }
  173. $content = $this->contentModel->where(['id' => $id])->value('content');
  174. $gameList = Db::name('cy_game')->field('id,name')->order('name asc')->select();
  175. $this->assign('articleInfo', $articleInfo);
  176. $this->assign('content', $content);
  177. $this->assign('game_list', $gameList);
  178. return $this->fetch();
  179. }
  180. public function del()
  181. {
  182. $id = input('id', 0, 'intval');
  183. if(empty($id)) $this->error('记录ID不能为空');
  184. $ret = $this->articleModel->where([
  185. 'id' => $id,
  186. ])->setField('isdelete', 1);
  187. if (!$ret) {
  188. $this->error('删除错误!');
  189. }
  190. $this->success('删除成功', url('index'));
  191. }
  192. /**
  193. * 微信公众号-文章列表
  194. */
  195. public function wxArticleList(){
  196. $where = [];
  197. // 标题
  198. if (input('request.title') != '') {
  199. $where['title'] = ['like', '%' . input('request.title') .'%'];
  200. }
  201. $start = $this->request->param('start_time');
  202. $end = $this->request->param('end_time');
  203. //开始时间和结束时间不为空时
  204. if ($start != '' && $end != '') {
  205. $where['create_time'] = [
  206. ['>=', strtotime($start)],
  207. ['<=', strtotime($end . ' 23:59:59')],
  208. ];
  209. } //开始时间不为空时
  210. elseif ($start != '') {
  211. $where['create_time'] = ['>=', strtotime($start)];
  212. } //结束时间不为空时
  213. elseif ($end != '') {
  214. $where['create_time'] = ['<=', strtotime($end . ' 23:59:59')];
  215. }
  216. $wechatDb = Config::get('db_config_wechat.database'); // 微信公众号数据库
  217. $where['is_del'] = 0;
  218. $infoList = Db::table($wechatDb . '.nw_wx_article')
  219. ->field('*')
  220. ->where($where)
  221. ->order('create_time', 'desc')
  222. ->paginate(10, false, ['query' => input('get.')]);
  223. $this->assign('article', $infoList);
  224. $this->assign('page', $infoList->render());
  225. $this->assign('wxlink',Config::get('WX_ARTICLE_LINK'));
  226. return $this->fetch('wx_article_list');
  227. }
  228. /**
  229. * 微信公众号-文章 添加
  230. */
  231. public function wxArticleAdd(){
  232. if (request()->isPost()){
  233. $fileUpload = new FileUpload();
  234. //新闻图片
  235. $image_url = $fileUpload->set('allowExt', 'jpg,jpeg,png,gif')->set('maxsize', 1024000 *3)->set('dir','image/wechat/pic/')->upload(request()->file('image'));
  236. $image_error = $fileUpload->getError();
  237. $data = [
  238. 'title' => input('post.title'),
  239. 'releaser' => input('post.releaser') ? input('post.releaser') : '祈盟网络游戏',
  240. 'content' => input('post.content'),
  241. 'create_time' => NOW_TIMESTAMP
  242. ];
  243. $result = $this->validate($data, [
  244. ['title', 'require', '标题不能为空'],
  245. ['releaser', 'require', '发布人不能为空'],
  246. ['content', 'require', '内容不能为空'],
  247. ]);
  248. if (true !== $result) {
  249. $this->error($result);
  250. }
  251. //新闻图片上传出错时
  252. elseif(!empty(request()->file('image')) && empty($image_url))
  253. {
  254. $this->error('新闻图片'.$image_error);
  255. }
  256. //有上传新闻图片时
  257. if(!empty($image_url))
  258. $data['image'] = $image_url;
  259. $wechatDb = Config::get('db_config_wechat.database'); // 微信公众号数据库
  260. $shortLink = strtolower(random(8));
  261. while (Db::table($wechatDb.'.nw_wx_article')->field('id')->where(['short_link' => $shortLink])->find()) {
  262. $shortLink = strtolower(random(8));
  263. }
  264. $data['short_link'] = $shortLink;
  265. Db::table($wechatDb.'.nw_wx_article')->insert($data);
  266. $this->success('文章添加成功!', 'article/wxArticleList');
  267. }
  268. return $this->fetch('wx_article_add');
  269. }
  270. /**
  271. * 微信公众号-文章 删除
  272. */
  273. public function wxArticleDel()
  274. {
  275. $id = input('id' , 0 , 'intval');
  276. $isRelation = input('check' , '' , 'trim');
  277. if(empty($id)) $this->error('记录ID不能为空');
  278. $wechatDb = Config::get('db_config_wechat.database'); // 微信公众号数据库
  279. // 检查是否有关联分享礼包
  280. if ($isRelation == 'isRelation'){
  281. $isExsit = Db::table($wechatDb . '.nw_wx_sharelibao')->where(['article_id' => $id,'is_del'=>0])->find();
  282. if (!empty($isExsit)) {
  283. $msg = '该文章已关联分享礼包“'.$isExsit['name'].'”,删除后该礼包也将被自动删除,是否确认删除?';
  284. }else{
  285. $msg = '该文章未关联分享礼包,是否确认删除?';
  286. }
  287. $this->success($msg);
  288. }
  289. if ($isRelation != 'sureDel') $this->error('参数错误!');
  290. Db::startTrans();
  291. try{
  292. Db::table($wechatDb . '.nw_wx_article')->where(['id' => $id])->setField('is_del', 1);
  293. Db::table($wechatDb . '.nw_wx_sharelibao')->where(['article_id' => $id])->setField('is_del', 1);
  294. Db::commit();
  295. }catch (\Exception $e) {
  296. // 回滚事务
  297. Db::rollback();
  298. $this->error('操作失败:'.$e->getMessage());
  299. }
  300. $this->success('删除成功', url('wxArticleList'));
  301. }
  302. /**
  303. * 微信公众号-文章 编辑
  304. */
  305. public function wxArticleEdit()
  306. {
  307. $id = input('id', 0, 'intval');
  308. if (empty($id)) {
  309. $this->error('ID不能为空');
  310. }
  311. $wechatDb = Config::get('db_config_wechat.database'); // 微信公众号数据库
  312. $articleInfo = Db::table($wechatDb . '.nw_wx_article')->where(['id' => $id])->find();
  313. if (empty($articleInfo)) {
  314. $this->error('资讯信息不能为空! ');
  315. }
  316. if (request()->isPost()) {
  317. $fileUpload = new FileUpload();
  318. //新闻图片
  319. $image_url = $fileUpload->set('allowExt', 'jpg,jpeg,png,gif')->set('maxsize', 1024000 *3)->set('dir','image/news/pic/')->upload(request()->file('image'));
  320. $image_error = $fileUpload->getError();
  321. $data = [
  322. 'title' => input('post.title'),
  323. 'releaser' => input('post.releaser'),
  324. 'content' => input('post.content'),
  325. 'update_time' => time(),
  326. 'update_id' => session('ADMIN_ID'),
  327. ];
  328. $result = $this->validate($data, [
  329. ['title', 'require', '标题不能为空'],
  330. ['content', 'require', '内容不能为空']
  331. ]);
  332. if (true !== $result) {
  333. $this->error($result);
  334. }//新闻图片上传出错时
  335. elseif(!empty(request()->file('image')) && empty($image_url))
  336. {
  337. $this->error('新闻图片'.$image_error);
  338. }
  339. $addIMG = input('post.addIMG');
  340. if ($addIMG == 2){
  341. $data['image'] = '';
  342. }
  343. //有上传新闻图片时
  344. if(!empty($image_url) && $addIMG == 1)
  345. $data['image'] = $image_url;
  346. Db::table($wechatDb . '.nw_wx_article')->where(['id'=>$id])->update($data);
  347. $this->success('编辑成功!', 'article/wxArticleList');
  348. }
  349. $this->assign('info', $articleInfo);
  350. return $this->fetch('wx_article_edit');
  351. }
  352. }