NewsController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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('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. $this->success('编辑成功',$page);
  190. }else{
  191. $this->error('编辑失败');
  192. }
  193. }
  194. /**
  195. * 添加分类页面
  196. */
  197. public function cateAdd(){
  198. return $this->fetch();
  199. }
  200. /**
  201. * 添加分类提交
  202. */
  203. public function cateAddPost(){
  204. $param = $this->request->param();
  205. isset($param['catname'])&&trim($param['catname'])!='' or $this->error('请填写分类名称');
  206. $data = array(
  207. 'catname' => $param['catname'],
  208. 'enname' => $param['enname'],
  209. 'seo_title' => $param['seo_title'],
  210. 'seo_keywords' => $param['seo_keywords'],
  211. 'seo_desc' => $param['seo_desc'],
  212. 'list_order' => $param['list_order'],
  213. 'create_time' => time()
  214. );
  215. $res = Db::name('news_category')->insert($data);
  216. if($res){
  217. $this->success('添加成功');
  218. }else{
  219. $this->error('添加失败');
  220. }
  221. }
  222. /**
  223. * 分类编辑
  224. */
  225. public function cateEdit(){
  226. $param = $this->request->param();
  227. isset($param['id']) or $this->error('没有id');
  228. $res = Db::name('news_category')->where('id',$param['id'])->find();
  229. $this->assign('info',$res);
  230. return $this->fetch();
  231. }
  232. public function cateEditPost(){
  233. $param = $this->request->param();
  234. isset($param['catname'])&&trim($param['catname'])!='' or $this->error('请填写分类名称');
  235. isset($param['parent_id']) or $param['parent_id'] = 0;
  236. $data = array(
  237. 'id' => $param['id'],
  238. 'catname' => $param['catname'],
  239. 'enname' => $param['enname'],
  240. 'seo_title' => $param['seo_title'],
  241. 'seo_keywords' => $param['seo_keywords'],
  242. 'seo_desc' => $param['seo_desc'],
  243. 'list_order' => $param['list_order'],
  244. 'update_time' => time()
  245. );
  246. $res = Db::name('news_category')->update($data);
  247. if($res){
  248. $this->success('编辑成功');
  249. }else{
  250. $this->error('编辑失败');
  251. }
  252. }
  253. public function cateList(){
  254. $lists = Db::name('news_category')->order('list_order')->select();
  255. $this->assign('lists',$lists);
  256. return $this->fetch();
  257. }
  258. public function cateDelete(){
  259. $param = $this->request->param();
  260. isset($param['id']) or $this->error('没有id');
  261. $res = Db::name('news_category')->where('id',$param['id'])->delete();
  262. if($res){
  263. $this->success('删除成功');
  264. }else{
  265. $this->error('删除失败');
  266. }
  267. }
  268. public function chongfu(){
  269. $param = $this->request->param();
  270. $title = isset($param['title'])?trim($param['title']):'';
  271. if($title==''){
  272. echo 0;
  273. }
  274. $res = Db::name('news')->where('title',$title)->count();
  275. if($res>0){
  276. echo 1;
  277. }else{
  278. echo 0;
  279. }
  280. exit;
  281. }
  282. public function seo(){
  283. $param = $this->request->param();
  284. $page = isset($param['page'])?$param['page']:1;
  285. $msg = $param['msg'];
  286. if(count($msg)==0){
  287. $this->error('请选择要提交的信息');
  288. }
  289. $str = implode(",",$msg);
  290. if(isset($param['pc'])){
  291. $this->baidu($str,$page);
  292. }
  293. if(isset($param['mobile'])){
  294. $this->shouji($str,$page);
  295. }
  296. if(isset($param['xiongzhang'])){
  297. $this->bear($str,$page);
  298. }
  299. $this->success('提交成功');
  300. }
  301. // 百度链接自动提交
  302. public function baidu($ids,$page){
  303. $where = [];
  304. $where[] = ['id','in', $ids];
  305. $artRes = Db::name('news')->where($where)->select()->toArray();
  306. // print_r($artRes);die;
  307. $urls = array();
  308. $dataRes = array();
  309. $local = $_SERVER['SERVER_NAME'];
  310. foreach ($artRes as $k => &$v) {
  311. $v['arcurl'] = 'http://'.$local.'/news'.$v['id']."/";
  312. $urls[] = $v['arcurl'];
  313. }
  314. $site_info = cmf_get_option('site_info');
  315. // print_r($site_info);die;
  316. if($site_info['site_tj_pc'] == ''){
  317. $this->error("请设置百度提交参数!",'');
  318. }
  319. $site_baidu = $site_info['site_tj_pc'];
  320. $site_baidu = explode('&amp;', $site_baidu);
  321. $site_baidu = implode('&', $site_baidu);
  322. $site_baidu = str_replace(PHP_EOL, '', $site_baidu);
  323. // dump($dataRes);die;
  324. // print_r($urls);exit;
  325. if(count($urls)>0 && trim($site_baidu)!=''){
  326. $api = $site_baidu;
  327. $ch = curl_init();
  328. $options = array(
  329. CURLOPT_URL => $api,
  330. CURLOPT_POST => true,
  331. CURLOPT_RETURNTRANSFER => true,
  332. CURLOPT_POSTFIELDS => implode("\n", $urls),
  333. CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
  334. );
  335. curl_setopt_array($ch, $options);
  336. $curlresult = curl_exec($ch);
  337. $cuarray = json_decode($curlresult,true);
  338. // dump($cuarray);die;
  339. if($cuarray['success']>0){
  340. $this->success("共提交".$cuarray['success']."条。今日自动提交剩余".$cuarray['remain'].'条','jiameng/index?page='.$page);
  341. }else{
  342. $this->error("今天提交已经到上线,明天再来吧!",'jiameng/index?page='.$page);
  343. }
  344. }else{
  345. $this->success("没有新百度Pc连接提交",'jiameng/index?page='.$page);
  346. }
  347. }
  348. // 百度手机链接自动提交
  349. public function shouji($ids,$page){
  350. $where = [];
  351. $where[] = ['id','in', $ids];
  352. $artRes = Db::name('news')->where($where)->select()->toArray();
  353. $urls = array();
  354. $dataRes = array();
  355. $local = $_SERVER['SERVER_NAME'];
  356. $host = explode('.', $local);
  357. if(count($host)==3){
  358. $host = $host[1].'.'.$host[2];
  359. }else{
  360. $host = $host[1].'.'.$host[2].'.'.$host[3];
  361. }
  362. $local = 'm.'.$host;
  363. foreach ($artRes as $k => &$v) {
  364. $v['arcurl'] = 'http://'.$local.'/b'.$v['id']."/";
  365. $urls[] = $v['arcurl'];
  366. }
  367. $site_info = cmf_get_option('site_info');
  368. if($site_info['site_tj_mobile'] == ''){
  369. $this->error("请设置百度提交参数!",'jiameng/index?page='.$page);
  370. }
  371. $site_shouji = $site_info['site_tj_mobile'];
  372. $site_shouji = explode('&amp;', $site_shouji);
  373. $site_shouji = implode('&', $site_shouji);
  374. $site_shouji = str_replace(PHP_EOL, '', $site_shouji);
  375. // print_r($urls);exit;
  376. if(count($urls)>0 && trim($site_shouji)!=''){
  377. $api = $site_shouji;
  378. $ch = curl_init();
  379. $options = array(
  380. CURLOPT_URL => $api,
  381. CURLOPT_POST => true,
  382. CURLOPT_RETURNTRANSFER => true,
  383. CURLOPT_POSTFIELDS => implode("\n", $urls),
  384. CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
  385. );
  386. curl_setopt_array($ch, $options);
  387. $curlresult = curl_exec($ch);
  388. $cuarray = json_decode($curlresult,true);
  389. // dump($cuarray);die;
  390. if($cuarray['success']>0){
  391. //如果提交过并且成功的将其状态标记为1
  392. // foreach ($dataRes as $k => $v) {
  393. // Db::name('brand_post')->where('id',$k)->update(['shouji' => '1']);
  394. // }
  395. $this->success("<br/>共提交".$cuarray['success']."条。今日自动提交剩余".$cuarray['remain'].'条<br/>','jiameng/index?page='.$page);
  396. }else{
  397. $this->error("今天提交已经到上线,明天再来吧!",'jiameng/index?page='.$page);
  398. }
  399. }else{
  400. $this->success("没有新手机百度连接提交",'jiameng/index?page='.$page);
  401. }
  402. }
  403. // 百度链接自动提交
  404. public function bear($ids,$page){
  405. $where = [];
  406. $where[] = ['id','in', $ids];
  407. $artRes = Db::name('news')->where($where)->select()->toArray();
  408. //dump($artRes);die;
  409. $urls = array();
  410. $dataRes = array();
  411. $local = $_SERVER['SERVER_NAME'];
  412. foreach ($artRes as $k => &$v) {
  413. $v['arcurl'] = 'http://'.$local.'/b'.$v['id']."/";
  414. $urls[] = $v['arcurl'];
  415. }
  416. // dump($urls);exit;
  417. $site_info = cmf_get_option('site_info');
  418. //dump($site_info);die;
  419. if($site_info['site_tj_xiongzhang'] == ''){
  420. $this->error("请设置熊掌号提交参数!",'jiameng/index?page='.$page);
  421. }
  422. $site_xiongzhang = $site_info['site_tj_xiongzhang'];
  423. $site_xiongzhang = explode('&amp;', $site_xiongzhang);
  424. $site_xiongzhang = implode('&', $site_xiongzhang);
  425. $site_xiongzhang = str_replace(PHP_EOL, '', $site_xiongzhang);
  426. // dump($dataRes);die;
  427. if(count($urls)>0 && trim($site_xiongzhang)!=''){
  428. $api = trim($site_xiongzhang);
  429. $ch = curl_init();
  430. $options = array(
  431. CURLOPT_URL => $api,
  432. CURLOPT_POST => true,
  433. CURLOPT_RETURNTRANSFER => true,
  434. CURLOPT_POSTFIELDS => implode("\n", $urls),
  435. CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
  436. );
  437. curl_setopt_array($ch, $options);
  438. $curlresult = curl_exec($ch);
  439. $cuarray = json_decode($curlresult,true);
  440. // dump($cuarray);die;
  441. if($cuarray['success']>0){
  442. //如果提交过并且成功的将其状态标记为1
  443. foreach ($dataRes as $k => $v) {
  444. Db::name('brand_post')->where('id',$k)->update(['xiongzhang' => '1']);
  445. }
  446. $this->success("<br/>共提交".$cuarray['success']."条。今日自动提交剩余".$cuarray['remain'].'条<br/>','jiameng/index?page='.$page);
  447. }else{
  448. $this->error("今天提交已经到上线,明天再来吧!",'jiameng/index?page='.$page);
  449. }
  450. }else{
  451. $this->success("没有新熊掌天级连接提交",'jiameng/index?page='.$page);
  452. }
  453. }
  454. }
  455. ?>