2025-03-17 10:09:27 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace app\api\controller;
|
|
|
|
|
|
2025-03-24 16:42:36 +08:00
|
|
|
use app\api\model\WechatFriendModel;
|
2025-03-17 10:09:27 +08:00
|
|
|
use think\facade\Request;
|
2025-04-01 09:26:06 +08:00
|
|
|
use think\facade\Log;
|
2025-03-17 10:09:27 +08:00
|
|
|
|
|
|
|
|
class WechatFriendController extends BaseController
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* 获取微信好友列表数据
|
2025-04-12 15:08:21 +08:00
|
|
|
* @param string $pageIndex 页码
|
|
|
|
|
* @param string $pageSize 每页大小
|
|
|
|
|
* @param string $preFriendId 上一个好友ID
|
|
|
|
|
* @param bool $isJob 是否为任务调用
|
2025-03-17 10:09:27 +08:00
|
|
|
* @return \think\response\Json
|
|
|
|
|
*/
|
2025-04-23 18:13:01 +08:00
|
|
|
public function getlist($pageIndex = '', $pageSize = '', $preFriendId = '', $isJob = false,$isDel = '')
|
2025-03-17 10:09:27 +08:00
|
|
|
{
|
2025-04-12 15:08:21 +08:00
|
|
|
// 获取授权token
|
|
|
|
|
$authorization = trim($this->request->header('authorization', $this->authorization));
|
|
|
|
|
if (empty($authorization)) {
|
|
|
|
|
if ($isJob) {
|
|
|
|
|
return json_encode(['code' => 500, 'msg' => '缺少授权信息']);
|
|
|
|
|
} else {
|
|
|
|
|
return errorJson('缺少授权信息');
|
|
|
|
|
}
|
2025-03-17 10:09:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2025-04-23 18:13:01 +08:00
|
|
|
// 根据isDel设置对应的isDeleted值
|
|
|
|
|
$isDeleted = null; // 默认值
|
|
|
|
|
if ($isDel === '0' || $isDel === 0) {
|
|
|
|
|
$isDeleted = false;
|
|
|
|
|
} elseif ($isDel === '1' || $isDel === 1) {
|
|
|
|
|
$isDeleted = true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-17 10:09:27 +08:00
|
|
|
// 构建请求参数
|
|
|
|
|
$params = [
|
2025-03-18 14:56:14 +08:00
|
|
|
'accountKeyword' => '',
|
|
|
|
|
'addFrom' => '[]',
|
2025-03-17 10:09:27 +08:00
|
|
|
'allotAccountId' => input('allotAccountId', ''),
|
2025-03-18 14:56:14 +08:00
|
|
|
'containSubDepartment' => false,
|
|
|
|
|
'departmentId' => '',
|
|
|
|
|
'extendFields' => '{}',
|
|
|
|
|
'gender' => '',
|
|
|
|
|
'groupId' => null,
|
2025-04-23 18:13:01 +08:00
|
|
|
'isDeleted' => $isDeleted,
|
2025-03-18 14:56:14 +08:00
|
|
|
'isPass' => null,
|
2025-04-12 15:08:21 +08:00
|
|
|
'keyword' => input('keyword', ''),
|
2025-03-18 14:56:14 +08:00
|
|
|
'labels' => '[]',
|
2025-04-01 09:26:06 +08:00
|
|
|
'pageIndex' => !empty($pageIndex) ? $pageIndex : input('pageIndex', 0),
|
|
|
|
|
'pageSize' => !empty($pageSize) ? $pageSize : input('pageSize', 20),
|
|
|
|
|
'preFriendId' => !empty($preFriendId) ? $preFriendId : input('preFriendId', ''),
|
2025-03-17 10:09:27 +08:00
|
|
|
'wechatAccountKeyword' => input('wechatAccountKeyword', '')
|
|
|
|
|
];
|
2025-04-12 15:08:21 +08:00
|
|
|
|
2025-03-17 10:09:27 +08:00
|
|
|
// 设置请求头
|
|
|
|
|
$headerData = ['client:system'];
|
2025-03-18 14:56:14 +08:00
|
|
|
$header = setHeader($headerData, $authorization);
|
2025-03-17 10:09:27 +08:00
|
|
|
|
|
|
|
|
// 发送请求获取好友列表
|
2025-04-12 15:08:21 +08:00
|
|
|
$result = requestCurl($this->baseUrl . 'api/WechatFriend/friendlistData', $params, 'POST', $header, 'json');
|
2025-03-17 10:09:27 +08:00
|
|
|
$response = handleApiResponse($result);
|
|
|
|
|
|
|
|
|
|
// 保存数据到数据库
|
2025-03-18 14:56:14 +08:00
|
|
|
if (is_array($response)) {
|
|
|
|
|
foreach ($response as $item) {
|
2025-03-17 10:09:27 +08:00
|
|
|
$this->saveFriend($item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-12 15:08:21 +08:00
|
|
|
if ($isJob) {
|
|
|
|
|
return json_encode(['code' => 200, 'msg' => 'success', 'data' => $response]);
|
|
|
|
|
} else {
|
2025-04-01 09:26:06 +08:00
|
|
|
return successJson($response);
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-17 10:09:27 +08:00
|
|
|
} catch (\Exception $e) {
|
2025-04-12 15:08:21 +08:00
|
|
|
if ($isJob) {
|
|
|
|
|
return json_encode(['code' => 500, 'msg' => '获取微信好友列表失败:' . $e->getMessage()]);
|
|
|
|
|
} else {
|
2025-04-01 09:26:06 +08:00
|
|
|
return errorJson('获取微信好友列表失败:' . $e->getMessage());
|
|
|
|
|
}
|
2025-03-17 10:09:27 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 保存微信好友数据到数据库
|
|
|
|
|
* @param array $item 微信好友数据
|
|
|
|
|
*/
|
|
|
|
|
private function saveFriend($item)
|
|
|
|
|
{
|
|
|
|
|
$data = [
|
2025-03-29 17:01:56 +08:00
|
|
|
'id' => $item['id'],
|
2025-03-17 10:09:27 +08:00
|
|
|
'wechatAccountId' => $item['wechatAccountId'],
|
|
|
|
|
'alias' => $item['alias'],
|
|
|
|
|
'wechatId' => $item['wechatId'],
|
|
|
|
|
'conRemark' => $item['conRemark'],
|
|
|
|
|
'nickname' => $item['nickname'],
|
|
|
|
|
'pyInitial' => $item['pyInitial'],
|
|
|
|
|
'quanPin' => $item['quanPin'],
|
|
|
|
|
'avatar' => $item['avatar'],
|
|
|
|
|
'gender' => $item['gender'],
|
|
|
|
|
'region' => $item['region'],
|
|
|
|
|
'addFrom' => $item['addFrom'],
|
|
|
|
|
'labels' => is_array($item['labels']) ? json_encode($item['labels']) : json_encode([]),
|
|
|
|
|
'signature' => $item['signature'],
|
|
|
|
|
'isDeleted' => $item['isDeleted'],
|
|
|
|
|
'isPassed' => $item['isPassed'],
|
2025-04-15 08:56:26 +08:00
|
|
|
'deleteTime' => !empty($item['isDeleted']) ? strtotime($item['deleteTime']) : 0,
|
2025-03-17 10:09:27 +08:00
|
|
|
'accountId' => $item['accountId'],
|
|
|
|
|
'extendFields' => is_array($item['extendFields']) ? json_encode($item['extendFields']) : json_encode([]),
|
|
|
|
|
'accountUserName' => $item['accountUserName'],
|
|
|
|
|
'accountRealName' => $item['accountRealName'],
|
|
|
|
|
'accountNickname' => $item['accountNickname'],
|
|
|
|
|
'ownerAlias' => $item['ownerAlias'],
|
|
|
|
|
'ownerWechatId' => $item['ownerWechatId'],
|
|
|
|
|
'ownerNickname' => $item['ownerNickname'],
|
|
|
|
|
'ownerAvatar' => $item['ownerAvatar'],
|
|
|
|
|
'phone' => $item['phone'],
|
|
|
|
|
'thirdParty' => is_array($item['thirdParty']) ? json_encode($item['thirdParty']) : json_encode([]),
|
|
|
|
|
'groupId' => $item['groupId'],
|
2025-04-15 08:56:26 +08:00
|
|
|
'passTime' => !empty($item['isPassed']) && $item['passTime'] != '0001-01-01T00:00:00' ? strtotime($item['passTime']) : 0,
|
2025-03-17 10:09:27 +08:00
|
|
|
'additionalPicture' => $item['additionalPicture'],
|
|
|
|
|
'desc' => $item['desc'],
|
|
|
|
|
'country' => $item['country'],
|
2025-04-11 16:15:48 +08:00
|
|
|
'privince' => isset($item['privince']) ? $item['privince'] : '',
|
2025-03-18 14:56:14 +08:00
|
|
|
'city' => isset($item['city']) ? $item['city'] : '',
|
2025-04-15 08:56:26 +08:00
|
|
|
'createTime' => isset($item['createTime']) ? strtotime($item['createTime']) : 0,
|
2025-04-01 09:26:06 +08:00
|
|
|
'updateTime' => time()
|
2025-03-17 10:09:27 +08:00
|
|
|
];
|
|
|
|
|
|
2025-04-12 15:08:21 +08:00
|
|
|
// 使用ID作为唯一性判断
|
|
|
|
|
$friend = WechatFriendModel::where('id', $item['id'])->find();
|
2025-03-17 10:09:27 +08:00
|
|
|
|
|
|
|
|
if ($friend) {
|
|
|
|
|
$friend->save($data);
|
|
|
|
|
} else {
|
|
|
|
|
WechatFriendModel::create($data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|