Files
cunkebao_v3/Server/application/api/controller/WechatFriendController.php

186 lines
7.5 KiB
PHP
Raw Normal View History

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-10-15 16:39:10 +08:00
use think\Db;
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
2025-04-30 11:15:19 +08:00
* @param bool $isInner 是否为任务调用
2025-03-17 10:09:27 +08:00
* @return \think\response\Json
*/
2025-05-16 15:12:49 +08:00
public function getlist($data = [], $isInner = 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)) {
2025-04-30 11:15:19 +08:00
if ($isInner) {
2025-04-12 15:08:21 +08:00
return json_encode(['code' => 500, 'msg' => '缺少授权信息']);
} else {
return errorJson('缺少授权信息');
}
2025-03-17 10:09:27 +08:00
}
2025-05-16 15:12:49 +08:00
$pageIndex = !empty($data['pageIndex']) ? $data['pageIndex'] : '';
$pageSize = !empty($data['pageSize']) ? $data['pageSize'] : '';
$preFriendId = !empty($data['preFriendId']) ? $data['preFriendId'] : '';
$friendKeyword = !empty($data['friendKeyword']) ? $data['friendKeyword'] : '';
$wechatAccountKeyword = !empty($data['wechatAccountKeyword']) ? $data['wechatAccountKeyword'] : '';
2025-03-17 10:09:27 +08:00
try {
// 初始化isUpdate标志为false
$isUpdate = false;
// 根据isDel设置对应的isDeleted值
$isDeleted = null; // 默认值
2025-04-25 16:43:54 +08:00
if ($isDel == '0' || $isDel == 0) {
$isDeleted = false;
2025-04-25 16:43:54 +08:00
} 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,
'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-05-16 15:12:49 +08:00
'friendKeyword' => !empty($friendKeyword) ? $friendKeyword : input('friendKeyword', ''),
'wechatAccountKeyword' => !empty($wechatAccountKeyword) ? $wechatAccountKeyword : input('wechatAccountKeyword', '')
2025-03-17 10:09:27 +08:00
];
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)) {
$isUpdate = false;
2025-10-15 16:39:10 +08:00
$updated = $this->saveFriend($response);
if($updated && $isDel == 0){
$isUpdate = true;
2025-10-15 14:08:56 +08:00
}
2025-03-17 10:09:27 +08:00
}
2025-04-30 11:15:19 +08:00
if ($isInner) {
return json_encode(['code' => 200, 'msg' => 'success', 'data' => $response, 'isUpdate' => $isUpdate]);
2025-04-12 15:08:21 +08:00
} 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-30 11:15:19 +08:00
if ($isInner) {
2025-04-12 15:08:21 +08:00
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 微信好友数据
* @return bool 是否创建或更新了记录
2025-03-17 10:09:27 +08:00
*/
2025-10-15 16:39:10 +08:00
private function saveFriend($data)
2025-03-17 10:09:27 +08:00
{
2025-10-15 16:39:10 +08:00
$sqlData = [];
foreach ($data as $item) {
$sqlData[] = [
'id' => $item['id'],
'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([]),
'siteLabels' => json_encode([]),
'signature' => $item['signature'],
'isDeleted' => $item['isDeleted'],
'isPassed' => $item['isPassed'],
'deleteTime' => !empty($item['isDeleted']) ? strtotime($item['deleteTime']) : 0,
'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'],
'passTime' => !empty($item['isPassed']) && $item['passTime'] != '0001-01-01T00:00:00' ? strtotime($item['passTime']) : 0,
'additionalPicture' => $item['additionalPicture'],
'desc' => $item['desc'],
'country' => $item['country'],
'privince' => isset($item['privince']) ? $item['privince'] : '',
'city' => isset($item['city']) ? $item['city'] : '',
'createTime' => isset($item['createTime']) ? strtotime($item['createTime']) : 0,
'updateTime' => time()
];
}
if (empty($sqlData)) {
return false;
}
2025-10-15 14:08:56 +08:00
2025-10-15 16:39:10 +08:00
// 先查已存在ID拆分插入与更新两批
$ids = array_column($sqlData, 'id');
$existingIds = WechatFriendModel::whereIn('id', $ids)->column('id');
$existingSet = array_flip($existingIds);
2025-03-17 10:09:27 +08:00
2025-10-15 16:39:10 +08:00
$toInsert = [];
$toUpdate = [];
foreach ($sqlData as $row) {
if (isset($existingSet[$row['id']])) {
$toUpdate[] = $row;
} else {
$toInsert[] = $row;
}
}
$isUpdate = false;
if (!empty($toInsert)) {
$m = new WechatFriendModel();
$m->insertAll($toInsert,true);
$isUpdate = false;
}
if (!empty($toUpdate)) {
// 批量更新使用 saveAll按主键更新
$m = new WechatFriendModel();
$m->saveAll($toUpdate);
$isUpdate = true;
2025-03-17 10:09:27 +08:00
}
2025-10-15 16:39:10 +08:00
return $isUpdate;
2025-03-17 10:09:27 +08:00
}
}