NewsController.php 17 KB

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