NewsController.php 16 KB

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