wesmiler 2 years ago
parent
commit
b7dffdf860
2 changed files with 30 additions and 0 deletions
  1. 6 0
      app/Services/Api/GoodsService.php
  2. 24 0
      app/Services/WalletService.php

+ 6 - 0
app/Services/Api/GoodsService.php

@@ -22,6 +22,7 @@ use App\Services\BaseService;
 use App\Services\ConfigService;
 use App\Services\RedisService;
 use App\Services\SupplyService;
+use App\Services\WalletService;
 use BN\Red;
 use Illuminate\Support\Facades\DB;
 
@@ -98,8 +99,13 @@ class GoodsService extends BaseService
             $locale = $locale ? $locale : session('locale_lang');
             $locale = $locale ? $locale : 'zh-cn';
             $supplyList = config('goods.supplyList');
+            $usdtPrice = WalletService::make()->getBianRatePrice();
+            $xdPrice = ConfigService::make()->getConfigByCode('xd_price',100);
+            $xdPrice = $xdPrice>0 && $xdPrice<=10000? $xdPrice : 100;
             foreach ($list['data'] as &$item) {
                 $item['supply_name'] = isset($supplyList[$item['supply_type']])? $supplyList[$item['supply_type']] : '';
+                $item['original_price'] = $item['cost_price'];
+                $item['cost_price'] = $usdtPrice>0? moneyFormat($item['cost_price']/$usdtPrice*$xdPrice, 2) : $item['cost_price'];
             }
             unset($item);
         }else{

+ 24 - 0
app/Services/WalletService.php

@@ -33,6 +33,7 @@ use Tron\TRX;
 class WalletService extends BaseService
 {
     protected $apiUrl = '';
+    protected $bianUrl = 'https://p2p.binance.com/bapi/c2c/v2/public/c2c/adv/quoted-price';
     protected $config = [];
     // 静态对象
     protected static $instance = null;
@@ -785,6 +786,29 @@ class WalletService extends BaseService
         }
     }
 
+    /**
+     * USDT 币安汇率
+     * @return string
+     */
+    public function getBianRatePrice()
+    {
+        // 币安买价
+        $cacheKey ="caches:wallet:usdt_rate";
+        $price = RedisService::get($cacheKey);
+        if($price){
+            return $price;
+        }
 
+        $headers = ["Content-Type: application/json; charset=utf-8"];
+        $result = httpRequest(self::$bianUrl, json_encode(['fromUserRole'=>'USER','assets'=>['USDT'],'fiatCurrency'=>'CNY','tradeType'=>'BUY']), 'post', '',5,$headers);
+        $datas = isset($result['data']) ? $result['data'] : [];
+        $data = isset($datas[0]) ? $datas[0] : [];
+        $price = isset($data['referencePrice']) ? moneyFormat($data['referencePrice'],2) : '0.00';
+        RedisService::set("caches:wallet:usdt_buy_result", ['result' => $result,'rates'=>$price,'data'=>$data], 7200);
+        if($price){
+            RedisService::set($cacheKey, $price, rand(5,10));
+        }
+        return $price>0? $price : 0;
+    }
 
 }