Browse Source

Weenier 168otc项目部署 0630

wesmiler 3 years ago
parent
commit
0c362817a3

+ 36 - 6
app/Console/Commands/Socket.php

@@ -2,6 +2,7 @@
 
 namespace App\Console\Commands;
 
+use App\Services\Common\ChatMessageService;
 use Illuminate\Console\Command;
 
 class Socket extends Command
@@ -58,6 +59,8 @@ class Socket extends Command
         //创建websocket服务器对象,监听0.0.0.0:7104端口
         $this->ws = new \swoole_websocket_server("0.0.0.0", env('SOCKET_PORT','6420'));
 
+//        $this->ws->set();
+
         //监听WebSocket连接打开事件
         $this->ws->on('open',[$this,'open']);
 
@@ -79,8 +82,8 @@ class Socket extends Command
      * @param $request
      */
     public function open($ws, $request){
-
-        var_dump($request->fd . "连接成功");
+        echo "连接成功:".$request->fd."\n";
+        $this->ws->push($request->fd,'连接成功');
     }
 
     /**
@@ -89,7 +92,32 @@ class Socket extends Command
      * @param $frame
      */
     public function message($ws,$frame){
-        $this->ws->push($frame->fd, '收到消息');
+        if($frame->data == 'ping'){
+            $this->ws->push($frame->fd, 'pong');
+            return false;
+        }
+
+        // 消息处理
+        $data = $frame->data? json_decode($frame->data, true) : [];
+        $op = isset($data['op'])? $data['op'] : '';
+        if($data){
+            switch($op){
+                case 'bind': // 绑定用户
+                    if(!ChatMessageService::make()->bind($frame->fd, $data)){
+                        $this->ws->push($frame->fd, lang(ChatMessageService::make()->getError()));
+                        return false;
+                    }
+                    break;
+                default:  // 默认聊天
+                    if(!ChatMessageService::make()->saveData($frame->fd, $data)){
+
+                    }
+                    break;
+
+            }
+        }else{
+            $this->ws->push($frame->fd, 'not data');
+        }
     }
 
     /**
@@ -106,14 +134,16 @@ class Socket extends Command
      * @param $ws
      * @param $fd
      */
-    public function close($ws,$fd){
-        $this->ws->close();
+    public function close($ws,$fd=''){
+//        var_dump($ws);
+        echo '连接关闭:'.$fd."\n";
+        $this->ws->close($fd);
     }
 
     /**
      * 停止运行
      */
-    public function stop()
+    public function stop($ws)
     {
         $this->ws->close();
     }

+ 4 - 4
app/Models/MemberModel.php

@@ -68,13 +68,13 @@ class MemberModel extends BaseModel
             }
 
             // 钱包余额
-            $trcNum = UsdtWalletService::make()->getTrxBalance($info['trc_address']);
+            $trcNum = UsdtWalletService::make()->getTrxBalance($info['trc_address'], true);
             $info['trc_num'] = floatval($trcNum);
-            $trcUsdtNum = UsdtWalletService::make()->getTrc20Usdt($info['trc_address']);
+            $trcUsdtNum = UsdtWalletService::make()->getTrc20Usdt($info['trc_address'], true);
             $info['trc_usdt_num'] = floatval($trcUsdtNum);
-            $ercNum = UsdtWalletService::make()->getErcBalance($info['erc_hexaddress']);
+            $ercNum = UsdtWalletService::make()->getErcBalance($info['erc_hexaddress'], true);
             $info['erc_num'] = floatval($ercNum);
-            $ercUsdtNum = UsdtWalletService::make()->getErc20Usdt($info['erc_hexaddress']);
+            $ercUsdtNum = UsdtWalletService::make()->getErc20Usdt($info['erc_hexaddress'], true);
             $info['erc_usdt_num'] = floatval($ercUsdtNum);
         }
         return $info;

+ 4 - 4
app/Models/UserModel.php

@@ -71,13 +71,13 @@ class UserModel extends BaseModel
                 $info['erc_num'] = '0.00';
                 $info['erc_usdt_num'] = '0.00';
                 if($memberInfo){
-                    $trcNum = UsdtWalletService::make()->getTrxBalance($memberInfo['trc_address']);
+                    $trcNum = UsdtWalletService::make()->getTrxBalance($memberInfo['trc_address'], true);
                     $info['trc_num'] = floatval($trcNum);
-                    $trcUsdtNum = UsdtWalletService::make()->getTrc20Usdt($memberInfo['trc_address']);
+                    $trcUsdtNum = UsdtWalletService::make()->getTrc20Usdt($memberInfo['trc_address'], true);
                     $info['trc_usdt_num'] = floatval($trcUsdtNum);
-                    $ercNum = UsdtWalletService::make()->getErcBalance($memberInfo['erc_hexaddress']);
+                    $ercNum = UsdtWalletService::make()->getErcBalance($memberInfo['erc_hexaddress'], true);
                     $info['erc_num'] = floatval($ercNum);
-                    $ercUsdtNum = UsdtWalletService::make()->getErc20Usdt($memberInfo['erc_hexaddress']);
+                    $ercUsdtNum = UsdtWalletService::make()->getErc20Usdt($memberInfo['erc_hexaddress'], true);
                     $info['erc_usdt_num'] = floatval($ercUsdtNum);
                 }
             }

+ 1 - 1
app/Services/Api/MemberService.php

@@ -250,7 +250,7 @@ class MemberService extends BaseService
         if (isset($params['idcard_hand_img']) && strpos($params['idcard_hand_img'], 'http') === false) {
             $data['idcard_hand_img'] = $params['idcard_hand_img'];
         }
-        
+
         $info = $this->getInfo($userId);
         $idcardCheck = isset($info['idcard_check']) ? $info['idcard_check'] : 0;
         if ($idcardCheck == 1) {

+ 80 - 0
app/Services/Common/ChatMessageService.php

@@ -0,0 +1,80 @@
+<?php
+// +----------------------------------------------------------------------
+// | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
+// +----------------------------------------------------------------------
+// | 版权所有 2017~2021 LARAVEL研发中心
+// +----------------------------------------------------------------------
+// | 官方网站: http://www.laravel.cn
+// +----------------------------------------------------------------------
+// | Author: laravel开发员 <laravel.qq.com>
+// +----------------------------------------------------------------------
+
+namespace App\Services\Common;
+
+use App\Models\AdModel;
+use App\Models\ApiModel;
+use App\Models\ChatMessageModel;
+use App\Services\BaseService;
+use App\Services\RedisService;
+
+/**
+ * 聊天管理-服务类
+ * Class ChatMessageService
+ * @package App\Services\Common
+ */
+class ChatMessageService extends BaseService
+{
+    // 静态对象
+    protected static $instance = null;
+
+    /**
+     * 构造函数
+     * ChatMessageService constructor.
+     */
+    public function __construct()
+    {
+        $this->model = new ChatMessageModel();
+    }
+
+    /**
+     * 静态入口
+     * @return static|null
+     */
+    public static function make()
+    {
+        if (!self::$instance) {
+            self::$instance = (new static());
+        }
+        return self::$instance;
+    }
+
+    /**
+     * 绑定用户
+     * @param $fd
+     * @param $data
+     * @return bool
+     */
+    public function bind($fd, $data)
+    {
+        $userId = isset($data['from_uid'])? intval($data['from_uid']) : 0;
+        if($userId<=0){
+            $this->error = '1013';
+            return false;
+        }
+
+        RedisService::set("caches:chats:bind:{$userId}", ['fd'=> $fd, 'user_id'=> $userId], 86400);
+        return true;
+    }
+
+    /**
+     * 添加或编辑
+     * @return array
+     */
+    public function saveData($fd, $data)
+    {
+
+
+        return parent::edit($data); // TODO: Change the autogenerated stub
+    }
+
+}

+ 36 - 14
app/Services/UsdtWalletService.php

@@ -676,11 +676,11 @@ class UsdtWalletService extends BaseService
      * @param $address
      * @return false|float|string
      */
-    public function getTrxBalance($address)
+    public function getTrxBalance($address, $cache=false)
     {
-        $cacheKey = "caches:wallet:balance:{$address}";
-        if (RedisService::get($cacheKey)) {
-            return false;
+        $cacheKey = "caches:wallet:balance:trx_{$address}";
+        if ($data = RedisService::get($cacheKey) && $cache) {
+            return $data;
         }
 
         if (empty($address)) {
@@ -704,7 +704,9 @@ class UsdtWalletService extends BaseService
             $address = new \Tron\Address($address['base58'], '', $address['hex']);
             $result = $trxWallet->balance($address);
 
-            return $result ? floatval($result) : '0.00';
+            $result = $result ? floatval($result) : '0.00';
+            RedisService::set($cacheKey, $result, rand(3, 5));
+            return $result;
 
         } catch (\Exception $exception) {
             $this->error = $exception->getMessage();
@@ -717,7 +719,7 @@ class UsdtWalletService extends BaseService
      * @param $address
      * @return false|float|string
      */
-    public function getTrc20Usdt($address)
+    public function getTrc20Usdt($address, $cache=false)
     {
         if (empty($address)) {
             $this->error = '1018';
@@ -729,6 +731,11 @@ class UsdtWalletService extends BaseService
             return false;
         }
 
+        $cacheKey = "caches:wallet:balance:usdt_{$address}";
+        if ($data = RedisService::get($cacheKey) && $cache) {
+            return $data;
+        }
+
         try {
             $headers = ["TRON-PRO-API-KEY" => $this->config['tron_api_key']];
             $api = new Api(new Client(['base_uri' => $this->config['tron_api_url'], 'headers' => $headers]));
@@ -739,7 +746,10 @@ class UsdtWalletService extends BaseService
             $address = $tron->getAddress();
             $address = new \Tron\Address($address['base58'], '', $address['hex']);
             $result = $trxWallet->balance($address);
-            return $result ? floatval($result) : '0.00';
+
+            $result = $result ? floatval($result) : '0.00';
+            RedisService::set($cacheKey, $result, rand(3, 5));
+            return $result;
 
         } catch (\Exception $exception) {
             $this->error = $exception->getMessage();
@@ -812,7 +822,7 @@ class UsdtWalletService extends BaseService
      * @throws \Tron\Exceptions\TransactionException
      * @throws \Tron\Exceptions\TronErrorException
      */
-    public function ercTransfer($force = false)
+    public function ercTransfer($to, $amount, $from, $fromPrivate)
     {
         if ($amount <= 0) {
             $this->error = '2205';
@@ -966,7 +976,7 @@ class UsdtWalletService extends BaseService
      * @param $address
      * @return false|float|string
      */
-    public function getErcBalance($address)
+    public function getErcBalance($address, $cache=false)
     {
         if (empty($address)) {
             $this->error = '1018';
@@ -983,6 +993,11 @@ class UsdtWalletService extends BaseService
             return false;
         }
 
+        $cacheKey = "caches:wallet:balance:erc_{$address}";
+        if ($data = RedisService::get($cacheKey) && $cache) {
+            return $data;
+        }
+
         try {
             $url = $this->config['eth_api_url'] . '/' . $this->config['eth_api_key'];
             $headers = ["Content-Type" => 'application/json'];
@@ -995,8 +1010,9 @@ class UsdtWalletService extends BaseService
             $result = curl_api($url, json_encode($data), $headers);
             $result = $result ? json_decode($result, true) : [];
             $balance = isset($result['result']) ? hexdec($result['result']) : 0.00;
-            return $balance ? floatval($balance/pow(10,18)) : '0.00';
-
+            $balance = $balance ? floatval($balance/pow(10,18)) : '0.00';
+            RedisService::set($cacheKey, $balance, rand(3, 5));
+            return $balance;
         } catch (\Exception $exception) {
             $this->error = $exception->getMessage();
             return false;
@@ -1008,7 +1024,7 @@ class UsdtWalletService extends BaseService
      * @param $address
      * @return false|float|string
      */
-    public function getErc20Usdt($address)
+    public function getErc20Usdt($address, $cache=false)
     {
 
         if (empty($address)) {
@@ -1031,6 +1047,11 @@ class UsdtWalletService extends BaseService
             return false;
         }
 
+        $cacheKey = "caches:wallet:balance:erc_usdt_{$address}";
+        if ($data = RedisService::get($cacheKey) && $cache) {
+            return $data;
+        }
+
         try {
             $url = $this->config['eth_api_url'] . '/' . $this->config['eth_api_key'];
             $headers = ["Content-Type" => 'application/json'];
@@ -1047,8 +1068,9 @@ class UsdtWalletService extends BaseService
             $balance = isset($result['result']) ? hexdec($result['result']) : 0.00;
             $balance = $balance? floatval($balance/1000000) : '0.00';
 
-            return $balance? $balance : '0.00';
-
+            $balance = $balance? $balance : '0.00';
+            RedisService::set($cacheKey, $balance, rand(3, 5));
+            return $balance;
         } catch (\Exception $exception) {
             $this->error = $exception->getMessage();
             return false;