From 5e54a94e47cf99152128b0f305ad77b91c4c8b29 Mon Sep 17 00:00:00 2001
From: elf <360197197@qq.com>
Date: Mon, 27 Feb 2023 23:00:11 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Application/Base/Tool/GameResource.class.php  |  47 +----
 .../Tool/GameResource/TzjClient.class.php     | 192 ++++++++++++++++++
 2 files changed, 195 insertions(+), 44 deletions(-)
 create mode 100644 Application/Base/Tool/GameResource/TzjClient.class.php

diff --git a/Application/Base/Tool/GameResource.class.php b/Application/Base/Tool/GameResource.class.php
index 1a0bdf725..eed67abe8 100644
--- a/Application/Base/Tool/GameResource.class.php
+++ b/Application/Base/Tool/GameResource.class.php
@@ -2,16 +2,7 @@
 
 namespace Base\Tool;
 
-use GuzzleHttp\Client;
-use GuzzleHttp\Exception\RequestException;
-
-use Base\Tool\GameResource\YzchzbClient;
-use Base\Tool\GameResource\LsxxClient;
-use Base\Tool\GameResource\LeyouClient;
-use Base\Tool\GameResource\SlzqClient;
-use Base\Tool\GameResource\XlqyClient;
-use Base\Tool\GameResource\XyyClient;
-use Base\Tool\GameResource\YhxxjClient;
+use Base\Tool\GameResource\TzjClient;
 
 /**
  * 游戏资源接口
@@ -22,40 +13,8 @@ class GameResource
     private $game;
 
     private $clientMap = [
-        191 => LeyouClient::class,  // 九天仙尘(安卓版)
-        192 => LeyouClient::class,  // 九天仙尘(苹果版)
-        229 => YzchzbClient::class, // 远征手游之楚汉争霸(安卓版)
-        230 => YzchzbClient::class, // 远征手游之楚汉争霸(苹果版)
-        231 => LsxxClient::class,   // 乱世枭雄(安卓版)
-        232 => LsxxClient::class,   // 乱世枭雄(苹果版)
-        239 => LeyouClient::class,  // 剑仙诀(安卓版)
-        240 => LeyouClient::class,  // 剑仙诀(苹果版)
-        247 => LeyouClient::class,  // 剑破长空(安卓版)
-        248 => LeyouClient::class,  // 剑破长空(苹果版)
-        275 => LeyouClient::class,  // 一梦仙境(安卓版)
-        276 => LeyouClient::class,  // 一梦仙境(苹果版)
-        280 => LeyouClient::class,  // 万剑八荒(安卓版)
-        281 => LeyouClient::class,  // 万剑八荒(苹果版)
-        279 => LeyouClient::class,  // 烈火长歌行(苹果版)
-        286 => LeyouClient::class,  // 烈火长歌行(安卓版)
-        291 => XlqyClient::class,   // 玄灵契约(安卓版)
-        292 => XlqyClient::class,   // 玄灵契约(苹果版)
-        256 => XyyClient::class,   // 逍遥游(安卓版)
-        257 => XyyClient::class,   // 逍遥游(苹果版)
-        293 => XyyClient::class,   // 逍遥游之仙魔之战(安卓版)
-        294 => XyyClient::class,   // 逍遥游之仙魔之战(苹果版)
-        299 => ZxjClient::class,   // 诛神记(安卓版)
-        300 => ZxjClient::class,   // 诛神记(苹果版)
-        303 => SlzqClient::class,  // 神灵之契(安卓版)
-        304 => SlzqClient::class,  // 神灵之契(苹果版)
-        307 => LeyouClient::class,  // 云中如梦令(安卓版)
-        308 => LeyouClient::class,  // 云中如梦令(苹果版)
-        337 => LeyouClient::class,  // 混沌天决(安卓版)
-        338 => LeyouClient::class,  // 混沌天决(苹果版)
-        363 => YhxxjClient::class,  // 云海寻仙记(安卓版)
-        364 => YhxxjClient::class,  // 云海寻仙记(苹果版)
-        365 => YhxxjClient::class,  // 云海寻仙记2(安卓版)
-        366 => YhxxjClient::class,  // 云海寻仙记2(苹果版)
+        6 => TzjClient::class,  // 天之禁2(安卓版)
+        7 => TzjClient::class,  // 天之禁2(苹果版)
     ];
 
     public function __construct($game)
diff --git a/Application/Base/Tool/GameResource/TzjClient.class.php b/Application/Base/Tool/GameResource/TzjClient.class.php
new file mode 100644
index 000000000..a5cd0afa2
--- /dev/null
+++ b/Application/Base/Tool/GameResource/TzjClient.class.php
@@ -0,0 +1,192 @@
+<?php
+
+namespace Base\Tool\GameResource;
+
+use Base\Tool\PlatformLog;
+use GuzzleHttp\Client;
+use Think\Log;
+
+/**
+ * 天之禁2-测试资源接口
+ */
+class TzjClient
+{
+    const SIGN_NAME = 'sign';
+    const SUCCESS = 1;
+
+    protected $key;
+    protected $brAppId;
+    protected $brPCode;
+    
+    protected $client;
+
+    private $apis = [
+        'mail' => ['url' => '/wf/m/mail/brRom/1/brPCode/{brPCode}/brAppId/{brAppId}', 'method' => 'post'],
+        'prop' => ['url' => '/wf/m/prop/brRom/1/brPCode/{brPCode}/brAppId/{brAppId}', 'method' => 'post'],
+        'pay' => ['url' => '/wf/m/pay/brRom/1/brPCode/{brPCode}/brAppId/{brAppId}', 'method' => 'post'],
+    ];
+
+    public function __construct($game = null)
+    {
+        $this->client = new Client([
+            'base_uri' => 'http://club.game.267zf.com',
+            'timeout'  => 10.0,
+        ]);
+    }
+
+    public function api($api, array $params = [])
+    {
+        $api = $this->apis[$api] ?? null;
+        if (is_null($api)) {
+            throw new \Exception('接口不存在');
+        }
+        $params[self::SIGN_NAME] = $this->sign($api, $params);
+        try {
+            $response = $this->request($api, $params);
+            return json_decode($response, true);
+        } catch (\Exception $e) {
+            $env = C('APP_ENV', null, 'prod');
+            $this->log('RequestError ' . $e->getMessage(), Log::ERR);
+            return [
+                'code' => -99,
+                'msg' => '网络异常',
+            ];
+        }
+    }
+
+    protected function buildUrl($url) {
+        $url = str_replace('{brPCode}', $this->brPCode, $url);
+        $url = str_replace('{brAppId}', $this->brAppId, $url);
+        return $url;
+    }
+
+    public function request($api, $params)
+    {
+        if ($api['method'] == 'get') {
+            return $this->get($this->buildUrl($api['url']), $params);
+        } else {
+            return $this->post($this->buildUrl($api['url']), $params);
+        }
+    }
+
+    protected function post($url, array $params = [])
+    {
+        $response = $this->client->post($url, [
+            'verify' => false,
+            'form_params' => $params,
+        ]);
+        $result = (string)$response->getBody();
+        $this->log('RequestData ' . json_encode($params));
+        $this->log('ResponseData ' . $result);
+        return $result;
+    }
+
+    protected function get($url, array $params = [])
+    {
+        $response = $this->client->get($url, [
+            'verify' => false,
+            'query' => $params,
+        ]);
+        $result = (string)$response->getBody();
+        $this->log('RequestData ' . json_encode($params));
+        $this->log('ResponseData ' . $result);
+        return $result;
+    }
+    
+    protected function sign($api, $params)
+    {
+        $apiBizFieldMap = [
+            'mail' => 'mailData',
+            'prop' => 'propData',
+            'pay' => 'moneyFen',
+        ];
+        $signFields = ['userId', 'serverId', 'roleId', $apiBizFieldMap[$api], 'orderNum', 'time'];
+        $signArray = [];
+        foreach ($signFields as $field) {
+            $signArray[] = $params[$field];
+        }
+        $signString = implode('', $signArray) . $this->key;
+        $this->log('SignString ' . $signString);
+        return md5($signString);
+    }
+
+    public function apply($order, $role)
+    {
+        $data = [
+            'userId' => $role['user_id'],
+            'roleId' => $role['role_id'],
+            'serverId' => $role['server_id'],
+            'orderNum' => $order['order_no'],
+            'time' => time(),
+        ];
+
+        $refId = $order['ref_id'];
+        $api = '';
+        if ($api == 'mail') {
+            $data['mailData'] = $refId;
+        } elseif ($api == 'prop') {
+            $data['propData'] = $refId;
+        } elseif ($api == 'pay') {
+            $data['moneyFen'] = intval($order['ref_amount'] * 100);
+        }
+
+        $result = $this->api($api, $data);
+        if ($result == self::SUCCESS) {
+            return [
+                'status' => true,
+                'message' => $result['msg'],
+                'result' => ['result' => $result]
+            ];
+        } else {
+            return [
+                'status' => false,
+                'message' => $result['msg'],
+                'result' => ['result' => $result]
+            ];
+        }
+    }
+
+    public function getResourceTypes($deviceType)
+    {
+        if ($deviceType == 'andriod') {
+            return [
+                ['id' => 1, 'name' => '邮件', 'device_type' => 'andriod'],
+                ['id' => 2, 'name' => '道具', 'device_type' => 'andriod'],
+                ['id' => 3, 'name' => '元宝', 'device_type' => 'andriod'],
+            ];
+        } elseif ($deviceType == 'ios') {
+            return [
+                ['id' => 1, 'name' => '邮件', 'device_type' => 'andriod'],
+                ['id' => 2, 'name' => '道具', 'device_type' => 'andriod'],
+                ['id' => 3, 'name' => '元宝', 'device_type' => 'andriod'],
+            ];
+        }
+    }
+
+    public function getResources($typeId, $deviceType)
+    {
+        if ($typeId == 3) {
+            return [
+                1 => ['ref_id' => 1, 'name' => '6元宝', 'amount' => 6],
+                2 => ['ref_id' => 2, 'name' => '30元宝', 'amount' => 30],
+                3 => ['ref_id' => 3, 'name' => '98元宝', 'amount' => 98],
+                // 4 => ['ref_id' => 4, 'name' => '128元宝', 'amount' => 128],
+                5 => ['ref_id' => 5, 'name' => '198元宝', 'amount' => 198],
+                6 => ['ref_id' => 6, 'name' => '328元宝', 'amount' => 328],
+                7 => ['ref_id' => 7, 'name' => '648元宝', 'amount' => 648],
+                8 => ['ref_id' => 8, 'name' => '1000元宝', 'amount' => 1000],
+                9 => ['ref_id' => 9, 'name' => '2000元宝', 'amount' => 2000],
+                // 10 => ['ref_id' => 10, 'name' => '3000元宝', 'amount' => 3000],
+                11 => ['ref_id' => 11, 'name' => '5000元宝', 'amount' => 5000],
+                // 12 => ['ref_id' => 12, 'name' => '10000元宝', 'amount' => 10000],
+                // 13 => ['ref_id' => 13, 'name' => '20000元宝', 'amount' => 20000],
+            ];
+        } else {
+            return [];
+        }
+    }
+
+    public function log($message, $level = Log::INFO) {
+        PlatformLog::write($message, 'game_api/tzj', $level);
+    }
+}
\ No newline at end of file