【触客宝】 朋友圈同步逻辑优化
This commit is contained in:
@@ -8,6 +8,7 @@ use think\cache\driver\Redis;
|
|||||||
use think\Db;
|
use think\Db;
|
||||||
use think\Log;
|
use think\Log;
|
||||||
use WebSocket\Client;
|
use WebSocket\Client;
|
||||||
|
use think\facade\Env;
|
||||||
|
|
||||||
class WebSocketController extends BaseController
|
class WebSocketController extends BaseController
|
||||||
{
|
{
|
||||||
@@ -15,15 +16,54 @@ class WebSocketController extends BaseController
|
|||||||
protected $accountId;
|
protected $accountId;
|
||||||
protected $client;
|
protected $client;
|
||||||
|
|
||||||
public function __construct()
|
/************************************
|
||||||
|
* 初始化相关功能
|
||||||
|
************************************/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造函数 - 初始化WebSocket连接
|
||||||
|
* @param array $userData 用户数据
|
||||||
|
*/
|
||||||
|
public function __construct($userData = [])
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
|
||||||
|
if(!empty($userData) && count($userData)){
|
||||||
|
|
||||||
|
if (empty($userData['userName']) || empty($userData['password'])) {
|
||||||
|
return json_encode(['code'=>400,'msg'=>'参数缺失']);
|
||||||
|
}
|
||||||
|
$params = [
|
||||||
|
'grant_type' => 'password',
|
||||||
|
'username' => $userData['userName'],
|
||||||
|
'password' => $userData['password']
|
||||||
|
];
|
||||||
|
|
||||||
|
// 调用登录接口获取token
|
||||||
|
// 设置请求头
|
||||||
|
$headerData = ['client:kefu-client'];
|
||||||
|
$header = setHeader($headerData, '', 'plain');
|
||||||
|
$result = requestCurl('https://kf.quwanzhi.com:9991/token', $params, 'POST',$header);
|
||||||
|
$result_array = handleApiResponse($result);
|
||||||
|
|
||||||
|
if (isset($result_array['access_token']) && !empty($result_array['access_token'])) {
|
||||||
|
$authorization = $result_array['access_token'];
|
||||||
|
$this->authorized = $authorization;
|
||||||
|
$this->accountId = $userData['accountId'];
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return json_encode(['code'=>400,'msg'=>'获取系统授权信息失败']);
|
||||||
|
}
|
||||||
|
}else{
|
||||||
$this->authorized = $this->request->header('authorization', '');
|
$this->authorized = $this->request->header('authorization', '');
|
||||||
$this->accountId = $this->request->param('accountId', '');
|
$this->accountId = $this->request->param('accountId', '');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if (empty($this->authorized) || empty($this->accountId)) {
|
if (empty($this->authorized) || empty($this->accountId)) {
|
||||||
$data['authorized'] = $this->authorized;
|
$data['authorized'] = $this->authorized;
|
||||||
$data['accountId'] = $this->accountId;
|
$data['accountId'] = $this->accountId;
|
||||||
$this->error('缺失关键参数', $data);
|
return json_encode(['code'=>400,'msg'=>'缺失关键参数']);
|
||||||
}
|
}
|
||||||
|
|
||||||
//证书
|
//证书
|
||||||
@@ -38,6 +78,8 @@ class WebSocketController extends BaseController
|
|||||||
"cmdType" => "CmdSignIn",
|
"cmdType" => "CmdSignIn",
|
||||||
"seq" => 1,
|
"seq" => 1,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
$content = json_encode($result);
|
$content = json_encode($result);
|
||||||
$this->client = new Client("wss://kf.quwanzhi.com:9993",
|
$this->client = new Client("wss://kf.quwanzhi.com:9993",
|
||||||
[
|
[
|
||||||
@@ -53,9 +95,66 @@ class WebSocketController extends BaseController
|
|||||||
$this->client->send($content);
|
$this->client->send($content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/************************************
|
||||||
|
* 朋友圈相关功能
|
||||||
|
************************************/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取指定账号朋友圈信息
|
||||||
|
* @param array $data 请求参数
|
||||||
|
* @return \think\response\Json
|
||||||
|
*/
|
||||||
|
public function getMoments($data = [])
|
||||||
|
{
|
||||||
|
|
||||||
|
$count = !empty($data['count']) ? $data['count'] : 10;
|
||||||
|
$wechatAccountId = !empty($data['wechatAccountId']) ? $data['wechatAccountId'] : '';
|
||||||
|
$wechatFriendId = !empty($data['id']) ? $data['id'] : '';
|
||||||
|
|
||||||
|
//过滤消息
|
||||||
|
if (empty($wechatAccountId)) {
|
||||||
|
return json_encode(['code'=>400,'msg'=>'指定账号不能为空']);
|
||||||
|
}
|
||||||
|
if (empty($wechatFriendId)) {
|
||||||
|
return json_encode(['code'=>400,'msg'=>'指定好友不能为空']);
|
||||||
|
}
|
||||||
|
$msg = '获取朋友圈信息成功';
|
||||||
|
$message = [];
|
||||||
|
try {
|
||||||
|
$params = [
|
||||||
|
"cmdType" => "CmdFetchMoment",
|
||||||
|
"count" => $count,
|
||||||
|
"createTimeSec" => time(),
|
||||||
|
"isTimeline" => false,
|
||||||
|
"prevSnsId" => 0,
|
||||||
|
"wechatAccountId" => $wechatAccountId,
|
||||||
|
"wechatFriendId" => $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['result']) && !empty($message['result'])) {
|
||||||
|
$this->saveMomentsToDatabase($message['result'], $wechatAccountId, $wechatFriendId);
|
||||||
|
}
|
||||||
|
|
||||||
|
//关闭WS链接
|
||||||
|
$this->client->close();
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$msg = $e->getMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
return json_encode(['code'=>200,'msg'=>$msg,'data'=>$message]);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 朋友圈点赞
|
* 朋友圈点赞
|
||||||
|
* @return \think\response\Json
|
||||||
*/
|
*/
|
||||||
public function momentInteract()
|
public function momentInteract()
|
||||||
{
|
{
|
||||||
@@ -63,19 +162,19 @@ class WebSocketController extends BaseController
|
|||||||
$data = $this->request->param();
|
$data = $this->request->param();
|
||||||
|
|
||||||
if (empty($data)) {
|
if (empty($data)) {
|
||||||
$this->error('参数缺失');
|
return json_encode(['code'=>400,'msg'=>'参数缺失']);
|
||||||
}
|
}
|
||||||
$dataArray = $data;
|
$dataArray = $data;
|
||||||
if (!is_array($dataArray)) {
|
if (!is_array($dataArray)) {
|
||||||
$this->error('数据格式错误');
|
return json_encode(['code'=>400,'msg'=>'数据格式错误']);
|
||||||
}
|
}
|
||||||
|
|
||||||
//过滤消息
|
//过滤消息
|
||||||
if (empty($dataArray['snsId'])) {
|
if (empty($dataArray['snsId'])) {
|
||||||
$this->error('snsId不能为空');
|
return json_encode(['code'=>400,'msg'=>'snsId不能为空']);
|
||||||
}
|
}
|
||||||
if (empty($dataArray['wechatAccountId'])) {
|
if (empty($dataArray['wechatAccountId'])) {
|
||||||
$this->error('微信id不能为空');
|
return json_encode(['code'=>400,'msg'=>'微信id不能为空']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -95,15 +194,15 @@ class WebSocketController extends BaseController
|
|||||||
//关闭WS链接
|
//关闭WS链接
|
||||||
$this->client->close();
|
$this->client->close();
|
||||||
//Log::write('WS个人消息发送');
|
//Log::write('WS个人消息发送');
|
||||||
successJson($message, '点赞成功');
|
return json_encode(['code'=>200,'msg'=>'点赞成功','data'=>$message]);
|
||||||
} else {
|
} else {
|
||||||
errorJson('非法请求');
|
return json_encode(['code'=>400,'msg'=>'非法请求']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 朋友圈取消点赞
|
* 朋友圈取消点赞
|
||||||
|
* @return \think\response\Json
|
||||||
*/
|
*/
|
||||||
public function momentCancelInteract()
|
public function momentCancelInteract()
|
||||||
{
|
{
|
||||||
@@ -111,19 +210,19 @@ class WebSocketController extends BaseController
|
|||||||
$data = $this->request->param();
|
$data = $this->request->param();
|
||||||
|
|
||||||
if (empty($data)) {
|
if (empty($data)) {
|
||||||
$this->error('参数缺失');
|
return json_encode(['code'=>400,'msg'=>'参数缺失']);
|
||||||
}
|
}
|
||||||
$dataArray = $data;
|
$dataArray = $data;
|
||||||
if (!is_array($dataArray)) {
|
if (!is_array($dataArray)) {
|
||||||
$this->error('数据格式错误');
|
return json_encode(['code'=>400,'msg'=>'数据格式错误']);
|
||||||
}
|
}
|
||||||
|
|
||||||
//过滤消息
|
//过滤消息
|
||||||
if (empty($dataArray['snsId'])) {
|
if (empty($dataArray['snsId'])) {
|
||||||
$this->error('snsId不能为空');
|
return json_encode(['code'=>400,'msg'=>'snsId不能为空']);
|
||||||
}
|
}
|
||||||
if (empty($dataArray['wechatAccountId'])) {
|
if (empty($dataArray['wechatAccountId'])) {
|
||||||
$this->error('微信id不能为空');
|
return json_encode(['code'=>400,'msg'=>'微信id不能为空']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -145,20 +244,136 @@ class WebSocketController extends BaseController
|
|||||||
//关闭WS链接
|
//关闭WS链接
|
||||||
$this->client->close();
|
$this->client->close();
|
||||||
//Log::write('WS个人消息发送');
|
//Log::write('WS个人消息发送');
|
||||||
successJson($message, '取消点赞成功');
|
return json_encode(['code'=>200,'msg'=>'取消点赞成功','data'=>$message]);
|
||||||
} else {
|
} else {
|
||||||
errorJson('非法请求');
|
return json_encode(['code'=>400,'msg'=>'非法请求']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取指定账号朋友圈图片地址
|
||||||
|
* @return \think\response\Json
|
||||||
|
*/
|
||||||
|
public function getMomentSourceRealUrl()
|
||||||
|
{
|
||||||
|
if ($this->request->isPost()) {
|
||||||
|
$data = $this->request->param();
|
||||||
|
|
||||||
|
if (empty($data)) {
|
||||||
|
return json_encode(['code'=>400,'msg'=>'参数缺失']);
|
||||||
|
}
|
||||||
|
$dataArray = $data;
|
||||||
|
if (!is_array($dataArray)) {
|
||||||
|
return json_encode(['code'=>400,'msg'=>'数据格式错误']);
|
||||||
|
}
|
||||||
|
//获取数据条数
|
||||||
|
// $count = isset($dataArray['count']) ? $dataArray['count'] : 10;
|
||||||
|
//过滤消息
|
||||||
|
if (empty($dataArray['wechatAccountId'])) {
|
||||||
|
return json_encode(['code'=>400,'msg'=>'指定账号不能为空']);
|
||||||
|
}
|
||||||
|
if (empty($dataArray['snsId'])) {
|
||||||
|
return json_encode(['code'=>400,'msg'=>'指定消息ID不能为空']);
|
||||||
|
}
|
||||||
|
if (empty($dataArray['snsUrls'])) {
|
||||||
|
return json_encode(['code'=>400,'msg'=>'资源信息不能为空']);
|
||||||
|
}
|
||||||
|
$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();
|
||||||
|
}
|
||||||
|
|
||||||
|
return json_encode(['code'=>200,'msg'=>$msg,'data'=>$message]);
|
||||||
|
} else {
|
||||||
|
return json_encode(['code'=>400,'msg'=>'非法请求']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存朋友圈数据到数据库
|
||||||
|
* @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'] ?? [];
|
||||||
|
|
||||||
|
// 检查朋友圈数据是否已存在
|
||||||
|
$momentId = Db::table('s2_wechat_moments')
|
||||||
|
->where('snsId', $moment['snsId'])
|
||||||
|
->where('wechatAccountId', $wechatAccountId)
|
||||||
|
->value('id');
|
||||||
|
|
||||||
|
$dataToSave = [
|
||||||
|
'commentList' => json_encode($moment['commentList'] ?? [], 256),
|
||||||
|
'createTime' => $moment['createTime'] ?? 0,
|
||||||
|
'likeList' => json_encode($moment['likeList'] ?? [], 256),
|
||||||
|
'content' => $momentEntity['content'] ?? '',
|
||||||
|
'lat' => $momentEntity['lat'] ?? 0,
|
||||||
|
'lng' => $momentEntity['lng'] ?? 0,
|
||||||
|
'location' => $momentEntity['location'] ?? '',
|
||||||
|
'picSize' => $momentEntity['picSize'] ?? 0,
|
||||||
|
'resUrls' => json_encode($momentEntity['resUrls'] ?? [], 256),
|
||||||
|
'userName' => $momentEntity['userName'] ?? '',
|
||||||
|
'snsId' => $moment['snsId'] ?? '',
|
||||||
|
'type' => $moment['type'] ?? 0,
|
||||||
|
'update_time' => time()
|
||||||
|
];
|
||||||
|
|
||||||
|
if ($momentId) {
|
||||||
|
// 如果已存在,则更新数据
|
||||||
|
Db::table('s2_wechat_moments')->where('id', $momentId)->update($dataToSave);
|
||||||
|
} else {
|
||||||
|
if(empty($wechatFriendId)){
|
||||||
|
$wechatFriendId = WechatFriend::where('wechatAccountId', $wechatAccountId)->where('wechatId', $momentEntity['userName'])->value('id');
|
||||||
|
}
|
||||||
|
// 如果不存在,则插入新数据
|
||||||
|
$dataToSave['wechatAccountId'] = $wechatAccountId;
|
||||||
|
$dataToSave['wechatFriendId'] = $wechatFriendId;
|
||||||
|
$dataToSave['create_time'] = time();
|
||||||
|
Db::table('s2_wechat_moments')->insert($dataToSave);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Log::write('朋友圈数据已存入数据库,共' . count($momentList) . '条');
|
||||||
|
return true;
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
//Log::write('保存朋友圈数据失败:' . $e->getMessage(), 'error');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/************************************
|
||||||
|
* 消息发送相关功能
|
||||||
|
************************************/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 个人消息发送
|
* 个人消息发送
|
||||||
|
* @return \think\response\Json
|
||||||
*/
|
*/
|
||||||
public function sendPersonal()
|
public function sendPersonal()
|
||||||
{
|
{
|
||||||
@@ -166,26 +381,26 @@ class WebSocketController extends BaseController
|
|||||||
$data = $this->request->param();
|
$data = $this->request->param();
|
||||||
|
|
||||||
if (empty($data)) {
|
if (empty($data)) {
|
||||||
$this->error('参数缺失');
|
return json_encode(['code'=>400,'msg'=>'参数缺失']);
|
||||||
}
|
}
|
||||||
$dataArray = $data;
|
$dataArray = $data;
|
||||||
if (!is_array($dataArray)) {
|
if (!is_array($dataArray)) {
|
||||||
$this->error('数据格式错误');
|
return json_encode(['code'=>400,'msg'=>'数据格式错误']);
|
||||||
}
|
}
|
||||||
|
|
||||||
//过滤消息
|
//过滤消息
|
||||||
if (empty($dataArray['content'])) {
|
if (empty($dataArray['content'])) {
|
||||||
$this->error('内容缺失');
|
return json_encode(['code'=>400,'msg'=>'内容缺失']);
|
||||||
}
|
}
|
||||||
if (empty($dataArray['wechatAccountId'])) {
|
if (empty($dataArray['wechatAccountId'])) {
|
||||||
$this->error('微信id不能为空');
|
return json_encode(['code'=>400,'msg'=>'微信id不能为空']);
|
||||||
}
|
}
|
||||||
if (empty($dataArray['wechatFriendId'])) {
|
if (empty($dataArray['wechatFriendId'])) {
|
||||||
$this->error('接收人不能为空');
|
return json_encode(['code'=>400,'msg'=>'接收人不能为空']);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty($dataArray['msgType'])) {
|
if (empty($dataArray['msgType'])) {
|
||||||
$this->error('类型缺失');
|
return json_encode(['code'=>400,'msg'=>'类型缺失']);
|
||||||
}
|
}
|
||||||
|
|
||||||
//消息拼接 msgType(1:文本 3:图片 43:视频 47:动图表情包 49:小程序)
|
//消息拼接 msgType(1:文本 3:图片 43:视频 47:动图表情包 49:小程序)
|
||||||
@@ -207,41 +422,43 @@ class WebSocketController extends BaseController
|
|||||||
//关闭WS链接
|
//关闭WS链接
|
||||||
$this->client->close();
|
$this->client->close();
|
||||||
//Log::write('WS个人消息发送');
|
//Log::write('WS个人消息发送');
|
||||||
successJson($message, '消息成功发送');
|
return json_encode(['code'=>200,'msg'=>'消息成功发送','data'=>$message]);
|
||||||
|
//return successJson($message, '消息成功发送');
|
||||||
} else {
|
} else {
|
||||||
errorJson('非法请求');
|
return json_encode(['code'=>400,'msg'=>'非法请求']);
|
||||||
|
//return errorJson('非法请求');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送群消息
|
* 发送群消息
|
||||||
|
* @return \think\response\Json
|
||||||
*/
|
*/
|
||||||
public function sendCommunity()
|
public function sendCommunity()
|
||||||
{
|
{
|
||||||
if ($this->request->isPost()) {
|
if ($this->request->isPost()) {
|
||||||
$data = $this->request->post();
|
$data = $this->request->post();
|
||||||
if (empty($data)) {
|
if (empty($data)) {
|
||||||
$this->error('参数缺失');
|
return json_encode(['code'=>400,'msg'=>'参数缺失']);
|
||||||
}
|
}
|
||||||
$dataArray = $data;
|
$dataArray = $data;
|
||||||
if (!is_array($dataArray)) {
|
if (!is_array($dataArray)) {
|
||||||
$this->error('数据格式错误');
|
return json_encode(['code'=>400,'msg'=>'数据格式错误']);
|
||||||
}
|
}
|
||||||
|
|
||||||
//过滤消息
|
//过滤消息
|
||||||
if (empty($dataArray['content'])) {
|
if (empty($dataArray['content'])) {
|
||||||
$this->error('内容缺失');
|
return json_encode(['code'=>400,'msg'=>'内容缺失']);
|
||||||
}
|
}
|
||||||
if (empty($dataArray['wechatAccountId'])) {
|
if (empty($dataArray['wechatAccountId'])) {
|
||||||
$this->error('微信id不能为空');
|
return json_encode(['code'=>400,'msg'=>'微信id不能为空']);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty($dataArray['msgType'])) {
|
if (empty($dataArray['msgType'])) {
|
||||||
$this->error('类型缺失');
|
return json_encode(['code'=>400,'msg'=>'类型缺失']);
|
||||||
}
|
}
|
||||||
if (empty($dataArray['wechatChatroomId'])) {
|
if (empty($dataArray['wechatChatroomId'])) {
|
||||||
$this->error('群id不能为空');
|
return json_encode(['code'=>400,'msg'=>'群id不能为空']);
|
||||||
}
|
}
|
||||||
|
|
||||||
$msg = '消息成功发送';
|
$msg = '消息成功发送';
|
||||||
@@ -270,41 +487,42 @@ class WebSocketController extends BaseController
|
|||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$msg = $e->getMessage();
|
$msg = $e->getMessage();
|
||||||
}
|
}
|
||||||
successJson($message,$msg);
|
return json_encode(['code'=>200,'msg'=>$msg,'data'=>$message]);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
errorJson('非法请求');
|
return json_encode(['code'=>400,'msg'=>'非法请求']);
|
||||||
|
//return errorJson('非法请求');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送群消息
|
* 发送群消息(内部调用版)
|
||||||
|
* @param array $data 消息数据
|
||||||
|
* @return \think\response\Json
|
||||||
*/
|
*/
|
||||||
public function sendCommunitys($data = [])
|
public function sendCommunitys($data = [])
|
||||||
{
|
{
|
||||||
|
|
||||||
if (empty($data)) {
|
if (empty($data)) {
|
||||||
$this->error('参数缺失');
|
return json_encode(['code'=>400,'msg'=>'参数缺失']);
|
||||||
}
|
}
|
||||||
$dataArray = $data;
|
$dataArray = $data;
|
||||||
if (!is_array($dataArray)) {
|
if (!is_array($dataArray)) {
|
||||||
$this->error('数据格式错误');
|
return json_encode(['code'=>400,'msg'=>'数据格式错误']);
|
||||||
}
|
}
|
||||||
|
|
||||||
//过滤消息
|
//过滤消息
|
||||||
if (empty($dataArray['content'])) {
|
if (empty($dataArray['content'])) {
|
||||||
$this->error('内容缺失');
|
return json_encode(['code'=>400,'msg'=>'内容缺失']);
|
||||||
}
|
}
|
||||||
if (empty($dataArray['wechatAccountId'])) {
|
if (empty($dataArray['wechatAccountId'])) {
|
||||||
$this->error('微信id不能为空');
|
return json_encode(['code'=>400,'msg'=>'微信id不能为空']);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty($dataArray['msgType'])) {
|
if (empty($dataArray['msgType'])) {
|
||||||
$this->error('类型缺失');
|
return json_encode(['code'=>400,'msg'=>'类型缺失']);
|
||||||
}
|
}
|
||||||
if (empty($dataArray['wechatChatroomId'])) {
|
if (empty($dataArray['wechatChatroomId'])) {
|
||||||
$this->error('群id不能为空');
|
return json_encode(['code'=>400,'msg'=>'群id不能为空']);
|
||||||
}
|
}
|
||||||
|
|
||||||
$msg = '消息成功发送';
|
$msg = '消息成功发送';
|
||||||
@@ -334,181 +552,6 @@ class WebSocketController extends BaseController
|
|||||||
$msg = $e->getMessage();
|
$msg = $e->getMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
successJson($message,$msg);
|
return json_encode(['code'=>200,'msg'=>$msg,'data'=>$message]);
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取指定账号朋友圈信息
|
|
||||||
*/
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
successJson($message,$msg);
|
|
||||||
} else {
|
|
||||||
errorJson('非法请求');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 保存朋友圈数据到数据库
|
|
||||||
* @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::table('s2_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::table('s2_wechat_moments')->where('id', $exists['id'])->update($dataToSave);
|
|
||||||
} else {
|
|
||||||
// 如果不存在,则插入新数据
|
|
||||||
$dataToSave['wechatAccountId'] = $wechatAccountId;
|
|
||||||
$dataToSave['wechatFriendId'] = $wechatFriendId;
|
|
||||||
$dataToSave['create_time'] = time();
|
|
||||||
Db::table('s2_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();
|
|
||||||
}
|
|
||||||
|
|
||||||
successJson($message,$msg);
|
|
||||||
} else {
|
|
||||||
errorJson('非法请求');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user