NewsController.php 16 KB

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