LiveController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace addons\WechatMini\merchant\modules\live\controllers;
  3. use Yii;
  4. use common\enums\StatusEnum;
  5. use common\helpers\ResultHelper;
  6. use common\models\base\SearchModel;
  7. use common\traits\MerchantCurd;
  8. use common\helpers\ArrayHelper;
  9. use common\helpers\StringHelper;
  10. use addons\WechatMini\merchant\controllers\BaseController;
  11. use addons\WechatMini\common\models\live\Live;
  12. use addons\WechatMini\common\models\live\GoodsMap;
  13. /**
  14. * Class LiveController
  15. * @package addons\WechatMini\merchant\modules\live\controllers
  16. * @author jianyan74 <751393839@qq.com>
  17. */
  18. class LiveController extends BaseController
  19. {
  20. use MerchantCurd;
  21. /**
  22. * @var Live
  23. */
  24. public $modelClass = Live::class;
  25. /**
  26. * @return string
  27. * @throws \yii\web\NotFoundHttpException
  28. */
  29. public function actionIndex()
  30. {
  31. $searchModel = new SearchModel([
  32. 'model' => $this->modelClass,
  33. 'scenario' => 'default',
  34. 'relations' => [],
  35. 'partialMatchAttributes' => ['name'], // 模糊查询
  36. 'defaultOrder' => [
  37. 'roomid' => SORT_DESC,
  38. 'id' => SORT_DESC,
  39. ],
  40. 'pageSize' => $this->pageSize,
  41. ]);
  42. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  43. $dataProvider->query
  44. ->andWhere(['>=', 'status', StatusEnum::DISABLED])
  45. ->andWhere(['merchant_id' => $this->getMerchantId()]);
  46. return $this->render($this->action->id, [
  47. 'dataProvider' => $dataProvider,
  48. 'searchModel' => $searchModel,
  49. ]);
  50. }
  51. /**
  52. * 编辑/创建
  53. *
  54. * @return mixed
  55. */
  56. public function actionEdit()
  57. {
  58. $id = Yii::$app->request->get('id', null);
  59. /** @var Live $model */
  60. $model = $this->findModel($id);
  61. $oldModel = ArrayHelper::toArray($model);
  62. if ($model->load(Yii::$app->request->post())) {
  63. if (!$model->validate()) {
  64. return ResultHelper::json(422, $this->getError($model));
  65. }
  66. if (empty($model->anchor_wechat)) {
  67. return ResultHelper::json(422, '请填写主播微信号');
  68. }
  69. try {
  70. if ($oldModel['cover_img'] != $model->cover_img || empty($model->cover_media)) {
  71. $coverImg = Yii::$app->wechat->miniProgram->media->uploadImage(StringHelper::getLocalFilePath($model->cover_img));
  72. $model->cover_media = $coverImg['media_id'] ?? '';
  73. }
  74. if ($oldModel['share_img'] != $model->share_img || empty($model->share_media)) {
  75. $shareImg = Yii::$app->wechat->miniProgram->media->uploadImage(StringHelper::getLocalFilePath($model->share_img));
  76. $model->share_media = $shareImg['media_id'] ?? '';
  77. }
  78. if ($oldModel['feeds_img'] != $model->cover_img || empty($model->feeds_media)) {
  79. $feedsImg = Yii::$app->wechat->miniProgram->media->uploadImage(StringHelper::getLocalFilePath($model->feeds_img));
  80. $model->feeds_media = $feedsImg['media_id'] ?? '';
  81. }
  82. $data = [
  83. 'name' => $model->name,
  84. 'coverImg' => $model->cover_media,
  85. 'startTime' => StringHelper::dateToInt($model->start_time),
  86. 'endTime' => StringHelper::dateToInt($model->end_time),
  87. 'anchorName' => $model->anchor_name,
  88. 'anchorWechat' => $model->anchor_wechat,
  89. 'shareImg' => $model->share_media,
  90. 'feedsImg' => $model->feeds_media,
  91. 'isFeedsPublic' => $model->is_feeds_public,
  92. 'type' => $model->live_type,
  93. 'closeLike' => $model->close_like,
  94. 'closeGoods' => $model->close_goods,
  95. 'closeComment' => $model->close_comment,
  96. 'closeReplay' => $model->close_replay,
  97. 'closeShare' => $model->close_share,
  98. 'closeKf' => $model->close_kf,
  99. ];
  100. if (!empty($model->roomid)) {
  101. $data['id'] = $model->roomid;
  102. $result = Yii::$app->wechat->miniProgram->broadcast->updateLiveRoom($data);
  103. Yii::$app->services->base->getWechatError($result);
  104. $model->share_path = Yii::$app->wechat->miniProgram->broadcast->getShareQrcode(['roomId' => $model->roomid]);
  105. // 获取推流地址
  106. if (empty($model->push_addr)) {
  107. $pushUrl = Yii::$app->wechat->miniProgram->broadcast->getPushUrl(['roomId' => $model->roomid]);
  108. $model->push_addr = $pushUrl['pushAddr'] ?? '';
  109. }
  110. } else {
  111. $result = Yii::$app->wechat->miniProgram->broadcast->createLiveRoom($data);
  112. Yii::$app->services->base->getWechatError($result);
  113. $model->roomid = $result['roomId'];
  114. $model->qrcode_url = $result['qrcode_url'] ?? '';
  115. // 获取推流地址
  116. $pushUrl = Yii::$app->wechat->miniProgram->broadcast->getPushUrl(['roomId' => $model->roomid]);
  117. $model->push_addr = $pushUrl['pushAddr'] ?? '';
  118. // 获取直播间分享二维码
  119. $model->share_path = Yii::$app->wechat->miniProgram->broadcast->getShareQrcode(['roomId' => $model->roomid]);
  120. }
  121. $model->save();
  122. return ResultHelper::json(200, 'ok');
  123. } catch (\Exception $e) {
  124. return ResultHelper::json(422, $e->getMessage());
  125. }
  126. }
  127. return $this->render($this->action->id, [
  128. 'model' => $model,
  129. 'referrer' => Yii::$app->request->referrer,
  130. ]);
  131. }
  132. /**
  133. * 伪删除
  134. *
  135. * @param $id
  136. * @return mixed
  137. */
  138. public function actionDestroy($id)
  139. {
  140. if (!($model = $this->modelClass::findOne($id))) {
  141. return $this->message("找不到数据", $this->redirect(Yii::$app->request->referrer), 'error');
  142. }
  143. $model->status = StatusEnum::DELETE;
  144. if ($model->save()) {
  145. Yii::$app->wechat->miniProgram->broadcast->deleteLiveRoom(['roomId' => $model->roomid]);
  146. return $this->message("删除成功", $this->redirect(Yii::$app->request->referrer));
  147. }
  148. return $this->message("删除失败", $this->redirect(Yii::$app->request->referrer), 'error');
  149. }
  150. /**
  151. * 同步
  152. *
  153. * @param int $offset
  154. * @param int $count
  155. * @param int $clear
  156. * @return array|mixed
  157. * @throws \Psr\SimpleCache\InvalidArgumentException
  158. */
  159. public function actionSync($offset = 0, $count = 20, $clear = 0)
  160. {
  161. if ($clear == StatusEnum::ENABLED) {
  162. Live::updateAll(['status' => StatusEnum::DELETE], ['merchant_id' => Yii::$app->services->merchant->getNotNullId()]);
  163. GoodsMap::deleteAll(['merchant_id' => Yii::$app->services->merchant->getNotNullId()]);
  164. }
  165. try {
  166. $res = Yii::$app->wechatMiniService->live->sync($offset, $count);
  167. if (is_array($res)) {
  168. return ResultHelper::json(200, '同步成功', $res);
  169. }
  170. return ResultHelper::json(201, '同步完成');
  171. } catch (\Exception $e) {
  172. return ResultHelper::json(422, $e->getMessage());
  173. }
  174. }
  175. }
粤ICP备19079148号