afa 6 kuukautta sitten
vanhempi
sitoutus
ea7127f48e

+ 4 - 11
application/api/controller/Teac.php

@@ -194,7 +194,7 @@ class Teac extends Api
     }
 
     //取消交易
-    public function cancelTrade(ProductTeac $productTeac, UserTeac $userTeac)
+    public function cancelTrade(ProductTeac $productTeac, TeacLogin $teacLogin)
     {
         $id = $this->request->post('id/d', 0);
         $row = $productTeac::where(['id' => $id, 'user_id' => $this->auth->id])->find();
@@ -203,17 +203,10 @@ class Teac extends Api
         Db::startTrans();
         try {
 
-      
-            // 出售购买
-            $userTeac::setUserCreateOrder($this->auth->id, $row['id'], ProductTeac::Buying, $params['num'], $row['price'],  $fee);
-
-            //添加扣除相应茶宝Teac
-            $teacLogin::setCreateBuyingOrder($this->auth->id, $row['user_id'], $row['price'], $params['num'], $fee);
-
-            //修改状态
-            if($row->stock - $row->num == $params['num']) $row->status = ProductTeac::Complete;
+            //返回相应茶宝Teac
+            $teacLogin::setUserReturnOrder($this->auth->id, $row['type_id'], $row['frozen']);
   
-            $row->status = ProductTeac::Complete;
+            $row->status = ProductTeac::Closure;
             $row->save();
             // 提交事务
             Db::commit();

+ 14 - 51
application/api/logic/TeacLogin.php

@@ -88,63 +88,26 @@ class TeacLogin
     }
 
       /**
-       * 根据时间戳获取最近的区块高度     
-       * api接口返回数据格式
-       * @param $orderInfo
-       * @return array|string
+       * 退回相应茶宝/Teac   
+       * @param $uid    用户id
+       * @param $typeId 订单类型
+       * @param $stock  剩余数
+       * @return void
        */
-      public function getBlockNoByTime($time):array
+      public static function setUserReturnOrder(int $uid, int $typeId,  int $stock)
       {
-        if(!Cache::has('block_' . $time)){
-            if (empty($time)) {
-                return _error('时间戳不能为空');
-            }
-
-            //https://api.bscscan.com/api?module=block&action=getblocknobytime&timestamp=1601510400&closest=before&apikey=YourApiKeyToken
-
-            $url = "https://api.bscscan.com/api?module=block&action=getblocknobytime&";
-            $url .= "&closest=before";//closest 值还有个参数 after 控制返回接近时间戳之前还是之后的区块高度
-            $url .= "&apikey=" . $this->bsc_api_key;
-            $url .= "&timestamp=" . $time;
-            dump($url);
-            $body = Http::get($url);
-            if (empty($body)) {
-                return _error('获取区块高度api返回内容为空');
-            }
-            // 转成数组
-            $rsArr = json_decode($body, true);
-            if (empty($rsArr) || !is_array($rsArr)) {
-                return _error('获取区块高度api返回数据异常,json转换失败');
-            }
-
-            if ($rsArr['status'] != '1') {
-                return _error($rsArr['message'] . ' -- ' . $rsArr['result']);
-            }
-
-
-            Cache::set('block_' . $time, _success($rsArr['result']), 3600);
-            }
-            //获取开始预约前的区块高度
-            return Cache::get('block_' . $time);
-      }
 
-      public function getBlcokNoByCache($time){
-            if(!Cache::has('block')){
-                  $get_block = (new BscApi())->getBlockNoByTime($start_time);
-                  if($get_block['code'] == 0){
-                  Log::info($get_block['msg'] . date('Y-m-d H:i:s'));
-                  dump('获取区块高度有误');
-                  dump($get_block);
-                  return;
-                  }
-                  Cache::set('block', $get_block['data'], 3600);
+            if($typeId == ProductTeac::Sell) {
+                $asset = Asset::TEAC ;
+                $action = LedgerTeacChangeModel::SellCancel;
+            }else{
+                $asset = Asset::TOKEN ;
+                $action = LedgerTokenChangeModel::BuyCancel;
             }
-            //获取开始预约前的区块高度
-            $start_block = Cache::get('block');
+            return Loader::model('LedgerWalletModel')->changeWalletAccount($uid, $asset, $stock, $action, $uid);
       }
 
-    
+   
 
 
-  
 }

+ 3 - 1
application/common/model/LedgerTeacChangeModel.php

@@ -16,6 +16,7 @@ class LedgerTeacChangeModel extends Model
     const SellBuy          = 2;
     const Buying           = 3;
     const BuySell          = 4;
+    const SellCancel       = 5;
     /*
      * 支付状态
      * 0未支付 100支付中 200支付成功 400支付失败
@@ -27,7 +28,7 @@ class LedgerTeacChangeModel extends Model
         self::SellBuy          => '出售购买',
         self::Buying           => '存储',
         self::BuySell          => '存储出售',
-
+        self::SellCancel       => '取消出售',
     ];
 
     //getStatusList
@@ -39,6 +40,7 @@ class LedgerTeacChangeModel extends Model
             self::SellBuy   => __('出售购买'),
             self::Buying    => __('存储'),
             self::BuySell   => __('存储出售'),
+            self::SellCancel   => __('取消出售'),
         ];
     }
 

+ 4 - 3
application/common/model/UserTeac.php

@@ -26,15 +26,16 @@ class UserTeac extends Model
 
     //添加订单记录
     public static function setUserCreateOrder(int $uid, int $tradeId, int $typeId, int $num, float $price, float $fee)
-    {
+    {   
+        $total_price = bcmul($price, $num, 2); //总价
         return self::create([
             'user_id'       => $uid,
             'trade_id'      => $tradeId,
             'type_id'       => $typeId,
             'num'           => $num,
             'price'         => $price,
-            'total_price'   => bcmul($price, $num, 2),
-            'fee'           => $fee
+            'total_price'   => $total_price,
+            'fee'           => bcmul($total_price, $fee, 2)
         ]);
     }