Ver Fonte

Weenier 168otc项目部署 0704

wesmiler há 3 anos atrás
pai
commit
d3be4ce99e

+ 120 - 0
app/Console/Commands/Socket.php

@@ -0,0 +1,120 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+
+class Socket extends Command
+{
+    public $ws;
+
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'swoole:socket {op?}';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'websocket ';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        $op = $this->argument('op');
+        $op = $op? $op : 'start';
+        if($op == 'start'){
+            echo 'socket start ...';
+            $this->start();
+        }else if ($op == 'stop'){
+            echo 'socket stop ...';
+            $this->stop();
+        }
+    }
+
+    /**
+     * 运行
+     */
+    public function start()
+    {
+        //创建websocket服务器对象,监听0.0.0.0:7104端口
+        $this->ws = new \swoole_websocket_server("0.0.0.0", env('SOCKET_PORT','6420'));
+
+        //监听WebSocket连接打开事件
+        $this->ws->on('open',[$this,'open']);
+
+        //监听WebSocket消息事件
+        $this->ws->on('message',[$this,'message']);
+
+        //监听WebSocket主动推送消息事件
+        $this->ws->on('request',[$this,'request']);
+
+        //监听WebSocket连接关闭事件
+        $this->ws->on('close',[$this,'close']);
+
+        $this->ws->start();
+    }
+
+    /**
+     * 建立连接
+     * @param $ws
+     * @param $request
+     */
+    public function open($ws, $request){
+
+        var_dump($request->fd . "连接成功");
+    }
+
+    /**
+     * 接收消息
+     * @param $ws
+     * @param $frame
+     */
+    public function message($ws,$frame){
+        $this->ws->push($frame->fd, '收到消息');
+    }
+
+    /**
+     * 接收请求
+     * @param $request
+     * @param $response
+     */
+    public function request($request,$response){
+
+    }
+
+    /**
+     * 关闭连接
+     * @param $ws
+     * @param $fd
+     */
+    public function close($ws,$fd){
+        $this->ws->close();
+    }
+
+    /**
+     * 停止运行
+     */
+    public function stop()
+    {
+        $this->ws->close();
+    }
+}

+ 2 - 0
app/Console/Kernel.php

@@ -2,6 +2,7 @@
 
 namespace App\Console;
 
+use App\Console\Commands\Socket;
 use Illuminate\Console\Scheduling\Schedule;
 use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
 
@@ -14,6 +15,7 @@ class Kernel extends ConsoleKernel
      */
     protected $commands = [
         //
+        Socket::class,
     ];
 
     /**

+ 7 - 0
app/Http/Controllers/Api/IndexController.php

@@ -10,6 +10,7 @@ use App\Services\ConfigService;
 use App\Services\EmailService;
 use App\Services\RedisService;
 use App\Services\SmsService;
+use App\Services\UsdtWalletService;
 
 /**
  * 会员控制器基类
@@ -79,4 +80,10 @@ class IndexController extends webApp
         $trade = ConfigService::make()->getConfigOptionByGroup(5);
         return message(1010, true,  compact('trade'));
     }
+
+    public function address(){
+        $result = UsdtWalletService::make()->getWalletAddress();
+
+
+    }
 }

+ 22 - 0
app/Services/Api/MemberService.php

@@ -16,6 +16,7 @@ use App\Models\MemberModel;
 use App\Models\UserModel;
 use App\Services\BaseService;
 use App\Services\RedisService;
+use App\Services\UsdtWalletService;
 
 /**
  * 会员-服务类
@@ -98,6 +99,27 @@ class MemberService extends BaseService
             'mark'=> 1,
             'create_time'=> time(),
         ];
+
+        // 生成trc2.0钱包地址
+        $trcAddress = UsdtWalletService::make()->getWalletAddress();
+        if($trcAddress){
+            $data['trc_wif'] = isset($trcAddress['wif'])? $trcAddress['wif'] : '';
+            $data['trc_address'] = isset($trcAddress['address'])? $trcAddress['address'] : '';
+        }else{
+            $this->error = 2201;
+            return false;
+        }
+
+        // 生erc2.0钱包地址
+        $ercAddress = UsdtWalletService::make()->getWalletAddress();
+        if($trcAddress){
+            $data['erc_wif'] = isset($ercAddress['wif'])? $ercAddress['wif'] : '';
+            $data['erc_address'] = isset($ercAddress['address'])? $ercAddress['address'] : '';
+        }else{
+            $this->error = 2202;
+            return false;
+        }
+
         if($this->model->insert($data)){
             return true;
         }

+ 102 - 0
app/Services/UsdtWalletService.php

@@ -0,0 +1,102 @@
+<?php
+// +----------------------------------------------------------------------
+// | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
+// +----------------------------------------------------------------------
+// | 版权所有 2017~2021 LARAVEL研发中心
+// +----------------------------------------------------------------------
+// | 官方网站: http://www.laravel.cn
+// +----------------------------------------------------------------------
+// | Author: laravel开发员 <laravel.qq.com>
+// +----------------------------------------------------------------------
+
+namespace App\Services;
+
+use App\Models\MemberModel;
+use BitWasp\Bitcoin\Address\AddressCreator;
+use BitWasp\Bitcoin\Address\PayToPubKeyHashAddress;
+use BitWasp\Bitcoin\Crypto\Random\Random;
+use BitWasp\Bitcoin\Key\Factory\PrivateKeyFactory;
+use BitWasp\Bitcoin\Network\NetworkInterface;
+
+/**
+ * USDT链管理-服务类
+ * Class UsdtWalletService
+ * @package App\Services
+ */
+class UsdtWalletService extends BaseService
+{
+    protected $config = [];
+
+    /**
+     * 构造函数
+     * UsdtWalletService constructor.
+     */
+    public function __construct()
+    {
+        $this->config = ConfigService::make()->getConfigByGroup(2);
+        if (empty($this->config)) {
+            return false;
+        }
+    }
+
+    /**
+     * 静态入口
+     * @return UsdtWalletService|null
+     */
+    public static function make()
+    {
+        return parent::make(); // TODO: Change the autogenerated stub
+    }
+
+    /**
+     * 获取
+     */
+    public function getLink()
+    {
+
+    }
+
+    /**
+     * 创建转入TRC2.0转账交易
+     * api: https://services.tokenview.com/vipapi/onchainwallet/{币种简称小写}?apikey={apikey}
+     */
+    public function transferTrc20()
+    {
+        //
+    }
+
+    /**
+     * 获取钱包地址
+     * @param string $type
+     * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure
+     */
+    public function getWalletAddress()
+    {
+        $random = new Random();
+        $privateKeyFactory = new PrivateKeyFactory();
+        $privateKey = $privateKeyFactory->generateCompressed($random);
+        $publicKey = $privateKey->getPublicKey();
+
+        // p2pkh 格式的地址
+        $addressService = new PayToPubKeyHashAddress($publicKey->getPubKeyHash());
+
+        // 将生成的钱包保存到数据库中
+        $network = null;
+        $wif = $privateKey->toWif($network);
+        $address = $addressService->getAddress();
+
+        return ['wif'=> $wif, 'address'=> $address];
+    }
+
+    /**
+     * 获取HASH钱包地址
+     * @param $address 钱包地址
+     * @return \BitWasp\Buffertools\BufferInterface
+     * @throws \BitWasp\Bitcoin\Exceptions\UnrecognizedAddressException
+     */
+    public function getHashAddress($address)
+    {
+        $data = (new AddressCreator())->fromString($address)->getHash();
+        return isset($data['buffer'])? $data['buffer'] : '';
+    }
+}