代码同步
This commit is contained in:
@@ -50,6 +50,142 @@ class AccountController extends BaseController
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建部门
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function createDepartment()
|
||||
{
|
||||
// 获取授权token
|
||||
$authorization = trim($this->request->header('authorization', ''));
|
||||
if (empty($authorization)) {
|
||||
return errorJson('缺少授权信息');
|
||||
}
|
||||
|
||||
try {
|
||||
// 获取请求参数
|
||||
$name = $this->request->param('name', '');
|
||||
$memo = $this->request->param('memo', '');
|
||||
if (empty($name)) {
|
||||
return errorJson('请输入公司名称');
|
||||
}
|
||||
|
||||
|
||||
// 参数验证
|
||||
if (empty($name)) {
|
||||
return errorJson('部门名称不能为空');
|
||||
}
|
||||
|
||||
// 构建请求参数,设置固定的departmentIdArr和parentId
|
||||
$params = [
|
||||
'name' => $name,
|
||||
'memo' => $memo,
|
||||
'departmentIdArr' => [914],
|
||||
'parentId' => 914
|
||||
];
|
||||
|
||||
// 设置请求头
|
||||
$headerData = ['client:system'];
|
||||
$header = setHeader($headerData, $authorization, 'json');
|
||||
|
||||
// 发送请求创建部门
|
||||
$result = requestCurl($this->baseUrl . 'api/Department/createDepartment', $params, 'POST', $header,'json');
|
||||
|
||||
|
||||
// 尝试提取部门ID
|
||||
if (is_int($result)) {
|
||||
return successJson($result);
|
||||
}else{
|
||||
return errorJson($result);
|
||||
}
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return response('创建部门失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建新账号
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function createAccount()
|
||||
{
|
||||
// 获取授权token
|
||||
$authorization = trim($this->request->header('authorization', ''));
|
||||
if (empty($authorization)) {
|
||||
return errorJson('缺少授权信息');
|
||||
}
|
||||
|
||||
try {
|
||||
// 获取请求参数
|
||||
$userName = $this->request->param('userName', '');
|
||||
$password = $this->request->param('password', '');
|
||||
$realName = $this->request->param('realName', '');
|
||||
$nickname = $this->request->param('nickname', '');
|
||||
$memo = $this->request->param('memo', '');
|
||||
$departmentId = $this->request->param('departmentId', 0);
|
||||
|
||||
// 用户名验证
|
||||
if (empty($userName)) {
|
||||
return errorJson('用户名不能为空');
|
||||
}
|
||||
|
||||
// 自定义用户名验证:只能使用英文字母或数字
|
||||
if (!preg_match('/^[a-zA-Z][a-zA-Z0-9]{5,9}$/', $userName)) {
|
||||
return errorJson('用户名必须以字母开头,只能包含字母和数字,长度6-10位');
|
||||
}
|
||||
|
||||
// 密码验证
|
||||
if (empty($password)) {
|
||||
return errorJson('密码不能为空');
|
||||
}
|
||||
|
||||
// 使用validateString验证密码,添加自定义选项
|
||||
$passwordValidation = validateString($password, 'password');
|
||||
if (!$passwordValidation['status']) {
|
||||
return errorJson($passwordValidation['message']);
|
||||
}
|
||||
|
||||
// 真实姓名验证
|
||||
if (empty($realName)) {
|
||||
return errorJson('真实姓名不能为空');
|
||||
}
|
||||
|
||||
|
||||
// 部门ID验证
|
||||
if (empty($departmentId)) {
|
||||
return errorJson('部门ID不能为空');
|
||||
}
|
||||
|
||||
// 构建请求参数
|
||||
$params = [
|
||||
'userName' => $userName,
|
||||
'password' => $password,
|
||||
'realName' => $realName,
|
||||
'nickname' => $nickname,
|
||||
'memo' => $memo,
|
||||
'departmentId' => $departmentId,
|
||||
'departmentIdArr' => empty($departmentId) ? [914] : [914, $departmentId]
|
||||
];
|
||||
// 设置请求头
|
||||
$headerData = ['client:system'];
|
||||
$header = setHeader($headerData, $authorization, 'json');
|
||||
|
||||
// 发送请求创建账号
|
||||
$result = requestCurl($this->baseUrl . 'api/account/newAccount', $params, 'POST', $header, 'json');
|
||||
|
||||
|
||||
if (is_int($result)) {
|
||||
return successJson($result);
|
||||
}else{
|
||||
return errorJson($result);
|
||||
}
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return errorJson('创建账号失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存账号数据到数据库
|
||||
* @param array $item 账号数据
|
||||
|
||||
@@ -14,10 +14,10 @@ class DeviceController extends BaseController
|
||||
* 获取设备列表
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function getlist()
|
||||
public function getlist($pageIndex = '',$pageSize = '',$authorization = '')
|
||||
{
|
||||
// 获取授权token
|
||||
$authorization = trim($this->request->header('authorization', ''));
|
||||
$authorization = !empty($authorization) ? $authorization : trim($this->request->header('authorization', ''));
|
||||
if (empty($authorization)) {
|
||||
return errorJson('缺少授权信息');
|
||||
}
|
||||
@@ -42,8 +42,8 @@ class DeviceController extends BaseController
|
||||
'alive' => $this->request->param('alive', ''),
|
||||
'hasWechat' => $this->request->param('hasWechat', ''),
|
||||
'departmentId' => $this->request->param('departmentId', ''),
|
||||
'pageIndex' => $this->request->param('pageIndex', 0),
|
||||
'pageSize' => $this->request->param('pageSize', 20)
|
||||
'pageIndex' => !empty($pageIndex) ? $pageIndex : $this->request->param('pageIndex', 0),
|
||||
'pageSize' => !empty($pageSize) ? $pageSize : $this->request->param('pageSize', 20)
|
||||
];
|
||||
|
||||
// 设置请求头
|
||||
|
||||
@@ -50,6 +50,58 @@ class FriendTaskController extends BaseController
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加好友任务
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function addFriendTask()
|
||||
{
|
||||
// 获取授权token
|
||||
$authorization = trim($this->request->header('authorization', ''));
|
||||
if (empty($authorization)) {
|
||||
return errorJson('缺少授权信息');
|
||||
}
|
||||
|
||||
try {
|
||||
// 获取请求参数
|
||||
$phone = $this->request->param('phone', '');
|
||||
$message = $this->request->param('message', '');
|
||||
$remark = $this->request->param('remark', '');
|
||||
$labels = $this->request->param('labels', []);
|
||||
$wechatAccountId = $this->request->param('wechatAccountId', 0);
|
||||
|
||||
// 参数验证
|
||||
if (empty($phone)) {
|
||||
return errorJson('手机号不能为空');
|
||||
}
|
||||
|
||||
if (empty($wechatAccountId)) {
|
||||
return errorJson('微信号不能为空');
|
||||
}
|
||||
|
||||
// 构建请求参数
|
||||
$params = [
|
||||
'phone' => $phone,
|
||||
'message' => $message,
|
||||
'remark' => $remark,
|
||||
'labels' => is_array($labels) ? $labels : [$labels],
|
||||
'wechatAccountId' => (int)$wechatAccountId
|
||||
];
|
||||
|
||||
// 设置请求头
|
||||
$headerData = ['client:system'];
|
||||
$header = setHeader($headerData, $authorization, 'json');
|
||||
|
||||
// 发送请求添加好友任务
|
||||
$result = requestCurl($this->baseUrl . 'api/AddFriendByPhoneTask/add', $params, 'POST', $header, 'json');
|
||||
|
||||
// 处理响应
|
||||
return successJson([], '添加好友任务创建成功');
|
||||
} catch (\Exception $e) {
|
||||
return errorJson('添加好友任务失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存添加好友记录到数据库
|
||||
* @param array $item 添加好友记录数据
|
||||
|
||||
157
Server/application/api/controller/MomentsController.php
Normal file
157
Server/application/api/controller/MomentsController.php
Normal file
@@ -0,0 +1,157 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use think\facade\Request;
|
||||
|
||||
class MomentsController extends BaseController
|
||||
{
|
||||
/**
|
||||
* 发布朋友圈
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function addJob()
|
||||
{
|
||||
// 获取授权token
|
||||
$authorization = trim($this->request->header('authorization', ''));
|
||||
if (empty($authorization)) {
|
||||
return errorJson('缺少授权信息');
|
||||
}
|
||||
|
||||
try {
|
||||
// 获取请求参数
|
||||
$text = $this->request->param('text', ''); // 朋友圈文本内容
|
||||
$picUrlList = $this->request->param('picUrlList', []); // 图片URL列表
|
||||
$videoUrl = $this->request->param('videoUrl', ''); // 视频URL
|
||||
$immediately = $this->request->param('immediately', true); // 是否立即发布
|
||||
$timingTime = $this->request->param('timingTime', ''); // 定时发布时间
|
||||
$beginTime = $this->request->param('beginTime', ''); // 开始时间
|
||||
$endTime = $this->request->param('endTime', ''); // 结束时间
|
||||
$isUseLocation = $this->request->param('isUseLocation', false); // 是否使用位置信息
|
||||
$poiName = $this->request->param('poiName', ''); // 位置名称
|
||||
$poiAddress = $this->request->param('poiAddress', ''); // 位置地址
|
||||
$lat = $this->request->param('lat', 0); // 纬度
|
||||
$lng = $this->request->param('lng', 0); // 经度
|
||||
$momentContentType = $this->request->param('momentContentType', 1); // 朋友圈内容类型
|
||||
$publicMode = $this->request->param('publicMode', 0); // 发布模式
|
||||
$altList = $this->request->param('altList', ''); // 替代列表
|
||||
$link = $this->request->param('link', []); // 链接信息
|
||||
$jobPublishWechatMomentsItems = $this->request->param('jobPublishWechatMomentsItems', []); // 发布账号和评论信息
|
||||
|
||||
// 必填参数验证
|
||||
if (empty($jobPublishWechatMomentsItems) || !is_array($jobPublishWechatMomentsItems)) {
|
||||
return errorJson('至少需要选择一个发布账号');
|
||||
}
|
||||
|
||||
// 根据朋友圈类型验证必填字段
|
||||
if ($momentContentType == 1 && empty($text)) { // 纯文本
|
||||
return errorJson('朋友圈内容不能为空');
|
||||
} else if ($momentContentType == 2 && (empty($picUrlList) || empty($text))) { // 图片+文字
|
||||
return errorJson('朋友圈内容和图片不能为空');
|
||||
} else if ($momentContentType == 3 && (empty($videoUrl) || empty($text))) { // 视频+文字
|
||||
return errorJson('朋友圈内容和视频不能为空');
|
||||
} else if ($momentContentType == 4 && (empty($link) || empty($text))) { // 链接+文字
|
||||
return errorJson('朋友圈内容和链接不能为空');
|
||||
}
|
||||
|
||||
// 构建请求参数
|
||||
$params = [
|
||||
'text' => $text,
|
||||
'picUrlList' => $picUrlList,
|
||||
'videoUrl' => $videoUrl,
|
||||
'immediately' => $immediately,
|
||||
'timingTime' => $timingTime,
|
||||
'beginTime' => $beginTime,
|
||||
'endTime' => $endTime,
|
||||
'isUseLocation' => $isUseLocation,
|
||||
'poiName' => $poiName,
|
||||
'poiAddress' => $poiAddress,
|
||||
'lat' => $lat,
|
||||
'lng' => $lng,
|
||||
'momentContentType' => (int)$momentContentType,
|
||||
'publicMode' => (int)$publicMode,
|
||||
'altList' => $altList,
|
||||
'link' => $link,
|
||||
'jobPublishWechatMomentsItems' => $jobPublishWechatMomentsItems
|
||||
];
|
||||
|
||||
// 设置请求头
|
||||
$headerData = ['client:system'];
|
||||
$header = setHeader($headerData, $authorization, 'json');
|
||||
|
||||
// 发送请求发布朋友圈
|
||||
$result = requestCurl($this->baseUrl . 'api/JobPublishWechatMoments/addJob', $params, 'POST', $header, 'json');
|
||||
|
||||
// 处理响应
|
||||
if (is_numeric($result)) {
|
||||
return successJson(['jobId' => $result], '朋友圈任务创建成功');
|
||||
} else {
|
||||
// 尝试解析JSON
|
||||
$response = json_decode($result, true);
|
||||
if (json_last_error() === JSON_ERROR_NONE && isset($response['id'])) {
|
||||
return successJson(['jobId' => $response['id']], '朋友圈任务创建成功');
|
||||
}
|
||||
|
||||
// 如果返回的是错误信息
|
||||
return errorJson(is_string($result) ? $result : '创建朋友圈任务失败');
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return errorJson('发布朋友圈失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取朋友圈任务列表
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function getList()
|
||||
{
|
||||
// 获取授权token
|
||||
$authorization = trim($this->request->header('authorization', ''));
|
||||
if (empty($authorization)) {
|
||||
return errorJson('缺少授权信息');
|
||||
}
|
||||
|
||||
try {
|
||||
// 获取请求参数
|
||||
$keyword = $this->request->param('keyword', ''); // 关键词搜索
|
||||
$jobStatus = $this->request->param('jobStatus', ''); // 任务状态筛选
|
||||
$contentType = $this->request->param('contentType', ''); // 内容类型筛选
|
||||
$only = $this->request->param('only', 'false'); // 是否只查看自己的
|
||||
$pageIndex = $this->request->param('pageIndex', 0); // 当前页码
|
||||
$pageSize = $this->request->param('pageSize', 10); // 每页数量
|
||||
$from = $this->request->param('from', ''); // 开始日期
|
||||
$to = $this->request->param('to', ''); // 结束日期
|
||||
|
||||
// 构建请求参数
|
||||
$params = [
|
||||
'keyword' => $keyword,
|
||||
'jobStatus' => $jobStatus,
|
||||
'contentType' => $contentType,
|
||||
'only' => $only,
|
||||
'pageIndex' => (int)$pageIndex,
|
||||
'pageSize' => (int)$pageSize
|
||||
];
|
||||
|
||||
// 添加日期筛选条件(如果有)
|
||||
if (!empty($from)) {
|
||||
$params['from'] = $from;
|
||||
}
|
||||
if (!empty($to)) {
|
||||
$params['to'] = $to;
|
||||
}
|
||||
|
||||
// 设置请求头
|
||||
$headerData = ['client:system'];
|
||||
$header = setHeader($headerData, $authorization, 'json');
|
||||
|
||||
// 发送请求获取朋友圈任务列表
|
||||
$result = requestCurl($this->baseUrl . 'api/JobPublishWechatMoments/listPagination', $params, 'GET', $header, 'json');
|
||||
$response = handleApiResponse($result);
|
||||
|
||||
return successJson($response);
|
||||
} catch (\Exception $e) {
|
||||
return errorJson('获取朋友圈任务列表失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
410
Server/application/api/controller/WebSocketController.php
Normal file
410
Server/application/api/controller/WebSocketController.php
Normal file
@@ -0,0 +1,410 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
|
||||
use think\cache\driver\Redis;
|
||||
use think\Db;
|
||||
use think\Log;
|
||||
use WebSocket\Client;
|
||||
|
||||
class WebSocketController extends BaseController
|
||||
{
|
||||
protected $authorized;
|
||||
protected $accountId;
|
||||
protected $client;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->authorized = $this->request->header('authorized', '');
|
||||
$this->accountId = $this->request->param('accountId', '');
|
||||
if (empty($this->authorized) || empty($this->accountId)) {
|
||||
$data['authorized'] = $this->authorized;
|
||||
$data['accountId'] = $this->accountId;
|
||||
$this->error('缺失关键参数', $data);
|
||||
}
|
||||
|
||||
//证书
|
||||
$context = stream_context_create();
|
||||
stream_context_set_option($context, 'ssl', 'verify_peer', false);
|
||||
stream_context_set_option($context, 'ssl', 'verify_peer_name', false);
|
||||
//开启WS链接
|
||||
$result = [
|
||||
"accessToken" => $this->authorized,
|
||||
"accountId" => $this->accountId,
|
||||
"client" => "kefu-client",
|
||||
"cmdType" => "CmdSignIn",
|
||||
"seq" => 1,
|
||||
];
|
||||
$content = json_encode($result);
|
||||
$this->client = new Client("wss://kf.quwanzhi.com:9993",
|
||||
[
|
||||
'filter' => ['text', 'binary', 'ping', 'pong', 'close','receive', 'send'],
|
||||
'context' => $context,
|
||||
'headers' => [
|
||||
'Sec-WebSocket-Protocol' => 'soap',
|
||||
'origin' => 'localhost',
|
||||
],
|
||||
'timeout' => 86400,
|
||||
]
|
||||
);
|
||||
$this->client->send($content);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 个人消息发送
|
||||
*/
|
||||
public function sendPersonal()
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$data = $this->request->param();
|
||||
|
||||
if (empty($data)) {
|
||||
$this->error('参数缺失');
|
||||
}
|
||||
$dataArray = $data;
|
||||
if (!is_array($dataArray)) {
|
||||
$this->error('数据格式错误');
|
||||
}
|
||||
|
||||
//过滤消息
|
||||
if (empty($dataArray['content'])) {
|
||||
$this->error('内容缺失');
|
||||
}
|
||||
if (empty($dataArray['wechatAccountId'])) {
|
||||
$this->error('微信id不能为空');
|
||||
}
|
||||
if (empty($dataArray['wechatFriendId'])) {
|
||||
$this->error('接收人不能为空');
|
||||
}
|
||||
|
||||
if (empty($dataArray['msgType'])) {
|
||||
$this->error('类型缺失');
|
||||
}
|
||||
|
||||
//消息拼接 msgType(1:文本 3:图片 43:视频 47:动图表情包 49:小程序)
|
||||
$result = [
|
||||
"cmdType" => "CmdSendMessage",
|
||||
"content" => $dataArray['content'],
|
||||
"msgSubType" => 0,
|
||||
"msgType" => $dataArray['msgType'],
|
||||
"seq" => time(),
|
||||
"wechatAccountId" => $dataArray['wechatAccountId'],
|
||||
"wechatChatroomId" => 0,
|
||||
"wechatFriendId" => $dataArray['wechatFriendId'],
|
||||
];
|
||||
|
||||
$result = json_encode($result);
|
||||
$this->client->send($result);
|
||||
$message = $this->client->receive();
|
||||
//关闭WS链接
|
||||
$this->client->close();
|
||||
Log::write('WS个人消息发送');
|
||||
$this->success('消息成功发送', json_decode($message, 1), 200);
|
||||
} else {
|
||||
$this->error('非法请求');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发送群消息
|
||||
*/
|
||||
public function sendCommunity()
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$data = $this->request->post();
|
||||
if (empty($data)) {
|
||||
$this->error('参数缺失');
|
||||
}
|
||||
$dataArray = $data;
|
||||
if (!is_array($dataArray)) {
|
||||
$this->error('数据格式错误');
|
||||
}
|
||||
|
||||
//过滤消息
|
||||
if (empty($dataArray['content'])) {
|
||||
$this->error('内容缺失');
|
||||
}
|
||||
if (empty($dataArray['wechatAccountId'])) {
|
||||
$this->error('微信id不能为空');
|
||||
}
|
||||
|
||||
if (empty($dataArray['msgType'])) {
|
||||
$this->error('类型缺失');
|
||||
}
|
||||
if (empty($dataArray['wechatChatroomId'])) {
|
||||
$this->error('群id不能为空');
|
||||
}
|
||||
|
||||
$msg = '消息成功发送';
|
||||
$message = [];
|
||||
try {
|
||||
//消息拼接 msgType(1:文本 3:图片 43:视频 47:动图表情包 49:小程序)
|
||||
$result = [
|
||||
"cmdType" => "CmdSendMessage",
|
||||
"content" => htmlspecialchars_decode($dataArray['content']),
|
||||
"msgSubType" => 0,
|
||||
"msgType" => $dataArray['msgType'],
|
||||
"seq" => time(),
|
||||
"wechatAccountId" => $dataArray['wechatAccountId'],
|
||||
"wechatChatroomId" => $dataArray['wechatChatroomId'],
|
||||
"wechatFriendId" => 0,
|
||||
];
|
||||
|
||||
$result = json_encode($result);
|
||||
$this->client->send($result);
|
||||
$message = $this->client->receive();
|
||||
//关闭WS链接
|
||||
$this->client->close();
|
||||
Log::write('WS群消息发送');
|
||||
Log::write($message);
|
||||
$message = json_decode($message, 1);
|
||||
} catch (\Exception $e) {
|
||||
$msg = $e->getMessage();
|
||||
}
|
||||
|
||||
$this->success($msg, $message, 200);
|
||||
|
||||
} else {
|
||||
$this->error('非法请求');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发送群消息
|
||||
*/
|
||||
public function sendCommunitys($data = [])
|
||||
{
|
||||
|
||||
if (empty($data)) {
|
||||
$this->error('参数缺失');
|
||||
}
|
||||
$dataArray = $data;
|
||||
if (!is_array($dataArray)) {
|
||||
$this->error('数据格式错误');
|
||||
}
|
||||
|
||||
//过滤消息
|
||||
if (empty($dataArray['content'])) {
|
||||
$this->error('内容缺失');
|
||||
}
|
||||
if (empty($dataArray['wechatAccountId'])) {
|
||||
$this->error('微信id不能为空');
|
||||
}
|
||||
|
||||
if (empty($dataArray['msgType'])) {
|
||||
$this->error('类型缺失');
|
||||
}
|
||||
if (empty($dataArray['wechatChatroomId'])) {
|
||||
$this->error('群id不能为空');
|
||||
}
|
||||
|
||||
$msg = '消息成功发送';
|
||||
$message = [];
|
||||
try {
|
||||
//消息拼接 msgType(1:文本 3:图片 43:视频 47:动图表情包 49:小程序)
|
||||
$result = [
|
||||
"cmdType" => "CmdSendMessage",
|
||||
"content" => $dataArray['content'],
|
||||
"msgSubType" => 0,
|
||||
"msgType" => $dataArray['msgType'],
|
||||
"seq" => time(),
|
||||
"wechatAccountId" => $dataArray['wechatAccountId'],
|
||||
"wechatChatroomId" => $dataArray['wechatChatroomId'],
|
||||
"wechatFriendId" => 0,
|
||||
];
|
||||
|
||||
$result = json_encode($result);
|
||||
$this->client->send($result);
|
||||
$message = $this->client->receive();
|
||||
//关闭WS链接
|
||||
$this->client->close();
|
||||
Log::write('WS群消息发送');
|
||||
Log::write($message);
|
||||
$message = json_decode($message, 1);
|
||||
} catch (\Exception $e) {
|
||||
$msg = $e->getMessage();
|
||||
}
|
||||
|
||||
$this->success($msg, $message, 200);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取指定账号朋友圈信息
|
||||
*/
|
||||
public function getMoments()
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$data = $this->request->param();
|
||||
|
||||
if (empty($data)) {
|
||||
$this->error('参数缺失');
|
||||
}
|
||||
$dataArray = $data;
|
||||
if (!is_array($dataArray)) {
|
||||
$this->error('数据格式错误');
|
||||
}
|
||||
//获取数据条数
|
||||
$count = isset($dataArray['count']) ? $dataArray['count'] : 5;
|
||||
//过滤消息
|
||||
if (empty($dataArray['wechatAccountId'])) {
|
||||
$this->error('指定账号不能为空');
|
||||
}
|
||||
if (empty($dataArray['wechatFriendId'])) {
|
||||
$this->error('指定好友不能为空');
|
||||
}
|
||||
$msg = '获取朋友圈信息成功';
|
||||
$message = [];
|
||||
try {
|
||||
$params = [
|
||||
"cmdType" => "CmdFetchMoment",
|
||||
"count" => $count,
|
||||
"createTimeSec" => time(),
|
||||
"isTimeline" => false,
|
||||
"prevSnsId" => 0,
|
||||
"wechatAccountId" => $dataArray['wechatAccountId'],
|
||||
"wechatFriendId" => $dataArray['wechatFriendId'],
|
||||
"seq" => time(),
|
||||
];
|
||||
$params = json_encode($params);
|
||||
Log::write('WS获取朋友圈信息参数:' . json_encode($params, 256));
|
||||
$this->client->send($params);
|
||||
$message = $this->client->receive();
|
||||
Log::write('WS获取朋友圈信息成功,结果:' . $message);
|
||||
$message = json_decode($message, 1);
|
||||
|
||||
// 存储朋友圈数据到数据库
|
||||
if (isset($message['momentList']) && !empty($message['momentList'])) {
|
||||
$this->saveMomentsToDatabase($message['momentList'], $dataArray['wechatAccountId'], $dataArray['wechatFriendId']);
|
||||
}
|
||||
|
||||
//关闭WS链接
|
||||
$this->client->close();
|
||||
} catch (\Exception $e) {
|
||||
$msg = $e->getMessage();
|
||||
}
|
||||
$this->success($msg, $message, 200);
|
||||
} else {
|
||||
$this->error('非法请求');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存朋友圈数据到数据库
|
||||
* @param array $momentList 朋友圈数据列表
|
||||
* @param int $wechatAccountId 微信账号ID
|
||||
* @param string $wechatFriendId 微信好友ID
|
||||
* @return bool
|
||||
*/
|
||||
protected function saveMomentsToDatabase($momentList, $wechatAccountId, $wechatFriendId)
|
||||
{
|
||||
if (empty($momentList) || !is_array($momentList)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
foreach ($momentList as $moment) {
|
||||
// 提取momentEntity中的数据
|
||||
$momentEntity = $moment['momentEntity'] ?? [];
|
||||
|
||||
// 检查朋友圈数据是否已存在
|
||||
$exists = Db::name('wechat_moments')
|
||||
->where('snsId', $moment['snsId'])
|
||||
->where('wechatAccountId', $wechatAccountId)
|
||||
->find();
|
||||
|
||||
$dataToSave = [
|
||||
'commentList' => json_encode($moment['commentList'] ?? [], JSON_UNESCAPED_UNICODE),
|
||||
'createTime' => $moment['createTime'] ?? 0,
|
||||
'likeList' => json_encode($moment['likeList'] ?? [], JSON_UNESCAPED_UNICODE),
|
||||
'content' => $momentEntity['content'] ?? '',
|
||||
'lat' => $momentEntity['lat'] ?? 0,
|
||||
'lng' => $momentEntity['lng'] ?? 0,
|
||||
'location' => $momentEntity['location'] ?? '',
|
||||
'picSize' => $momentEntity['picSize'] ?? 0,
|
||||
'resUrls' => json_encode($momentEntity['resUrls'] ?? [], JSON_UNESCAPED_UNICODE),
|
||||
'userName' => $momentEntity['userName'] ?? '',
|
||||
'snsId' => $moment['snsId'] ?? '',
|
||||
'type' => $moment['type'] ?? 0,
|
||||
'update_time' => time()
|
||||
];
|
||||
|
||||
if ($exists) {
|
||||
// 如果已存在,则更新数据
|
||||
Db::name('wechat_moments')->where('id', $exists['id'])->update($dataToSave);
|
||||
} else {
|
||||
// 如果不存在,则插入新数据
|
||||
$dataToSave['wechatAccountId'] = $wechatAccountId;
|
||||
$dataToSave['wechatFriendId'] = $wechatFriendId;
|
||||
$dataToSave['create_time'] = time();
|
||||
Db::name('wechat_moments')->insert($dataToSave);
|
||||
}
|
||||
}
|
||||
|
||||
Log::write('朋友圈数据已存入数据库,共' . count($momentList) . '条');
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
Log::write('保存朋友圈数据失败:' . $e->getMessage(), 'error');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定账号朋友圈图片地址
|
||||
*/
|
||||
public function getMomentSourceRealUrl()
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$data = $this->request->param();
|
||||
|
||||
if (empty($data)) {
|
||||
$this->error('参数缺失');
|
||||
}
|
||||
$dataArray = $data;
|
||||
if (!is_array($dataArray)) {
|
||||
$this->error('数据格式错误');
|
||||
}
|
||||
//获取数据条数
|
||||
// $count = isset($dataArray['count']) ? $dataArray['count'] : 10;
|
||||
//过滤消息
|
||||
if (empty($dataArray['wechatAccountId'])) {
|
||||
$this->error('指定账号不能为空');
|
||||
}
|
||||
if (empty($dataArray['snsId'])) {
|
||||
$this->error('指定消息ID不能为空');
|
||||
}
|
||||
if (empty($dataArray['snsUrls'])) {
|
||||
$this->error('资源信息不能为空');
|
||||
}
|
||||
$msg = '获取朋友圈资源链接成功';
|
||||
$message = [];
|
||||
try {
|
||||
$params = [
|
||||
"cmdType" => $dataArray['type'],
|
||||
"snsId" => $dataArray['snsId'],
|
||||
"urls" => $dataArray['snsUrls'],
|
||||
"wechatAccountId" => $dataArray['wechatAccountId'],
|
||||
"seq" => time(),
|
||||
];
|
||||
$params = json_encode($params);
|
||||
$this->client->send($params);
|
||||
$message = $this->client->receive();
|
||||
Log::write('WS获取朋友圈图片/视频链接成功,结果:' . json_encode($message, 256));
|
||||
//关闭WS链接
|
||||
$this->client->close();
|
||||
} catch (\Exception $e) {
|
||||
$msg = $e->getMessage();
|
||||
}
|
||||
$this->success($msg, $message, 200);
|
||||
} else {
|
||||
$this->error('非法请求');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,4 +11,5 @@
|
||||
|
||||
return [
|
||||
'test' => 'app\common\command\TestCommand',
|
||||
'device:list' => 'app\common\command\DeviceListCommand',
|
||||
];
|
||||
|
||||
@@ -113,12 +113,12 @@ if (!function_exists('errorJson')) {
|
||||
|
||||
|
||||
if (!function_exists('successJson')) {
|
||||
function successJson($data = [] ,$error = '操作成功', $code = 200)
|
||||
function successJson($data = [] ,$msg = '操作成功', $code = 200)
|
||||
{
|
||||
return json([
|
||||
'data' => $data,
|
||||
'code' => $code,
|
||||
'msg' => $error,
|
||||
'msg' => $msg,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -137,6 +137,7 @@ if (!function_exists('validateString')) {
|
||||
$config = [
|
||||
'password' => [
|
||||
'min_length' => 6,
|
||||
'max_length' => 20,
|
||||
'pattern' => '/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z0-9]+$/',
|
||||
'error' => '密码必须包含英文和数字,不能包含特殊符号'
|
||||
],
|
||||
|
||||
57
Server/application/common/command/DeviceListCommand.php
Normal file
57
Server/application/common/command/DeviceListCommand.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\command;
|
||||
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\Output;
|
||||
use think\facade\Log;
|
||||
use think\Queue;
|
||||
use app\job\DeviceListJob;
|
||||
|
||||
class DeviceListCommand extends BaseCommand
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('device:list')
|
||||
->setDescription('获取设备列表,并根据分页自动处理下一页');
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
$output->writeln('开始处理设备列表任务...');
|
||||
|
||||
try {
|
||||
// 初始页码
|
||||
$pageIndex = 0;
|
||||
$pageSize = 100; // 每页获取100条记录
|
||||
|
||||
// 将第一页任务添加到队列
|
||||
$this->addToQueue($pageIndex, $pageSize);
|
||||
|
||||
$output->writeln('设备列表任务已添加到队列');
|
||||
} catch (\Exception $e) {
|
||||
Log::error('设备列表任务添加失败:' . $e->getMessage());
|
||||
$output->writeln('设备列表任务添加失败:' . $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加任务到队列
|
||||
* @param int $pageIndex 页码
|
||||
* @param int $pageSize 每页大小
|
||||
*/
|
||||
protected function addToQueue($pageIndex, $pageSize)
|
||||
{
|
||||
$data = [
|
||||
'pageIndex' => $pageIndex,
|
||||
'pageSize' => $pageSize
|
||||
];
|
||||
|
||||
// 添加到队列,设置任务名为 device_list
|
||||
Queue::push(DeviceListJob::class, $data, 'device_list');
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,9 @@ namespace app\common\service;
|
||||
use app\common\model\User;
|
||||
use app\common\util\JwtUtil;
|
||||
use think\facade\Log;
|
||||
use think\facade\Cache;
|
||||
use think\facade\Config;
|
||||
use think\facade\Env;
|
||||
|
||||
class AuthService
|
||||
{
|
||||
@@ -154,4 +157,71 @@ class AuthService
|
||||
'token_expired' => $expireTime
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取系统授权信息,使用缓存存储10分钟
|
||||
* @return string
|
||||
*/
|
||||
public static function getSystemAuthorization()
|
||||
{
|
||||
// 定义缓存键名
|
||||
$cacheKey = 'system_authorization_token';
|
||||
|
||||
// 尝试从缓存获取授权信息
|
||||
$authorization = Cache::get($cacheKey);
|
||||
|
||||
// 如果缓存中没有或已过期,则重新获取
|
||||
if (empty($authorization)) {
|
||||
try {
|
||||
// 从环境变量中获取API用户名和密码
|
||||
$username = Env::get('api.username', '');
|
||||
$password = Env::get('api.password', '');
|
||||
|
||||
if (empty($username) || empty($password)) {
|
||||
Log::error('缺少API用户名或密码配置');
|
||||
return '';
|
||||
}
|
||||
|
||||
// 构建登录参数
|
||||
$params = [
|
||||
'grant_type' => 'password',
|
||||
'username' => $username,
|
||||
'password' => $password
|
||||
];
|
||||
|
||||
// 获取API基础URL
|
||||
$baseUrl = Env::get('api.wechat_url', '');
|
||||
if (empty($baseUrl)) {
|
||||
Log::error('缺少API基础URL配置');
|
||||
return '';
|
||||
}
|
||||
|
||||
// 调用登录接口获取token
|
||||
// 设置请求头
|
||||
$headerData = ['client:system'];
|
||||
$header = setHeader($headerData, '', 'plain');
|
||||
$result = requestCurl($baseUrl . 'token', $params, 'POST',$header);
|
||||
$result_array = handleApiResponse($result);
|
||||
|
||||
if (isset($result_array['access_token']) && !empty($result_array['access_token'])) {
|
||||
$authorization = $result_array['access_token'];
|
||||
|
||||
// 存入缓存,有效期10分钟(600秒)
|
||||
Cache::set($cacheKey, $authorization, 600);
|
||||
Cache::set('system_refresh_token', $result_array['refresh_token'], 600);
|
||||
|
||||
Log::info('已重新获取系统授权信息并缓存');
|
||||
return $authorization;
|
||||
} else {
|
||||
Log::error('获取系统授权信息失败:' . ($response['message'] ?? '未知错误'));
|
||||
return '';
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
Log::error('获取系统授权信息异常:' . $e->getMessage());
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
return $authorization;
|
||||
}
|
||||
}
|
||||
123
Server/application/job/DeviceListJob.php
Normal file
123
Server/application/job/DeviceListJob.php
Normal file
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
namespace app\job;
|
||||
|
||||
use think\queue\Job;
|
||||
use think\facade\Log;
|
||||
use think\Queue;
|
||||
use think\facade\Config;
|
||||
use app\api\controller\DeviceController;
|
||||
use app\common\service\AuthService;
|
||||
|
||||
class DeviceListJob
|
||||
{
|
||||
/**
|
||||
* 队列任务处理
|
||||
* @param Job $job 队列任务
|
||||
* @param array $data 任务数据
|
||||
* @return void
|
||||
*/
|
||||
public function fire(Job $job, $data)
|
||||
{
|
||||
try {
|
||||
// 如果任务执行成功后删除任务
|
||||
if ($this->processDeviceList($data, $job->attempts())) {
|
||||
$job->delete();
|
||||
Log::info('设备列表任务执行成功,页码:' . $data['pageIndex']);
|
||||
} else {
|
||||
if ($job->attempts() > 3) {
|
||||
// 超过重试次数,删除任务
|
||||
Log::error('设备列表任务执行失败,已超过重试次数,页码:' . $data['pageIndex']);
|
||||
$job->delete();
|
||||
} else {
|
||||
// 任务失败,重新放回队列
|
||||
Log::warning('设备列表任务执行失败,重试次数:' . $job->attempts() . ',页码:' . $data['pageIndex']);
|
||||
$job->release(Config::get('queue.failed_delay', 10));
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
// 出现异常,记录日志
|
||||
Log::error('设备列表任务异常:' . $e->getMessage());
|
||||
if ($job->attempts() > 3) {
|
||||
$job->delete();
|
||||
} else {
|
||||
$job->release(Config::get('queue.failed_delay', 10));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理设备列表获取
|
||||
* @param array $data 任务数据
|
||||
* @param int $attempts 重试次数
|
||||
* @return bool
|
||||
*/
|
||||
protected function processDeviceList($data, $attempts)
|
||||
{
|
||||
// 获取参数
|
||||
$pageIndex = isset($data['pageIndex']) ? $data['pageIndex'] : 0;
|
||||
$pageSize = isset($data['pageSize']) ? $data['pageSize'] : 100;
|
||||
|
||||
Log::info('开始获取设备列表,页码:' . $pageIndex . ',页大小:' . $pageSize);
|
||||
|
||||
// 实例化控制器
|
||||
$deviceController = new DeviceController();
|
||||
|
||||
// 构建请求参数
|
||||
$params = [
|
||||
'pageIndex' => $pageIndex,
|
||||
'pageSize' => $pageSize
|
||||
];
|
||||
|
||||
// 设置请求信息
|
||||
$request = request();
|
||||
$request->withGet($params);
|
||||
|
||||
// 获取系统授权信息
|
||||
$authorization = AuthService::getSystemAuthorization();
|
||||
if (empty($authorization)) {
|
||||
Log::error('获取系统授权信息失败');
|
||||
return false;
|
||||
}
|
||||
|
||||
// 调用设备列表获取方法
|
||||
$result = $deviceController->getlist($pageIndex,$pageSize,$authorization);
|
||||
$response = json_decode($result,true);
|
||||
|
||||
|
||||
// 判断是否成功
|
||||
if ($response['code'] == 200) {
|
||||
$data = $response['data'];
|
||||
|
||||
// 判断是否有下一页
|
||||
if (!empty($data) && count($data['results']) > 0) {
|
||||
// 有下一页,将下一页任务添加到队列
|
||||
$nextPageIndex = $pageIndex + 1;
|
||||
$this->addNextPageToQueue($nextPageIndex, $pageSize);
|
||||
Log::info('添加下一页任务到队列,页码:' . $nextPageIndex);
|
||||
}
|
||||
|
||||
return true;
|
||||
} else {
|
||||
$errorMsg = isset($response['msg']) ? $response['msg'] : '未知错误';
|
||||
Log::error('获取设备列表失败:' . $errorMsg);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加下一页任务到队列
|
||||
* @param int $pageIndex 页码
|
||||
* @param int $pageSize 每页大小
|
||||
*/
|
||||
protected function addNextPageToQueue($pageIndex, $pageSize)
|
||||
{
|
||||
$data = [
|
||||
'pageIndex' => $pageIndex,
|
||||
'pageSize' => $pageSize
|
||||
];
|
||||
|
||||
// 添加到队列,设置任务名为 device_list
|
||||
Queue::push(self::class, $data, 'device_list');
|
||||
}
|
||||
}
|
||||
23
Server/application/sql/wechat_moments_tables.sql
Normal file
23
Server/application/sql/wechat_moments_tables.sql
Normal file
@@ -0,0 +1,23 @@
|
||||
-- 朋友圈表
|
||||
CREATE TABLE IF NOT EXISTS `wechat_moments` (
|
||||
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
|
||||
`wechatAccountId` int(11) NOT NULL COMMENT '微信账号ID',
|
||||
`wechatFriendId` varchar(64) NOT NULL COMMENT '微信好友ID',
|
||||
`snsId` varchar(64) NOT NULL COMMENT '朋友圈消息ID',
|
||||
`commentList` text COMMENT '评论列表JSON',
|
||||
`createTime` bigint(20) DEFAULT '0' COMMENT '创建时间戳',
|
||||
`likeList` text COMMENT '点赞列表JSON',
|
||||
`content` text COMMENT '朋友圈内容',
|
||||
`lat` decimal(10,6) DEFAULT '0.000000' COMMENT '纬度',
|
||||
`lng` decimal(10,6) DEFAULT '0.000000' COMMENT '经度',
|
||||
`location` varchar(255) DEFAULT '' COMMENT '位置信息',
|
||||
`picSize` int(11) DEFAULT '0' COMMENT '图片大小',
|
||||
`resUrls` text COMMENT '资源URL列表',
|
||||
`userName` varchar(64) DEFAULT '' COMMENT '用户名',
|
||||
`type` int(11) DEFAULT '0' COMMENT '朋友圈类型',
|
||||
`create_time` int(11) DEFAULT NULL COMMENT '数据创建时间',
|
||||
`update_time` int(11) DEFAULT NULL COMMENT '数据更新时间',
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `idx_sns_account` (`snsId`,`wechatAccountId`),
|
||||
KEY `idx_account_friend` (`wechatAccountId`,`wechatFriendId`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='微信朋友圈数据表';
|
||||
Reference in New Issue
Block a user