NewsController.php 18 KB

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