Art.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Db;
  4. use app\common\util\Pinyin;
  5. class Art extends Base
  6. {
  7. public function __construct()
  8. {
  9. parent::__construct();
  10. }
  11. public function data()
  12. {
  13. $param = input();
  14. $param['page'] = intval($param['page']) < 1 ? 1 : $param['page'];
  15. $param['limit'] = intval($param['limit']) < 1 ? $this->_pagesize : $param['limit'];
  16. $where = [];
  17. if (!empty($param['type'])) {
  18. $where['type_id|type_id_1'] = ['eq', $param['type']];
  19. }
  20. if (!empty($param['level'])) {
  21. $where['art_level'] = ['eq', $param['level']];
  22. }
  23. if (in_array($param['status'], ['0', '1'])) {
  24. $where['art_status'] = ['eq', $param['status']];
  25. }
  26. if (!empty($param['lock'])) {
  27. $where['art_lock'] = ['eq', $param['lock']];
  28. }
  29. if(!empty($param['pic'])){
  30. if($param['pic'] == '1'){
  31. $where['art_pic'] = ['eq',''];
  32. }
  33. elseif($param['pic'] == '2'){
  34. $where['art_pic'] = ['like','http%'];
  35. }
  36. elseif($param['pic'] == '3'){
  37. $where['art_pic'] = ['like','%#err%'];
  38. }
  39. }
  40. if(!empty($param['wd'])){
  41. $param['wd'] = urldecode($param['wd']);
  42. $param['wd'] = mac_filter_xss($param['wd']);
  43. $where['art_name'] = ['like','%'.$param['wd'].'%'];
  44. }
  45. if (!empty($param['recycle'])) {
  46. $where['art_recycle_time'] = ['>', 0];
  47. }
  48. if(!empty($param['repeat'])){
  49. if($param['page'] ==1){
  50. Db::execute('DROP TABLE IF EXISTS '.config('database.prefix').'tmpart');
  51. Db::execute('CREATE TABLE `'.config('database.prefix').'tmpart` (`id1` int unsigned DEFAULT NULL, `name1` varchar(1024) NOT NULL DEFAULT \'\') ENGINE=MyISAM');
  52. Db::execute('INSERT INTO `'.config('database.prefix').'tmpart` (SELECT min(art_id)as id1,art_name as name1 FROM '.config('database.prefix').'art WHERE art_recycle_time = 0 GROUP BY name1 HAVING COUNT(name1)>1)');
  53. }
  54. $order='art_name asc';
  55. $res = model('Art')->listRepeatData($where,$order,$param['page'],$param['limit']);
  56. }
  57. else{
  58. $order='art_time desc';
  59. $res = model('Art')->listData($where,$order,$param['page'],$param['limit']);
  60. }
  61. $artIds = array_column($res['list'], 'art_id');
  62. $artSeoStatusMap = [];
  63. if (!empty($artIds)) {
  64. $seoRows = Db::name('seo_ai_result')
  65. ->field('seo_obj_id,seo_status')
  66. ->where('seo_mid', 2)
  67. ->where('seo_obj_id', 'in', $artIds)
  68. ->where('seo_status', 'in', [1, 2])
  69. ->select();
  70. foreach ((array)$seoRows as $seoRow) {
  71. $artSeoStatusMap[intval($seoRow['seo_obj_id'])] = intval($seoRow['seo_status']);
  72. }
  73. }
  74. foreach($res['list'] as $k=>&$v){
  75. $v['ismake'] = 1;
  76. if($GLOBALS['config']['view']['art_detail'] >0 && $v['art_time_make'] < $v['art_time']){
  77. $v['ismake'] = 0;
  78. }
  79. $v['seo_ai_status'] = isset($artSeoStatusMap[$v['art_id']]) ? intval($artSeoStatusMap[$v['art_id']]) : 0;
  80. }
  81. $this->assign('list', $res['list']);
  82. $this->assign('total', $res['total']);
  83. $this->assign('page', $res['page']);
  84. $this->assign('limit', $res['limit']);
  85. $param['page'] = '{page}';
  86. $param['limit'] = '{limit}';
  87. $this->assign('param', $param);
  88. $type_tree = model('Type')->getCache('type_tree');
  89. $this->assign('type_tree', $type_tree);
  90. $this->assign('title', lang('admin/art/title'));
  91. return $this->fetch('admin@art/index');
  92. }
  93. public function batch()
  94. {
  95. $param = input();
  96. if (!empty($param)) {
  97. mac_echo('<style type="text/css">body{font-size:12px;color: #333333;line-height:21px;}span{font-weight:bold;color:#FF0000}</style>');
  98. if(empty($param['ck_del']) && empty($param['ck_level']) && empty($param['ck_status']) && empty($param['ck_lock']) && empty($param['ck_hits']) && empty($param['ck_replace']) ){
  99. return $this->error(lang('param_err'));
  100. }
  101. $where = $this->artBatchFilterWhere($param);
  102. if($param['ck_del'] == 1){
  103. $res = model('Art')->recycleData($where);
  104. mac_echo($res['code'] == 1 ? lang('recycle_ok') : $res['msg']);
  105. mac_jump( url('art/batch') ,3);
  106. exit;
  107. }
  108. if($param['ck_del'] == 4){
  109. $res = model('Art')->delData($where);
  110. mac_echo(lang('multi_del_ok'));
  111. mac_jump( url('art/batch') ,3);
  112. exit;
  113. }
  114. if(empty($param['page'])){
  115. $param['page'] = 1;
  116. }
  117. if(empty($param['limit'])){
  118. $param['limit'] = 100;
  119. }
  120. if(empty($param['total'])) {
  121. $param['total'] = model('Art')->countData($where);
  122. $param['page_count'] = ceil($param['total'] / $param['limit']);
  123. }
  124. if($param['page'] > $param['page_count']) {
  125. mac_echo(lang('multi_set_ok'));
  126. mac_jump( url('art/batch') ,3);
  127. exit;
  128. }
  129. mac_echo( "<font color=red>".lang('admin/batch_tip',[$param['total'],$param['limit'],$param['page_count'],$param['page']])."</font>");
  130. $page = $param['page_count'] - $param['page'] + 1;
  131. $order='art_id desc';
  132. $res = model('Art')->listData($where,$order,$page,$param['limit']);
  133. foreach($res['list'] as $k=>$v){
  134. $where2 = [];
  135. $where2['art_id'] = $v['art_id'];
  136. $update = [];
  137. $des = $v['art_id'].','.$v['art_name'];
  138. if(!empty($param['ck_level']) && !empty($param['val_level'])){
  139. $update['art_level'] = $param['val_level'];
  140. $des .= '&nbsp;'.lang('level').':'.$param['val_level'].';';
  141. }
  142. if(!empty($param['ck_status']) && isset($param['val_status'])){
  143. $update['art_status'] = $param['val_status'];
  144. $des .= '&nbsp;'.lang('status').':'.($param['val_status'] ==1 ? '['.lang('reviewed').']':'['.lang('reviewed_not').']') .';';
  145. }
  146. if(!empty($param['ck_lock']) && isset($param['val_lock'])){
  147. $update['art_lock'] = $param['val_lock'];
  148. $des .= '&nbsp;'.lang('lock').':'.($param['val_lock']==1 ? '['.lang('lock').']':'['.lang('unlock').']').';';
  149. }
  150. if(!empty($param['ck_hits']) && !empty($param['val_hits_min']) && !empty($param['val_hits_max']) ){
  151. $update['art_hits'] = rand($param['val_hits_min'],$param['val_hits_max']);
  152. $des .= '&nbsp;'.lang('hits').':'.$update['art_hits'].';';
  153. }
  154. // 新增:批量替换功能
  155. if(!empty($param['ck_replace']) && !empty($param['replace_field']) && isset($param['replace_search'])){
  156. $field = $param['replace_field'];
  157. $replaceres = $this->batch_replace($field,$v,$param['replace_search'],$param['replace_with'],'art');
  158. if(isset($replaceres[$field])) $update[$field] = $replaceres[$field];
  159. if(!empty($replaceres['des'])) $des .= $replaceres['des'];
  160. }
  161. mac_echo($des);
  162. $res2 = model('Art')->where($where2)->update($update);
  163. }
  164. $param['page']++;
  165. $url = url('art/batch') .'?'. http_build_query($param);
  166. mac_jump( $url ,3);
  167. exit;
  168. }
  169. $type_tree = model('Type')->getCache('type_tree');
  170. $this->assign('type_tree',$type_tree);
  171. $this->assign('title',lang('admin/art/title'));
  172. return $this->fetch('admin@art/batch');
  173. }
  174. private function artBatchFilterWhere(&$param)
  175. {
  176. $where = [];
  177. if (!empty($param['type'])) {
  178. $where['type_id'] = ['eq', $param['type']];
  179. }
  180. if (!empty($param['level'])) {
  181. $where['art_level'] = ['eq', $param['level']];
  182. }
  183. if (in_array($param['status'] ?? '', ['0', '1'])) {
  184. $where['art_status'] = ['eq', $param['status']];
  185. }
  186. if (!empty($param['lock'])) {
  187. $where['art_lock'] = ['eq', $param['lock']];
  188. }
  189. if (!empty($param['pic'])) {
  190. if ($param['pic'] == '1') {
  191. $where['art_pic'] = ['eq', ''];
  192. } elseif ($param['pic'] == '2') {
  193. $where['art_pic'] = ['like', 'http%'];
  194. } elseif ($param['pic'] == '3') {
  195. $where['art_pic'] = ['like', '%#err%'];
  196. }
  197. }
  198. if (!empty($param['wd'])) {
  199. $param['wd'] = htmlspecialchars(urldecode($param['wd']));
  200. $where['art_name'] = ['like', '%' . $param['wd'] . '%'];
  201. }
  202. if (!empty($param['recycle'])) {
  203. $where['art_recycle_time'] = ['>', 0];
  204. }
  205. return $where;
  206. }
  207. public function exportData()
  208. {
  209. $param = input();
  210. $where = $this->artBatchFilterWhere($param);
  211. $this->base_export($param,'art',$where);
  212. }
  213. public function importData()
  214. {
  215. $this->base_import('art');
  216. }
  217. public function info()
  218. {
  219. if (Request()->isPost()) {
  220. $param = input('post.');
  221. $res = model('Art')->saveData($param);
  222. if($res['code']>1){
  223. return $this->error($res['msg']);
  224. }
  225. return $this->success($res['msg']);
  226. }
  227. $id = input('id');
  228. $where=[];
  229. $where['art_id'] = ['eq',$id];
  230. $where['_recycle'] = 'all';
  231. $res = model('Art')->infoData($where);
  232. $info = $res['info'];
  233. $this->assign('info',$info);
  234. $this->assign('art_page_list',(array)$info['art_page_list']);
  235. $seoAiStatus = 0;
  236. if (!empty($info['art_id'])) {
  237. $seoAi = model('SeoAiResult')->getByObject(2, intval($info['art_id']));
  238. if (!empty($seoAi)) {
  239. $seoAiStatus = intval($seoAi['seo_status']);
  240. }
  241. }
  242. $this->assign('seo_ai_status', $seoAiStatus);
  243. $type_tree = model('Type')->getCache('type_tree');
  244. $this->assign('type_tree',$type_tree);
  245. $this->assign('title',lang('admin/art/title'));
  246. return $this->fetch('admin@art/info');
  247. }
  248. public function aiSeoGenerate()
  249. {
  250. if (!Request()->isPost()) {
  251. return json(['code' => 0, 'msg' => lang('illegal_request'), 'data' => []]);
  252. }
  253. $id = intval(input('post.id'));
  254. if ($id <= 0) {
  255. return json(['code' => 0, 'msg' => lang('param_err'), 'data' => []]);
  256. }
  257. try {
  258. $res = \app\common\util\SeoAi::generateByMidObj(2, $id);
  259. } catch (\Exception $e) {
  260. \think\Log::error('AI SEO generate failed (art_id=' . $id . '): ' . $e->getMessage());
  261. return json(['code' => 0, 'msg' => $e->getMessage(), 'data' => []]);
  262. }
  263. if (empty($res['code']) || intval($res['code']) !== 1) {
  264. return json(['code' => 0, 'msg' => isset($res['msg']) ? $res['msg'] : lang('save_err'), 'data' => []]);
  265. }
  266. return json(['code' => 1, 'msg' => lang('save_ok'), 'data' => isset($res['data']) ? $res['data'] : []]);
  267. }
  268. public function restore()
  269. {
  270. $param = input();
  271. $ids = $param['ids'];
  272. if (empty($ids)) {
  273. return $this->error(lang('param_err'));
  274. }
  275. $where = ['art_id' => ['in', $ids]];
  276. $res = model('Art')->restoreData($where);
  277. if ($res['code'] > 1) {
  278. return $this->error($res['msg']);
  279. }
  280. return $this->success($res['msg']);
  281. }
  282. public function del()
  283. {
  284. $param = input();
  285. $ids = $param['ids'];
  286. $purge = !empty($param['purge']);
  287. if(!empty($ids)){
  288. $where=[];
  289. $where['art_id'] = ['in',$ids];
  290. if ($purge) {
  291. $res = model('Art')->delData($where);
  292. } else {
  293. $res = model('Art')->recycleData($where);
  294. }
  295. if($res['code']>1){
  296. return $this->error($res['msg']);
  297. }
  298. return $this->success($res['msg']);
  299. }
  300. elseif(!empty($param['repeat'])){
  301. $st = ' not in ';
  302. if($param['retain']=='max'){
  303. $st=' in ';
  304. }
  305. $sql = 'delete from '.config('database.prefix').'art where art_name in(select name1 from '.config('database.prefix').'tmpart) and art_id '.$st.'(select id1 from '.config('database.prefix').'tmpart)';
  306. $res = model('Art')->execute($sql);
  307. if($res===false){
  308. return $this->success(lang('del_err'));
  309. }
  310. return $this->success(lang('del_ok'));
  311. }
  312. return $this->error(lang('param_err'));
  313. }
  314. public function field()
  315. {
  316. $param = input();
  317. $ids = $param['ids'];
  318. $col = $param['col'];
  319. $val = $param['val'];
  320. $start = $param['start'];
  321. $end = $param['end'];
  322. if ($col == 'type_id' && $val==''){
  323. return $this->error("请选择分类提交");
  324. }
  325. if(!empty($ids) && in_array($col,['art_status','art_lock','art_level','art_hits','type_id'])){
  326. $where=[];
  327. $where['art_id'] = ['in',$ids];
  328. $update = [];
  329. if(empty($start)) {
  330. $update[$col] = $val;
  331. if($col == 'type_id'){
  332. $type_list = model('Type')->getCache();
  333. $id1 = intval($type_list[$val]['type_pid']);
  334. $update['type_id_1'] = $id1;
  335. }
  336. $res = model('Art')->fieldData($where, $update);
  337. }
  338. else{
  339. if(empty($end)){$end = 9999;}
  340. $ids = explode(',',$ids);
  341. foreach($ids as $k=>$v){
  342. $val = rand($start,$end);
  343. $where['art_id'] = ['eq',$v];
  344. $update[$col] = $val;
  345. $res = model('Art')->fieldData($where, $update);
  346. }
  347. }
  348. if($res['code']>1){
  349. return $this->error($res['msg']);
  350. }
  351. return $this->success($res['msg']);
  352. }
  353. return $this->error(lang('param_err'));
  354. }
  355. public function updateToday()
  356. {
  357. $param = input();
  358. $flag = $param['flag'];
  359. $res = model('Art')->updateToday($flag);
  360. return json($res);
  361. }
  362. }