LiveApply.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <?php
  2. /**
  3. * 商品管理
  4. */
  5. namespace app\admin\controller;
  6. use think\facade\View;
  7. use think\facade\Lang;
  8. use think\facade\Db;
  9. use TencentCloud\Common\Credential;
  10. use TencentCloud\Common\Profile\ClientProfile;
  11. use TencentCloud\Common\Profile\HttpProfile;
  12. use TencentCloud\Common\Exception\TencentCloudSDKException;
  13. use TencentCloud\Live\V20180801\LiveClient;
  14. use TencentCloud\Live\V20180801\Models\DropLiveStreamRequest;
  15. use TencentCloud\Live\V20180801\Models\DescribeLiveStreamStateRequest;
  16. use AlibabaCloud\Client\AlibabaCloud;
  17. /**
  18. * ============================================================================
  19. * DSKMS多用户商城
  20. * ============================================================================
  21. *
  22. * ----------------------------------------------------------------------------
  23. *
  24. * ============================================================================
  25. * 控制器
  26. */
  27. class LiveApply extends AdminControl {
  28. public function initialize() {
  29. parent::initialize();
  30. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/live_apply.lang.php');
  31. }
  32. /**
  33. * 商品管理
  34. */
  35. public function index() {
  36. /**
  37. * 查询条件
  38. */
  39. $condition = array();
  40. $store_model = model('store');
  41. if (config('ds_config.live_type') == 1) {
  42. $minipro_live_model = model('minipro_live');
  43. $minipro_live_list = $minipro_live_model->getMiniproLiveList($condition);
  44. $store_list = array();
  45. foreach ($minipro_live_list as $key => $val) {
  46. $minipro_live_list[$key]['minipro_live_image_url'] = ds_get_pic( ATTACH_MINIPRO_LIVE , $val['minipro_live_image']);
  47. if (!isset($store_list[$val['store_id']])) {
  48. $store_list[$val['store_id']] = $store_model->getStoreInfo(array('store_id' => $val['store_id']));
  49. }
  50. $minipro_live_list[$key]['store_name'] = $store_list[$val['store_id']]['store_name'] ? $store_list[$val['store_id']]['store_name'] : '';
  51. }
  52. View::assign('minipro_live_list', $minipro_live_list);
  53. View::assign('show_page', $minipro_live_model->page_info->render());
  54. } else {
  55. $live_apply_model = model('live_apply');
  56. $live_apply_state = input('param.live_apply_state');
  57. if (in_array($live_apply_state, array('0', '1', '2'))) {
  58. $condition[] = array('live_apply_state', '=', $live_apply_state);
  59. }
  60. $live_apply_list = $live_apply_model->getLiveApplyList($condition, '*', 10, 'live_apply_state asc,live_apply_id desc');
  61. $store_list = array();
  62. foreach ($live_apply_list as $key => $val) {
  63. if ($val['live_apply_cover_video']) {
  64. $live_apply_list[$key]['live_apply_cover_video_url'] = ds_get_pic( ATTACH_LIVE_APPLY . '/' . $val['live_apply_user_id'] , $val['live_apply_cover_video']);
  65. } elseif ($val['live_apply_cover_image']) {
  66. $live_apply_list[$key]['live_apply_cover_image_url'] = ds_get_pic( ATTACH_LIVE_APPLY . '/' . $val['live_apply_user_id'] , $val['live_apply_cover_image']);
  67. } else {
  68. $live_apply_list[$key]['live_apply_cover_image_url'] = ds_get_pic(ATTACH_COMMON,config('ds_config.default_goods_image'));
  69. }
  70. $live_apply_list[$key]['live_apply_user_name'] = '';
  71. switch ($val['live_apply_user_type']) {
  72. case 2:
  73. if (!isset($store_list[$val['live_apply_user_id']])) {
  74. $store_list[$val['live_apply_user_id']] = $store_model->getStoreInfo(array('store_id' => $val['live_apply_user_id']));
  75. }
  76. $live_apply_list[$key]['live_apply_user_name'] = $store_list[$val['live_apply_user_id']]['store_name'] ? $store_list[$val['live_apply_user_id']]['store_name'] : '';
  77. break;
  78. }
  79. }
  80. View::assign('live_apply_list', $live_apply_list);
  81. View::assign('show_page', $live_apply_model->page_info->render());
  82. }
  83. View::assign('search', $condition);
  84. $this->setAdminCurItem('index');
  85. return View::fetch();
  86. }
  87. /**
  88. * 删除商品
  89. */
  90. public function del() {
  91. $live_apply_id = input('param.live_apply_id');
  92. $live_apply_id_array = ds_delete_param($live_apply_id);
  93. if ($live_apply_id_array == FALSE) {
  94. ds_json_encode('10001', lang('ds_common_op_fail'));
  95. }
  96. $condition = array();
  97. $condition[] = array('live_apply_id', 'in', $live_apply_id_array);
  98. model('live_apply')->delLiveApply($condition);
  99. $this->log(lang('ds_del') . lang('live_apply') . ' ID:' . implode('、', $live_apply_id_array), 1);
  100. ds_json_encode('10000', lang('ds_common_op_succ'));
  101. }
  102. /**
  103. * 审核商品
  104. */
  105. public function view() {
  106. if (config('ds_config.live_type') == 1) {
  107. $minipro_live_id = input('param.minipro_live_id');
  108. $minipro_live_model = model('minipro_live');
  109. $minipro_live_info = $minipro_live_model->getMiniproLiveInfo(array(array('minipro_live_id', '=', $minipro_live_id)));
  110. if (!$minipro_live_info) {
  111. $this->error(lang('live_not_exist'));
  112. }
  113. $minipro_live_info['minipro_live_image_url'] = ds_get_pic( ATTACH_MINIPRO_LIVE , $minipro_live_info['minipro_live_image']);
  114. $minipro_live_room_goods_model = model('minipro_live_room_goods');
  115. $goods_list = $minipro_live_room_goods_model->getMiniproLiveRoomGoodsList(array(array('minipro_live_id', '=', $minipro_live_id)));
  116. foreach ($goods_list as $k => $v) {
  117. $goods_list[$k]['goods_image'] = goods_cthumb($v['goods_image']);
  118. }
  119. $minipro_live_info['goods_list'] = $goods_list;
  120. View::assign('live_apply_info', $minipro_live_info);
  121. echo View::fetch('view');
  122. } else {
  123. $live_apply_id = input('param.live_apply_id');
  124. $live_apply_model = model('live_apply');
  125. $live_apply_info = $live_apply_model->getLiveApplyInfo(array('live_apply_id' => $live_apply_id));
  126. if (!$live_apply_info) {
  127. $this->error(lang('live_not_exist'));
  128. }
  129. if (request()->isPost()) {
  130. $live_apply_model = model('live_apply');
  131. $data = array(
  132. 'live_apply_end_time' => strtotime(input('param.live_apply_end_time')),
  133. 'live_apply_video' => input('param.live_apply_video'),
  134. );
  135. if (!$data['live_apply_end_time']) {
  136. $this->error(lang('live_apply_end_time_empty'));
  137. }
  138. if ($live_apply_info['live_apply_state'] == 0) {
  139. if (intval(input('param.verify_state')) == 0) {
  140. $state = 2;
  141. $remark = input('param.verify_reason');
  142. if ($remark) {
  143. $store_ids = Db::name('live_apply')->where(array('live_apply_user_type' => 2, 'live_apply_id' => $live_apply_id))->column('live_apply_user_id');
  144. if ($store_ids) {
  145. $store_model = model('store');
  146. $store_list = $store_model->getStoreList(array(array('store_id', 'in', $store_ids)));
  147. if ($store_list) {
  148. foreach ($store_list as $store) {
  149. $param = array();
  150. $param['code'] = 'live_apply_verify';
  151. $param['store_id'] = $store['store_id'];
  152. $param['ali_param'] = array(
  153. 'remark' => $remark,
  154. 'live_apply_id' => $live_apply_id
  155. );
  156. $param['ten_param'] = array(
  157. $remark,
  158. $live_apply_id
  159. );
  160. $param['param'] = $param['ali_param'];
  161. $param['weixin_param'] = array(
  162. 'url' => config('ds_config.h5_store_site_url') . '/pages/seller/live_apply/LiveApplyList',
  163. 'data' => array(
  164. "keyword1" => array(
  165. "value" => $live_apply_info['live_apply_remark'],
  166. "color" => "#333"
  167. ),
  168. "keyword2" => array(
  169. "value" => $remark,
  170. "color" => "#333"
  171. )
  172. ),
  173. );
  174. model('cron')->addCron(array('cron_exetime'=>TIMESTAMP,'cron_type'=>'sendStoremsg','cron_value'=>serialize($param)));
  175. }
  176. }
  177. }
  178. }
  179. } else {
  180. $state = 1;
  181. //生成小程序码
  182. $wechat_model = model('wechat');
  183. $wechat_model->getOneWxconfig();
  184. $a = $wechat_model->getMiniProCode($live_apply_id, 'pages/seller/live_apply/LiveApplyDetail');
  185. if (@imagecreatefromstring($a) == false) {
  186. $a = json_decode($a);
  187. $this->error(lang('live_minipro_code_fail') . lang('ds_colon') . $a->errmsg);
  188. } else {
  189. if (is_dir(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_LIVE_APPLY) || (!is_dir(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_LIVE_APPLY) && mkdir(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_LIVE_APPLY, 0755, true))) {
  190. file_put_contents(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_LIVE_APPLY . DIRECTORY_SEPARATOR . $live_apply_id . '.png', $a);
  191. } else {
  192. $this->error(lang('live_minipro_code_fail') . lang('ds_colon') . lang('directory_no_right'));
  193. }
  194. }
  195. }
  196. $data['live_apply_state'] = $state;
  197. }
  198. $live_apply_model->editLiveApply($data, array('live_apply_id' => $live_apply_id));
  199. $this->log(lang('ds_verify') . lang('live_apply') . ' ID:' . $live_apply_id, 1);
  200. dsLayerOpenSuccess(lang('ds_common_op_succ'));
  201. } else {
  202. //判断当前流状态
  203. $live_apply_info['active'] = false;
  204. if ($live_apply_info['live_apply_state'] == 1 && $live_apply_info['live_apply_end_time'] > TIMESTAMP) {
  205. if (config('ds_config.video_type') == 'aliyun') {
  206. if (!config('ds_config.aliyun_live_push_domain')) {
  207. ds_json_encode(10001, lang('aliyun_live_push_domain_empty'));
  208. }
  209. if (!config('ds_config.aliyun_live_push_key')) {
  210. ds_json_encode(10001, lang('aliyun_live_push_key_empty'));
  211. }
  212. if (!config('ds_config.aliyun_live_play_domain')) {
  213. ds_json_encode(10001, lang('aliyun_live_play_domain_empty'));
  214. }
  215. if (!config('ds_config.aliyun_live_play_key')) {
  216. ds_json_encode(10001, lang('aliyun_live_play_key_empty'));
  217. }
  218. $regionId = 'cn-shanghai';
  219. AlibabaCloud::accessKeyClient(config('ds_config.aliyun_access_key_id'), config('ds_config.aliyun_access_key_secret'))
  220. ->regionId($regionId)
  221. ->asDefaultClient();
  222. try {
  223. $result = AlibabaCloud::rpc()
  224. ->product('live')
  225. // ->scheme('https') // https | http
  226. ->version('2016-11-01')
  227. ->action('DescribeLiveStreamsOnlineList')
  228. ->method('POST')
  229. ->host('live.aliyuncs.com')
  230. ->options([
  231. 'query' => [
  232. 'RegionId' => $regionId,
  233. 'DomainName' => config('ds_config.aliyun_live_push_domain'),
  234. 'AppName' => "live",
  235. 'StreamName' => 'live_apply_' . $live_apply_info['live_apply_id'],
  236. 'PageSize' => "1",
  237. 'PageNum' => "1",
  238. 'QueryType' => "strict",
  239. ],
  240. ])
  241. ->request();
  242. if ($result->TotalNum) {
  243. $live_apply_info['active'] = true;
  244. //生成播放地址
  245. $live_apply_info['live_apply_play_url'] = model('live_apply')->getPlayUrl('live_apply_' . $live_apply_info['live_apply_id'], $live_apply_info['live_apply_end_time']);
  246. }
  247. } catch (\Exception $e) {
  248. }
  249. } else {
  250. if (!config('ds_config.live_push_domain')) {
  251. $this->error(lang('live_push_domain_empty'));
  252. }
  253. if (!config('ds_config.live_push_key')) {
  254. $this->error(lang('live_push_key_empty'));
  255. }
  256. if (!config('ds_config.live_play_domain')) {
  257. $this->error(lang('live_play_domain_empty'));
  258. }
  259. try {
  260. $cred = new Credential(config('ds_config.vod_tencent_secret_id'), config('ds_config.vod_tencent_secret_key'));
  261. $httpProfile = new HttpProfile();
  262. $httpProfile->setEndpoint("live.tencentcloudapi.com");
  263. $clientProfile = new ClientProfile();
  264. $clientProfile->setHttpProfile($httpProfile);
  265. $client = new LiveClient($cred, "", $clientProfile);
  266. $req = new DescribeLiveStreamStateRequest();
  267. $params = '{"AppName":"live","DomainName":"' . config('ds_config.live_push_domain') . '","StreamName":"' . 'live_apply_' . $live_apply_info['live_apply_id'] . '"}';
  268. $req->fromJsonString($params);
  269. $resp = $client->DescribeLiveStreamState($req);
  270. } catch (TencentCloudSDKException $e) {
  271. $this->error($e->getMessage());
  272. }
  273. if ($resp->StreamState == 'active') {
  274. $live_apply_info['active'] = true;
  275. //生成播放地址
  276. $live_apply_info['live_apply_play_url'] = model('live_apply')->getPlayUrl('live_apply_' . $live_apply_info['live_apply_id'], $live_apply_info['live_apply_end_time']);
  277. }
  278. }
  279. }
  280. if ($live_apply_info['live_apply_cover_video']) {
  281. $live_apply_info['live_apply_cover_video_url'] = ds_get_pic( ATTACH_LIVE_APPLY . '/' . $live_apply_info['live_apply_user_id'] , $live_apply_info['live_apply_cover_video']);
  282. } elseif ($live_apply_info['live_apply_cover_image']) {
  283. $live_apply_info['live_apply_cover_image_url'] = ds_get_pic( ATTACH_LIVE_APPLY . '/' . $live_apply_info['live_apply_user_id'] , $live_apply_info['live_apply_cover_image']);
  284. } else {
  285. $live_apply_info['live_apply_cover_image_url'] = ds_get_pic(ATTACH_COMMON,config('ds_config.default_goods_image'));
  286. }
  287. $goods_commonid = Db::name('live_apply_goods')->where('live_apply_id', $live_apply_info['live_apply_id'])->column('goods_commonid');
  288. $goods_model = model('goods');
  289. if (!empty($goods_commonid)) {
  290. $goods_list = $goods_model->getGoodsUnionList(array(array('goodscommon.goods_commonid', 'in', $goods_commonid)), 'goods_id,goodscommon.goods_name,goodscommon.goods_image,goodscommon.goods_price');
  291. foreach ($goods_list as $k => $v) {
  292. $goods_list[$k]['goods_image'] = goods_cthumb($v['goods_image']);
  293. }
  294. $live_apply_info['goods_list'] = $goods_list;
  295. } else {
  296. $live_apply_info['goods_list'] = array();
  297. }
  298. View::assign('live_apply_info', $live_apply_info);
  299. echo View::fetch('view');
  300. }
  301. }
  302. }
  303. public function close() {
  304. $live_apply_id = input('param.live_apply_id');
  305. $live_apply_model = model('live_apply');
  306. $live_apply = $live_apply_model->getLiveApplyInfo(array('live_apply_id' => $live_apply_id));
  307. if (!$live_apply) {
  308. ds_json_encode(10001, lang('live_not_exist'));
  309. }
  310. if (config('ds_config.video_type') == 'aliyun') {
  311. $regionId = 'cn-shanghai';
  312. AlibabaCloud::accessKeyClient(config('ds_config.aliyun_access_key_id'), config('ds_config.aliyun_access_key_secret'))
  313. ->regionId($regionId)
  314. ->asDefaultClient();
  315. try {
  316. $result = AlibabaCloud::rpc()
  317. ->product('live')
  318. // ->scheme('https') // https | http
  319. ->version('2016-11-01')
  320. ->action('ForbidLiveStream')
  321. ->method('POST')
  322. ->host('live.aliyuncs.com')
  323. ->options([
  324. 'query' => [
  325. 'RegionId' => $regionId,
  326. 'AppName' => "live",
  327. 'StreamName' => 'live_apply_' . $live_apply['live_apply_id'],
  328. 'LiveStreamType' => "publisher",
  329. 'DomainName' => config('ds_config.aliyun_live_push_domain'),
  330. ],
  331. ])
  332. ->request();
  333. } catch (\Exception $e) {
  334. ds_json_encode(10001, $e->getMessage());
  335. }
  336. } else {
  337. try {
  338. $cred = new Credential(config('ds_config.vod_tencent_secret_id'), config('ds_config.vod_tencent_secret_key'));
  339. $httpProfile = new HttpProfile();
  340. $httpProfile->setEndpoint("live.tencentcloudapi.com");
  341. $clientProfile = new ClientProfile();
  342. $clientProfile->setHttpProfile($httpProfile);
  343. $client = new LiveClient($cred, "", $clientProfile);
  344. $req = new DropLiveStreamRequest();
  345. $params = '{"AppName":"live","DomainName":"' . config('ds_config.live_push_domain') . '","StreamName":"' . 'live_apply_' . $live_apply['live_apply_id'] . '"}';
  346. $req->fromJsonString($params);
  347. $resp = $client->DropLiveStream($req);
  348. } catch (TencentCloudSDKException $e) {
  349. ds_json_encode(10001, $e->getMessage());
  350. }
  351. }
  352. $this->log(lang('live_cut_off') . ' ID:' . $live_apply_id, 1);
  353. ds_json_encode(10000, lang('ds_common_op_succ'));
  354. }
  355. /**
  356. * 获取卖家栏目列表,针对控制器下的栏目
  357. */
  358. protected function getAdminItemList() {
  359. $menu_array = array(
  360. array(
  361. 'name' => 'index',
  362. 'text' => lang('ds_list'),
  363. 'url' => url('LiveApply/index')
  364. ),
  365. );
  366. return $menu_array;
  367. }
  368. }