LiveApply.php 21 KB

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