Download.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. <?php
  2. /**
  3. * 易优CMS
  4. * ============================================================================
  5. * 版权所有 2016-2028 海南赞赞网络科技有限公司,并保留所有权利。
  6. * 网站地址: http://www.eyoucms.com
  7. * ----------------------------------------------------------------------------
  8. * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
  9. * ============================================================================
  10. * Author: 小虎哥 <1105415366@qq.com>
  11. * Date: 2018-4-3
  12. */
  13. namespace app\admin\controller;
  14. use think\Page;
  15. use think\Db;
  16. class Download extends Base
  17. {
  18. // 模型标识
  19. public $nid = 'download';
  20. // 模型ID
  21. public $channeltype = '';
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $channeltype_list = config('global.channeltype_list');
  26. $this->channeltype = $channeltype_list[$this->nid];
  27. empty($this->channeltype) && $this->channeltype = 4;
  28. $this->assign('nid', $this->nid);
  29. $this->assign('channeltype', $this->channeltype);
  30. }
  31. /**
  32. * 列表
  33. */
  34. public function index()
  35. {
  36. $assign_data = array();
  37. $condition = array();
  38. // 获取到所有GET参数
  39. $param = input('param.');
  40. $flag = input('flag/s');
  41. $typeid = input('typeid/d', 0);
  42. $begin = strtotime(input('add_time_begin'));
  43. $end = strtotime(input('add_time_end'));
  44. // 应用搜索条件
  45. foreach (['keywords','typeid','flag','is_release'] as $key) {
  46. if (isset($param[$key]) && $param[$key] !== '') {
  47. if ($key == 'keywords') {
  48. $condition['a.title'] = array('LIKE', "%{$param[$key]}%");
  49. } else if ($key == 'typeid') {
  50. $typeid = $param[$key];
  51. $hasRow = model('Arctype')->getHasChildren($typeid);
  52. $typeids = get_arr_column($hasRow, 'id');
  53. /*权限控制 by 小虎哥*/
  54. $admin_info = session('admin_info');
  55. if (0 < intval($admin_info['role_id'])) {
  56. $auth_role_info = $admin_info['auth_role_info'];
  57. if(! empty($auth_role_info)){
  58. if(! empty($auth_role_info['permission']['arctype'])){
  59. if (!empty($typeid)) {
  60. $typeids = array_intersect($typeids, $auth_role_info['permission']['arctype']);
  61. }
  62. }
  63. }
  64. }
  65. /*--end*/
  66. $condition['a.typeid'] = array('IN', $typeids);
  67. } else if ($key == 'flag') {
  68. if ('is_release' == $param[$key]) {
  69. $condition['a.users_id'] = array('gt', 0);
  70. } else {
  71. $condition['a.'.$param[$key]] = array('eq', 1);
  72. }
  73. // } else if ($key == 'is_release') {
  74. // if (0 < intval($param[$key])) {
  75. // $condition['a.users_id'] = array('gt', intval($param[$key]));
  76. // }
  77. } else {
  78. $condition['a.'.$key] = array('eq', $param[$key]);
  79. }
  80. }
  81. }
  82. /*权限控制 by 小虎哥*/
  83. $admin_info = session('admin_info');
  84. if (0 < intval($admin_info['role_id'])) {
  85. $auth_role_info = $admin_info['auth_role_info'];
  86. if(! empty($auth_role_info)){
  87. if(isset($auth_role_info['only_oneself']) && 1 == $auth_role_info['only_oneself']){
  88. $condition['a.admin_id'] = $admin_info['admin_id'];
  89. }
  90. }
  91. }
  92. /*--end*/
  93. // 时间检索
  94. if ($begin > 0 && $end > 0) {
  95. $condition['a.add_time'] = array('between',"$begin,$end");
  96. } else if ($begin > 0) {
  97. $condition['a.add_time'] = array('egt', $begin);
  98. } else if ($end > 0) {
  99. $condition['a.add_time'] = array('elt', $end);
  100. }
  101. // 模型ID
  102. $condition['a.channel'] = array('eq', $this->channeltype);
  103. // 多语言
  104. $condition['a.lang'] = array('eq', $this->admin_lang);
  105. // 回收站
  106. $condition['a.is_del'] = array('eq', 0);
  107. /*自定义排序*/
  108. $orderby = input('param.orderby/s');
  109. $orderway = input('param.orderway/s');
  110. if (!empty($orderby)) {
  111. $orderby = "a.{$orderby} {$orderway}";
  112. $orderby .= ", a.aid desc";
  113. } else {
  114. $orderby = "a.aid desc";
  115. }
  116. /*end*/
  117. /**
  118. * 数据查询,搜索出主键ID的值
  119. */
  120. $count = DB::name('archives')->alias('a')->where($condition)->count('aid');// 查询满足要求的总记录数
  121. $Page = new Page($count, config('paginate.list_rows'));// 实例化分页类 传入总记录数和每页显示的记录数
  122. $fields = "b.*, a.*, a.aid as aid";
  123. $list = DB::name('archives')
  124. ->field($fields)
  125. ->alias('a')
  126. ->join('__ARCTYPE__ b', 'a.typeid = b.id', 'LEFT')
  127. ->where($condition)
  128. ->order($orderby)
  129. ->limit($Page->firstRow.','.$Page->listRows)
  130. ->getAllWithIndex('aid');
  131. foreach ($list as $key => $val) {
  132. $val['arcurl'] = get_arcurl($val);
  133. $val['litpic'] = handle_subdir_pic($val['litpic']); // 支持子目录
  134. $list[$key] = $val;
  135. }
  136. $show = $Page->show(); // 分页显示输出
  137. $assign_data['page'] = $show; // 赋值分页输出
  138. $assign_data['list'] = $list; // 赋值数据集
  139. $assign_data['pager'] = $Page; // 赋值分页对象
  140. // 栏目ID
  141. $assign_data['typeid'] = $typeid; // 栏目ID
  142. /*当前栏目信息*/
  143. $arctype_info = array();
  144. if ($typeid > 0) {
  145. $arctype_info = M('arctype')->field('typename')->find($typeid);
  146. }
  147. $assign_data['arctype_info'] = $arctype_info;
  148. /*--end*/
  149. /*选项卡*/
  150. $tab = input('param.tab/d', 3);
  151. $assign_data['tab'] = $tab;
  152. /*--end*/
  153. $this->assign($assign_data);
  154. return $this->fetch();
  155. }
  156. /**
  157. * 添加
  158. */
  159. public function add()
  160. {
  161. if (IS_POST) {
  162. $post = input('post.');
  163. $content = input('post.addonFieldExt.content', '', null);
  164. // 根据标题自动提取相关的关键字
  165. $seo_keywords = $post['seo_keywords'];
  166. if (!empty($seo_keywords)) {
  167. $seo_keywords = str_replace(',', ',', $seo_keywords);
  168. } else {
  169. // $seo_keywords = get_split_word($post['title'], $content);
  170. }
  171. // 自动获取内容第一张图片作为封面图
  172. $is_remote = !empty($post['is_remote']) ? $post['is_remote'] : 0;
  173. $litpic = '';
  174. if ($is_remote == 1) {
  175. $litpic = $post['litpic_remote'];
  176. } else {
  177. $litpic = $post['litpic_local'];
  178. }
  179. if (empty($litpic)) {
  180. $litpic = get_html_first_imgurl($content);
  181. }
  182. $post['litpic'] = $litpic;
  183. /*是否有封面图*/
  184. if (empty($post['litpic'])) {
  185. $is_litpic = 0; // 无封面图
  186. } else {
  187. $is_litpic = 1; // 有封面图
  188. }
  189. // SEO描述
  190. $seo_description = '';
  191. if (empty($post['seo_description']) && !empty($content)) {
  192. $seo_description = @msubstr(checkStrHtml($content), 0, config('global.arc_seo_description_length'), false);
  193. } else {
  194. $seo_description = $post['seo_description'];
  195. }
  196. // 外部链接跳转
  197. $jumplinks = '';
  198. $is_jump = isset($post['is_jump']) ? $post['is_jump'] : 0;
  199. if (intval($is_jump) > 0) {
  200. $jumplinks = $post['jumplinks'];
  201. }
  202. // 模板文件,如果文档模板名与栏目指定的一致,默认就为空。让它跟随栏目的指定而变
  203. if ($post['type_tempview'] == $post['tempview']) {
  204. unset($post['type_tempview']);
  205. unset($post['tempview']);
  206. }
  207. //处理自定义文件名,仅由字母数字下划线和短横杆组成,大写强制转换为小写
  208. if (!empty($post['htmlfilename'])) {
  209. $post['htmlfilename'] = preg_replace("/[^a-zA-Z0-9_-]+/", "", $post['htmlfilename']);
  210. $post['htmlfilename'] = strtolower($post['htmlfilename']);
  211. //判断是否存在相同的自定义文件名
  212. $filenameCount = Db::name('archives')->where([
  213. 'htmlfilename' => $post['htmlfilename'],
  214. ])->count();
  215. if (!empty($filenameCount)) {
  216. $this->error("自定义文件名已存在,请重新设置!");
  217. }
  218. }
  219. // --存储数据
  220. $newData = array(
  221. 'typeid'=> empty($post['typeid']) ? 0 : $post['typeid'],
  222. 'channel' => $this->channeltype,
  223. 'is_b' => empty($post['is_b']) ? 0 : $post['is_b'],
  224. 'is_head' => empty($post['is_head']) ? 0 : $post['is_head'],
  225. 'is_special' => empty($post['is_special']) ? 0 : $post['is_special'],
  226. 'is_recom' => empty($post['is_recom']) ? 0 : $post['is_recom'],
  227. 'is_jump' => $is_jump,
  228. 'is_litpic' => $is_litpic,
  229. 'jumplinks' => $jumplinks,
  230. 'seo_keywords' => $seo_keywords,
  231. 'seo_description' => $seo_description,
  232. 'admin_id' => session('admin_info.admin_id'),
  233. 'lang' => $this->admin_lang,
  234. 'sort_order' => 100,
  235. 'add_time' => strtotime($post['add_time']),
  236. 'update_time' => strtotime($post['add_time']),
  237. );
  238. $data = array_merge($post, $newData);
  239. $aid = Db::name('archives')->insertGetId($data);
  240. $_POST['aid'] = $aid;
  241. if ($aid) {
  242. // ---------后置操作
  243. model('Download')->afterSave($aid, $data, 'add');
  244. // ---------end
  245. adminLog('新增下载:'.$data['title']);
  246. // 生成静态页面代码
  247. $successData = [
  248. 'aid' => $aid,
  249. 'tid' => $post['typeid'],
  250. ];
  251. $this->success("操作成功!", null, $successData);
  252. exit;
  253. }
  254. $this->error("操作失败!");
  255. exit;
  256. }
  257. $typeid = input('param.typeid/d', 0);
  258. $assign_data['typeid'] = $typeid; // 栏目ID
  259. // 栏目信息
  260. $arctypeInfo = Db::name('arctype')->find($typeid);
  261. /*允许发布文档列表的栏目*/
  262. $arctype_html = allow_release_arctype($typeid, array($this->channeltype));
  263. $assign_data['arctype_html'] = $arctype_html;
  264. /*--end*/
  265. /*自定义字段*/
  266. $addonFieldExtList = model('Field')->getChannelFieldList($this->channeltype);
  267. $channelfieldBindRow = Db::name('channelfield_bind')->where([
  268. 'typeid' => ['IN', [0,$typeid]],
  269. ])->column('field_id');
  270. if (!empty($channelfieldBindRow)) {
  271. foreach ($addonFieldExtList as $key => $val) {
  272. if (!in_array($val['id'], $channelfieldBindRow)) {
  273. unset($addonFieldExtList[$key]);
  274. }
  275. }
  276. }
  277. $assign_data['addonFieldExtList'] = $addonFieldExtList;
  278. $assign_data['aid'] = 0;
  279. /*--end*/
  280. // 阅读权限
  281. $arcrank_list = get_arcrank_list();
  282. $assign_data['arcrank_list'] = $arcrank_list;
  283. /*模板列表*/
  284. $archivesLogic = new \app\admin\logic\ArchivesLogic;
  285. $templateList = $archivesLogic->getTemplateList($this->nid);
  286. $this->assign('templateList', $templateList);
  287. /*--end*/
  288. /*默认模板文件*/
  289. $tempview = 'view_'.$this->nid.'.'.config('template.view_suffix');
  290. !empty($arctypeInfo['tempview']) && $tempview = $arctypeInfo['tempview'];
  291. $this->assign('tempview', $tempview);
  292. /*--end*/
  293. /*会员等级信息*/
  294. $assign_data['users_level'] = DB::name('users_level')->field('level_id,level_name')->where('lang',$this->admin_lang)->select();
  295. /*--end*/
  296. /*下载模型自定义属性字段*/
  297. $attr_field = Db::name('download_attr_field')->where('field_use',1)->select();
  298. $assign_data['attr_field'] = $attr_field;
  299. /*--end*/
  300. // URL模式
  301. $tpcache = config('tpcache');
  302. $assign_data['seo_pseudo'] = !empty($tpcache['seo_pseudo']) ? $tpcache['seo_pseudo'] : 1;
  303. $this->assign($assign_data);
  304. return $this->fetch();
  305. }
  306. /**
  307. * 编辑
  308. */
  309. public function edit()
  310. {
  311. if (IS_POST) {
  312. $post = input('post.');
  313. $typeid = input('post.typeid/d', 0);
  314. $content = input('post.addonFieldExt.content', '', null);
  315. // 根据标题自动提取相关的关键字
  316. $seo_keywords = $post['seo_keywords'];
  317. if (!empty($seo_keywords)) {
  318. $seo_keywords = str_replace(',', ',', $seo_keywords);
  319. } else {
  320. // $seo_keywords = get_split_word($post['title'], $content);
  321. }
  322. // 自动获取内容第一张图片作为封面图
  323. $is_remote = !empty($post['is_remote']) ? $post['is_remote'] : 0;
  324. $litpic = '';
  325. if ($is_remote == 1) {
  326. $litpic = $post['litpic_remote'];
  327. } else {
  328. $litpic = $post['litpic_local'];
  329. }
  330. if (empty($litpic)) {
  331. $litpic = get_html_first_imgurl($content);
  332. }
  333. $post['litpic'] = $litpic;
  334. /*是否有封面图*/
  335. if (empty($post['litpic'])) {
  336. $is_litpic = 0; // 无封面图
  337. } else {
  338. $is_litpic = !empty($post['is_litpic']) ? $post['is_litpic'] : 0; // 有封面图
  339. }
  340. // SEO描述
  341. $seo_description = '';
  342. if (empty($post['seo_description']) && !empty($content)) {
  343. $seo_description = @msubstr(checkStrHtml($content), 0, config('global.arc_seo_description_length'), false);
  344. } else {
  345. $seo_description = $post['seo_description'];
  346. }
  347. // --外部链接
  348. $jumplinks = '';
  349. $is_jump = isset($post['is_jump']) ? $post['is_jump'] : 0;
  350. if (intval($is_jump) > 0) {
  351. $jumplinks = $post['jumplinks'];
  352. }
  353. // 模板文件,如果文档模板名与栏目指定的一致,默认就为空。让它跟随栏目的指定而变
  354. if ($post['type_tempview'] == $post['tempview']) {
  355. unset($post['type_tempview']);
  356. unset($post['tempview']);
  357. }
  358. //处理自定义文件名,仅由字母数字下划线和短横杆组成,大写强制转换为小写
  359. if (!empty($post['htmlfilename'])) {
  360. $post['htmlfilename'] = preg_replace("/[^a-zA-Z0-9_-]+/", "", $post['htmlfilename']);
  361. $post['htmlfilename'] = strtolower($post['htmlfilename']);
  362. //判断是否存在相同的自定义文件名
  363. $filenameCount = Db::name('archives')->where([
  364. 'aid' => ['NEQ', $post['aid']],
  365. 'htmlfilename' => $post['htmlfilename'],
  366. ])->count();
  367. if (!empty($filenameCount)) {
  368. $this->error("自定义文件名已存在,请重新设置!");
  369. }
  370. }
  371. // 同步栏目切换模型之后的文档模型
  372. $channel = Db::name('arctype')->where(['id'=>$typeid])->getField('current_channel');
  373. // --存储数据
  374. $newData = array(
  375. 'typeid'=> $typeid,
  376. 'channel' => $channel,
  377. 'is_b' => empty($post['is_b']) ? 0 : $post['is_b'],
  378. 'is_head' => empty($post['is_head']) ? 0 : $post['is_head'],
  379. 'is_special' => empty($post['is_special']) ? 0 : $post['is_special'],
  380. 'is_recom' => empty($post['is_recom']) ? 0 : $post['is_recom'],
  381. 'is_jump' => $is_jump,
  382. 'is_litpic' => $is_litpic,
  383. 'jumplinks' => $jumplinks,
  384. 'seo_keywords' => $seo_keywords,
  385. 'seo_description' => $seo_description,
  386. 'add_time' => strtotime($post['add_time']),
  387. 'update_time' => getTime(),
  388. );
  389. $data = array_merge($post, $newData);
  390. $r = Db::name('archives')->where([
  391. 'aid' => $data['aid'],
  392. 'lang' => $this->admin_lang,
  393. ])->update($data);
  394. if ($r) {
  395. // ---------后置操作
  396. model('Download')->afterSave($data['aid'], $data, 'edit');
  397. // ---------end
  398. adminLog('编辑下载:'.$data['title']);
  399. // 生成静态页面代码
  400. $successData = [
  401. 'aid' => $data['aid'],
  402. 'tid' => $typeid,
  403. ];
  404. $this->success("操作成功!", null, $successData);
  405. exit;
  406. }
  407. $this->error("操作失败!");
  408. exit;
  409. }
  410. $assign_data = array();
  411. $id = input('id/d');
  412. $info = model('Download')->getInfo($id);
  413. if (empty($info)) {
  414. $this->error('数据不存在,请联系管理员!');
  415. exit;
  416. }
  417. /*兼容采集没有归属栏目的文档*/
  418. if (empty($info['channel'])) {
  419. $channelRow = Db::name('channeltype')->field('id as channel')
  420. ->where('id',$this->channeltype)
  421. ->find();
  422. $info = array_merge($info, $channelRow);
  423. }
  424. /*--end*/
  425. $typeid = $info['typeid'];
  426. // 栏目信息
  427. $arctypeInfo = Db::name('arctype')->find($typeid);
  428. $info['channel'] = $arctypeInfo['current_channel'];
  429. if (is_http_url($info['litpic'])) {
  430. $info['is_remote'] = 1;
  431. $info['litpic_remote'] = handle_subdir_pic($info['litpic']);
  432. } else {
  433. $info['is_remote'] = 0;
  434. $info['litpic_local'] = handle_subdir_pic($info['litpic']);
  435. }
  436. // SEO描述
  437. if (!empty($info['seo_description'])) {
  438. $info['seo_description'] = @msubstr(checkStrHtml($info['seo_description']), 0, config('global.arc_seo_description_length'), false);
  439. }
  440. $assign_data['field'] = $info;
  441. // 下载文件
  442. $downfile_list = model('DownloadFile')->getDownFile($id);
  443. $assign_data['downfile_list'] = $downfile_list;
  444. // 下载文件中是否存在远程链接
  445. $is_remote_file = 0;
  446. foreach ($downfile_list as $key => $value) {
  447. if (1 == $value['is_remote']) {
  448. $is_remote_file = 1;
  449. break;
  450. }
  451. }
  452. $assign_data['is_remote_file'] = $is_remote_file;
  453. /*允许发布文档列表的栏目,文档所在模型以栏目所在模型为主,兼容切换模型之后的数据编辑*/
  454. $arctype_html = allow_release_arctype($typeid, array($info['channel']));
  455. $assign_data['arctype_html'] = $arctype_html;
  456. /*--end*/
  457. /*自定义字段*/
  458. $addonFieldExtList = model('Field')->getChannelFieldList($info['channel'], 0, $id, $info);
  459. $channelfieldBindRow = Db::name('channelfield_bind')->where([
  460. 'typeid' => ['IN', [0,$typeid]],
  461. ])->column('field_id');
  462. if (!empty($channelfieldBindRow)) {
  463. foreach ($addonFieldExtList as $key => $val) {
  464. if (!in_array($val['id'], $channelfieldBindRow)) {
  465. unset($addonFieldExtList[$key]);
  466. }
  467. }
  468. }
  469. $assign_data['addonFieldExtList'] = $addonFieldExtList;
  470. $assign_data['aid'] = $id;
  471. /*--end*/
  472. // 阅读权限
  473. $arcrank_list = get_arcrank_list();
  474. $assign_data['arcrank_list'] = $arcrank_list;
  475. /*模板列表*/
  476. $archivesLogic = new \app\admin\logic\ArchivesLogic;
  477. $templateList = $archivesLogic->getTemplateList($this->nid);
  478. $this->assign('templateList', $templateList);
  479. /*--end*/
  480. /*默认模板文件*/
  481. $tempview = $info['tempview'];
  482. empty($tempview) && $tempview = $arctypeInfo['tempview'];
  483. $this->assign('tempview', $tempview);
  484. /*--end*/
  485. /*会员等级信息*/
  486. $assign_data['users_level'] = DB::name('users_level')->field('level_id,level_name')->where('lang',$this->admin_lang)->select();
  487. /*--end*/
  488. /*下载模型自定义属性字段*/
  489. $attr_field = Db::name('download_attr_field')->where('field_use',1)->select();
  490. $assign_data['attr_field'] = $attr_field;
  491. /*--end*/
  492. // URL模式
  493. $tpcache = config('tpcache');
  494. $assign_data['seo_pseudo'] = !empty($tpcache['seo_pseudo']) ? $tpcache['seo_pseudo'] : 1;
  495. $this->assign($assign_data);
  496. return $this->fetch();
  497. }
  498. /**
  499. * 删除
  500. */
  501. public function del()
  502. {
  503. if (IS_POST) {
  504. $archivesLogic = new \app\admin\logic\ArchivesLogic;
  505. $archivesLogic->del();
  506. }
  507. }
  508. public function template_set()
  509. {
  510. if (IS_AJAX_POST) {
  511. $post = input('post.');
  512. // 修改是否使用
  513. if (!empty($post) && isset($post['field_use'])) {
  514. $data['field_use'] = $post['field_use'];
  515. $data['update_time'] = getTime();
  516. Db::name('download_attr_field')->where('field_id',$post['field_id'])->update($data);
  517. $this->success("更新成功!");
  518. }
  519. // 修改标题
  520. if (!empty($post) && isset($post['field_title'])) {
  521. $data['field_title'] = $post['field_title'];
  522. $data['update_time'] = getTime();
  523. Db::name('download_attr_field')->where('field_id',$post['field_id'])->update($data);
  524. $this->success("更新成功!");
  525. }
  526. }
  527. $list = Db::name('download_attr_field')->select();
  528. $assign_data['list'] = $list;
  529. $this->assign($assign_data);
  530. return $this->fetch();
  531. }
  532. public function get_template()
  533. {
  534. if (IS_AJAX_POST) {
  535. $list = Db::name('download_attr_field')->where('field_use',1)->select();
  536. $this->success("查询成功!", null, $list);
  537. }
  538. }
  539. }