Video.php 14 KB

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