作者 Your Name

gx

1 <?php 1 <?php
2 /** 2 /**
3 * @remark : 3 * @remark :
4 - * @name :socket.php 4 + * @name :Socket.php
5 * @author :lyh 5 * @author :lyh
6 * @method :post 6 * @method :post
7 * @time :2023/8/24 10:43 7 * @time :2023/8/24 10:43
@@ -9,24 +9,27 @@ @@ -9,24 +9,27 @@
9 9
10 namespace App\Helper; 10 namespace App\Helper;
11 11
12 -class socket 12 +class Socket
13 { 13 {
14 public function socket($data){ 14 public function socket($data){
  15 + @file_put_contents(storage_path('logs/lyh_error.log'), var_export(1111, true) . PHP_EOL, FILE_APPEND);
15 // Socket 服务器的 IP 和端口 16 // Socket 服务器的 IP 和端口
16 - $socketServerIp = '43.154.15.250'; 17 + $socketServerIp = '127.0.0.1';
17 $socketServerPort = 9555; // 替换为实际端口 18 $socketServerPort = 9555; // 替换为实际端口
18 // 创建一个 TCP Socket 客户端 19 // 创建一个 TCP Socket 客户端
19 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); 20 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  21 + @file_put_contents(storage_path('logs/lyh_error.log'), var_export($socket, true) . PHP_EOL, FILE_APPEND);
20 if ($socket === false) { 22 if ($socket === false) {
21 return response()->json(['error' => 'Socket creation failed']); 23 return response()->json(['error' => 'Socket creation failed']);
22 } 24 }
23 // 连接到 Socket 服务器 25 // 连接到 Socket 服务器
24 $result = socket_connect($socket, $socketServerIp, $socketServerPort); 26 $result = socket_connect($socket, $socketServerIp, $socketServerPort);
  27 + @file_put_contents(storage_path('logs/lyh_error.log'), var_export($result, true) . PHP_EOL, FILE_APPEND);
25 if ($result === false) { 28 if ($result === false) {
26 return response()->json(['error' => 'Socket connection failed']); 29 return response()->json(['error' => 'Socket connection failed']);
27 } 30 }
28 // 发送数据到 Socket 服务器 31 // 发送数据到 Socket 服务器
29 - socket_write($socket, $data, strlen($data)); 32 + socket_write($socket, 'ceshixiaoxi', strlen($data));
30 // 从服务器接收数据 33 // 从服务器接收数据
31 $response = socket_read($socket, 1024); 34 $response = socket_read($socket, 1024);
32 // 关闭 Socket 连接 35 // 关闭 Socket 连接
@@ -14,6 +14,7 @@ use App\Events\WebSocketMessage; @@ -14,6 +14,7 @@ use App\Events\WebSocketMessage;
14 use App\Events\WebSocketMessageSent; 14 use App\Events\WebSocketMessageSent;
15 use App\Helper\Arr; 15 use App\Helper\Arr;
16 use App\Helper\Common; 16 use App\Helper\Common;
  17 +use App\Helper\Socket;
17 use App\Helper\Translate; 18 use App\Helper\Translate;
18 use App\Helper\Wechat; 19 use App\Helper\Wechat;
19 use App\Http\Logic\Bside\User\UserLoginLogic; 20 use App\Http\Logic\Bside\User\UserLoginLogic;
@@ -78,12 +79,14 @@ class LoginController extends BaseController @@ -78,12 +79,14 @@ class LoginController extends BaseController
78 */ 79 */
79 public function autologin(UserLoginLogic $logic, EncryptUtils $encrypt) 80 public function autologin(UserLoginLogic $logic, EncryptUtils $encrypt)
80 { 81 {
  82 + @file_put_contents(storage_path('logs/lyh_error.log'), var_export(11111, true) . PHP_EOL, FILE_APPEND);
81 $serviceSettingModel = new Service(); 83 $serviceSettingModel = new Service();
82 $info = $serviceSettingModel->read(['type'=>4]); 84 $info = $serviceSettingModel->read(['type'=>4]);
83 if($info === false){ 85 if($info === false){
84 $this->response('当前访问地址不存在',Code::USER_ERROR); 86 $this->response('当前访问地址不存在',Code::USER_ERROR);
85 } 87 }
86 $data = $encrypt->unlock_url($this->param['code'], $info['values']); 88 $data = $encrypt->unlock_url($this->param['code'], $info['values']);
  89 + @file_put_contents(storage_path('logs/lyh_error.log'), var_export($data, true) . PHP_EOL, FILE_APPEND);
87 $data = json_decode($data, true); 90 $data = json_decode($data, true);
88 if(!isset($data['project_id']) && !isset($data['user_id'])){ 91 if(!isset($data['project_id']) && !isset($data['user_id'])){
89 $this->response('无效Code',Code::USER_ERROR); 92 $this->response('无效Code',Code::USER_ERROR);
@@ -167,26 +170,15 @@ class LoginController extends BaseController @@ -167,26 +170,15 @@ class LoginController extends BaseController
167 public function eventMessage(){ 170 public function eventMessage(){
168 $message = file_get_contents("php://input"); 171 $message = file_get_contents("php://input");
169 $message = simplexml_load_string($message, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOERROR); 172 $message = simplexml_load_string($message, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOERROR);
170 -<<<<<<< HEAD  
171 @file_put_contents(storage_path('logs/hook/wx.log'), var_export($message, true) . PHP_EOL, FILE_APPEND); 173 @file_put_contents(storage_path('logs/hook/wx.log'), var_export($message, true) . PHP_EOL, FILE_APPEND);
172 -=======  
173 - $arr = [  
174 - 'ToUserName' => 'gh_27174ac5c9d8',  
175 - 'FromUserName' => 'oMbcI6gRzvfrU8ssGsEyvzXOO94w',  
176 - 'CreateTime' => '1693385898',  
177 - 'MsgType' => 'event',  
178 - 'Event' => 'subscribe',  
179 - 'EventKey' => 'qrscene_global-v6_v6',  
180 - 'Ticket' => 'gQF27zwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyWnppdHhWTlRjOEcxTlBpTE5BY2IAAgRjBO9kAwQQDgAA',  
181 - ];  
182 //查看当前账户是否绑定 174 //查看当前账户是否绑定
183 $userModel = new UserModel(); 175 $userModel = new UserModel();
184 $info = $userModel->read(['wechat'=>$message->FromUserName]); 176 $info = $userModel->read(['wechat'=>$message->FromUserName]);
185 if($info === false){ 177 if($info === false){
186 178
187 - } 179 + $socket = new Socket();
  180 + $socket->socket(json_encode($data));
188 @file_put_contents(storage_path('logs/lyh_error.log'), var_export($message, true) . PHP_EOL, FILE_APPEND); 181 @file_put_contents(storage_path('logs/lyh_error.log'), var_export($message, true) . PHP_EOL, FILE_APPEND);
189 ->>>>>>> cb4013abb5967a24f5c482c21f3a93a48789077d  
190 return "<xml> 182 return "<xml>
191 <ToUserName><![CDATA[$message->FromUserName]]></ToUserName> 183 <ToUserName><![CDATA[$message->FromUserName]]></ToUserName>
192 <FromUserName><![CDATA[$message->ToUserName]]></FromUserName> 184 <FromUserName><![CDATA[$message->ToUserName]]></FromUserName>
@@ -223,4 +215,52 @@ class LoginController extends BaseController @@ -223,4 +215,52 @@ class LoginController extends BaseController
223 $this->response('success',Code::SUCCESS,['info'=>$info]); 215 $this->response('success',Code::SUCCESS,['info'=>$info]);
224 } 216 }
225 217
  218 + /**
  219 + * @remark :微信登录处理数据
  220 + * @name :wechatLogin
  221 + * @author :lyh
  222 + * @method :post
  223 + * @time :2023/8/31 9:13
  224 + */
  225 + public function wechatLogin($wechat){
  226 + //查看当前账户是否绑定
  227 + $userModel = new UserModel();
  228 + $info = $userModel->read(['wechat'=>$wechat]);
  229 + if($info === false){
  230 + $data = [
  231 + 'code'=>Code::USER_LOGIN_ERROE,
  232 + 'message'=>'当前用户未绑定账户,请绑定后',
  233 + ];
  234 + }else {
  235 + //获取项目详情
  236 + $info = $this->assembleParam($info['mobile'],$info['project_id']);
  237 + if(isset($info['token']) && !empty($info['token'])){
  238 + //清除上一次用户缓存
  239 + Cache::pull($info['token']);
  240 + }
  241 + //生成新token
  242 + $token = md5(uniqid().$info['id']);
  243 + //存储缓存
  244 + $info['token'] = $token;
  245 + Cache::add($token,$info,3600);
  246 + //更新用户信息
  247 + $this->model->edit(['token'=>$token],['id'=>$info['id']]);
  248 + $data = [
  249 + 'code'=>Code::SUCCESS,
  250 + 'message'=>'当前用户未绑定账户,请绑定后',
  251 + 'data'=>$info
  252 + ];
  253 + }
  254 + return $data;
  255 + }
  256 +
  257 +
  258 + public function ceshi(){
  259 + $data = [
  260 + 'code'=>Code::USER_LOGIN_ERROE,
  261 + 'message'=>'当前用户未绑定账户,请绑定后',
  262 + ];
  263 + $socket = new Socket();
  264 + return $socket->socket(json_encode($data));
  265 + }
226 } 266 }
@@ -30,10 +30,8 @@ $server->on('open', function ($server, $request) { @@ -30,10 +30,8 @@ $server->on('open', function ($server, $request) {
30 $server->on('message', function ($server, $frame) { 30 $server->on('message', function ($server, $frame) {
31 $message = $frame->data; 31 $message = $frame->data;
32 echo "收到消息: $message\n"; 32 echo "收到消息: $message\n";
33 -  
34 // 获取客户端连接的文件描述符 33 // 获取客户端连接的文件描述符
35 $fd = $frame->fd; 34 $fd = $frame->fd;
36 -  
37 // 回复消息给客户端 35 // 回复消息给客户端
38 $response = "我已收到消息: $message"; 36 $response = "我已收到消息: $message";
39 $server->push($fd, $response); 37 $server->push($fd, $response);