LiveDetailForm.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\plugins\wxlive\models\live;
  8. use app\plugins\wxlive\models\Wechat;
  9. use app\utils\CurlHelper;
  10. use yii\base\Model;
  11. class LiveDetailForm extends Model
  12. {
  13. public $store_id;
  14. public $page;
  15. public $roomid;
  16. public $file;
  17. public $data;
  18. public $goods_id;
  19. public $roomId;
  20. public $limit = 10;
  21. /**
  22. * 获取直播间列表
  23. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  24. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  25. * @throws \GuzzleHttp\Exception\GuzzleException
  26. */
  27. public function getLiveList()
  28. {
  29. $start = ($this->page - 1) * $this->limit;
  30. $wechat = Wechat::getWechat();
  31. if (!is_array($wechat) || !isset($wechat['code'])) {
  32. return $wechat->broadcast->getRooms($start, $this->limit);
  33. } else {
  34. return [
  35. 'errcode' => 1
  36. ];
  37. }
  38. }
  39. public function deleteRoom()
  40. {
  41. $wechat = Wechat::getWechat();
  42. return $wechat->broadcast->deleteLiveRoom([
  43. 'id' => $this->roomId,
  44. ]);
  45. }
  46. public function getHuifang()
  47. {
  48. $wechat = Wechat::getWechat();
  49. return $wechat->broadcast->getPlaybacks($this->roomid, 0, 10);
  50. }
  51. public function getMedia()
  52. {
  53. $file = $this->file;
  54. $root_url = $this->saveTempImage($file);
  55. $wechat = Wechat::getWechat();
  56. return $wechat->media->upload('image', $root_url);
  57. }
  58. public function submitLive()
  59. {
  60. set_time_limit(0);
  61. $wechat = Wechat::getWechat();
  62. $coverImg = trim($this->data['coverImg']);
  63. $coverImgPath = $this->saveTempImage($coverImg);
  64. $coverImgMedia = $wechat->media->upload('image', $coverImgPath);
  65. if (isset($coverImgMedia['errcode'])) {
  66. return $coverImgMedia;
  67. }
  68. $shareImg = trim($this->data['shareImg']);
  69. $shareImgPath = $this->saveTempImage($shareImg);
  70. $shareImgMedia = $wechat->media->upload('image', $shareImgPath);
  71. if (isset($shareImgMedia['errcode'])) {
  72. return $shareImgMedia;
  73. }
  74. $feedsImg = trim($this->data['feedsImg']);
  75. $feedsImgPath = $this->saveTempImage($feedsImg);
  76. $feedsImgMedia = $wechat->media->upload('image', $feedsImgPath);
  77. if (isset($feedsImgMedia['errcode'])) {
  78. return $feedsImgMedia;
  79. }
  80. $postData = [
  81. 'type' => 1,
  82. 'name' => trim($this->data['name']),
  83. 'coverImg' => $coverImgMedia['media_id'],
  84. 'shareImg' => $shareImgMedia['media_id'],
  85. 'feedsImg' => $feedsImgMedia['media_id'],
  86. 'startTime' => strtotime(trim($this->data['startTime'])),
  87. 'endTime' => strtotime(trim($this->data['endTime'])),
  88. 'anchorName' => trim($this->data['anchorName']),
  89. 'anchorWechat' => trim($this->data['anchorWechat']),
  90. 'closeComment' =>intval( $this->data['closeComment']),
  91. 'closeLike' => intval($this->data['closeLike']),
  92. 'closeGoods' => intval($this->data['closeGoods']),
  93. 'closeReplay' => intval($this->data['closeReplay']),
  94. 'closeShare' => intval($this->data['closeShare']),
  95. 'closeKf' => intval($this->data['closeKf']),
  96. ];
  97. $wechat = Wechat::getWechat();
  98. return $wechat->broadcast->createLiveRoom($postData);
  99. }
  100. public function addGoods()
  101. {
  102. if (empty($this->goods_id)) {
  103. return [
  104. 'code' => 1,
  105. 'msg' => '请选择商品'
  106. ];
  107. }
  108. $goods_id = $this->goods_id;
  109. $postData = [
  110. 'ids' => $goods_id,
  111. 'roomId' => trim($this->roomId)
  112. ];
  113. $wechat = Wechat::getWechat();
  114. return $wechat->broadcast->addGoods($postData);
  115. }
  116. //获取网络图片到临时目录
  117. private function saveTempImage($url)
  118. {
  119. if (strpos($url,'http') === false) {
  120. $url = 'http:'. trim($url);
  121. }
  122. if (!is_dir(\Yii::$app->runtimePath . '/image')) {
  123. mkdir(\Yii::$app->runtimePath . '/image');
  124. }
  125. $save_path = \Yii::$app->runtimePath . '/image/' . md5($url) . '.jpg';
  126. CurlHelper::download($url, $save_path);
  127. return $save_path;
  128. }
  129. /**
  130. * 获取已经审核通过的商品列表
  131. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  132. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  133. * @throws \GuzzleHttp\Exception\GuzzleException
  134. */
  135. public function getGoodsList()
  136. {
  137. $wechat = Wechat::getWechat();
  138. $start = ($this->page - 1) * $this->limit;
  139. return $wechat->broadcast->getApproved([
  140. 'offset' => $start,
  141. 'limit' => $this->limit,
  142. 'status' => 2
  143. ]);
  144. }
  145. }