代码提交

This commit is contained in:
wong
2025-06-06 17:09:27 +08:00
parent c0f80df780
commit 034d7f6c48
6 changed files with 446 additions and 0 deletions

View File

@@ -0,0 +1,156 @@
<?php
namespace library\s2;
use think\Exception;
use think\facade\Env;
class CurlHandle
{
private $baseUrl = '';
private $authorization = '';
private $header = [];
private $method = 'get';
private static $instant = null;
/**
* 获取域名
* @return void
*/
private function getBaseUrl()
{
$this->baseUrl = Env::get('api.wechat_url');
}
/**
* CurlHandle constructor.
*/
public function __construct()
{
$this->getBaseUrl();
}
/**
* 设置头部
*
* @param array $headerData 头部数组
* @param string $authorization
* @param string $type 类型 默认json (json,plain)
* @return array
*/
public function setHeader($key, $value): CurlHandle
{
if (is_array($key)) {
$this->header = array_merge($this->header, $key);
} else {
$this->header[$key] = $value;
}
return $this;
}
private function getHearder(): array
{
$header = [];
foreach ($this->header as $key => $value) {
$header[] = $key . ':' . $value;
}
return $header;
}
public function setMethod(string $method): CurlHandle
{
$this->method = $method;
return $this;
}
/**
* @param string $baseUrl
* @return $this
*/
public function setBaseUrl(string $baseUrl): CurlHandle
{
$this->baseUrl = $baseUrl;
return $this;
}
/**
* @param string $url 请求的链接
* @param array $params 请求附带的参数
* @param string $method 请求的方式, 支持GET, POST, PUT, DELETE等
* @param array $header 头部
* @param string $type 数据类型支持dataBuild、json等
* @return bool|string
*/
public function send($url, $params = [], $type = 'dataBuild')
{
$str = '';
if (!empty($url)) {
try {
$ch = curl_init();
$method = $this->method;
$url = $this->baseUrl . $url;
// 处理GET请求的参数
if (strtoupper($method) == 'GET' && !empty($params)) {
$url = $url . '?' . dataBuild($params);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //30秒超时
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getHearder());
// 处理不同的请求方法
if (strtoupper($method) != 'GET') {
// 设置请求方法
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));
// 处理参数格式
if ($type == 'dataBuild') {
$params = dataBuild($params);
} elseif ($type == 'json') {
$params = json_encode($params);
} else {
$params = dataBuild($params);
}
// 设置请求体
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //是否验证对等证书,1则验证0则不验证
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$str = curl_exec($ch);
if(curl_errno($ch)) {
echo 'Curl error: ' .curl_errno($ch) . ':' . curl_error($ch);
}
curl_close($ch);
} catch (Exception $e) {
$str = '';
}
}
return $str;
}
/**
* 单例
*
* @return static|null
*/
public static function getInstant()
{
if (self::$instant instanceof self) {
return self::$instant;
}
return new static();
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace library\s2\interfaces;
interface AccountInterface
{
/**
* 创建账号
*
* @param string $accout
* @param string $password
* @return mixed
*/
public function create(string $accout, string $password);
/**
* 删除账号
*
* @param int $id
* @return mixed
*/
public function delete(int $id);
}

View File

@@ -0,0 +1,13 @@
<?php
namespace library\s2\interfaces;
interface DeviceInterface
{
/**
* 获取设备列表
* @param array $params
* @return array
*/
public function getlist(array $params): array;
}

View File

@@ -0,0 +1,15 @@
<?php
namespace library\s2\interfaces;
interface LoginInterface
{
/**
* 创建账号
*
* @param string $accout
* @param string $password
* @return mixed
*/
public function login(string $accout, string $password): LoginInterface;
}

View File

@@ -0,0 +1,25 @@
<?php
namespace library\s2\logics;
use library\s2\interfaces\AccountInterface;
use library\s2\interfaces\LoginInterface;
class AccountLogic implements AccountInterface, LoginInterface
{
public function create(string $accout, string $password)
{
// TODO: Implement create() method.
}
public function login(string $accout, string $password): LoginInterface
{
// TODO: Implement login() method.
return $this;
}
public function delete(int $id)
{
// TODO: Implement delete() method.
}
}

View File

@@ -0,0 +1,214 @@
<?php
namespace library\s2\logics;
use app\api\model\DeviceGroupModel;
use app\api\model\DeviceModel;
use app\common\service\AuthService;
use library\s2\CurlHandle;
use library\s2\interfaces\DeviceInterface;
use think\facade\Log;
class DeviceLogic implements DeviceInterface
{
/**
* 获取设备列表
* @inheritDoc
*/
public function getlist(array $params = []): array
{
try {
// 构建请求参数
$params = [
'accountId' => $params['accountId'] ?? '',
'keyword' => $params['keyword'] ?? '',
'imei' => $params['imei'] ?? '',
'groupId' => $params['groupId'] ?? '',
'brand' => $params['brand'] ?? '',
'model' => $params['model'] ?? '',
'deleteType' => $params['deleteType'] ?? 'unDeleted',
'operatingSystem' => $params['operatingSystem'] ?? '',
'softwareVersion' => $params['softwareVersion'] ?? '',
'phoneAppVersion' => $params['phoneAppVersion'] ?? '',
'recorderVersion' => $params['recorderVersion'] ?? '',
'contactsVersion' => $params['contactsVersion'] ?? '',
'rooted' => $params['rooted'] ?? '',
'xPosed' => $params['xPosed'] ?? '',
'alive' => $params['alive'] ?? '',
'hasWechat' => $params['hasWechat'] ?? '',
'departmentId' => $params['departmentId'] ?? '',
'pageIndex' => $params['pageIndex'] ?? 0,
'pageSize' => $params['pageSize'] ?? 20
];
$JWT = AuthService::getSystemAuthorization();
$result = CurlHandle::getInstant()
->setHeader('Content-Type', 'text/plain')
->setHeader('authorization', 'bearer ' . $JWT)
->setMethod('get')
->send('api/Account/myTenantPageAccounts', $params);
$response = handleApiResponse($result);
// 保存数据到数据库
if (!empty($response['results'])) {
foreach ($response['results'] as $item) {
$this->saveData($item);
}
}
return json_encode(['code' => 200, 'msg' => '获取公司账号列表成功', 'data' => $response]);
} catch (\Exception $e) {
return json_encode(['code' => 500, 'msg' => '获取公司账号列表失败:' . $e->getMessage()]);
}
}
private function saveData($item)
{
$data = [
'id' => isset($item['id']) ? $item['id'] : '',
'userName' => isset($item['userName']) ? $item['userName'] : '',
'nickname' => isset($item['nickname']) ? $item['nickname'] : '',
'realName' => isset($item['realName']) ? $item['realName'] : '',
'groupName' => isset($item['groupName']) ? $item['groupName'] : '',
'wechatAccounts' => isset($item['wechatAccounts']) ? json_encode($item['wechatAccounts']) : json_encode([]),
'alive' => isset($item['alive']) ? $item['alive'] : false,
'lastAliveTime' => isset($item['lastAliveTime']) ? $item['lastAliveTime'] : null,
'tenantId' => isset($item['tenantId']) ? $item['tenantId'] : 0,
'groupId' => isset($item['groupId']) ? $item['groupId'] : 0,
'currentAccountId' => isset($item['currentAccountId']) ? $item['currentAccountId'] : 0,
'imei' => $item['imei'],
'memo' => isset($item['memo']) ? $item['memo'] : '',
'createTime' => isset($item['createTime']) ? strtotime($item['createTime']) : 0,
'isDeleted' => isset($item['isDeleted']) ? $item['isDeleted'] : false,
'deletedAndStop' => isset($item['deletedAndStop']) ? $item['deletedAndStop'] : false,
'deleteTime' => empty($item['isDeleted']) ? 0 : strtotime($item['deleteTime']),
'rooted' => isset($item['rooted']) ? $item['rooted'] : false,
'xPosed' => isset($item['xPosed']) ? $item['xPosed'] : false,
'brand' => isset($item['brand']) ? $item['brand'] : '',
'model' => isset($item['model']) ? $item['model'] : '',
'operatingSystem' => isset($item['operatingSystem']) ? $item['operatingSystem'] : '',
'softwareVersion' => isset($item['softwareVersion']) ? $item['softwareVersion'] : '',
'extra' => isset($item['extra']) ? json_encode($item['extra']) : json_encode([]),
'phone' => isset($item['phone']) ? $item['phone'] : '',
'lastUpdateTime' => isset($item['lastUpdateTime']) ? ($item['lastUpdateTime'] == '0001-01-01T00:00:00' ? 0 : strtotime($item['lastUpdateTime'])) : 0
];
// 使用imei作为唯一性判断
$device = DeviceModel::where('id', $item['id'])->find();
if ($device) {
$device->save($data);
} else {
// autoLike自动点赞
// momentsSync朋友圈同步
// autoCustomerDev自动开发客户
// groupMessageDeliver群消息推送
// autoGroup自动建群
$data['taskConfig'] = json_encode([
'autoLike' => true,
'momentsSync' => true,
'autoCustomerDev' => true,
'groupMessageDeliver' => true,
'autoGroup' => true,
]);
DeviceModel::create($data);
}
}
/**
* 保存设备数据
* @param array $item 设备数据
* @return bool
*/
public function saveDevice($item)
{
try {
$data = [
'id' => isset($item['id']) ? $item['id'] : '',
'userName' => isset($item['userName']) ? $item['userName'] : '',
'nickname' => isset($item['nickname']) ? $item['nickname'] : '',
'realName' => isset($item['realName']) ? $item['realName'] : '',
'groupName' => isset($item['groupName']) ? $item['groupName'] : '',
'wechatAccounts' => isset($item['wechatAccounts']) ? json_encode($item['wechatAccounts']) : json_encode([]),
'alive' => isset($item['alive']) ? $item['alive'] : false,
'lastAliveTime' => isset($item['lastAliveTime']) ? $item['lastAliveTime'] : null,
'tenantId' => isset($item['tenantId']) ? $item['tenantId'] : 0,
'groupId' => isset($item['groupId']) ? $item['groupId'] : 0,
'currentAccountId' => isset($item['currentAccountId']) ? $item['currentAccountId'] : 0,
'imei' => $item['imei'],
'memo' => isset($item['memo']) ? $item['memo'] : '',
'createTime' => isset($item['createTime']) ? strtotime($item['createTime']) : 0,
'isDeleted' => isset($item['isDeleted']) ? $item['isDeleted'] : false,
'deletedAndStop' => isset($item['deletedAndStop']) ? $item['deletedAndStop'] : false,
'deleteTime' => empty($item['isDeleted']) ? 0 : strtotime($item['deleteTime']),
'rooted' => isset($item['rooted']) ? $item['rooted'] : false,
'xPosed' => isset($item['xPosed']) ? $item['xPosed'] : false,
'brand' => isset($item['brand']) ? $item['brand'] : '',
'model' => isset($item['model']) ? $item['model'] : '',
'operatingSystem' => isset($item['operatingSystem']) ? $item['operatingSystem'] : '',
'softwareVersion' => isset($item['softwareVersion']) ? $item['softwareVersion'] : '',
'extra' => isset($item['extra']) ? json_encode($item['extra']) : json_encode([]),
'phone' => isset($item['phone']) ? $item['phone'] : '',
'lastUpdateTime' => isset($item['lastUpdateTime']) ? ($item['lastUpdateTime'] == '0001-01-01T00:00:00' ? 0 : strtotime($item['lastUpdateTime'])) : 0
];
// 使用ID作为唯一性判断
$device = DeviceModel::where('id', $item['id'])->find();
if ($device) {
$device->save($data);
} else {
$data['taskConfig'] = json_encode([
'autoLike' => true,
'momentsSync' => true,
'autoCustomerDev' => true,
'groupMessageDeliver' => true,
'autoGroup' => true,
]);
DeviceModel::create($data);
}
return true;
} catch (\Exception $e) {
Log::error('保存设备数据失败:' . $e->getMessage());
return false;
}
}
/**
* 保存设备分组数据
* @param array $item 设备分组数据
* @return bool
*/
public function saveDeviceGroup($item)
{
try {
$data = [
'id' => $item['id'],
'tenantId' => $item['tenantId'],
'groupName' => $item['groupName'],
'groupMemo' => $item['groupMemo'],
'count' => isset($item['count']) ? $item['count'] : 0,
'createTime' => $item['createTime'] == '0001-01-01T00:00:00' ? 0 : strtotime($item['createTime'])
];
// 使用ID作为唯一性判断
$group = DeviceGroupModel::where('id', $item['id'])->find();
if ($group) {
$group->save($data);
} else {
DeviceGroupModel::create($data);
}
return true;
} catch (\Exception $e) {
Log::error('保存设备分组数据失败:' . $e->getMessage());
return false;
}
}
}