NewsController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. <?php
  2. namespace app\admin\controller;
  3. use cmf\controller\AdminBaseController;
  4. use think\Db;
  5. class NewsController extends AdminBaseController{
  6. public function initialize(){
  7. parent::initialize();
  8. $username = $_SESSION['think']['name'];
  9. $userid = $_SESSION['think']['ADMIN_ID'];
  10. //判断是否是编辑管理员
  11. $iseditor = 0;
  12. $iseditor = Db::name('role_user')->where('role_id',3)->where('user_id',$userid)->count();
  13. $this->iseditor = $iseditor;
  14. $this->uname = $username;
  15. $this->assign('iseditor',$iseditor);
  16. }
  17. public function index(){
  18. $param = $this->request->param();
  19. $page = isset($param['page'])?intval($param['page']):1;
  20. $this->assign('page',$page);
  21. $cates = Db::name('category')->where('parent_id',0)->order('list_order')->select();
  22. foreach($cates as $k=>$v){
  23. $son = Db::name('category')->where('parent_id',$v['id'])->order('list_order')->select();
  24. $v['son'] = $son;
  25. $cates[$k] = $v;
  26. }
  27. $this->assign('cates',$cates);
  28. $keywords = isset($param['keywords'])?trim($param['keywords']):'';
  29. $catid = isset($param['catid'])?intval($param['catid']):0;
  30. $query = array(
  31. 'catid' => $catid,
  32. 'title' => $keywords
  33. );
  34. $map = array();
  35. if($this->iseditor){
  36. $map['author'] = $this->uname;
  37. //加多一个编辑人员下的,如果是审核了,就不显示文章。2020-05-15
  38. $map['status'] = 0;
  39. }
  40. if($catid){
  41. $map['catid'] = $catid;
  42. }
  43. if($keywords!=''){
  44. $lists = Db::name('news')->where($map)->where('title','like','%'.$keywords.'%')->order('id desc')->paginate(20,false,['query'=>$query]);
  45. }else{
  46. $lists = Db::name('news')->where($map)->order('id desc')->paginate(20,false,['query'=>$query]);
  47. }
  48. //文章分类
  49. $article_list = Db::name('news_category')->order('list_order')->select();
  50. $article_cates = array();
  51. foreach($article_list as $k=>$v){
  52. $article_cates[$v['id']] = $v['catname'];
  53. }
  54. $this->assign('article_cates',$article_cates);
  55. $this->assign('keywords',$keywords);
  56. $this->assign('catid',$catid);
  57. $this->assign('lists',$lists);
  58. return $this->fetch();
  59. }
  60. public function add(){
  61. //如果是管理员的就是这样。
  62. $admin_id=$_SESSION['think']['ADMIN_ID'];
  63. $user = Db::name('role_user')->field('role_id,user_id')->where('user_id',$admin_id)->find();
  64. // echo Db::name('role_user')->getLastSql();die();
  65. // echo ($user['role_id']) ;die;
  66. // echo $admin_id;die();
  67. if($user['role_id']==3){
  68. $param = $this->request->param();
  69. $cates = Db::name('user')->field('category')->where('id',$user['user_id'])->find();
  70. $category=unserialize($cates['category']);
  71. $categorys='';
  72. //遍历数组成字符串,然后再进行循环读取。
  73. foreach($category as $k=>$value){
  74. if(($k+1)==count($category)){
  75. $categorys.=$value."";
  76. }else{
  77. $categorys.=$value.",";
  78. }
  79. }
  80. // echo $categorys;die();
  81. //foreach($cates as $k=>$v){
  82. //sdfsdfs
  83. $cates = Db::name('category')->where('id','in',$categorys)->order('id')->select();
  84. // echo Db::name('category')->getLastSql();die();
  85. //}
  86. //文章分类
  87. //文章分类
  88. $article_list = Db::name('news_category')->order('list_order')->select();
  89. $article_cates = array();
  90. foreach($article_list as $k=>$v){
  91. $article_cates[$v['id']] = $v['catname'];
  92. }
  93. }else{
  94. $param = $this->request->param();
  95. isset($param['pid']) or $param['pid'] = 0;
  96. $cates = Db::name('category')->where('parent_id',0)->order('list_order')->select();
  97. foreach($cates as $k=>$v){
  98. $son = Db::name('category')->where('parent_id',$v['id'])->order('list_order')->select();
  99. $v['son'] = $son;
  100. $cates[$k] = $v;
  101. }
  102. //文章分类
  103. //文章分类
  104. $article_list = Db::name('news_category')->order('list_order')->select();
  105. $article_cates = array();
  106. foreach($article_list as $k=>$v){
  107. $article_cates[$v['id']] = $v['catname'];
  108. }
  109. }
  110. //var_dump($_SESSION['think']['ADMIN_ID']);die;
  111. //通过admin_id来进行读取用户的角色信息
  112. //
  113. $this->assign('pid',$param['pid']);
  114. $this->assign('article_cates',$article_cates);
  115. $this->assign('cates',$cates);
  116. return $this->fetch();
  117. }
  118. public function addPost(){
  119. $param = $this->request->param();
  120. $param['create_time'] = time();
  121. $data['content'] = $param['content'];
  122. unset($param['content']);
  123. if($this->iseditor){
  124. $param['author'] = $this->uname;
  125. $param['status'] = 0;
  126. }
  127. $res = Db::name('news')->insertGetId($param);
  128. $data['id'] = $res;
  129. Db::name('news_data')->insert($data);
  130. if($res){
  131. $this->success('添加成功');
  132. }else{
  133. $this->error('添加失败');
  134. }
  135. }
  136. public function delete(){
  137. $param = $this->request->param();
  138. isset($param['id'])&&$param['id']!='' or $this->error('需要id');
  139. $res = Db::name('news')->where('id',$param['id'])->delete();
  140. if($res){
  141. $this->success('删除成功');
  142. }else{
  143. $this->error('删除失败');
  144. }
  145. }
  146. public function edit(){
  147. $param = $this->request->param();
  148. $page = isset($param['page'])?intval($param['page']):1;
  149. $this->assign('page',$page);
  150. isset($param['id']) or $this->error('需要id');
  151. $cates = Db::name('category')->where('parent_id',0)->order('list_order')->select();
  152. foreach($cates as $k=>$v){
  153. $son = Db::name('category')->where('parent_id',$v['id'])->order('list_order')->select();
  154. $v['son'] = $son;
  155. $cates[$k] = $v;
  156. }
  157. $this->assign('cates',$cates);
  158. //文章分类
  159. //文章分类
  160. $article_list = Db::name('news_category')->order('list_order')->select();
  161. $article_cates = array();
  162. foreach($article_list as $k=>$v){
  163. $article_cates[$v['id']] = $v['catname'];
  164. }
  165. $this->assign('article_cates',$article_cates);
  166. $info = Db::name('news')->where('id',$param['id'])->find();
  167. $content = Db::name('news_data')->where('id',$param['id'])->find();
  168. $info['content'] = $content['content'];
  169. if(empty($info['id'])){
  170. $this->error('未找到信息');
  171. }
  172. $this->assign('info',$info);
  173. return $this->fetch();
  174. }
  175. public function editPost(){
  176. $param = $this->request->param();
  177. $page = $param['page'];
  178. unset($param['page']);
  179. isset($param['id']) or $this->error('需要id');
  180. $param['update_time'] = time();
  181. $data['content'] = $param['content'];
  182. unset($param['content']);
  183. $res = Db::name('news')->update($param);
  184. $data['id'] = $param['id'];
  185. Db::name('news_data')->update($data);
  186. if($res){
  187. // $this->success('编辑成功','news/index?page='.$page);
  188. $this->success('编辑成功','news/index');
  189. }else{
  190. $this->error('编辑失败');
  191. }
  192. }
  193. /**
  194. * 添加分类页面
  195. */
  196. public function cateAdd(){
  197. return $this->fetch();
  198. }
  199. /**
  200. * 添加分类提交
  201. */
  202. public function cateAddPost(){
  203. $param = $this->request->param();
  204. isset($param['catname'])&&trim($param['catname'])!='' or $this->error('请填写分类名称');
  205. $data = array(
  206. 'catname' => $param['catname'],
  207. 'enname' => $param['enname'],
  208. 'seo_title' => $param['seo_title'],
  209. 'seo_keywords' => $param['seo_keywords'],
  210. 'seo_desc' => $param['seo_desc'],
  211. 'list_order' => $param['list_order'],
  212. 'create_time' => time()
  213. );
  214. $res = Db::name('news_category')->insert($data);
  215. if($res){
  216. $this->success('添加成功');
  217. }else{
  218. $this->error('添加失败');
  219. }
  220. }
  221. /**
  222. * 分类编辑
  223. */
  224. public function cateEdit(){
  225. $param = $this->request->param();
  226. isset($param['id']) or $this->error('没有id');
  227. $res = Db::name('news_category')->where('id',$param['id'])->find();
  228. $this->assign('info',$res);
  229. return $this->fetch();
  230. }
  231. public function cateEditPost(){
  232. $param = $this->request->param();
  233. isset($param['catname'])&&trim($param['catname'])!='' or $this->error('请填写分类名称');
  234. isset($param['parent_id']) or $param['parent_id'] = 0;
  235. $data = array(
  236. 'id' => $param['id'],
  237. 'catname' => $param['catname'],
  238. 'enname' => $param['enname'],
  239. 'seo_title' => $param['seo_title'],
  240. 'seo_keywords' => $param['seo_keywords'],
  241. 'seo_desc' => $param['seo_desc'],
  242. 'list_order' => $param['list_order'],
  243. 'update_time' => time()
  244. );
  245. $res = Db::name('news_category')->update($data);
  246. if($res){
  247. $this->success('编辑成功');
  248. }else{
  249. $this->error('编辑失败');
  250. }
  251. }
  252. public function cateList(){
  253. $lists = Db::name('news_category')->order('list_order')->select();
  254. $this->assign('lists',$lists);
  255. return $this->fetch();
  256. }
  257. public function cateDelete(){
  258. $param = $this->request->param();
  259. isset($param['id']) or $this->error('没有id');
  260. $res = Db::name('news_category')->where('id',$param['id'])->delete();
  261. if($res){
  262. $this->success('删除成功');
  263. }else{
  264. $this->error('删除失败');
  265. }
  266. }
  267. public function chongfu(){
  268. $param = $this->request->param();
  269. $title = isset($param['title'])?trim($param['title']):'';
  270. if($title==''){
  271. echo 0;
  272. }
  273. $res = Db::name('news')->where('title',$title)->count();
  274. if($res>0){
  275. echo 1;
  276. }else{
  277. echo 0;
  278. }
  279. exit;
  280. }
  281. public function seo(){
  282. $param = $this->request->param();
  283. $page = isset($param['page'])?$param['page']:1;
  284. $msg = $param['msg'];
  285. if(count($msg)==0){
  286. $this->error('请选择要提交的信息');
  287. }
  288. $str = implode(",",$msg);
  289. if(isset($param['pc'])){
  290. $this->baidu($str,$page);
  291. }
  292. if(isset($param['mobile'])){
  293. $this->shouji($str,$page);
  294. }
  295. if(isset($param['xiongzhang'])){
  296. $this->bear($str,$page);
  297. }
  298. $this->success('提交成功');
  299. }
  300. // 百度链接自动提交
  301. public function baidu($ids,$page){
  302. $where = [];
  303. $where[] = ['id','in', $ids];
  304. $artRes = Db::name('news')->where($where)->select()->toArray();
  305. // print_r($artRes);die;
  306. $urls = array();
  307. $dataRes = array();
  308. $local = $_SERVER['SERVER_NAME'];
  309. foreach ($artRes as $k => &$v) {
  310. $v['arcurl'] = 'http://'.$local.'/news'.$v['id']."/";
  311. $urls[] = $v['arcurl'];
  312. }
  313. $site_info = cmf_get_option('site_info');
  314. // print_r($site_info);die;
  315. if($site_info['site_tj_pc'] == ''){
  316. $this->error("请设置百度提交参数!",'');
  317. }
  318. $site_baidu = $site_info['site_tj_pc'];
  319. $site_baidu = explode('&amp;', $site_baidu);
  320. $site_baidu = implode('&', $site_baidu);
  321. $site_baidu = str_replace(PHP_EOL, '', $site_baidu);
  322. // dump($dataRes);die;
  323. // print_r($urls);exit;
  324. if(count($urls)>0 && trim($site_baidu)!=''){
  325. $api = $site_baidu;
  326. $ch = curl_init();
  327. $options = array(
  328. CURLOPT_URL => $api,
  329. CURLOPT_POST => true,
  330. CURLOPT_RETURNTRANSFER => true,
  331. CURLOPT_POSTFIELDS => implode("\n", $urls),
  332. CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
  333. );
  334. curl_setopt_array($ch, $options);
  335. $curlresult = curl_exec($ch);
  336. $cuarray = json_decode($curlresult,true);
  337. // dump($cuarray);die;
  338. if($cuarray['success']>0){
  339. $this->success("共提交".$cuarray['success']."条。今日自动提交剩余".$cuarray['remain'].'条','jiameng/index?page='.$page);
  340. }else{
  341. $this->error("今天提交已经到上线,明天再来吧!",'jiameng/index?page='.$page);
  342. }
  343. }else{
  344. $this->success("没有新百度Pc连接提交",'jiameng/index?page='.$page);
  345. }
  346. }
  347. // 百度手机链接自动提交
  348. public function shouji($ids,$page){
  349. $where = [];
  350. $where[] = ['id','in', $ids];
  351. $artRes = Db::name('news')->where($where)->select()->toArray();
  352. $urls = array();
  353. $dataRes = array();
  354. $local = $_SERVER['SERVER_NAME'];
  355. $host = explode('.', $local);
  356. if(count($host)==3){
  357. $host = $host[1].'.'.$host[2];
  358. }else{
  359. $host = $host[1].'.'.$host[2].'.'.$host[3];
  360. }
  361. $local = 'm.'.$host;
  362. foreach ($artRes as $k => &$v) {
  363. $v['arcurl'] = 'http://'.$local.'/b'.$v['id']."/";
  364. $urls[] = $v['arcurl'];
  365. }
  366. $site_info = cmf_get_option('site_info');
  367. if($site_info['site_tj_mobile'] == ''){
  368. $this->error("请设置百度提交参数!",'jiameng/index?page='.$page);
  369. }
  370. $site_shouji = $site_info['site_tj_mobile'];
  371. $site_shouji = explode('&amp;', $site_shouji);
  372. $site_shouji = implode('&', $site_shouji);
  373. $site_shouji = str_replace(PHP_EOL, '', $site_shouji);
  374. // print_r($urls);exit;
  375. if(count($urls)>0 && trim($site_shouji)!=''){
  376. $api = $site_shouji;
  377. $ch = curl_init();
  378. $options = array(
  379. CURLOPT_URL => $api,
  380. CURLOPT_POST => true,
  381. CURLOPT_RETURNTRANSFER => true,
  382. CURLOPT_POSTFIELDS => implode("\n", $urls),
  383. CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
  384. );
  385. curl_setopt_array($ch, $options);
  386. $curlresult = curl_exec($ch);
  387. $cuarray = json_decode($curlresult,true);
  388. // dump($cuarray);die;
  389. if($cuarray['success']>0){
  390. //如果提交过并且成功的将其状态标记为1
  391. // foreach ($dataRes as $k => $v) {
  392. // Db::name('brand_post')->where('id',$k)->update(['shouji' => '1']);
  393. // }
  394. $this->success("<br/>共提交".$cuarray['success']."条。今日自动提交剩余".$cuarray['remain'].'条<br/>','jiameng/index?page='.$page);
  395. }else{
  396. $this->error("今天提交已经到上线,明天再来吧!",'jiameng/index?page='.$page);
  397. }
  398. }else{
  399. $this->success("没有新手机百度连接提交",'jiameng/index?page='.$page);
  400. }
  401. }
  402. // 百度链接自动提交
  403. public function bear($ids,$page){
  404. $where = [];
  405. $where[] = ['id','in', $ids];
  406. $artRes = Db::name('news')->where($where)->select()->toArray();
  407. //dump($artRes);die;
  408. $urls = array();
  409. $dataRes = array();
  410. $local = $_SERVER['SERVER_NAME'];
  411. foreach ($artRes as $k => &$v) {
  412. $v['arcurl'] = 'http://'.$local.'/b'.$v['id']."/";
  413. $urls[] = $v['arcurl'];
  414. }
  415. // dump($urls);exit;
  416. $site_info = cmf_get_option('site_info');
  417. //dump($site_info);die;
  418. if($site_info['site_tj_xiongzhang'] == ''){
  419. $this->error("请设置熊掌号提交参数!",'jiameng/index?page='.$page);
  420. }
  421. $site_xiongzhang = $site_info['site_tj_xiongzhang'];
  422. $site_xiongzhang = explode('&amp;', $site_xiongzhang);
  423. $site_xiongzhang = implode('&', $site_xiongzhang);
  424. $site_xiongzhang = str_replace(PHP_EOL, '', $site_xiongzhang);
  425. // dump($dataRes);die;
  426. if(count($urls)>0 && trim($site_xiongzhang)!=''){
  427. $api = trim($site_xiongzhang);
  428. $ch = curl_init();
  429. $options = array(
  430. CURLOPT_URL => $api,
  431. CURLOPT_POST => true,
  432. CURLOPT_RETURNTRANSFER => true,
  433. CURLOPT_POSTFIELDS => implode("\n", $urls),
  434. CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
  435. );
  436. curl_setopt_array($ch, $options);
  437. $curlresult = curl_exec($ch);
  438. $cuarray = json_decode($curlresult,true);
  439. // dump($cuarray);die;
  440. if($cuarray['success']>0){
  441. //如果提交过并且成功的将其状态标记为1
  442. foreach ($dataRes as $k => $v) {
  443. Db::name('brand_post')->where('id',$k)->update(['xiongzhang' => '1']);
  444. }
  445. $this->success("<br/>共提交".$cuarray['success']."条。今日自动提交剩余".$cuarray['remain'].'条<br/>','jiameng/index?page='.$page);
  446. }else{
  447. $this->error("今天提交已经到上线,明天再来吧!",'jiameng/index?page='.$page);
  448. }
  449. }else{
  450. $this->success("没有新熊掌天级连接提交",'jiameng/index?page='.$page);
  451. }
  452. }
  453. }
  454. ?>