NewsController.php 18 KB

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