2025-04-18 10:38:50 +08:00
< ? php
namespace app\cunkebao\controller\friend ;
use app\common\model\Device as DeviceModel ;
use app\common\model\DeviceUser as DeviceUserModel ;
2025-05-12 15:32:42 +08:00
use app\common\model\WechatFriendShip as WechatFriendShipModel ;
2025-04-18 10:38:50 +08:00
use app\cunkebao\controller\BaseController ;
2025-04-22 19:17:14 +08:00
use think\Db ;
2025-04-18 10:38:50 +08:00
/**
* 设备管理控制器
*/
class GetFriendListV1Controller extends BaseController
{
/**
* 获取好友列表
* @ return \think\response\Json
*/
public function index ()
{
2025-04-18 13:46:34 +08:00
$page = $this -> request -> param ( 'page' , 1 );
$limit = $this -> request -> param ( 'limit' , 20 );
2025-04-21 09:19:50 +08:00
$keyword = $this -> request -> param ( 'keyword' , '' );
2025-05-14 17:29:32 +08:00
$deviceIds = $this -> request -> param ( 'deviceIds' , '' );
if ( ! empty ( $deviceIds )){
$deviceIds = explode ( ',' , $deviceIds );
}
2025-04-18 10:38:50 +08:00
try {
$where = [];
if ( $this -> getUserInfo ( 'isAdmin' ) == 1 ) {
2025-07-03 10:44:41 +08:00
$where [] = [ 'isDeleted' , '=' , 0 ];
2025-04-18 10:38:50 +08:00
} else {
2025-07-03 10:44:41 +08:00
$where [] = [ 'isDeleted' , '=' , 0 ];
2025-04-18 10:38:50 +08:00
}
2025-04-22 19:17:14 +08:00
if ( ! empty ( $keyword )){
2025-07-03 10:44:41 +08:00
$where [] = [ 'nickname|alias|wechatId' , 'like' , '%' . $keyword . '%' ];
2025-04-21 09:19:50 +08:00
}
2025-04-18 13:46:34 +08:00
2025-07-11 16:16:10 +08:00
$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 ]);
2025-07-03 10:44:41 +08:00
2025-07-11 16:16:10 +08:00
if ( ! empty ( $deviceIds )){
2025-07-14 09:31:23 +08:00
$wechatIds = $wechatIds -> where ( 'd.id' , 'in' , $deviceIds );
2025-05-14 17:29:32 +08:00
}
2025-07-11 16:16:10 +08:00
$wechatIds = $wechatIds -> column ( 'dwl.wechatId' );
2025-05-14 17:29:32 +08:00
2025-07-11 16:16:10 +08:00
$where [] = [ 'ownerWechatId' , 'in' , $wechatIds ];
2025-05-14 17:29:32 +08:00
2025-07-03 10:44:41 +08:00
$data = Db :: table ( 's2_wechat_friend' )
-> field ([ 'nickname' , 'avatar' , 'alias' , 'id' , 'wechatId' , 'ownerNickname' , 'ownerAlias' , 'ownerWechatId' , 'createTime' ])
2025-04-18 10:38:50 +08:00
-> where ( $where );
2025-04-18 13:46:34 +08:00
$total = $data -> count ();
2025-07-03 10:44:41 +08:00
$list = $data -> page ( $page , $limit ) -> order ( 'id DESC' ) -> select ();
2025-04-18 10:38:50 +08:00
2025-07-03 10:44:41 +08:00
// $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();
2025-04-18 10:38:50 +08:00
2025-07-03 10:44:41 +08:00
2025-04-18 10:38:50 +08:00
return json ([
'code' => 200 ,
'msg' => '获取成功' ,
'data' => [
2025-04-18 13:46:34 +08:00
'list' => $list ,
'total' => $total ,
2025-06-27 10:02:53 +08:00
'companyId' => $this -> getUserInfo ( 'companyId' )
2025-04-18 10:38:50 +08:00
]
]);
} catch ( \Exception $e ) {
return json ([
'code' => $e -> getCode (),
'msg' => $e -> getMessage ()
]);
}
}
}