微信号的好友列表
This commit is contained in:
@@ -23,6 +23,7 @@ Route::group('v1/', function () {
|
||||
|
||||
// 设备微信相关
|
||||
Route::group('device/wechats', function () {
|
||||
Route::get('friends', 'app\\devices\\controller\\DeviceWechat@getFriends'); // 获取微信好友列表
|
||||
Route::get('count', 'app\\devices\\controller\\DeviceWechat@count'); // 获取在线微信账号数量
|
||||
Route::get('device-count', 'app\\devices\\controller\\DeviceWechat@deviceCount'); // 获取有登录微信的设备数量
|
||||
Route::get('', 'app\\devices\\controller\\DeviceWechat@index'); // 获取在线微信账号列表
|
||||
|
||||
@@ -274,23 +274,6 @@ class DeviceWechat extends Controller
|
||||
]
|
||||
];
|
||||
|
||||
// 获取微信好友列表
|
||||
$friends = Db::table('tk_wechat_friend')
|
||||
->where('wechatAccountId', $id)
|
||||
->where('isDeleted', 0)
|
||||
->field([
|
||||
'id',
|
||||
'wechatId',
|
||||
'nickname',
|
||||
'avatar',
|
||||
'gender',
|
||||
'region',
|
||||
'signature',
|
||||
'labels',
|
||||
'createTime'
|
||||
])
|
||||
->select();
|
||||
|
||||
// 处理返回数据
|
||||
$data = [
|
||||
'basicInfo' => [
|
||||
@@ -322,7 +305,6 @@ class DeviceWechat extends Controller
|
||||
'lastUpdateTime' => $wechat['updateTime']
|
||||
],
|
||||
'restrictions' => $restrictions,
|
||||
'friends' => $friends
|
||||
];
|
||||
|
||||
return json([
|
||||
@@ -528,4 +510,73 @@ class DeviceWechat extends Controller
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信好友列表
|
||||
* 根据wechatId查询微信好友,支持分页和关键词筛选
|
||||
*
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function getFriends()
|
||||
{
|
||||
try {
|
||||
// 获取请求参数
|
||||
$wechatId = Request::param('wechatId');
|
||||
$page = (int)Request::param('page', 1);
|
||||
$limit = (int)Request::param('limit', 20);
|
||||
$keyword = Request::param('keyword', '');
|
||||
|
||||
// 参数验证
|
||||
if (empty($wechatId)) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '参数错误:微信ID不能为空'
|
||||
]);
|
||||
}
|
||||
|
||||
// 查询参数
|
||||
$params = [];
|
||||
if (!empty($keyword)) {
|
||||
$params['keyword'] = $keyword;
|
||||
}
|
||||
|
||||
// 调用模型方法获取好友列表
|
||||
$result = \app\devices\model\WechatFriend::getFriendsByWechatId($wechatId, $params, $page, $limit);
|
||||
|
||||
|
||||
|
||||
// 处理返回的数据
|
||||
$friendsList = [];
|
||||
foreach ($result['list'] as $friend) {
|
||||
$friendsList[] = [
|
||||
'wechatId' => $friend['wechatId'],
|
||||
'avatar' => $friend['avatar'] ?: '/placeholder.svg',
|
||||
'labels' => $friend['labels'] ?: [],
|
||||
'accountNickname' => $friend['accountNickname'] ?: '',
|
||||
'accountRealName' => $friend['accountRealName'] ?: '',
|
||||
'nickname' => $friend['nickname'] ?: '',
|
||||
'remark' => $friend['conRemark'] ?: '',
|
||||
'alias' => $friend['alias'] ?: '',
|
||||
'gender' => $friend['gender'] ?: 0,
|
||||
'region' => $friend['region'] ?: ''
|
||||
];
|
||||
}
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '获取成功',
|
||||
'data' => [
|
||||
'total' => $result['total'],
|
||||
'page' => $result['page'],
|
||||
'limit' => $result['limit'],
|
||||
'list' => $friendsList
|
||||
]
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '获取失败:' . $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
84
Server/application/devices/model/WechatFriend.php
Normal file
84
Server/application/devices/model/WechatFriend.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
namespace app\devices\model;
|
||||
|
||||
use think\Model;
|
||||
use think\Db;
|
||||
|
||||
/**
|
||||
* 微信好友模型类
|
||||
*/
|
||||
class WechatFriend extends Model
|
||||
{
|
||||
// 设置表名
|
||||
protected $name = 'wechat_friend';
|
||||
protected $prefix = 'tk_';
|
||||
|
||||
// 设置主键
|
||||
protected $pk = 'id';
|
||||
|
||||
// 自动写入时间戳
|
||||
protected $autoWriteTimestamp = 'datetime';
|
||||
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = 'createTime';
|
||||
protected $updateTime = 'updateTime';
|
||||
|
||||
// 定义字段类型
|
||||
protected $type = [
|
||||
'id' => 'integer',
|
||||
'wechatAccountId' => 'integer',
|
||||
'gender' => 'integer',
|
||||
'addFrom' => 'integer',
|
||||
'isDeleted' => 'integer',
|
||||
'isPassed' => 'integer',
|
||||
'accountId' => 'integer',
|
||||
'groupId' => 'integer',
|
||||
'labels' => 'json',
|
||||
'deleteTime' => 'datetime',
|
||||
'passTime' => 'datetime',
|
||||
'createTime' => 'datetime'
|
||||
];
|
||||
|
||||
/**
|
||||
* 根据微信账号ID获取好友列表
|
||||
*
|
||||
* @param string $ownerWechatId 所有者微信ID
|
||||
* @param array $params 查询条件参数
|
||||
* @param int $page 页码
|
||||
* @param int $limit 每页数量
|
||||
* @return array 好友列表和总数
|
||||
*/
|
||||
public static function getFriendsByWechatId($ownerWechatId, $params = [], $page = 1, $limit = 20)
|
||||
{
|
||||
// 构建基础查询
|
||||
$query = self::where('ownerWechatId', $ownerWechatId)
|
||||
->where('isDeleted', 0);
|
||||
|
||||
// 添加筛选条件(昵称、备注、微信号、标签)
|
||||
if (!empty($params['keyword'])) {
|
||||
$keyword = $params['keyword'];
|
||||
$query->where(function($q) use ($keyword) {
|
||||
$q->whereOr('nickname', 'like', "%{$keyword}%")
|
||||
->whereOr('conRemark', 'like', "%{$keyword}%")
|
||||
->whereOr('alias', 'like', "%{$keyword}%")
|
||||
->whereOr("JSON_SEARCH(labels, 'one', '%{$keyword}%') IS NOT NULL");
|
||||
});
|
||||
}
|
||||
|
||||
// 计算总数
|
||||
$total = $query->count();
|
||||
|
||||
// 分页查询数据
|
||||
$friends = $query->page($page, $limit)
|
||||
->order('createTime desc')
|
||||
->field('wechatId, alias, avatar, labels, accountNickname, accountRealName, nickname, conRemark, gender, region')
|
||||
->select();
|
||||
|
||||
return [
|
||||
'list' => $friends,
|
||||
'total' => $total,
|
||||
'page' => $page,
|
||||
'limit' => $limit
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user