request->param('page',1); $limit = $this->request->param('limit',20); $keyword = $this->request->param('keyword',''); $deviceIds = $this->request->param('deviceIds',''); if(!empty($deviceIds)){ $deviceIds = explode(',',$deviceIds); } try { $where = []; if ($this->getUserInfo('isAdmin') == 1) { $where[] = ['isDeleted','=',0]; } else { $where[] = ['isDeleted','=',0]; } if(!empty($keyword)){ $where[] = ['nickname|alias|wechatId','like','%'.$keyword.'%']; } $wechatIds = Db::name('device')->alias('d') ->join('device_wechat_login dwl','dwl.deviceId=d.id AND dwl.companyId='.$this->getUserInfo('companyId')) ->where(['d.companyId' => $this->getUserInfo('companyId'),'d.deleteTime' => 0])->group('dwl.deviceId')->order('dwl.id desc'); /*$wechatIds = Db::name('device')->alias('d') // 仅关联每个设备在 device_wechat_login 中的最新一条记录 ->join('(SELECT MAX(id) AS id, deviceId FROM ck_device_wechat_login WHERE companyId='.$companyId.' GROUP BY deviceId) dwl_max','dwl_max.deviceId = d.id') ->join('device_wechat_login dwl','dwl.id = dwl_max.id') ->where(['d.companyId' => $companyId,'d.deleteTime' => 0]);*/ if (!empty($deviceIds)){ $wechatIds = $wechatIds->where('d.id','in',$deviceIds); } $wechatIds = $wechatIds->column('dwl.wechatId'); $where[] = ['ownerWechatId','in',$wechatIds]; $data = Db::table('s2_wechat_friend') ->field(['nickname','avatar','alias','id','wechatId','ownerNickname','ownerAlias','ownerWechatId','createTime']) ->where($where); $total = $data->count(); $list = $data->page($page, $limit)->order('id DESC')->select(); // $data = WechatFriendShipModel::alias('wf') // ->field(['wa1.nickname','wa1.avatar','wa1.alias','wf.id','wf.wechatId','wa2.nickname as ownerNickname','wa2.alias as ownerAlias','wa2.wechatId as ownerWechatId','wf.createTime']) // ->Join('wechat_account wa1','wf.wechatId = wa1.wechatId') // ->Join('wechat_account wa2','wf.ownerWechatId = wa2.wechatId') // ->where($where); // // $total = $data->count(); // $list = $data->page($page, $limit)->order('wf.id DESC')->group('wf.id')->select(); return json([ 'code' => 200, 'msg' => '获取成功', 'data' => [ 'list' => $list, 'total' => $total, 'companyId' => $this->getUserInfo('companyId') ] ]); } catch (\Exception $e) { return json([ 'code' => $e->getCode(), 'msg' => $e->getMessage() ]); } } }