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

115 lines
4.6 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\WechatAccountModel;
2025-03-17 10:09:27 +08:00
class WechatController extends BaseController
{
/**
* 保存微信账号数据到数据库
* @param array $item 微信账号数据
*/
private function saveWechatAccount($item)
{
$data = [
2025-03-29 17:01:56 +08:00
'id' => $item['id'],
2025-03-17 10:09:27 +08:00
'wechatId' => $item['wechatId'],
'deviceAccountId' => $item['deviceAccountId'],
'imei' => $item['imei'],
'deviceMemo' => $item['deviceMemo'],
'accountUserName' => $item['accountUserName'],
'accountRealName' => $item['accountRealName'],
'accountNickname' => $item['accountNickname'],
'keFuAlive' => $item['keFuAlive'],
'deviceAlive' => $item['deviceAlive'],
'wechatAlive' => $item['wechatAlive'],
'yesterdayMsgCount' => $item['yesterdayMsgCount'],
'sevenDayMsgCount' => $item['sevenDayMsgCount'],
'thirtyDayMsgCount' => $item['thirtyDayMsgCount'],
'totalFriend' => $item['totalFriend'],
'maleFriend' => $item['maleFriend'],
'femaleFriend' => $item['femaleFriend'],
'wechatGroupName' => $item['wechatGroupName'],
'tenantId' => $item['tenantId'],
'nickname' => $item['nickname'],
'alias' => $item['alias'],
'avatar' => $item['avatar'],
'gender' => $item['gender'],
'region' => $item['region'],
'signature' => $item['signature'],
'bindQQ' => $item['bindQQ'],
'bindEmail' => $item['bindEmail'],
'bindMobile' => $item['bindMobile'],
'currentDeviceId' => $item['currentDeviceId'],
'isDeleted' => $item['isDeleted'],
'deleteTime' => $item['deleteTime'],
'groupId' => $item['groupId'],
'memo' => $item['memo'],
'wechatVersion' => $item['wechatVersion'],
2025-04-02 10:19:03 +08:00
'labels' => !empty($item['labels']) ? json_encode($item['labels']) : json_encode([]),
'updateTime' => time()
2025-03-17 10:09:27 +08:00
];
2025-03-29 17:01:56 +08:00
$account = WechatAccountModel::where('id', $item['id'])->find();
2025-03-17 10:09:27 +08:00
if ($account) {
$account->save($data);
} else {
WechatAccountModel::create($data);
}
}
2025-04-09 14:45:27 +08:00
public function getlist($pageIndex = '',$pageSize = '',$isJob = false)
2025-03-17 10:09:27 +08:00
{
// 获取授权token
2025-04-09 14:45:27 +08:00
$authorization = trim($this->request->header('authorization', $this->authorization));
2025-03-17 10:09:27 +08:00
if (empty($authorization)) {
2025-04-02 10:19:03 +08:00
if($isJob){
return json_encode(['code'=>500,'msg'=>'缺少授权信息']);
}else{
return errorJson('缺少授权信息');
}
2025-03-17 10:09:27 +08:00
}
try {
// 构建请求参数
$params = [
2025-03-18 14:56:14 +08:00
'wechatAlive' => $this->request->param('wechatAlive', ''),
'keyword' => $this->request->param('keyword', ''),
'groupId' => $this->request->param('groupId', ''),
'departmentId' => $this->request->param('departmentId', ''),
'hasDevice' => $this->request->param('hasDevice', ''),
'deviceGroupId' => $this->request->param('deviceGroupId', ''),
'containSubDepartment' => $this->request->param('containSubDepartment', 'false'),
2025-04-02 10:19:03 +08:00
'pageIndex' => !empty($pageIndex) ? $pageIndex : $this->request->param('pageIndex', 0),
'pageSize' => !empty($pageSize) ? $pageSize : $this->request->param('pageSize', 10)
2025-03-17 10:09:27 +08:00
];
// 设置请求头
$headerData = ['client:system'];
$header = setHeader($headerData, $authorization, 'plain');
// 发送请求
$result = requestCurl($this->baseUrl . 'api/WechatAccount/list', $params, 'GET', $header);
$response = handleApiResponse($result);
// 保存数据到数据库
if (!empty($response['results'])) {
foreach ($response['results'] as $item) {
$this->saveWechatAccount($item);
}
}
2025-04-02 10:19:03 +08:00
if($isJob){
return json_encode(['code'=>200,'msg'=>'获取微信账号列表成功','data'=>$response]);
}else{
return successJson($response);
}
2025-03-17 10:09:27 +08:00
} catch (\Exception $e) {
2025-04-02 10:19:03 +08:00
if($isJob){
return json_encode(['code'=>500,'msg'=>'获取微信账号列表失败:' . $e->getMessage()]);
}else{
return errorJson('获取微信账号列表失败:' . $e->getMessage());
}
2025-03-17 10:09:27 +08:00
}
}
}