Vod.php 34 KB

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