Liveroom.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. namespace addons\shopro\controller\goods;
  3. use addons\shopro\controller\Common;
  4. use addons\shopro\service\goods\GoodsService;
  5. use think\Db;
  6. use app\utils\Service\Tencent\TencentIm;
  7. class Liveroom extends Common
  8. {
  9. protected $noNeedLogin = ['get_room_attr'];
  10. protected $noNeedRight = ['*'];
  11. //检查是不是主播或管理员,主要用来商品操作的鉴权
  12. private function check_auth($room_no,$user_id){
  13. $whereor = [
  14. 'admin_id' => $user_id,
  15. 'status' => 1,
  16. ];
  17. $check = Db::name('live_room_admin')->where('room_no',$room_no)->where('user_id',$user_id)->whereOr(function($query) use ($whereor) {
  18. $query->where($whereor);
  19. })->find();
  20. if(empty($check)){
  21. $this->error('没有操作权限');
  22. }
  23. $rs = empty($check) ? false : true;
  24. return $rs;
  25. }
  26. //商城所有商品列表
  27. public function goods_index()
  28. {
  29. //鉴权
  30. $room_no = input('room_no','','trim');
  31. $user = auth_user();
  32. $this->check_auth($room_no,$user['id']);
  33. //全平台所有商品列表
  34. $service = new GoodsService();
  35. $service->up();
  36. $keyword = input('keyword','','trim');
  37. if ($keyword) {
  38. $service->search($keyword);
  39. }
  40. $goods = $service->order('weigh desc,id desc')->select_autopage();
  41. $goods = collection($goods)->toArray();
  42. $goods = list_domain_image($goods,['image']);//主图
  43. //本直播间已经有的,标记
  44. $goods_ids = Db::name('live_room_goods')->where('room_no',$room_no)->where('status','up')->column('goods_id');
  45. foreach($goods as $key => $good){
  46. $good['goods_added'] = 0;
  47. if(in_array($good['id'],$goods_ids)){
  48. $good['goods_added'] = 1;
  49. }
  50. $goods[$key] = $good;
  51. }
  52. $this->success('获取成功', $goods);
  53. }
  54. //直播间商品列表顶部三个统计
  55. public function liveroom_index_tongji()
  56. {
  57. //鉴权
  58. $room_no = input('room_no','','trim');
  59. $user = auth_user();
  60. $this->check_auth($room_no,$user['id']);
  61. $session = input('session','','trim');
  62. $room_log = Db::name('live_room_log')->where('session',$session)->where('room_no',$room_no)->find();
  63. $rs = [
  64. 'sales_total_price' => $room_log['order_total_amount'], //销售额
  65. 'paid_ordernum' => $room_log['order_paidnum'], //支付订单量
  66. 'uv' => $room_log['uv'], //访客数
  67. ];
  68. $this->success('获取成功', $rs);
  69. }
  70. //直播间商品列表
  71. public function liveroom_index()
  72. {
  73. //鉴权
  74. $room_no = input('room_no','','trim');
  75. $user = auth_user();
  76. $this->check_auth($room_no,$user['id']);
  77. //直播间上架商品
  78. $goods_list = Db::name('live_room_goods')->field('goods_id,goods_no,is_top')->where('room_no',$room_no)->where('status','up')->order('is_top desc,goods_no asc,id desc')->select();
  79. if(empty($goods_list))
  80. {
  81. $this->success(1,null);
  82. }
  83. $goods_ids = array_column($goods_list,'goods_id');
  84. //商品列表
  85. $service = new GoodsService();
  86. $service->up();
  87. $keyword = input('keyword','','trim');
  88. if ($keyword) {
  89. $service->search($keyword);
  90. }
  91. $service->whereIds($goods_ids);
  92. $goods = $service->select_autopage();
  93. $goods = collection($goods)->toArray();
  94. $goods = list_domain_image($goods,['image']);//主图
  95. //追加字段
  96. foreach($goods as $key => $good){
  97. foreach($goods_list as $k => $v){
  98. if($good['id'] == $v['goods_id']){
  99. $good['is_top'] = $v['is_top'];
  100. $good['goods_no'] = $v['goods_no'];
  101. }
  102. }
  103. $goods[$key] = $good;
  104. }
  105. $rs = [
  106. 'number' => count($goods_ids),
  107. 'list' => $goods,
  108. ];
  109. $this->success('获取成功', $rs);
  110. }
  111. //添加
  112. public function add(){
  113. //鉴权
  114. $room_no = input('room_no','','trim');
  115. $user = auth_user();
  116. $this->check_auth($room_no,$user['id']);
  117. //检查商品
  118. $goods_id = input('goods_id',0,'intval');
  119. $goods_info = Db::name('shopro_goods')->where('id',$goods_id)->where('status','up')->find();
  120. if(empty($goods_info)){
  121. $this->error('该商品已下架');
  122. }
  123. //商品序号拿最大的+1
  124. $max = Db::name('live_room_goods')->where('room_no',$room_no)->order('goods_no desc')->value('goods_no');
  125. $max = $max ? $max : 0;
  126. $new_goods_no = $max + 1;
  127. //直播间上架商品
  128. $goods_check = Db::name('live_room_goods')->where('room_no',$room_no)->where('goods_id',$goods_id)->find();
  129. if($goods_check){
  130. if($goods_check['status'] == 'up'){$this->error('该商品已存在');}
  131. //重新上架
  132. Db::name('live_room_goods')->where('room_no',$room_no)->where('goods_id',$goods_id)->update(['status'=>'up','goods_no'=>$new_goods_no,'update_time'=>time()]);
  133. }else{
  134. $room_info = Db::name('live_room')->where('room_no',$room_no)->find();
  135. $new = [
  136. 'room_id' => $room_info['id'],
  137. 'room_no' => $room_info['room_no'],
  138. 'user_id' => $room_info['user_id'],
  139. 'goods_id' => $goods_id,
  140. 'goods_no' => $new_goods_no,
  141. 'status' => 'up',
  142. 'create_time' => time(),
  143. 'update_time' => time(),
  144. ];
  145. Db::name('live_room_goods')->insertGetId($new);
  146. }
  147. $this->success('添加成功');
  148. }
  149. //删除
  150. public function down(){
  151. //鉴权
  152. $room_no = input('room_no','','trim');
  153. $user = auth_user();
  154. $this->check_auth($room_no,$user['id']);
  155. $goods_id = input('goods_id','');
  156. $goods_id = explode(',',$goods_id);
  157. Db::name('live_room_goods')->where('room_no',$room_no)->where('goods_id','IN',$goods_id)->update(['status'=>'down','goods_no'=>0]);
  158. $this->success('操作成功');
  159. }
  160. //设置为讲解中,取消讲解
  161. public function top(){
  162. //鉴权
  163. $room_no = input('room_no','','trim');
  164. $user = auth_user();
  165. $this->check_auth($room_no,$user['id']);
  166. $goods_id = input('goods_id',0,'intval');
  167. $is_top = input('is_top',0,'intval');
  168. //取消讲解
  169. if($is_top == 0){
  170. Db::name('live_room_goods')->where('room_no',$room_no)->update(['is_top'=>0]);
  171. //修改房间属性
  172. $top_good[] = [
  173. 'key' => 'top_good',
  174. 'value' => json_encode((object)[]),
  175. ];
  176. $im = new TencentIm();
  177. $im->modify_group_attr($room_no,$top_good);
  178. $this->success('操作成功');
  179. }
  180. //直播间商品
  181. $check = Db::name('live_room_goods')->where('room_no',$room_no)->where('goods_id',$goods_id)->where('status','up')->find();
  182. if(empty($check)){
  183. $this->error('不存在的商品');
  184. }
  185. //商品
  186. $service = new GoodsService();
  187. $service->up();
  188. $service->where('id',$goods_id);
  189. $goods = $service->find();
  190. if(empty($goods)){
  191. $this->error('该商品已下架');
  192. }
  193. //精简商品
  194. $goods = json_decode(json_encode($goods),true);
  195. $goods = [
  196. 'id' => $goods['id'],
  197. 'title' => $goods['title'],
  198. 'image' => localpath_to_netpath($goods['image']),
  199. 'price' => $goods['price'],
  200. 'is_sku'=> $goods['is_sku'],
  201. 'goods_sku_price_id' => 0,
  202. ];
  203. if($goods['is_sku'] != 1){
  204. $goods['goods_sku_price_id'] = Db::name('shopro_goods_sku_price')->where('goods_id',$goods['id'])->value('id');
  205. }
  206. //其他取消讲解,讲解这个
  207. $goods_id = input('goods_id',0,'intval');
  208. Db::name('live_room_goods')->where('room_no',$room_no)->update(['is_top'=>0]);
  209. Db::name('live_room_goods')->where('room_no',$room_no)->where('goods_id',$goods_id)->update(['is_top'=>1]);
  210. //修改房间属性
  211. $top_good[] = [
  212. 'key' => 'top_good',
  213. 'value' => json_encode($goods),
  214. ];
  215. $im = new TencentIm();
  216. $im->modify_group_attr($room_no,$top_good);
  217. $this->success('操作成功');
  218. }
  219. //获取属性
  220. public function get_room_attr(){
  221. $room_no = input('room_no','','trim');
  222. $im = new TencentIm();
  223. // $im->delete_group_attr($room_no,'jiangjie');
  224. $im->get_group_attr($room_no);
  225. dump($im->getData());
  226. }
  227. }