Room.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\live\model;
  11. use app\model\BaseModel;
  12. use app\model\system\Cron;
  13. use app\model\upload\Upload;
  14. class Room extends BaseModel
  15. {
  16. private $liveStatus = [
  17. '101' => '直播中',
  18. '102' => '未开始',
  19. '103' => '已结束',
  20. '104' => '禁播',
  21. '105' => '暂停中',
  22. '106' => '异常',
  23. '107' => '已过期',
  24. ];
  25. /**
  26. * 创建直播间
  27. * @param $data
  28. * @param $site_id
  29. */
  30. public function createRoom($data, $site_id)
  31. {
  32. $live = new Live($site_id);
  33. $res = $live->createRoom($data);
  34. if ($res[ 'code' ] == 0) {
  35. $this->syncLiveRoom(0, 1, $site_id);
  36. $cron_info = model("cron")->getInfo([ [ 'event', '=', 'LiveRoomStatus' ], [ 'relate_id', '=', $site_id ] ]);
  37. if (empty($cron_info)) {
  38. $cron = new Cron();
  39. $cron->addCron(2, 10, '轮询小程序直播状态', 'LiveRoomStatus', time(), $site_id);
  40. }
  41. }
  42. return $res;
  43. }
  44. /**
  45. * 编辑直播间信息
  46. * @param array $data
  47. * @param array $where
  48. */
  49. public function updateRoomInfo($data = [], $where = [])
  50. {
  51. $res = model('weapp_live_room')->update($data, $where);
  52. return $this->success($res);
  53. }
  54. /**
  55. * 获取直播间列表
  56. * @param array $condition
  57. * @param bool $field
  58. * @param string $order
  59. * @param int $page
  60. * @param int $list_rows
  61. * @param string $alias
  62. * @param array $join
  63. * @return array
  64. */
  65. public function getRoomPageList($condition = [], $field = true, $order = '', $page = 1, $list_rows = PAGE_LIST_ROWS, $alias = 'a', $join = [])
  66. {
  67. $data = model('weapp_live_room')->pageList($condition, $field, $order, $page, $list_rows, $alias, $join);
  68. if (!empty($data[ 'list' ])) {
  69. foreach ($data[ 'list' ] as $k => $item) {
  70. $data[ 'list' ][ $k ][ 'status_name' ] = $this->liveStatus[ $item[ 'live_status' ] ] ?? '';
  71. if (isset($item[ 'goods' ])) $data[ 'list' ][ $k ][ 'goods' ] = json_decode($item[ 'goods' ], true);
  72. }
  73. }
  74. return $this->success($data);
  75. }
  76. /**
  77. * 同步直播间列表
  78. */
  79. public function syncLiveRoom($start, $limit, $site_id)
  80. {
  81. $live = new Live($site_id);
  82. $result = $live->getRoomList($start, $limit);
  83. if ($result[ 'code' ] < 0) return $result;
  84. if (!empty($result[ 'data' ][ 'list' ])) {
  85. $upload = new Upload($site_id, 'shop');
  86. $upload->setPath('upload/live/room/');
  87. foreach ($result[ 'data' ][ 'list' ] as $item) {
  88. $data = [
  89. 'site_id' => $site_id,
  90. 'roomid' => $item[ 'roomid' ],
  91. 'name' => $item[ 'name' ],
  92. 'cover_img' => $item[ 'cover_img' ],
  93. 'start_time' => $item[ 'start_time' ],
  94. 'end_time' => $item[ 'end_time' ],
  95. 'anchor_name' => $item[ 'anchor_name' ],
  96. 'goods' => json_encode($item[ 'goods' ], JSON_UNESCAPED_UNICODE),
  97. 'live_status' => $item[ 'live_status' ],
  98. ];
  99. $room_info = model('weapp_live_room')->getInfo([ [ 'roomid', '=', $item[ 'roomid' ] ], [ 'site_id', '=', $site_id ] ], 'id');
  100. if (empty($room_info)) {
  101. if (is_url($item[ 'share_img' ])) {
  102. $pull_result = $upload->remotePull($item[ 'share_img' ]);
  103. $pull_result = $pull_result[ 'data' ];
  104. if (isset($pull_result[ 'pic_path' ]) && !empty($pull_result[ 'pic_path' ])) {
  105. $data[ 'share_img' ] = $pull_result[ 'pic_path' ];
  106. }
  107. }
  108. model('weapp_live_room')->add($data);
  109. } else {
  110. $data = [
  111. // 'goods' => json_encode($item[ 'goods' ], JSON_UNESCAPED_UNICODE),
  112. 'live_status' => $item[ 'live_status' ],
  113. ];
  114. model('weapp_live_room')->update($data, [ [ 'id', '=', $room_info[ 'id' ] ] ]);
  115. }
  116. }
  117. $total_page = ceil($result[ 'data' ][ 'total' ] / $limit);
  118. return $this->success([ 'page' => $start, 'total_page' => $total_page ]);
  119. } else {
  120. return $this->success([ 'page' => $start, 'total_page' => 1 ]);
  121. }
  122. }
  123. /**
  124. * 获取直播间信息
  125. */
  126. public function getRoomInfo($condition = [], $field = '*')
  127. {
  128. $data = model('weapp_live_room')->getInfo($condition, $field);
  129. if (!empty($data)) {
  130. $data[ 'status_name' ] = $this->liveStatus[ $data[ 'live_status' ] ] ?? '';
  131. if (isset($data[ 'goods' ]) && !empty($data[ 'goods' ])) $data[ 'goods' ] = json_decode($data[ 'goods' ], true);
  132. }
  133. return $this->success($data);
  134. }
  135. /**
  136. * 添加商品到直播间
  137. * @param $site_id
  138. * @param $room_id
  139. * @param $data
  140. */
  141. public function addGoods($site_id, $room_id, $data)
  142. {
  143. if (empty($data)) return $this->error('', '请先选择要添加的商品');
  144. $room_info = model('weapp_live_room')->getInfo([ [ 'site_id', '=', $site_id ], [ 'roomid', '=', $room_id ] ], 'goods');
  145. if (empty($room_info)) return $this->error('', '未查找到直播间信息');
  146. $data = json_decode($data, true);
  147. $goods_ids = [];
  148. $goods_data = [];
  149. foreach ($data as $item) {
  150. array_push($goods_ids, $item[ 'goods_id' ]);
  151. array_push($goods_data, [
  152. 'name' => $item[ 'name' ],
  153. 'cover_img' => $item[ 'cover_img' ],
  154. 'url' => $item[ 'url' ],
  155. 'price' => $item[ 'price' ]
  156. ]);
  157. }
  158. $live = new Live($site_id);
  159. $result = $live->roomAddGoods($room_id, $goods_ids);
  160. if ($result[ 'code' ] < 0) return $result;
  161. if (!empty($room_info[ 'goods' ])) {
  162. $room_goods = json_decode($room_info[ 'goods' ], true);
  163. $goods_data = array_merge($room_goods, $goods_data);
  164. }
  165. $res = model('weapp_live_room')->update([ 'goods' => json_encode($goods_data, JSON_UNESCAPED_UNICODE) ], [ [ 'site_id', '=', $site_id ], [ 'roomid', '=', $room_id ] ]);
  166. return $this->success($res);
  167. }
  168. /**
  169. * 轮询更新直播间状态
  170. * @param $site_id
  171. */
  172. public function updateRoomStatus($site_id)
  173. {
  174. $count = model('weapp_live_room')->getCount([ [ 'site_id', '=', $site_id ], [ 'live_status', 'in', [ '101', '102', '105' ] ] ]);
  175. if ($count) {
  176. $start = 0;
  177. $result = $this->syncLiveRoom($start, 20, $site_id);
  178. if (isset($result[ 'code' ]) && $result[ 'code' ] == 0 && $result[ 'total_page' ] > 1) {
  179. for ($i = 1; $i < $result[ 'data' ]; $i++) {
  180. $this->syncLiveRoom($i, 20, $site_id);
  181. }
  182. }
  183. }
  184. }
  185. /**
  186. * 删除直播间
  187. * @param $site_id
  188. * @param $room_ids
  189. */
  190. public function deleteRoom($site_id, $room_ids)
  191. {
  192. $res = model('weapp_live_room')->delete([ [ 'site_id', '=', $site_id ], [ 'id', 'in', $room_ids ] ]);
  193. return $this->success($res);
  194. }
  195. }