作者 lyh

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,11 +9,11 @@ @@ -9,11 +9,11 @@
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 // Socket 服务器的 IP 和端口 15 // Socket 服务器的 IP 和端口
16 - $socketServerIp = '43.154.15.250'; 16 + $socketServerIp = '127.0.0.1';
17 $socketServerPort = 9555; // 替换为实际端口 17 $socketServerPort = 9555; // 替换为实际端口
18 // 创建一个 TCP Socket 客户端 18 // 创建一个 TCP Socket 客户端
19 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); 19 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
@@ -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;
@@ -167,21 +168,9 @@ class LoginController extends BaseController @@ -167,21 +168,9 @@ class LoginController extends BaseController
167 public function eventMessage(){ 168 public function eventMessage(){
168 $message = file_get_contents("php://input"); 169 $message = file_get_contents("php://input");
169 $message = simplexml_load_string($message, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOERROR); 170 $message = simplexml_load_string($message, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOERROR);
170 - $arr = [  
171 - 'ToUserName' => 'gh_27174ac5c9d8',  
172 - 'FromUserName' => 'oMbcI6gRzvfrU8ssGsEyvzXOO94w',  
173 - 'CreateTime' => '1693385898',  
174 - 'MsgType' => 'event',  
175 - 'Event' => 'subscribe',  
176 - 'EventKey' => 'qrscene_global-v6_v6',  
177 - 'Ticket' => 'gQF27zwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyWnppdHhWTlRjOEcxTlBpTE5BY2IAAgRjBO9kAwQQDgAA',  
178 - ];  
179 - //查看当前账户是否绑定  
180 - $userModel = new UserModel();  
181 - $info = $userModel->read(['wechat'=>$message->FromUserName]);  
182 - if($info === false){  
183 -  
184 - } 171 + $data = $this->wechatLogin($message->FromUserName);
  172 + $socket = new Socket();
  173 + $socket->socket(json_encode($data));
185 @file_put_contents(storage_path('logs/lyh_error.log'), var_export($message, true) . PHP_EOL, FILE_APPEND); 174 @file_put_contents(storage_path('logs/lyh_error.log'), var_export($message, true) . PHP_EOL, FILE_APPEND);
186 return "<xml> 175 return "<xml>
187 <ToUserName><![CDATA[$message->FromUserName]]></ToUserName> 176 <ToUserName><![CDATA[$message->FromUserName]]></ToUserName>
@@ -219,4 +208,52 @@ class LoginController extends BaseController @@ -219,4 +208,52 @@ class LoginController extends BaseController
219 $this->response('success',Code::SUCCESS,['info'=>$info]); 208 $this->response('success',Code::SUCCESS,['info'=>$info]);
220 } 209 }
221 210
  211 + /**
  212 + * @remark :微信登录处理数据
  213 + * @name :wechatLogin
  214 + * @author :lyh
  215 + * @method :post
  216 + * @time :2023/8/31 9:13
  217 + */
  218 + public function wechatLogin($wechat){
  219 + //查看当前账户是否绑定
  220 + $userModel = new UserModel();
  221 + $info = $userModel->read(['wechat'=>$wechat]);
  222 + if($info === false){
  223 + $data = [
  224 + 'code'=>Code::USER_LOGIN_ERROE,
  225 + 'message'=>'当前用户未绑定账户,请绑定后',
  226 + ];
  227 + }else {
  228 + //获取项目详情
  229 + $info = $this->assembleParam($info['mobile'],$info['project_id']);
  230 + if(isset($info['token']) && !empty($info['token'])){
  231 + //清除上一次用户缓存
  232 + Cache::pull($info['token']);
  233 + }
  234 + //生成新token
  235 + $token = md5(uniqid().$info['id']);
  236 + //存储缓存
  237 + $info['token'] = $token;
  238 + Cache::add($token,$info,3600);
  239 + //更新用户信息
  240 + $this->model->edit(['token'=>$token],['id'=>$info['id']]);
  241 + $data = [
  242 + 'code'=>Code::SUCCESS,
  243 + 'message'=>'当前用户未绑定账户,请绑定后',
  244 + 'data'=>$info
  245 + ];
  246 + }
  247 + return $data;
  248 + }
  249 +
  250 +
  251 + public function ceshi(){
  252 + $data = [
  253 + 'code'=>Code::USER_LOGIN_ERROE,
  254 + 'message'=>'当前用户未绑定账户,请绑定后',
  255 + ];
  256 + $socket = new Socket();
  257 + return $socket->socket(json_encode($data));
  258 + }
222 } 259 }
@@ -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);