Vod.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Cache;
  5. use app\common\util\Pinyin;
  6. use app\common\validate\Vod as VodValidate;
  7. class Vod extends Base {
  8. use RecycleBinTrait;
  9. // 设置数据表(不含前缀)
  10. protected $name = 'vod';
  11. protected function getRecycleTimeField(): string
  12. {
  13. return 'vod_recycle_time';
  14. }
  15. // 定义时间戳字段名
  16. protected $createTime = '';
  17. protected $updateTime = '';
  18. // 自动完成
  19. protected $auto = [];
  20. protected $insert = [];
  21. protected $update = [];
  22. public function countData($where)
  23. {
  24. $this->ensureRecycleColumnExists();
  25. if(!is_array($where)){
  26. $where = json_decode($where,true);
  27. }
  28. $where = $this->mergeRecycleWhere($where);
  29. $where2='';
  30. if(!empty($where['_string'])){
  31. $where2 = $where['_string'];
  32. unset($where['_string']);
  33. }
  34. $total = $this->where($where)->where($where2)->count();
  35. return $total;
  36. }
  37. public function listData($where,$order,$page=1,$limit=20,$start=0,$field='*',$addition=1,$totalshow=1)
  38. {
  39. $this->ensureRecycleColumnExists();
  40. $page = $page > 0 ? (int)$page : 1;
  41. $limit = $limit ? (int)$limit : 20;
  42. $start = $start ? (int)$start : 0;
  43. if(!is_array($where)){
  44. $where = json_decode($where,true);
  45. }
  46. $where = $this->mergeRecycleWhere($where);
  47. $where2='';
  48. if(!empty($where['_string'])){
  49. $where2 = $where['_string'];
  50. unset($where['_string']);
  51. }
  52. $limit_str = ($limit * ($page-1) + $start) .",".$limit;
  53. if($totalshow==1) {
  54. $total = $this->where($where)->where($where2)->count();
  55. }
  56. $list = Db::name('Vod')->field($field)->where($where)->where($where2)->order($order)->limit($limit_str)->select();
  57. //分类
  58. $type_list = model('Type')->getCache('type_list');
  59. //用户组
  60. $group_list = model('Group')->getCache('group_list');
  61. foreach($list as $k=>$v){
  62. if($addition==1){
  63. if(!empty($v['type_id'])) {
  64. $list[$k]['type'] = $type_list[$v['type_id']];
  65. $list[$k]['type_1'] = $type_list[$list[$k]['type']['type_pid']];
  66. }
  67. if(!empty($v['group_id'])) {
  68. $list[$k]['group'] = $group_list[$v['group_id']];
  69. }
  70. }
  71. }
  72. return ['code'=>1,'msg'=>lang('data_list'),'page'=>$page,'pagecount'=>ceil($total/$limit),'limit'=>$limit,'total'=>$total,'list'=>$list];
  73. }
  74. public function listRepeatData($where,$order,$page=1,$limit=20,$start=0,$field='*',$addition=1)
  75. {
  76. $this->ensureRecycleColumnExists();
  77. $page = $page > 0 ? (int)$page : 1;
  78. $limit = $limit ? (int)$limit : 20;
  79. $start = $start ? (int)$start : 0;
  80. if(!is_array($where)){
  81. $where = json_decode($where,true);
  82. }
  83. $where = $this->mergeRecycleWhere($where);
  84. $limit_str = ($limit * ($page-1) + $start) .",".$limit;
  85. $total = $this
  86. ->join('vod_repeat t','t.name1 = vod_name')
  87. ->where($where)
  88. ->count();
  89. $list = Db::name('Vod')
  90. ->join('vod_repeat t','t.name1 = vod_name')
  91. ->field($field)
  92. ->where($where)
  93. ->order($order)
  94. ->limit($limit_str)
  95. ->select();
  96. //分类
  97. $type_list = model('Type')->getCache('type_list');
  98. //用户组
  99. $group_list = model('Group')->getCache('group_list');
  100. foreach($list as $k=>$v){
  101. if($addition==1){
  102. if(!empty($v['type_id'])) {
  103. $list[$k]['type'] = $type_list[$v['type_id']];
  104. $list[$k]['type_1'] = $type_list[$list[$k]['type']['type_pid']];
  105. }
  106. if(!empty($v['group_id'])) {
  107. $list[$k]['group'] = $group_list[$v['group_id']];
  108. }
  109. }
  110. }
  111. return ['code'=>1,'msg'=>lang('data_list'),'page'=>$page,'pagecount'=>ceil($total/$limit),'limit'=>$limit,'total'=>$total,'list'=>$list];
  112. }
  113. public function listCacheData($lp,$field='*')
  114. {
  115. if(!is_array($lp)){
  116. $lp = json_decode($lp,true);
  117. }
  118. $order = $lp['order'];
  119. $by = $lp['by'];
  120. $type = $lp['type'];
  121. $ids = $lp['ids'];
  122. $rel = $lp['rel'];
  123. $paging = $lp['paging'];
  124. $pageurl = $lp['pageurl'];
  125. $level = $lp['level'];
  126. $area = $lp['area'];
  127. $lang = $lp['lang'];
  128. $state = $lp['state'];
  129. $wd = $lp['wd'];
  130. $tag = $lp['tag'];
  131. $class = $lp['class'];
  132. $letter = $lp['letter'];
  133. $actor = $lp['actor'];
  134. $director = $lp['director'];
  135. $version = $lp['version'];
  136. $year = $lp['year'];
  137. $start = intval(abs($lp['start']));
  138. $num = intval(abs($lp['num']));
  139. $half = intval(abs($lp['half']));
  140. $weekday = $lp['weekday'];
  141. $tv = $lp['tv'];
  142. $timeadd = $lp['timeadd'];
  143. $timehits = $lp['timehits'];
  144. $time = $lp['time'];
  145. $hitsmonth = $lp['hitsmonth'];
  146. $hitsweek = $lp['hitsweek'];
  147. $hitsday = $lp['hitsday'];
  148. $hits = $lp['hits'];
  149. $not = $lp['not'];
  150. $cachetime = $lp['cachetime'];
  151. $isend = $lp['isend'];
  152. $plot = $lp['plot'];
  153. $typenot = $lp['typenot'];
  154. $name = $lp['name'];
  155. $page = 1;
  156. $where=[];
  157. $totalshow = 0;
  158. if(empty($num)){
  159. $num = 20;
  160. }
  161. if($start>0){
  162. $start--;
  163. }
  164. if(!in_array($paging, ['yes', 'no'])) {
  165. $paging = 'no';
  166. }
  167. $param = mac_param_url();
  168. if($paging=='yes') {
  169. $param = mac_search_len_check($param);
  170. $totalshow = 1;
  171. if(!empty($param['id'])){
  172. //$type = intval($param['id']);
  173. }
  174. if(!empty($param['level'])){
  175. $level = $param['level'];
  176. }
  177. if(!empty($param['ids'])){
  178. $ids = $param['ids'];
  179. }
  180. if(!empty($param['tid'])) {
  181. $tid = intval($param['tid']);
  182. }
  183. if(!empty($param['year'])){
  184. if(strlen($param['year'])==4){
  185. $year = intval($param['year']);
  186. }
  187. elseif(strlen($param['year'])==9){
  188. $s=substr($param['year'],0,4);
  189. $e=substr($param['year'],5,4);
  190. $s1 = intval($s);$s2 = intval($e);
  191. if($s1>$s2){
  192. $s1 = intval($e);$s2 = intval($s);
  193. }
  194. $tmp=[];
  195. for($i=$s1;$i<=$s2;$i++){
  196. $tmp[] = $i;
  197. }
  198. $year = join(',',$tmp);
  199. }
  200. }
  201. if(!empty($param['area'])){
  202. $area = $param['area'];
  203. }
  204. if(!empty($param['lang'])){
  205. $lang = $param['lang'];
  206. }
  207. if(!empty($param['tag'])){
  208. $tag = $param['tag'];
  209. }
  210. if(!empty($param['class'])){
  211. $class = $param['class'];
  212. }
  213. if(!empty($param['state'])){
  214. $state = $param['state'];
  215. }
  216. if(!empty($param['letter'])){
  217. $letter = $param['letter'];
  218. }
  219. if(!empty($param['version'])){
  220. $version = $param['version'];
  221. }
  222. if(!empty($param['actor'])){
  223. $actor = $param['actor'];
  224. }
  225. if(!empty($param['director'])){
  226. $director = $param['director'];
  227. }
  228. if(!empty($param['wd'])){
  229. $wd = $param['wd'];
  230. }
  231. if(!empty($param['name'])){
  232. $name = $param['name'];
  233. }
  234. if(!empty($param['by'])){
  235. $by = $param['by'];
  236. }
  237. if(!empty($param['order'])){
  238. $order = $param['order'];
  239. }
  240. if(!empty($param['page'])){
  241. $page = intval($param['page']);
  242. }
  243. if(isset($param['isend'])){
  244. $isend = intval($param['isend']);
  245. }
  246. foreach($param as $k=>$v){
  247. if(empty($v)){
  248. unset($param[$k]);
  249. }
  250. }
  251. if(empty($pageurl)){
  252. $pageurl = 'vod/type';
  253. }
  254. $param['page'] = 'PAGELINK';
  255. if($pageurl=='vod/type' || $pageurl=='vod/show'){
  256. $type = intval( $GLOBALS['type_id'] );
  257. $type_list = model('Type')->getCache('type_list');
  258. $type_info = $type_list[$type];
  259. $flag='type';
  260. if($pageurl == 'vod/show'){
  261. $flag='show';
  262. }
  263. $pageurl = mac_url_type($type_info,$param,$flag);
  264. }
  265. else{
  266. $pageurl = mac_url($pageurl,$param);
  267. }
  268. }
  269. $where['vod_status'] = ['eq',1];
  270. if(!empty($ids)) {
  271. if($ids!='all'){
  272. $where['vod_id'] = ['in',explode(',',$ids)];
  273. }
  274. }
  275. if(!empty($not)){
  276. $where['vod_id'] = ['not in',explode(',',$not)];
  277. }
  278. if(!empty($rel)){
  279. $tmp = explode(',',$rel);
  280. if(is_numeric($rel) || mac_array_check_num($tmp)==true ){
  281. $where['vod_id'] = ['in',$tmp];
  282. }
  283. else{
  284. $where['vod_rel_vod'] = ['like', mac_like_arr($rel),'OR'];
  285. }
  286. }
  287. if(!empty($level)) {
  288. if($level=='all'){
  289. $level = '1,2,3,4,5,6,7,8,9';
  290. }
  291. $where['vod_level'] = ['in',explode(',',$level)];
  292. }
  293. if(!empty($year)) {
  294. $where['vod_year'] = ['in',explode(',',$year)];
  295. }
  296. if(!empty($area)) {
  297. $where['vod_area'] = ['in',explode(',',$area)];
  298. }
  299. if(!empty($lang)) {
  300. $where['vod_lang'] = ['in',explode(',',$lang)];
  301. }
  302. if(!empty($state)) {
  303. $where['vod_state'] = ['in',explode(',',$state)];
  304. }
  305. if(!empty($version)) {
  306. $where['vod_version'] = ['in',explode(',',$version)];
  307. }
  308. if(!empty($weekday)){
  309. //$where['vod_weekday'] = ['in',explode(',',$weekday)];
  310. $where['vod_weekday'] = ['like', mac_like_arr($weekday),'OR'];
  311. }
  312. if(!empty($tv)){
  313. $where['vod_tv'] = ['in',explode(',',$tv)];
  314. }
  315. if(!empty($timeadd)){
  316. $s = intval(strtotime($timeadd));
  317. $where['vod_time_add'] =['gt',$s];
  318. }
  319. if(!empty($timehits)){
  320. $s = intval(strtotime($timehits));
  321. $where['vod_time_hits'] =['gt',$s];
  322. }
  323. if(!empty($time)){
  324. $s = intval(strtotime($time));
  325. $where['vod_time'] =['gt',$s];
  326. }
  327. if(!empty($letter)){
  328. if(substr($letter,0,1)=='0' && substr($letter,2,1)=='9'){
  329. $letter='0,1,2,3,4,5,6,7,8,9';
  330. }
  331. $where['vod_letter'] = ['in',explode(',',$letter)];
  332. }
  333. if(!empty($type)) {
  334. if($type=='current'){
  335. $type = intval( $GLOBALS['type_id'] );
  336. }
  337. if($type!='all') {
  338. $tmp_arr = explode(',',$type);
  339. $type_list = model('Type')->getCache('type_list');
  340. $type = [];
  341. foreach($type_list as $k2=>$v2){
  342. if(in_array($v2['type_id'].'',$tmp_arr) || in_array($v2['type_pid'].'',$tmp_arr)){
  343. $type[]=$v2['type_id'];
  344. }
  345. }
  346. $type = array_unique($type);
  347. $where['type_id'] = ['in', implode(',',$type) ];
  348. }
  349. }
  350. if(!empty($typenot)){
  351. $typenot_arr = array_map('intval', explode(',', $typenot));
  352. // 如果同时存在 type 和 typenot,需在已有 type 条件上排除 typenot
  353. if(!empty($type) && is_array($type)){
  354. $type = array_diff($type, $typenot_arr);
  355. if(!empty($type)){
  356. $where['type_id'] = ['in', implode(',', $type)];
  357. } else {
  358. // type 被完全排除后,设置一个永不匹配的条件,避免查询所有视频
  359. $where['type_id'] = ['eq', -1];
  360. }
  361. } else {
  362. $where['type_id'] = ['not in',$typenot];
  363. }
  364. }
  365. if(!empty($tid)) {
  366. $where['type_id|type_id_1'] = ['eq',$tid];
  367. }
  368. if(!in_array($GLOBALS['aid'],[13,14,15]) && !empty($param['id'])){
  369. //$where['vod_id'] = ['not in',$param['id']];
  370. }
  371. if(!empty($hitsmonth)){
  372. $tmp = explode(' ',$hitsmonth);
  373. if(count($tmp)==1){
  374. $where['vod_hits_month'] = ['gt', $tmp];
  375. }
  376. else{
  377. $where['vod_hits_month'] = [$tmp[0],$tmp[1]];
  378. }
  379. }
  380. if(!empty($hitsweek)){
  381. $tmp = explode(' ',$hitsweek);
  382. if(count($tmp)==1){
  383. $where['vod_hits_week'] = ['gt', $tmp];
  384. }
  385. else{
  386. $where['vod_hits_week'] = [$tmp[0],$tmp[1]];
  387. }
  388. }
  389. if(!empty($hitsday)){
  390. $tmp = explode(' ',$hitsday);
  391. if(count($tmp)==1){
  392. $where['vod_hits_day'] = ['gt', $tmp];
  393. }
  394. else{
  395. $where['vod_hits_day'] = [$tmp[0],$tmp[1]];
  396. }
  397. }
  398. if(!empty($hits)){
  399. $tmp = explode(' ',$hits);
  400. if(count($tmp)==1){
  401. $where['vod_hits'] = ['gt', $tmp];
  402. }
  403. else{
  404. $where['vod_hits'] = [$tmp[0],$tmp[1]];
  405. }
  406. }
  407. if(in_array($isend,['0','1'])){
  408. $where['vod_isend'] = $isend;
  409. }
  410. $vod_search = model('VodSearch');
  411. $vod_search_enabled = $vod_search->isFrontendEnabled();
  412. $max_id_count = $vod_search->maxIdCount;
  413. if ($vod_search_enabled) {
  414. // 开启搜索优化,查询并缓存Id
  415. $search_id_list = [];
  416. if(!empty($wd)) {
  417. $role = 'vod_name';
  418. if(!empty($GLOBALS['config']['app']['search_vod_rule'])){
  419. $role .= '|'.$GLOBALS['config']['app']['search_vod_rule'];
  420. }
  421. $where[$role] = ['like', '%' . $wd . '%'];
  422. if (count($search_id_list_tmp = $vod_search->getResultIdList($wd, $role)) <= $max_id_count) {
  423. $search_id_list += $search_id_list_tmp;
  424. unset($where[$role]);
  425. }
  426. }
  427. if(!empty($name)) {
  428. $where['vod_name'] = ['like',mac_like_arr($name),'OR'];
  429. if (count($search_id_list_tmp = $vod_search->getResultIdList($name, 'vod_name')) <= $max_id_count) {
  430. $search_id_list += $search_id_list_tmp;
  431. unset($where['vod_name']);
  432. }
  433. }
  434. if(!empty($tag)) {
  435. $where['vod_tag'] = ['like',mac_like_arr($tag),'OR'];
  436. if (count($search_id_list_tmp = $vod_search->getResultIdList($tag, 'vod_tag', true)) <= $max_id_count) {
  437. $search_id_list += $search_id_list_tmp;
  438. unset($where['vod_tag']);
  439. }
  440. }
  441. if(!empty($class)) {
  442. $where['vod_class'] = ['like',mac_like_arr($class), 'OR'];
  443. if (count($search_id_list_tmp = $vod_search->getResultIdList($class, 'vod_class', true)) <= $max_id_count) {
  444. $search_id_list += $search_id_list_tmp;
  445. unset($where['vod_class']);
  446. }
  447. }
  448. if(!empty($actor)) {
  449. $where['vod_actor'] = ['like', mac_like_arr($actor), 'OR'];
  450. if (count($search_id_list_tmp = $vod_search->getResultIdList($actor, 'vod_actor', true)) <= $max_id_count) {
  451. $search_id_list += $search_id_list_tmp;
  452. unset($where['vod_actor']);
  453. }
  454. }
  455. if(!empty($director)) {
  456. $where['vod_director'] = ['like',mac_like_arr($director),'OR'];
  457. if (count($search_id_list_tmp = $vod_search->getResultIdList($director, 'vod_director', true)) <= $max_id_count) {
  458. $search_id_list += $search_id_list_tmp;
  459. unset($where['vod_director']);
  460. }
  461. }
  462. $search_id_list = array_unique($search_id_list);
  463. if (!empty($search_id_list)) {
  464. $where['_string'] = "vod_id IN (" . join(',', $search_id_list) . ")";
  465. }
  466. } else {
  467. // 不开启搜索优化,使用默认条件
  468. if(!empty($wd)) {
  469. $role = 'vod_name';
  470. if(!empty($GLOBALS['config']['app']['search_vod_rule'])){
  471. $role .= '|'.$GLOBALS['config']['app']['search_vod_rule'];
  472. }
  473. $where[$role] = ['like', '%' . $wd . '%'];
  474. }
  475. if(!empty($name)) {
  476. $where['vod_name'] = ['like',mac_like_arr($name),'OR'];
  477. }
  478. if(!empty($tag)) {
  479. $where['vod_tag'] = ['like',mac_like_arr($tag),'OR'];
  480. }
  481. if(!empty($class)) {
  482. $where['vod_class'] = ['like',mac_like_arr($class), 'OR'];
  483. }
  484. if(!empty($actor)) {
  485. $where['vod_actor'] = ['like', mac_like_arr($actor), 'OR'];
  486. }
  487. if(!empty($director)) {
  488. $where['vod_director'] = ['like',mac_like_arr($director),'OR'];
  489. }
  490. }
  491. if(in_array($plot,['0','1'])){
  492. $where['vod_plot'] = $plot;
  493. }
  494. if(defined('ENTRANCE') && ENTRANCE == 'index' && $GLOBALS['config']['app']['popedom_filter'] ==1){
  495. $type_ids = mac_get_popedom_filter($GLOBALS['user']['group']['group_type']);
  496. if(!empty($type_ids)){
  497. if(!empty($where['type_id'])){
  498. $where['type_id'] = [ $where['type_id'],['not in', explode(',',$type_ids)] ];
  499. }
  500. else{
  501. $where['type_id'] = ['not in', explode(',',$type_ids)];
  502. }
  503. }
  504. }
  505. // 优化随机视频排序rnd的性能问题
  506. // https://github.com/magicblack/maccms10/issues/967
  507. $use_rand = false;
  508. if($by=='rnd'){
  509. $use_rand = true;
  510. $algo2_threshold = 2000;
  511. $data_count = $this->countData($where);
  512. $where_string_addon = "";
  513. if ($data_count > $algo2_threshold) {
  514. $rows = $this->field("vod_id")->where($where)->select();
  515. foreach ($rows as $row) {
  516. $id_list[] = $row['vod_id'];
  517. }
  518. if (
  519. !empty($id_list)
  520. ) {
  521. $random_count = intval($algo2_threshold / 2);
  522. $specified_list = array_rand($id_list, intval($algo2_threshold / 2));
  523. $random_keys = array_rand($id_list, $random_count);
  524. $specified_list = [];
  525. if ($random_count == 1) {
  526. $specified_list[] = $id_list[$random_keys];
  527. } else {
  528. foreach ($random_keys as $key) {
  529. $specified_list[] = $id_list[$key];
  530. }
  531. }
  532. if (!empty($specified_list)) {
  533. $where_string_addon = " AND vod_id IN (" . join(',', $specified_list) . ")";
  534. }
  535. }
  536. }
  537. if (!empty($where_string_addon)) {
  538. $where['_string'] .= $where_string_addon;
  539. $where['_string'] = trim($where['_string'], " AND ");
  540. } else {
  541. if ($data_count % $lp['num'] === 0) {
  542. $page_total = floor($data_count / $lp['num']);
  543. } else {
  544. $page_total = floor($data_count / $lp['num']) + 1;
  545. }
  546. if($data_count < $lp['num']){
  547. $lp['num'] = $data_count;
  548. }
  549. $randi = @mt_rand(1, $page_total);
  550. $page = $randi;
  551. }
  552. $by = 'hits_week';
  553. $order = 'desc';
  554. }
  555. if(!in_array($by, ['id', 'time','time_add','score','hits','hits_day','hits_week','hits_month','up','down','level','rnd'])) {
  556. $by = 'time';
  557. }
  558. if(!in_array($order, ['asc', 'desc'])) {
  559. $order = 'desc';
  560. }
  561. $order= 'vod_'.$by .' ' . $order;
  562. $where_cache = $where;
  563. if($use_rand){
  564. unset($where_cache['vod_id']);
  565. $where_cache['order'] = 'rnd';
  566. }
  567. $cach_name = $GLOBALS['config']['app']['cache_flag']. '_' .md5('vod_listcache_'.http_build_query($where_cache).'_'.$order.'_'.$page.'_'.$num.'_'.$start.'_'.$pageurl);
  568. $res = Cache::get($cach_name);
  569. if(empty($cachetime)){
  570. $cachetime = $GLOBALS['config']['app']['cache_time'];
  571. }
  572. if($GLOBALS['config']['app']['cache_core']==0 || empty($res)) {
  573. $res = $this->listData($where, $order, $page, $num, $start,$field,1, $totalshow);
  574. if($GLOBALS['config']['app']['cache_core']==1) {
  575. Cache::set($cach_name, $res, $cachetime);
  576. }
  577. }
  578. $res['pageurl'] = $pageurl;
  579. $res['half'] = $half;
  580. return $res;
  581. }
  582. public function infoData($where,$field='*',$cache=0)
  583. {
  584. if(empty($where) || !is_array($where)){
  585. return ['code'=>1001,'msg'=>lang('param_err')];
  586. }
  587. $this->ensureRecycleColumnExists();
  588. $where = $this->mergeRecycleWhere($where);
  589. $data_cache = false;
  590. $key = $GLOBALS['config']['app']['cache_flag']. '_'.'vod_detail_'.$where['vod_id'][1].'_'.$where['vod_en'][1];
  591. if($where['vod_id'][0]=='eq' || $where['vod_en'][0]=='eq'){
  592. $data_cache = true;
  593. }
  594. if($GLOBALS['config']['app']['cache_core']==1 && $data_cache) {
  595. $info = Cache::get($key);
  596. }
  597. if($GLOBALS['config']['app']['cache_core']==0 || $cache==0 || empty($info['vod_id'])) {
  598. $info = $this->field($field)->where($where)->find();
  599. if (empty($info)) {
  600. return ['code' => 1002, 'msg' => lang('obtain_err')];
  601. }
  602. $info = $info->toArray();
  603. $info['vod_play_list']=[];
  604. $info['vod_down_list']=[];
  605. $info['vod_plot_list']=[];
  606. $info['vod_pic_screenshot_list']=[];
  607. if (!empty($info['vod_play_from'])) {
  608. $info['vod_play_list'] = mac_play_list($info['vod_play_from'], $info['vod_play_url'], $info['vod_play_server'], $info['vod_play_note'], 'play');
  609. }
  610. if (!empty($info['vod_down_from'])) {
  611. $info['vod_down_list'] = mac_play_list($info['vod_down_from'], $info['vod_down_url'], $info['vod_down_server'], $info['vod_down_note'], 'down');
  612. }
  613. if (!empty($info['vod_plot_name'])) {
  614. $info['vod_plot_list'] = mac_plot_list($info['vod_plot_name'], $info['vod_plot_detail']);
  615. }
  616. if(!empty($info['vod_pic_screenshot'])){
  617. $info['vod_pic_screenshot_list'] = mac_screenshot_list($info['vod_pic_screenshot']);
  618. }
  619. //分类
  620. if (!empty($info['type_id'])) {
  621. $type_list = model('Type')->getCache('type_list');
  622. $info['type'] = $type_list[$info['type_id']];
  623. $info['type_1'] = $type_list[$info['type']['type_pid']];
  624. }
  625. //用户组
  626. if (!empty($info['group_id'])) {
  627. $group_list = model('Group')->getCache('group_list');
  628. $info['group'] = $group_list[$info['group_id']];
  629. }
  630. if($GLOBALS['config']['app']['cache_core']==1 && $data_cache && $cache==1) {
  631. Cache::set($key, $info);
  632. }
  633. }
  634. return ['code'=>1,'msg'=>lang('obtain_ok'),'info'=>$info];
  635. }
  636. public function saveData($data)
  637. {
  638. $validate = \think\Loader::validate('Vod');
  639. if(!$validate->check($data)){
  640. return ['code'=>1001,'msg'=>lang('param_err').':'.$validate->getError() ];
  641. }
  642. $key = 'vod_detail_'.$data['vod_id'];
  643. Cache::rm($key);
  644. $key = 'vod_detail_'.$data['vod_en'];
  645. Cache::rm($key);
  646. $key = 'vod_detail_'.$data['vod_id'].'_'.$data['vod_en'];
  647. Cache::rm($key);
  648. $type_list = model('Type')->getCache('type_list');
  649. $type_info = $type_list[$data['type_id']];
  650. $data['type_id_1'] = $type_info['type_pid'];
  651. if(empty($data['vod_en'])){
  652. $data['vod_en'] = Pinyin::get($data['vod_name']);
  653. }
  654. if(empty($data['vod_letter'])){
  655. $data['vod_letter'] = strtoupper(substr($data['vod_en'],0,1));
  656. }
  657. if(!empty($data['vod_content'])) {
  658. $pattern_src = '/<img[\s\S]*?src\s*=\s*[\"|\'](.*?)[\"|\'][\s\S]*?>/';
  659. @preg_match_all($pattern_src, $data['vod_content'], $match_src1);
  660. if (!empty($match_src1)) {
  661. foreach ($match_src1[1] as $v1) {
  662. $v2 = str_replace($GLOBALS['config']['upload']['protocol'] . ':', 'mac:', $v1);
  663. $data['vod_content'] = str_replace($v1, $v2, $data['vod_content']);
  664. }
  665. }
  666. unset($match_src1);
  667. }
  668. if(empty($data['vod_blurb'])){
  669. $data['vod_blurb'] = mac_substring( strip_tags($data['vod_content']) ,100);
  670. }
  671. if(empty($data['vod_play_url'])){
  672. $data['vod_play_url'] = '';
  673. }
  674. if(empty($data['vod_down_url'])){
  675. $data['vod_down_url'] = '';
  676. }
  677. if(!empty($data['vod_pic_screenshot'])){
  678. $data['vod_pic_screenshot'] = str_replace( array(chr(10),chr(13)), array('','#'),$data['vod_pic_screenshot']);
  679. }
  680. if(!empty($data['vod_play_from'])) {
  681. $data['vod_play_from'] = join('$$$', $data['vod_play_from']);
  682. $data['vod_play_server'] = join('$$$', $data['vod_play_server']);
  683. $data['vod_play_note'] = join('$$$', $data['vod_play_note']);
  684. $data['vod_play_url'] = join('$$$', $data['vod_play_url']);
  685. $data['vod_play_url'] = str_replace( array(chr(10),chr(13)), array('','#'),$data['vod_play_url']);
  686. }
  687. else{
  688. $data['vod_play_from'] = '';
  689. $data['vod_play_server'] = '';
  690. $data['vod_play_note'] = '';
  691. $data['vod_play_url'] = '';
  692. }
  693. if(!empty($data['vod_down_from'])) {
  694. $data['vod_down_from'] = join('$$$', $data['vod_down_from']);
  695. $data['vod_down_server'] = join('$$$', $data['vod_down_server']);
  696. $data['vod_down_note'] = join('$$$', $data['vod_down_note']);
  697. $data['vod_down_url'] = join('$$$', $data['vod_down_url']);
  698. $data['vod_down_url'] = str_replace(array(chr(10),chr(13)), array('','#'),$data['vod_down_url']);
  699. }else{
  700. $data['vod_down_from']='';
  701. $data['vod_down_server']='';
  702. $data['vod_down_note']='';
  703. $data['vod_down_url']='';
  704. }
  705. if($data['uptime']==1){
  706. $data['vod_time'] = time();
  707. }
  708. if($data['uptag']==1){
  709. $data['vod_tag'] = mac_get_tag($data['vod_name'], $data['vod_content']);
  710. }
  711. unset($data['uptime']);
  712. unset($data['uptag']);
  713. $data = VodValidate::formatDataBeforeDb($data);
  714. $seoObjId = 0;
  715. if(!empty($data['vod_id'])){
  716. $where=[];
  717. $where['vod_id'] = ['eq',$data['vod_id']];
  718. $res = $this->allowField(true)->where($where)->update($data);
  719. //编辑 先获取到之前的name
  720. $old_name = $this->where('vod_id',$data['vod_id'])->value('vod_name');
  721. if($old_name!=$data['vod_name']){
  722. $this->cacheRepeatWithName($old_name);
  723. $this->cacheRepeatWithName($data['vod_name']);
  724. }else{
  725. $this->cacheRepeatWithName($data['vod_name']);
  726. }
  727. $seoObjId = intval($data['vod_id']);
  728. }
  729. else{
  730. $data['vod_plot'] = 0;
  731. $data['vod_plot_name']='';
  732. $data['vod_plot_detail']='';
  733. $data['vod_time_add'] = time();
  734. $data['vod_time'] = time();
  735. $res = $this->allowField(true)->insert($data, false, true);
  736. $seoObjId = intval($this->getLastInsID());
  737. if ($res > 0 && model('VodSearch')->isFrontendEnabled()) {
  738. model('VodSearch')->checkAndUpdateTopResults(['vod_id' => $seoObjId] + $data);
  739. }
  740. //新增 针对当前name 判断是否重复
  741. $this->cacheRepeatWithName($data['vod_name']);
  742. }
  743. if(false === $res){
  744. return ['code'=>1002,'msg'=>lang('save_err').':'.$this->getError() ];
  745. }
  746. return ['code'=>1,'msg'=>lang('save_ok')];
  747. }
  748. public function savePlot($data)
  749. {
  750. $validate = \think\Loader::validate('Vod');
  751. if(!$validate->check($data)){
  752. return ['code'=>1001,'msg'=>lang('param_err').':'.$validate->getError() ];
  753. }
  754. $key = 'vod_detail_'.$data['vod_id'];
  755. Cache::rm($key);
  756. $key = 'vod_detail_'.$data['vod_en'];
  757. Cache::rm($key);
  758. $key = 'vod_detail_'.$data['vod_id'].'_'.$data['vod_en'];
  759. Cache::rm($key);
  760. if(!empty($data['vod_plot_name'])) {
  761. $data['vod_plot'] = 1;
  762. $data['vod_plot_name'] = join('$$$', $data['vod_plot_name']);
  763. $data['vod_plot_detail'] = join('$$$', $data['vod_plot_detail']);
  764. }else{
  765. $data['vod_plot'] = 0;
  766. $data['vod_plot_name']='';
  767. $data['vod_plot_detail']='';
  768. }
  769. if(!empty($data['vod_id'])){
  770. $where=[];
  771. $where['vod_id'] = ['eq',$data['vod_id']];
  772. $res = $this->allowField(true)->where($where)->update($data);
  773. }
  774. else{
  775. $res = false;
  776. }
  777. if(false === $res){
  778. return ['code'=>1002,'msg'=>lang('save_err').':'.$this->getError() ];
  779. }
  780. return ['code'=>1,'msg'=>lang('save_ok')];
  781. }
  782. public function delData($where)
  783. {
  784. if(!is_array($where)){
  785. $where = json_decode($where,true);
  786. }
  787. if(!is_array($where)){
  788. return ['code'=>1001,'msg'=>lang('param_err')];
  789. }
  790. $where['_recycle'] = 'all';
  791. $list = $this->listData($where,'',1,9999);
  792. if($list['code'] !==1){
  793. return ['code'=>1001,'msg'=>lang('del_err').':'.$this->getError() ];
  794. }
  795. $where = $this->mergeRecycleWhere($where);
  796. $path = './';
  797. foreach($list['list'] as $k=>$v){
  798. $pic = $path.$v['vod_pic'];
  799. if(file_exists($pic) && (substr($pic,0,8) == "./upload") || count( explode("./",$pic) ) ==1){
  800. unlink($pic);
  801. }
  802. $pic = $path.$v['vod_pic_thumb'];
  803. if(file_exists($pic) && (substr($pic,0,8) == "./upload") || count( explode("./",$pic) ) ==1){
  804. unlink($pic);
  805. }
  806. $pic = $path.$v['vod_pic_slide'];
  807. if(file_exists($pic) && (substr($pic,0,8) == "./upload") || count( explode("./",$pic) ) ==1){
  808. unlink($pic);
  809. }
  810. if($GLOBALS['config']['view']['vod_detail'] ==2 ){
  811. $lnk = mac_url_vod_detail($v);
  812. $lnk = reset_html_filename($lnk);
  813. if(file_exists($lnk)){
  814. unlink($lnk);
  815. }
  816. }
  817. }
  818. $res = $this->where($where)->delete();
  819. if($res===false){
  820. return ['code'=>1002,'msg'=>lang('del_err').':'.$this->getError() ];
  821. }
  822. return ['code'=>1,'msg'=>lang('del_ok')];
  823. }
  824. public function fieldData($where,$update)
  825. {
  826. if(!is_array($update)){
  827. return ['code'=>1001,'msg'=>lang('param_err')];
  828. }
  829. $res = $this->allowField(true)->where($where)->update($update);
  830. if($res===false){
  831. return ['code'=>1001,'msg'=>lang('set_err').':'.$this->getError() ];
  832. }
  833. $list = $this->field('vod_id,vod_name,vod_en')->where($where)->select();
  834. foreach($list as $k=>$v){
  835. $key = 'vod_detail_'.$v['vod_id'];
  836. Cache::rm($key);
  837. $key = 'vod_detail_'.$v['vod_en'];
  838. Cache::rm($key);
  839. }
  840. return ['code'=>1,'msg'=>lang('set_ok')];
  841. }
  842. public function updateToday($flag='vod')
  843. {
  844. $today = strtotime(date('Y-m-d'));
  845. $where = [];
  846. $where['vod_time'] = ['gt',$today];
  847. if($flag=='type'){
  848. $ids = $this->where($where)->column('type_id');
  849. }
  850. else{
  851. $ids = $this->where($where)->column('vod_id');
  852. }
  853. if(empty($ids)){
  854. $ids = [];
  855. }else{
  856. $ids = array_unique($ids);
  857. }
  858. return ['code'=>1,'msg'=>lang('obtain_ok'),'data'=> join(',',$ids) ];
  859. }
  860. public function cacheRepeatWithName($name)
  861. {
  862. try{
  863. Db::execute('delete from `' . config('database.prefix') . 'vod_repeat` where name1 =?', [$name]);
  864. Db::execute('INSERT INTO `' . config('database.prefix') . 'vod_repeat` (SELECT min(vod_id)as id1,vod_name as name1 FROM ' . config('database.prefix') . 'vod WHERE vod_name = ? AND vod_recycle_time = 0 GROUP BY name1 HAVING COUNT(name1)>1)', [$name]);
  865. }catch (\Exception $e){
  866. Db::execute('DROP TABLE IF EXISTS ' . config('database.prefix') . 'vod_repeat');
  867. Db::execute('CREATE TABLE `' . config('database.prefix') . 'vod_repeat` (`id1` int unsigned DEFAULT NULL, `name1` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT \'\') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci');
  868. Db::execute('ALTER TABLE `' . config('database.prefix') . 'vod_repeat` ADD INDEX `name1` (`name1`(100))');
  869. }
  870. Db::execute('INSERT INTO `' . config('database.prefix') . 'vod_repeat` (SELECT min(vod_id)as id1,vod_name as name1 FROM ' .
  871. config('database.prefix') . 'vod WHERE vod_recycle_time = 0 GROUP BY name1 HAVING COUNT(name1)>1)');
  872. Cache::set('vod_repeat_table_created_time',time());
  873. }
  874. public function createRepeatCache()
  875. {
  876. $prefix = config('database.prefix');
  877. $tableName = $prefix . 'vod_repeat';
  878. try{
  879. Db::execute("TRUNCATE TABLE `{$tableName}`");
  880. }catch (\Exception $e){
  881. //创建表
  882. Db::execute('DROP TABLE IF EXISTS ' . config('database.prefix') . 'vod_repeat');
  883. Db::execute('CREATE TABLE `' . config('database.prefix') . 'vod_repeat` (`id1` int unsigned DEFAULT NULL, `name1` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT \'\') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci');
  884. Db::execute('ALTER TABLE `' . config('database.prefix') . 'vod_repeat` ADD INDEX `name1` (`name1`(100))');
  885. }
  886. Db::execute('INSERT INTO `' . config('database.prefix') . 'vod_repeat` (SELECT min(vod_id)as id1,vod_name as name1 FROM ' .
  887. config('database.prefix') . 'vod WHERE vod_recycle_time = 0 GROUP BY name1 HAVING COUNT(name1)>1)');
  888. Cache::set('vod_repeat_table_created_time',time());
  889. }
  890. }