NewsController.php 18 KB

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