瀏覽代碼

Teac兑换购买

afa 6 月之前
父節點
當前提交
c5d754a208

+ 32 - 9
application/api/controller/Exchange.php

@@ -12,6 +12,7 @@ use app\common\model\ProductOrder;
 use app\common\model\ProductsModel;
 use app\common\model\RwaExchangeRecordModel;
 use app\common\model\LedgerWalletModel;
+use app\common\model\LedgerTeacChangeModel;
 use fast\Action;
 use fast\Asset;
 use think\Db;
@@ -30,8 +31,7 @@ class Exchange extends Api
             $this->lan = $this->request->getLan();
       }
 
-      //Ledger/getWelfareRedeConfig Exchange
-      //Ledger/submitWelfare
+  
       //Rwa福利兑换配置
       public function getWelfareRedeConfig(){
 
@@ -186,20 +186,43 @@ class Exchange extends Api
              
             $ids = $this->request->post('product_id/d', 0);
             $list = $productLists::where('is_teac', 1)
-                  ->where('id', $ids)
+                  ->where('id', $ids)->where('status', 1)
                   ->field('id,teac_price,thum,'.$this->lan.'_name as name,details')
                   ->find();
             $this->success('ok', $list);
       }
 
       //Teac兑换购买
-      public function getTeacOrder(ProductLists $productLists){
+      public function getTeacOrder(ProductLists $productLists, LedgerWalletModel $ledgerWalletModel){
              
-            $list = $productLists::where('is_teac', 1)
-                  ->field('id,teac_price,thum,'.$this->lan.'_name as name')
-                  ->order('weigh desc')
-                  ->find();
-            $this->success('ok', $list);
+            $params = $this->request->post();
+            $validate = \think\Loader::validate('Exchange');
+            if(!$validate->scene('order')->check($params)) $this->error($validate->getError());
+
+            $rows = $productLists::where('id', $params['product_id'])->where('is_teac', 1)->where('status', 1)->find();
+            if(empty($rows)) throw new Exception(__("产品暂未开放"));
+            // 启动事务
+            Db::startTrans();
+            try {
+
+                  $teac = $ledgerWalletModel::getWalletTeac($this->auth->id);
+
+                  $amount = bcmul($params['stock'], $rows->teac_price, 2);
+                  if(bccomp($amount, $teac, 2) > 0) throw new Exception(__("Teac购买金额足"), 15001);
+                  
+                  // 更新USDT和账变
+                  $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TEAC, -$amount, LedgerTeacChangeModel::Exchange, 0);
+
+      
+                  // 提交事务
+                  Db::commit();
+            } catch (Exception $e) {
+                  // 回滚事务
+                  Db::rollback();
+                  $this->error($e->getMessage(), null, $e->getCode());
+            }
+     
+            $this->success('ok');
       }
 
 }

+ 36 - 0
application/api/validate/Exchange.php

@@ -0,0 +1,36 @@
+<?php
+
+namespace app\api\validate;
+
+use think\Validate;
+
+class Exchange extends Validate
+{
+    /**
+     * 验证规则
+     */
+    protected $rule = [
+ 
+      'product_id'    => 'require',
+      'stock'         => 'require|number|gt:0'
+    ];
+    
+    /**
+     * 提示消息
+     */
+    protected $message = [
+
+      'product_id.require'  => '产品ID有误', 
+      'stock'                 => '数量有误'
+
+    ];
+    
+    /**
+     * 验证场景
+     */
+    protected $scene = [
+        'order'      => ['product_id', 'stock'],
+     
+    ];
+  
+}

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

@@ -17,7 +17,7 @@ class LedgerTeacChangeModel extends Model
     const Buying           = 3;
     const BuySell          = 4;
     const SellCancel       = 5;
-    const ProductOrder       = 6;
+    const Exchange         = 6;
     /*
      * 支付状态
      * 0未支付 100支付中 200支付成功 400支付失败
@@ -30,6 +30,7 @@ class LedgerTeacChangeModel extends Model
         self::Buying           => '存储',
         self::BuySell          => '存储出售',
         self::SellCancel       => '取消出售',
+        self::SellCancel       => '兑换产品',
     ];
 
     //getStatusList
@@ -42,6 +43,7 @@ class LedgerTeacChangeModel extends Model
             self::Buying    => __('存储'),
             self::BuySell   => __('存储出售'),
             self::SellCancel   => __('取消出售'),
+            self::Exchange     =>__('兑换产品') 
         ];
     }