123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- <?php
- namespace addons\shopro\controller\goods;
- use addons\shopro\controller\Common;
- use addons\shopro\service\goods\GoodsService;
- use think\Db;
- use app\utils\Service\Tencent\TencentIm;
- class Liveroom extends Common
- {
- protected $noNeedLogin = ['get_room_attr'];
- protected $noNeedRight = ['*'];
- //检查是不是主播或管理员,主要用来商品操作的鉴权
- private function check_auth($room_no,$user_id){
- $whereor = [
- 'admin_id' => $user_id,
- 'status' => 1,
- ];
- $check = Db::name('live_room_admin')->where('room_no',$room_no)->where('user_id',$user_id)->whereOr(function($query) use ($whereor) {
- $query->where($whereor);
- })->find();
- if(empty($check)){
- $this->error('没有操作权限');
- }
- $rs = empty($check) ? false : true;
- return $rs;
- }
- //商城所有商品列表
- public function goods_index()
- {
- //鉴权
- $room_no = input('room_no','','trim');
- $user = auth_user();
- $this->check_auth($room_no,$user['id']);
- //全平台所有商品列表
- $service = new GoodsService();
- $service->up();
- $keyword = input('keyword','','trim');
- if ($keyword) {
- $service->search($keyword);
- }
- $goods = $service->order('weigh desc,id desc')->select_autopage();
- $goods = collection($goods)->toArray();
- $goods = list_domain_image($goods,['image']);//主图
- //本直播间已经有的,标记
- $goods_ids = Db::name('live_room_goods')->where('room_no',$room_no)->where('status','up')->column('goods_id');
- foreach($goods as $key => $good){
- $good['goods_added'] = 0;
- if(in_array($good['id'],$goods_ids)){
- $good['goods_added'] = 1;
- }
- $goods[$key] = $good;
- }
- $this->success('获取成功', $goods);
- }
- //直播间商品列表顶部三个统计
- public function liveroom_index_tongji()
- {
- //鉴权
- $room_no = input('room_no','','trim');
- $user = auth_user();
- $this->check_auth($room_no,$user['id']);
- $session = input('session','','trim');
- $room_log = Db::name('live_room_log')->where('session',$session)->where('room_no',$room_no)->find();
- $rs = [
- 'sales_total_price' => $room_log['order_total_amount'], //销售额
- 'paid_ordernum' => $room_log['order_paidnum'], //支付订单量
- 'uv' => $room_log['uv'], //访客数
- ];
- $this->success('获取成功', $rs);
- }
- //直播间商品列表
- public function liveroom_index()
- {
- //鉴权
- $room_no = input('room_no','','trim');
- $user = auth_user();
- $this->check_auth($room_no,$user['id']);
- //直播间上架商品
- $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();
- if(empty($goods_list))
- {
- $this->success(1,null);
- }
- $goods_ids = array_column($goods_list,'goods_id');
- //商品列表
- $service = new GoodsService();
- $service->up();
- $keyword = input('keyword','','trim');
- if ($keyword) {
- $service->search($keyword);
- }
- $service->whereIds($goods_ids);
- $goods = $service->select_autopage();
- $goods = collection($goods)->toArray();
- $goods = list_domain_image($goods,['image']);//主图
- //追加字段
- foreach($goods as $key => $good){
- foreach($goods_list as $k => $v){
- if($good['id'] == $v['goods_id']){
- $good['is_top'] = $v['is_top'];
- $good['goods_no'] = $v['goods_no'];
- }
- }
- $goods[$key] = $good;
- }
- $rs = [
- 'number' => count($goods_ids),
- 'list' => $goods,
- ];
- $this->success('获取成功', $rs);
- }
- //添加
- public function add(){
- //鉴权
- $room_no = input('room_no','','trim');
- $user = auth_user();
- $this->check_auth($room_no,$user['id']);
- //检查商品
- $goods_id = input('goods_id',0,'intval');
- $goods_info = Db::name('shopro_goods')->where('id',$goods_id)->where('status','up')->find();
- if(empty($goods_info)){
- $this->error('该商品已下架');
- }
- //商品序号拿最大的+1
- $max = Db::name('live_room_goods')->where('room_no',$room_no)->order('goods_no desc')->value('goods_no');
- $max = $max ? $max : 0;
- $new_goods_no = $max + 1;
- //直播间上架商品
- $goods_check = Db::name('live_room_goods')->where('room_no',$room_no)->where('goods_id',$goods_id)->find();
- if($goods_check){
- if($goods_check['status'] == 'up'){$this->error('该商品已存在');}
- //重新上架
- 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()]);
- }else{
- $room_info = Db::name('live_room')->where('room_no',$room_no)->find();
- $new = [
- 'room_id' => $room_info['id'],
- 'room_no' => $room_info['room_no'],
- 'user_id' => $room_info['user_id'],
- 'goods_id' => $goods_id,
- 'goods_no' => $new_goods_no,
- 'status' => 'up',
- 'create_time' => time(),
- 'update_time' => time(),
- ];
- Db::name('live_room_goods')->insertGetId($new);
- }
- $this->success('添加成功');
- }
- //删除
- public function down(){
- //鉴权
- $room_no = input('room_no','','trim');
- $user = auth_user();
- $this->check_auth($room_no,$user['id']);
- $goods_id = input('goods_id','');
- $goods_id = explode(',',$goods_id);
-
- Db::name('live_room_goods')->where('room_no',$room_no)->where('goods_id','IN',$goods_id)->update(['status'=>'down','goods_no'=>0]);
- $this->success('操作成功');
- }
- //设置为讲解中,取消讲解
- public function top(){
- //鉴权
- $room_no = input('room_no','','trim');
- $user = auth_user();
- $this->check_auth($room_no,$user['id']);
- $goods_id = input('goods_id',0,'intval');
- $is_top = input('is_top',0,'intval');
- //取消讲解
- if($is_top == 0){
- Db::name('live_room_goods')->where('room_no',$room_no)->update(['is_top'=>0]);
- //修改房间属性
- $top_good[] = [
- 'key' => 'top_good',
- 'value' => json_encode((object)[]),
- ];
- $im = new TencentIm();
- $im->modify_group_attr($room_no,$top_good);
- $this->success('操作成功');
- }
- //直播间商品
- $check = Db::name('live_room_goods')->where('room_no',$room_no)->where('goods_id',$goods_id)->where('status','up')->find();
- if(empty($check)){
- $this->error('不存在的商品');
- }
- //商品
- $service = new GoodsService();
- $service->up();
- $service->where('id',$goods_id);
- $goods = $service->find();
- if(empty($goods)){
- $this->error('该商品已下架');
- }
- //精简商品
- $goods = json_decode(json_encode($goods),true);
- $goods = [
- 'id' => $goods['id'],
- 'title' => $goods['title'],
- 'image' => localpath_to_netpath($goods['image']),
- 'price' => $goods['price'],
- 'is_sku'=> $goods['is_sku'],
- 'goods_sku_price_id' => 0,
- ];
- if($goods['is_sku'] != 1){
- $goods['goods_sku_price_id'] = Db::name('shopro_goods_sku_price')->where('goods_id',$goods['id'])->value('id');
- }
- //其他取消讲解,讲解这个
- $goods_id = input('goods_id',0,'intval');
- Db::name('live_room_goods')->where('room_no',$room_no)->update(['is_top'=>0]);
- Db::name('live_room_goods')->where('room_no',$room_no)->where('goods_id',$goods_id)->update(['is_top'=>1]);
- //修改房间属性
- $top_good[] = [
- 'key' => 'top_good',
- 'value' => json_encode($goods),
- ];
- $im = new TencentIm();
- $im->modify_group_attr($room_no,$top_good);
- $this->success('操作成功');
- }
- //获取属性
- public function get_room_attr(){
- $room_no = input('room_no','','trim');
- $im = new TencentIm();
- // $im->delete_group_attr($room_no,'jiangjie');
- $im->get_group_attr($room_no);
- dump($im->getData());
- }
- }
|