afa 11 mēneši atpakaļ
vecāks
revīzija
847f5677f8

+ 5 - 12
application/api/controller/Ledger.php

@@ -28,12 +28,7 @@ class Ledger extends Api
      */
     public function assets()
     {
-        $where = [
-            'user_id' => $this->auth->getTokenUserID(),
-        ];
-        $info = (new LedgerWalletModel())
-            ->where($where)
-            ->find();
+     
         $res['assets']       = $this->auth->balance;
         $res['chabao_rate']  = getConfig('chabao_rate'); //茶宝汇率
         $res['withdrawal_fee']= getConfig('withdrawal_fee'); //提现收费
@@ -42,15 +37,12 @@ class Ledger extends Api
         $res['coin_list'] = [
             [
                 'coin_name' => 'USDT',
-                'coin_key' => 'usdt',
-                'amount' => $info['usdt']??0,
-                'frozen_amount' => $this->auth->frozen_amount, //冻结金额
-                'convert_rate'  => getConfig('convert_rate')   //折合比例
+                'coin_key'  => 'usdt',
+                'amount'    => LedgerWalletModel::getWalletChaBao($this->auth->getTokenUserID()),
+                'frozen_amount' => $this->auth->frozen_amount //冻结金额
             ]
         ];
         $this->success('', $res);
-
-
     }
     /**
      * 资产变动明细
@@ -137,6 +129,7 @@ class Ledger extends Api
         }
         $this->success('',$res);
     }
+
     public function actionGet()
     {
         $asset = $this->request->post('type'); // 资产类型

+ 16 - 25
application/api/controller/Order.php

@@ -8,12 +8,11 @@ use app\common\model\ProductOrder;
 use app\common\model\ProductPopular;
 use app\common\model\ProductLists;
 use app\common\model\LedgerWalletModel;
-use app\common\model\UserBalanceLog;
 use app\common\model\UserArea;
 use app\common\model\UserModel;
 use app\common\model\OfflineRechargeRecordModel;
 use Exception;
-use fast\MembershipLevel;
+use fast\Asset;
 use think\Db;
 use think\Env;
 
@@ -26,7 +25,7 @@ class Order extends Api
     /**
      * 创建订单
      */
-    public function create(ProductPopular $productPopular, ProductOrder $productOrder, ProductLists $productLists)
+    public function create(ProductPopular $productPopular, ProductOrder $productOrder, ProductLists $productLists, LedgerWalletModel $ledgerWalletModel)
     {
         $params = $this->request->post();
         $validate = \think\Loader::validate('Order');
@@ -47,7 +46,7 @@ class Order extends Api
         Db::startTrans();
         try {
 
-            $amount   =   bcsub($this->auth->balance, $this->auth->frozen_amount, 2);
+            $amount = $ledgerWalletModel::getWalletChaBao($this->auth->id);
             if(bccomp($order_info['price'], $amount, 2) > 0) throw new Exception(__("余额不足请前往充值"));
             if($order_info->stock == 0 || time() >= $order_info->end_time) throw new Exception(__("抢购已结束"));
             // 生成订单
@@ -57,8 +56,8 @@ class Order extends Api
             $productLists->where('id', $order_data['area_id'])->update(['status'=> $productLists::STOP]);
 
             //余额记录
-            UserBalanceLog::changeWalletAccount($this->auth->id, UserBalanceLog::Popular, $order_info['price'], 
-            $this->auth->balance, bcsub($this->auth->balance, $order_info['price'], 6), $order->id, '-');
+            $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$order_info['price'], $ledgerWalletModel::Popular, $this->auth->id);
+
             //扣除库存
             if($order_info->stock == 1 || time() >= $order_info->end_time) $order_info->status= $productPopular::STOP;
             $order_info->num  += 1;
@@ -78,7 +77,7 @@ class Order extends Api
     /**
      * 提货订单
      */
-    public function pickupOrder(UserArea $userArea, ProductOrder $productOrder, ProductLists $productLists)
+    public function pickupOrder(UserArea $userArea, ProductOrder $productOrder)
     {
         $params = $this->request->post();
         $validate = \think\Loader::validate('Order');
@@ -88,8 +87,8 @@ class Order extends Api
         if(empty($order_info)) $this->error( __("参数有误,无可用产品"));
         $order_data['name']     = $params['name'];
         $order_data['phone']    = $params['phone'];
-        $order_data['address']   = $params['address'];
-        $order_data['order_id']   = $params['order_id'];
+        $order_data['address']  = $params['address'];
+        $order_data['order_id'] = $params['order_id'];
         // 启动事务
         Db::startTrans();
         try {
@@ -152,7 +151,7 @@ class Order extends Api
      * 转让订单购买
      * @return void
      */
-    public function transferOrder(ProductOrder $productOrder, ProductTransfer $productTransfer)
+    public function transferOrder(ProductOrder $productOrder, ProductTransfer $productTransfer, LedgerWalletModel $ledgerWalletModel)
     {
         $params = $this->request->post();
         $validate = \think\Loader::validate('Order');
@@ -163,7 +162,8 @@ class Order extends Api
 
             $order_info = $productTransfer->where('id', $params['order_id'])->where('status', $productTransfer::NORMAL)->find();
             if(empty($order_info)) throw new Exception(__("订单不存在"));
-            if(bccomp($order_info['price'], $this->auth->balance, 2) > 0) throw new Exception(__("余额不足请前往充值"));
+            $chabao = $ledgerWalletModel::getWalletChaBao($this->auth->id);
+            if(bccomp($order_info['price'], $chabao, 2) > 0) throw new Exception(__("余额不足请前往充值"));
 
             $order_data['order_id']  = $params['order_id'];
             $order_data['product_id']= $order_info['product_id'];
@@ -178,21 +178,12 @@ class Order extends Api
             // 生成订单
             $order = $productOrder->create($order_data);
             //扣除余额记录 
-            $balance = bcsub($this->auth->balance, $this->auth->frozen_amount, 2);
-            UserBalanceLog::changeWalletAccount(
-                $this->auth->id, UserBalanceLog::Payment, 
-                $order_info['price'], $balance,
-                bcsub($balance, $order_info['price'], 2),
-                $order->id, '-');
+            $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$order_info['price'], $ledgerWalletModel::Payment, $order_info['user_id']);
+          
+
             //增加转让人余额
-            $amount   =   bcsub($order_info['price'], $order_info['fees'], 2);
-            $atBalance= UserModel::getUserAmount($order_info['user_id']);
-            UserBalanceLog::changeWalletAccount(
-                    $order_info['user_id'], UserBalanceLog::Receive, 
-                    $amount, 
-                    $atBalance,
-                    bcadd($atBalance, $amount, 2),
-                    $order->id);
+            $amount = bcsub($order_info['price'], $order_info['fees'], 2);
+            $ledgerWalletModel->changeWalletAccount($order_info['user_id'], Asset::TOKEN, $amount, $ledgerWalletModel::Receive, $this->auth->id);
 
             //修改原订单状态
             $productOrder->where('id', $order_info['order_id'])->setField('status', $productOrder::Complete);    

+ 9 - 8
application/api/controller/User.php

@@ -8,7 +8,7 @@ use app\common\model\LedgerSmhChangeModel;
 use app\common\model\LedgerTokenChangeModel;
 use app\common\model\LedgerWalletModel;
 use app\common\model\OfflineWithdrawRecordModel;
-use app\common\model\UserBalanceLog;
+
 use app\common\model\TeamLevelModel;
 use app\common\model\UserModel;
 use app\common\model\ParametersModel;
@@ -54,7 +54,7 @@ class User extends Api
             'phone'           => $user['phone'], // 手机号
             'rental_power'    => '0', // 自己购买的算力
             'team_power'      => '0', // 团队总算里
-            'balance'         => $user['balance'], // 余额
+            'balance'         => LedgerWalletModel::getWalletChaBao($this->auth->id), // 余额
             'team_level_id'   => $user['team_level_id'], // 团队等级ID
             'team_level_name' => $teamLevelName, // 团队等级名称
             'parent_id'       => $user['parent_id'], // 上级ID
@@ -88,19 +88,20 @@ class User extends Api
      * 余额记录信息
      * @return void
      */
-    public function getUserBalanceLog(UserBalanceLog $userBalanceLog)
+    public function getUserBalanceLog(LedgerTokenChangeModel $ledgerTokenChangeModel, LedgerWalletModel $ledgerWalletModel)
     {
         // 启动事务
         Db::startTrans();
         try {
-            $list['total'] = $userBalanceLog::where('user_id', $this->auth->id)
-                            ->where('type_id', $userBalanceLog::Share)
-                            ->sum("amount");
+            $list['total'] = $ledgerTokenChangeModel::where('user_id', $this->auth->id)
+                            ->where('action', $ledgerWalletModel::Share)
+                            ->sum("change_amount");
 
-            $list['data']  = $userBalanceLog::where('user_id', $this->auth->id)
+            $list['data']  = $ledgerTokenChangeModel::where('user_id', $this->auth->id)
                             ->order('id desc')
                             ->paginate($this->pageSize);
-            $list['statusList'] = $userBalanceLog::getStatusList();
+
+            $list['statusList'] = $ledgerWalletModel::getStatusList();
             // 提交事务
             Db::commit();
         } catch (Exception $e) {

+ 16 - 4
application/common/model/LedgerWalletModel.php

@@ -30,7 +30,7 @@ class LedgerWalletModel extends Model
     const Receive           = 2;
     const Recharge          = 3;
     const Withdraw          = 4;
-    const SameLevel         = 405;
+    const Share             = 5;
 
     /*
      * 支付状态
@@ -43,16 +43,22 @@ class LedgerWalletModel extends Model
         self::Receive           => '转让收款',
         self::Recharge          => '充值',
         self::Withdraw          => '提现',
-        self::SameLevel         => '平级奖励',
+        self::Share             => '分享',
     ];
 
 
+ 
+    public static function getWalletChaBao($userID)
+    {
+        return self::where('user_id', $userID)->value('token');
+    }
+
+
     public function getWallet($userID)
     {
         return $this->where('user_id', $userID)->find();
     }
 
-
     /**
      * 仅事务内使用,会附带"for update"
      * @param $userID
@@ -63,6 +69,12 @@ class LedgerWalletModel extends Model
         return $this->lock(true)->where('user_id', $userID)->find();
     }
 
+       
+    public static function getStatusList()
+    {
+        return [self::Popular => __('热销支付'), self::Payment  => __('转让支付'), self::Receive => __('转让收款'), self::Recharge => __('充值'), self::Withdraw  => __('提现'), self::Share => __('分享')];
+    }
+
     /**
      * 更新钱包余额并添加账变记录
      * @param int $uid 用户ID
@@ -115,7 +127,7 @@ class LedgerWalletModel extends Model
             'change_amount'  => $amount,
             'present_amount' => $newAmount,
             'create_time'    => time(),
-            'action'         => $action,
+            'action'         => $action
         ]);
 
         if (empty($insertRs)) {

+ 0 - 114
application/common/model/UserBalanceLog.php

@@ -1,114 +0,0 @@
-<?php
-
-namespace app\common\model;
-
-use fast\Asset;
-use fast\Http;
-use fast\Random;
-use think\Cache;
-use think\Log;
-use think\Model;
-use think\Request;
-
-class UserBalanceLog extends Model
-{
-
-    protected $name = "user_balance_log";
-
-    // 自动写入时间戳字段
-    protected $autoWriteTimestamp = 'int';
-
-    // 定义时间戳字段名
-    protected $createTime = 'create_time';
-    protected $updateTime = 'update_time';
-    protected $deleteTime = false;
-
-    // 追加属性
-    protected $append = [
-        'create_time_text',
-        'update_time_text'
-    ];
-
-    /*
-     * 获取ETC的USDT价格
-     */
-    //0支付 1转让支付 2 转让收款 3 充值 4 提现
-    const Popular           = 0;
-    const Payment           = 1;
-    const Receive           = 2;
-    const Recharge          = 3;
-    const Withdraw          = 4;
-    const Share             = 5;
-
-    /*
-    * 支付状态
-    * 0未支付 100支付中 200支付成功 400支付失败
-    */
-    public $pay_status = [
-        '-1'                    => '全部',
-        self::Popular           => '热销支付',
-        self::Payment           => '转让支付',
-        self::Receive           => '转让收款',
-        self::Recharge          => '充值',
-        self::Withdraw          => '提现',
-        self::Share             => '分享',
-    ];
-
-    public static function getStatusList()
-    {
-        return [self::Popular => __('热销支付'), self::Payment  => __('转让支付'), self::Receive => __('转让收款'), self::Recharge => __('充值'), self::Withdraw  => __('提现'), self::Share => __('分享')];
-    }
-
-    /**
-     * 更新钱包余额并添加账变记录
-     * @param int $uid 用户ID
-     * @param string $asset 资产类型
-     * @param string $amount 金额 正:表示加 负:表示减
-     * @param int $action 账变类型
-     * @return void
-     * @throws Exception
-     */
-    public static function changeWalletAccount(int $uid, int $type_id, string $amount, int $before, string $after, int $from_id, string $action = '+')
-    {
-        // 更新钱包余额
-        UserModel::updateForRBalance($uid, $amount, $action);
-
-        // 创建账变记录
-        $insertRs = self::create([
-            'user_id'        => $uid,
-            'from_id'        => $from_id,
-            'type_id'        => $type_id,
-            'amount'         => $amount,
-            'before'         => $before,
-            'after'          => $after,
-            'action'         => $action
-        ]);
-        if (empty($insertRs)) return false;
-    }
-
-
-    public function getCreateTimeTextAttr($value, $data)
-    {
-        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
-        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
-    }
-
-
-    public function getUpdateTimeTextAttr($value, $data)
-    {
-        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
-        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
-    }
-
-    protected function setCreateTimeAttr($value)
-    {
-        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
-    }
-
-    protected function setUpdateTimeAttr($value)
-    {
-        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
-    }
-
-
-}