Video.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use Overtrue\Pinyin\Pinyin;
  6. /**
  7. * 视频专区
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Video extends Backend
  12. {
  13. /**
  14. * Video模型对象
  15. * @var \app\admin\model\Video
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\Video;
  22. $this->view->assign("isPayList", $this->model->getIsPayList());
  23. $this->view->assign("statusList", $this->model->getStatusList());
  24. }
  25. /**
  26. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  27. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  28. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  29. */
  30. /**
  31. * 查看
  32. */
  33. public function index()
  34. {
  35. //当前是否为关联查询
  36. $this->relationSearch = true;
  37. //设置过滤方法
  38. $this->request->filter(['strip_tags', 'trim']);
  39. if ($this->request->isAjax()) {
  40. //如果发送的来源是Selectpage,则转发到Selectpage
  41. if ($this->request->request('keyField')) {
  42. return $this->selectpage();
  43. }
  44. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  45. $list = $this->model
  46. ->with(['type','cate','childcate'])
  47. ->where($where)
  48. ->order($sort, $order)
  49. ->paginate($limit);
  50. foreach ($list as $row) {
  51. $row->getRelation('type')->visible(['type','name']);
  52. $row->getRelation('cate')->visible(['name']);
  53. $row->getRelation('childcate')->visible(['name']);
  54. }
  55. $result = array("total" => $list->total(), "rows" => $list->items());
  56. return json($result);
  57. }
  58. return $this->view->fetch();
  59. }
  60. /**
  61. * 添加
  62. */
  63. public function add()
  64. {
  65. if ($this->request->isPost()) {
  66. $params = $this->request->post("row/a");
  67. if ($params) {
  68. $params = $this->preExcludeFields($params);
  69. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  70. $params[$this->dataLimitField] = $this->auth->id;
  71. }
  72. $result = false;
  73. Db::startTrans();
  74. try {
  75. //是否采用模型验证
  76. if ($this->modelValidate) {
  77. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  78. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  79. $this->model->validateFailException(true)->validate($validate);
  80. }
  81. //首字母
  82. if(empty($params['search_title'])){
  83. $params['search_title'] = $this->shouzimu($params['title']);
  84. }
  85. $result = $this->model->allowField(true)->save($params);
  86. $id = $this->model->id;
  87. Db::commit();
  88. } catch (ValidateException $e) {
  89. Db::rollback();
  90. $this->error($e->getMessage());
  91. } catch (PDOException $e) {
  92. Db::rollback();
  93. $this->error($e->getMessage());
  94. } catch (Exception $e) {
  95. Db::rollback();
  96. $this->error($e->getMessage());
  97. }
  98. if ($result !== false) {
  99. //内容同步
  100. $cspid = config('cspid');
  101. // $url = 'http://'.config('inject_ip').':'.config('inject_port').'/epgDataSync/v1/' . $cspid;
  102. $url = 'http://jscp.agency.gitv.tv/epgDataSync/v1/' . $cspid . '/';
  103. $videofile = $params['videofile'];
  104. $videofile = explode('/', $videofile);
  105. $video_url = $videofile[3];
  106. $data = [
  107. // 'cspId' => $cspid,
  108. 'cpAlbumId' => $id,
  109. 'albumName' => $params['title'],
  110. 'tvSets' => 1,
  111. 'updateToSet' => 1,
  112. 'isPurchase' => $params['is_pay'],
  113. 'isEffective' => $params['status'],
  114. 'isOnline' => $params['status'],
  115. 'albumTypes' => '健康',
  116. 'picBox' => config('upload.cdnurl') . $params['image'],
  117. 'albumUpdateTime' => date('YmdHis', time()),
  118. 'albumCreateTime' => date('YmdHis', time()),
  119. 'tvlist' => [
  120. [
  121. 'cpAlbumId' => $id,
  122. 'cpTvId' => $id,
  123. 'tvName' => $params['title'],
  124. 'tvIsEffective' => $params['status'],
  125. 'tvIsOnline' => $params['status'],
  126. 'isPurchase' => $params['is_pay'],
  127. 'playOrder' => 1,
  128. 'tvUrl' => 'ftp://'.config('ftp_user').':'.config('ftp_pwd').'@'.config('ftp_ip').':'.config('ftp_port').'/inject/'.$video_url
  129. ]
  130. ]
  131. ];
  132. $data = json_encode($data, 320);
  133. $header = [
  134. 'Content-Type: application/json'
  135. ];
  136. $rs = httpRequest($url, 'POST', $data, $header);
  137. if ($rs) {
  138. $rs = json_decode($rs, true);
  139. if ($rs['code'] == 'A000000') {
  140. //注入中兴
  141. $url2 = 'http://jscp.agency.gitv.tv/epgDataSync/v1/' . config('cspid_zx') . '/';
  142. $rs2 = httpRequest($url2, 'POST', $data, $header);
  143. if ($rs2) {
  144. $rs2 = json_decode($rs2, true);
  145. if ($rs2['code'] == 'A000000') {
  146. Db::name('video')->where(['id' => $id])->setField('inject_status', 1);
  147. }
  148. }
  149. }
  150. }
  151. $this->success();
  152. } else {
  153. $this->error(__('No rows were inserted'));
  154. }
  155. }
  156. $this->error(__('Parameter %s can not be empty', ''));
  157. }
  158. return $this->view->fetch();
  159. }
  160. /**
  161. * 编辑
  162. */
  163. public function edit($ids = null)
  164. {
  165. $row = $this->model->get($ids);
  166. if (!$row) {
  167. $this->error(__('No Results were found'));
  168. }
  169. $adminIds = $this->getDataLimitAdminIds();
  170. if (is_array($adminIds)) {
  171. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  172. $this->error(__('You have no permission'));
  173. }
  174. }
  175. if ($this->request->isPost()) {
  176. $params = $this->request->post("row/a");
  177. if ($params) {
  178. $params = $this->preExcludeFields($params);
  179. $result = false;
  180. $params['search_status'] = 0;
  181. Db::startTrans();
  182. try {
  183. //是否采用模型验证
  184. if ($this->modelValidate) {
  185. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  186. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  187. $row->validateFailException(true)->validate($validate);
  188. }
  189. $result = $row->allowField(true)->save($params);
  190. Db::commit();
  191. } catch (ValidateException $e) {
  192. Db::rollback();
  193. $this->error($e->getMessage());
  194. } catch (PDOException $e) {
  195. Db::rollback();
  196. $this->error($e->getMessage());
  197. } catch (Exception $e) {
  198. Db::rollback();
  199. $this->error($e->getMessage());
  200. }
  201. if ($result !== false) {
  202. //内容同步
  203. /*$cspid = config('cspid');
  204. // $url = 'http://'.config('inject_ip').':'.config('inject_port').'/epgDataSync/v1/' . $cspid . '/';
  205. $url = 'http://jscp.agency.gitv.tv/epgDataSync/v1/' . $cspid . '/';
  206. $videofile = $row['videofile'];
  207. $videofile = explode('/', $videofile);
  208. $video_url = $videofile[3];
  209. $data = [
  210. // 'cspId' => $cspid,
  211. 'cpAlbumId' => $ids,
  212. 'albumName' => $params['title'],
  213. 'tvSets' => 1,
  214. 'updateToSet' => 1,
  215. 'isPurchase' => $params['is_pay'],
  216. 'isEffective' => $params['status'],
  217. 'isOnline' => $params['status'],
  218. 'albumTypes' => '健康',
  219. 'picBox' => config('upload.cdnurl') . $params['image'],
  220. 'albumUpdateTime' => date('YmdHis', time()),
  221. // 'albumCreateTime' => date('YmdHis', time()),
  222. 'tvlist' => [
  223. [
  224. 'cpAlbumId' => $ids,
  225. 'cpTvId' => $ids,
  226. 'tvName' => $params['title'],
  227. 'tvIsEffective' => $params['status'],
  228. 'tvIsOnline' => $params['status'],
  229. 'isPurchase' => $params['is_pay'],
  230. 'playOrder' => 1,
  231. 'tvUrl' => 'ftp://'.config('ftp_user').':'.config('ftp_pwd').'@'.config('ftp_ip').':'.config('ftp_port').'/inject/'.$video_url
  232. ]
  233. ]
  234. ];
  235. $data = json_encode($data, 320);
  236. $header = [
  237. 'Content-Type: application/json'
  238. ];
  239. $rs = httpRequest($url, 'POST', $data, $header);
  240. if ($rs) {
  241. $rs = json_decode($rs, true);
  242. if ($rs['code'] == 'A000000') {
  243. Db::name('video')->where(['id' => $ids])->setField('inject_status', 1);
  244. }
  245. }*/
  246. $this->success();
  247. } else {
  248. $this->error(__('No rows were updated'));
  249. }
  250. }
  251. $this->error(__('Parameter %s can not be empty', ''));
  252. }
  253. $this->view->assign("row", $row);
  254. return $this->view->fetch();
  255. }
  256. /**
  257. * 删除
  258. */
  259. public function del($ids = "")
  260. {
  261. if (!$this->request->isPost()) {
  262. $this->error(__("Invalid parameters"));
  263. }
  264. $ids = $ids ? $ids : $this->request->post("ids");
  265. $params = Db::name('video')->find($ids);
  266. if (!$params) {
  267. $this->success();
  268. }
  269. if ($params['status'] != 0) {
  270. $this->error('请先将视频状态修改为不显示');
  271. }
  272. Db::startTrans();
  273. $rt = Db::name('video')->delete($ids);
  274. if (!$rt) {
  275. Db::rollback();
  276. $this->error('删除失败');
  277. }
  278. //内容同步
  279. $cspid = config('cspid');
  280. // $url = 'http://'.config('inject_ip').':'.config('inject_port').'/epgDeleteSync/v1/' . $cspid;
  281. $url = 'http://jscp.agency.gitv.tv/epgDeleteSync/v1/' . $cspid . '/';
  282. $videofile = $params['videofile'];
  283. $videofile = explode('/', $videofile);
  284. $video_url = $videofile[3];
  285. $data = [
  286. // 'cspId' => $cspid,
  287. 'cpAlbumId' => $ids,
  288. 'albumName' => $params['title'],
  289. 'tvSets' => 1,
  290. 'updateToSet' => 1,
  291. 'isPurchase' => $params['is_pay'],
  292. 'isEffective' => 0,
  293. 'isOnline' => 2,
  294. 'albumTypes' => '健康',
  295. 'picBox' => config('upload.cdnurl') . $params['image'],
  296. 'albumUpdateTime' => date('YmdHis', time()),
  297. // 'albumCreateTime' => date('YmdHis', time()),
  298. 'tvlist' => [
  299. [
  300. 'cpAlbumId' => $ids,
  301. 'cpTvId' => $ids,
  302. 'tvName' => $params['title'],
  303. 'tvIsEffective' => 0,
  304. 'tvIsOnline' => 2,
  305. 'isPurchase' => $params['is_pay'],
  306. 'playOrder' => 1,
  307. 'tvUrl' => 'ftp://'.config('ftp_user').':'.config('ftp_pwd').'@'.config('ftp_ip').':'.config('ftp_port').'/inject/'.$video_url
  308. ]
  309. ]
  310. ];
  311. $data = json_encode($data, 320);
  312. $header = [
  313. 'Content-Type: application/json'
  314. ];
  315. $rs = httpRequest($url, 'POST', $data, $header);
  316. if (!$rs) {
  317. Db::rollback();
  318. $this->error('删除失败');
  319. }
  320. $rs = json_decode($rs, true);
  321. if ($rs['code'] != 'A000000') {
  322. Db::rollback();
  323. $this->error('删除失败');
  324. }
  325. //删除中兴内容
  326. $url2 = 'http://jscp.agency.gitv.tv/epgDeleteSync/v1/' . config('cspid_zx') . '/';
  327. $rs2 = httpRequest($url2, 'POST', $data, $header);
  328. /*if (!$rs2) {
  329. Db::rollback();
  330. $this->error('删除失败');
  331. }
  332. $rs2 = json_decode($rs2, true);
  333. if ($rs2['code'] != 'A000000') {
  334. Db::rollback();
  335. $this->error('删除失败');
  336. }*/
  337. //生成ftp文件
  338. $content = [
  339. 'cpPrvdName' => '健康e家',
  340. 'cpPrvCode' => '41000144',
  341. 'cpPrvType' => '1006',
  342. 'ChnName' => '健康',
  343. 'ChnCode' => '1000020',
  344. 'actors' => '无',
  345. 'directors' => '无',
  346. 'contentYear' => date('Y'),
  347. 'tags' => '',
  348. 'contentTime' => date('Y-m-d H:i:s'),
  349. 'formatType' => '超清'
  350. ];
  351. //生成ftp文件
  352. $content['contentId'] = $params['movieid'];
  353. $content['extraContentID'] = $params['movieid'];
  354. $content['content'] = $params['title'];
  355. $content['intent'] = [
  356. 'action' => 'com.huxiu.heh.tv.ui.splash.SplashActivity',
  357. 'package' => 'com.huxiu.heh.tv',
  358. 'component' => [
  359. 'pkg' => 'com.huxiu.heh.tv',
  360. 'cls' => 'com.huxiu.heh.tv.ui.splash.SplashActivity'
  361. ],
  362. 'extras' => [
  363. // 'cmd' => '',
  364. // 'from' => '',
  365. // 'media_id' => ''
  366. 'type_id' => $params['video_type_id'],
  367. 'id' => $params['id']
  368. ]
  369. ];
  370. $content['isEffective'] = $params['status'];
  371. $content['pic'] = one_domain_image($params['image']);
  372. $content['isPaid'] = $params['is_pay'];
  373. $content = json_encode($content, 320);
  374. $filename = 'shipin_'.date('Ymd').'_'.date('His').'_increment'.'.json';
  375. error_log(print_r($content, 1) . PHP_EOL, 3, './jiankang/' . $filename);
  376. Db::commit();
  377. $this->success();
  378. }
  379. //中文字符串
  380. //ZWZFC
  381. private function shouzimu($string)
  382. {
  383. // 小内存型
  384. $pinyin = new Pinyin(); // 默认
  385. $pinyin_arr = $pinyin->convert($string);
  386. $shouzimu = '';
  387. if(!empty($pinyin_arr)){
  388. foreach($pinyin_arr as $key => $val){
  389. $shouzimu .= strtoupper(substr($val,0,1));
  390. }
  391. }
  392. return $shouzimu;
  393. }
  394. /**
  395. * 同步到旧版
  396. */
  397. public function tongbu(){
  398. $id = input('id');
  399. $video = Db::name('video')->find($id);
  400. if (!$video) {
  401. $this->error('未找到视频');
  402. }
  403. unset($video['id']);
  404. unset($video['search_title']);
  405. unset($video['good_number']);
  406. unset($video['collect_number']);
  407. unset($video['childcate_id']);
  408. unset($video['cate_id']);
  409. //旧版video表检索,有就更新,没有新增
  410. $check = Db::connect('database_jiankang')->table('hu_video')->where('id',$id)->find();
  411. if($check){
  412. Db::connect('database_jiankang')->table('hu_video')->where('id',$id)->update($video);
  413. }else{
  414. Db::connect('database_jiankang')->table('hu_video')->insertGetId($video);
  415. }
  416. $this->success();
  417. }
  418. }