Video.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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[4];
  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. Db::startTrans();
  166. try {
  167. //是否采用模型验证
  168. if ($this->modelValidate) {
  169. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  170. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  171. $row->validateFailException(true)->validate($validate);
  172. }
  173. $result = $row->allowField(true)->save($params);
  174. Db::commit();
  175. } catch (ValidateException $e) {
  176. Db::rollback();
  177. $this->error($e->getMessage());
  178. } catch (PDOException $e) {
  179. Db::rollback();
  180. $this->error($e->getMessage());
  181. } catch (Exception $e) {
  182. Db::rollback();
  183. $this->error($e->getMessage());
  184. }
  185. if ($result !== false) {
  186. //内容同步
  187. $cspid = config('cspid');
  188. // $url = 'http://'.config('inject_ip').':'.config('inject_port').'/epgDataSync/v1/' . $cspid . '/';
  189. $url = 'http://jscp.agency.gitv.tv/epgDataSync/v1/' . $cspid . '/';
  190. $videofile = $params['videofile'];
  191. $videofile = explode('/', $videofile);
  192. $video_url = $videofile[4];
  193. $data = [
  194. // 'cspId' => $cspid,
  195. 'cpAlbumId' => $ids,
  196. 'albumName' => $params['title'],
  197. 'tvSets' => 1,
  198. 'updateToSet' => 1,
  199. 'isPurchase' => $params['is_pay'],
  200. 'isEffective' => $params['status'],
  201. 'isOnline' => $params['status'],
  202. 'albumTypes' => '健康',
  203. 'picBox' => config('upload.cdnurl') . $params['image'],
  204. 'albumUpdateTime' => date('YmdHis', time()),
  205. // 'albumCreateTime' => date('YmdHis', time()),
  206. 'tvlist' => [
  207. [
  208. 'cpAlbumId' => $ids,
  209. 'cpTvId' => $ids,
  210. 'tvName' => $params['title'],
  211. 'tvIsEffective' => $params['status'],
  212. 'tvIsOnline' => $params['status'],
  213. 'isPurchase' => $params['is_pay'],
  214. 'playOrder' => 1,
  215. 'tvUrl' => 'ftp://'.config('ftp_user').':'.config('ftp_pwd').'@'.config('ftp_ip').':'.config('ftp_port').'/'.$video_url
  216. ]
  217. ]
  218. ];
  219. $data = json_encode($data, 320);
  220. $header = [
  221. 'Content-Type: application/json'
  222. ];
  223. $rs = httpRequest($url, 'POST', $data, $header);
  224. if ($rs) {
  225. $rs = json_decode($rs, true);
  226. if ($rs['code'] == 'A000000') {
  227. Db::name('video')->where(['id' => $ids])->setField('inject_status', 1);
  228. }
  229. }
  230. $this->success();
  231. } else {
  232. $this->error(__('No rows were updated'));
  233. }
  234. }
  235. $this->error(__('Parameter %s can not be empty', ''));
  236. }
  237. $this->view->assign("row", $row);
  238. return $this->view->fetch();
  239. }
  240. /**
  241. * 删除
  242. */
  243. public function del($ids = "")
  244. {
  245. if (!$this->request->isPost()) {
  246. $this->error(__("Invalid parameters"));
  247. }
  248. $ids = $ids ? $ids : $this->request->post("ids");
  249. $params = Db::name('video')->find($ids);
  250. if (!$params) {
  251. $this->success();
  252. }
  253. Db::startTrans();
  254. $rt = Db::name('video')->delete($ids);
  255. if (!$rt) {
  256. Db::rollback();
  257. $this->error('删除失败');
  258. }
  259. //内容同步
  260. $cspid = config('cspid');
  261. // $url = 'http://'.config('inject_ip').':'.config('inject_port').'/epgDeleteSync/v1/' . $cspid;
  262. $url = 'http://jscp.agency.gitv.tv/epgDeleteSync/v1/' . $cspid . '/';
  263. $data = [
  264. // 'cspId' => $cspid,
  265. 'cpAlbumId' => $ids,
  266. 'albumName' => $params['title'],
  267. 'tvSets' => 1,
  268. 'updateToSet' => 1,
  269. 'isPurchase' => $params['is_pay'],
  270. 'isEffective' => 0,
  271. 'isOnline' => 2,
  272. 'albumTypes' => '健康',
  273. 'picBox' => config('upload.cdnurl') . $params['image'],
  274. 'albumUpdateTime' => date('YmdHis', time()),
  275. // 'albumCreateTime' => date('YmdHis', time()),
  276. 'tvlist' => [
  277. [
  278. 'cpAlbumId' => $ids,
  279. 'cpTvId' => $ids,
  280. 'tvName' => $params['title'],
  281. 'tvIsEffective' => 0,
  282. 'tvIsOnline' => 2,
  283. 'isPurchase' => $params['is_pay'],
  284. 'playOrder' => 1,
  285. 'tvUrl' => 'ftp://'.config('ftp_user').':'.config('ftp_pwd').'@'.config('ftp_ip').':'.config('ftp_port').'/'.$params['title'].'.mp4'
  286. ]
  287. ]
  288. ];
  289. $data = json_encode($data, 320);
  290. $header = [
  291. 'Content-Type: application/json'
  292. ];
  293. $rs = httpRequest($url, 'POST', $data, $header);
  294. if (!$rs) {
  295. Db::rollback();
  296. $this->error('删除失败');
  297. }
  298. $rs = json_decode($rs, true);
  299. if ($rs['code'] != 'A000000') {
  300. Db::rollback();
  301. $this->error('删除失败');
  302. }
  303. Db::commit();
  304. $this->success();
  305. }
  306. }