董荣正 3 долоо хоног өмнө
parent
commit
ea9c10caad

+ 68 - 32
application/api/controller/Chabao.php

@@ -17,11 +17,14 @@ use think\Db;
 use app\common\model\LedgerTeacEcolyChangeModel;
 use app\common\library\Token;
 use think\Env;
+use app\common\model\ProductOrder;
+
+use function PHPSTORM_META\type;
 
 //茶宝Api
 class Chabao extends Api
 {
-    
+
     protected array $noNeedLogin = ['*'];
 
     protected $params = [];
@@ -30,14 +33,17 @@ class Chabao extends Api
     {
         parent::_initialize();
         $this->params = $this->request->post();
-        if(empty($this->params) || !array_filter($this->params)) $this->error('参数错误');
-        $sign = '';    
-        foreach ($this->params as $key => $value) {
-            if($key != 'sign') $sign .= $value;
+        if (empty($this->params['rwa_set'])) {
+            if (empty($this->params) || !array_filter($this->params)) $this->error('参数错误');
+            $sign = '';
+            foreach ($this->params as $key => $value) {
+                if ($key != 'sign') $sign .= $value;
+            }
+            $sign .= Env::get('rwa.AppID');
+            $sign = md5($sign);
+
+            if ($this->params['sign'] != $sign) $this->error('签名验签错误');
         }
-        $sign .= Env::get('rwa.AppID');
-        $sign = md5($sign);
-        if($this->params['sign'] != $sign) $this->error('签名验签错误');
     }
 
 
@@ -47,7 +53,7 @@ class Chabao extends Api
     public function login(UserModel $userModel)
     {
         $user = $userModel::where('phone', $this->params['mobile'])->find();
-        if(!$user) $this->error('账号不存在', [], 4000);
+        if (!$user) $this->error('账号不存在', [], 4000);
         Token::marshal($user['id'], $user['address']);
         $user['token'] = Token::getEncryptedToken($user['id']);
         $this->success('ok', $user);
@@ -60,16 +66,16 @@ class Chabao extends Api
     public function register(UserModel $userModel)
     {
         //手机号存在
-        if($userModel::where('phone', $this->params['mobile'])->find()) $this->error('账号已存在');
-        $newUserID = 0;    
+        if ($userModel::where('phone', $this->params['mobile'])->find()) $this->error('账号已存在');
+        $newUserID = 0;
         Db::startTrans();
         try {
             //注册
             $time = time();
             $data = [
                 'phone'         => $this->params['mobile'],
-                'nickname'      => "188" . substr((string)$time, 2) . rand(10, 99),//188开头,时间戳去掉开头两位,再后面再加两位随机数
-                'avatar'        => $this->request->domain().'/assets/img/logo.png',
+                'nickname'      => "188" . substr((string)$time, 2) . rand(10, 99), //188开头,时间戳去掉开头两位,再后面再加两位随机数
+                'avatar'        => $this->request->domain() . '/assets/img/logo.png',
                 'create_time'   => $time,
                 'team_level_id' => 0,
             ];
@@ -78,17 +84,16 @@ class Chabao extends Api
             // 创建钱包
             (new LedgerWalletModel())->insertGetId(['user_id' => $newUserID]);
             Token::marshal($newUserID);
-         
+
             // 提交事务
             Db::commit();
         } catch (Exception $e) {
             // 回滚事务
-           Db::rollback();
-           $this->error($e->getMessage());
+            Db::rollback();
+            $this->error($e->getMessage());
         }
-        if($newUserID == 0) $this->error('注册失败');
+        if ($newUserID == 0) $this->error('注册失败');
         $this->success('ok', ['token' => Token::getEncryptedToken($newUserID)]);
-         
     }
 
 
@@ -96,9 +101,9 @@ class Chabao extends Api
     public function bindAddress(UserModel $userModel)
     {
         $user = $userModel->getByAddress($this->params['address']);
-        
-        if(!$user) $this->error('钱包地址不存在');
-        if($user->phone) $this->error('钱包地址已被绑定');
+
+        if (!$user) $this->error('钱包地址不存在');
+        if ($user->phone) $this->error('钱包地址已被绑定');
 
         //修改手机号
         $user->phone = $this->params['mobile'];
@@ -111,9 +116,9 @@ class Chabao extends Api
     public function chalink(UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
     {
         $user = $userModel->getByPhone($this->params['mobile']);
-        
-        if(!$user) $this->error('钱包地址不存在');
-      
+
+        if (!$user) $this->error('钱包地址不存在');
+
         //添加茶宝
         $ledgerWalletModel->changeWalletAccount($user['id'], Asset::TOKEN, $this->params['chalink'], LedgerTokenChangeModel::ChaLink);
         $this->success('ok');
@@ -124,9 +129,9 @@ class Chabao extends Api
     public function teac(UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
     {
         $user = $userModel->getByPhone($this->params['mobile']);
-        
-        if(!$user) $this->error('钱包地址不存在');
-      
+
+        if (!$user) $this->error('钱包地址不存在');
+
         //添加Teac
         $ledgerWalletModel->changeWalletAccount($user['id'], Asset::TEAC_ANGEL, $this->params['teac'], LedgerTeacAngelChangeModel::TransferIn);
         $this->success('ok');
@@ -136,15 +141,46 @@ class Chabao extends Api
     public function teacecology(UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
     {
         $user = $userModel->getByPhone($this->params['mobile']);
-        
-        if(!$user) $this->error('钱包地址不存在');
-      
+
+        if (!$user) $this->error('钱包地址不存在');
+
         //添加Teac
         $ledgerWalletModel->changeWalletAccount($user['id'], Asset::TEAC_ECOLY, $this->params['teacecology'], LedgerTeacEcolyChangeModel::TransferIn);
         $this->success('ok');
     }
 
+    //茶付宝消费金额达到指定区间,赠送商品
 
+    public function give_rwa_goods_list()
+    {
 
-   
-}
+        $userModel = new UserModel();
+        $order_model = new ProductOrder();
+        $data = $this->params['data'];
+        $mobile_list = array_column($data, 'mobile');
+        $user = $userModel->whereIn('phone', $mobile_list)->field('phone,id')->select();
+
+        foreach ($data as &$item) {
+            $item['gift_record_type'] = 2;
+            $item['reason'] = '手机号未注册';
+            foreach($user as &$item_1){
+                if($item['mobile']==$item_1['phone']){
+                    $ret = $order_model->setGiverwagoods(0, 0, $item['product_id'], $item_1['id'], $order_model::Airdrop);
+                    if ($ret) {
+                        $item['gift_record_type'] = 1;
+                        $item['reason'] = '赠送RWA茶成功';
+                    } else {
+                        $item['gift_record_type'] = 0;
+                        $item['reason'] = '赠送RWA茶失败';
+                    }
+                    break;
+                }
+            }
+        }
+
+        unset($mobile_list);
+        unset($user);
+
+        $this->success('赠送RWA茶', $data);
+    }
+}

+ 110 - 84
application/common/model/ProductOrder.php

@@ -10,7 +10,7 @@ class ProductOrder extends Model
 
     // 表名
     protected $table = 'product_order';
-    
+
     // 自动写入时间戳字段
     protected $autoWriteTimestamp = 'int';
 
@@ -55,28 +55,28 @@ class ProductOrder extends Model
         self::Closure           => '关闭',
         self::Freeze            => '质押',
     ];
-   
+
 
     //抢购下单:未选择地区购买
-    public static function setPopularNoAreaOrder($num, $orderId, $price, $productId, $uid, $typeId):bool
+    public static function setPopularNoAreaOrder($num, $orderId, $price, $productId, $uid, $typeId): bool
     {
         $data = [];
         //抢购订单
         $time = time();
-        $is_popular = ($typeId == self::Popular)? self::Paid : 0;
+        $is_popular = ($typeId == self::Popular) ? self::Paid : 0;
         for ($i = 0; $i < $num; $i++) {
             $data[] = [
-                'order_id' => $orderId, 
-                'product_id' => $productId, 
-                'type_id' => $typeId, 
-                'status' => self::Paid, 
-                'area_id' => 0, 
-                'price' => $price, 
-                'popular_price' => $price, 
-                'fees'          => 0, 
-                'num'           => 1, 
+                'order_id' => $orderId,
+                'product_id' => $productId,
+                'type_id' => $typeId,
+                'status' => self::Paid,
+                'area_id' => 0,
+                'price' => $price,
+                'popular_price' => $price,
+                'fees'          => 0,
+                'num'           => 1,
                 'user_id'    => $uid,
-                'order_no'   => getOrderSN('R'.$i),
+                'order_no'   => getOrderSN('R' . $i),
                 'is_popular' => $is_popular,
                 'create_time' => $time,
                 'update_time' => $time
@@ -87,19 +87,19 @@ class ProductOrder extends Model
 
 
     //抢购下单:选择地区购买
-    public static function setPopularAreaOrder($areaArr, $orderId, $price, $productId, $uid, $typeId):bool
+    public static function setPopularAreaOrder($areaArr, $orderId, $price, $productId, $uid, $typeId): bool
     {
         $result = true;
         foreach ($areaArr as $item) {
             // 生成订单
             $order_arr['price']     = $price;
-            $order_arr['product_id']= $productId;
+            $order_arr['product_id'] = $productId;
             $order_arr['area_id']   = $item;
             self::setCreateOrder($orderId, $order_arr, $typeId, $uid, 0, getOrderSN('R'), 0, $price);
         }
         //修改区域状态
         $count = ProductArea::whereIn('id', $areaArr)->setField('status', ProductLists::Stop);
-        if($count !=  count($areaArr)) $result = false;
+        if ($count !=  count($areaArr)) $result = false;
         return $result;
     }
 
@@ -116,42 +116,44 @@ class ProductOrder extends Model
     public static function setCreateOrder(int $orderId, $orderInfo, $typeId, $userId, $fromUser, string $orderNo, float $fees, float $price): object
     {
         $data = [
-                'order_id'   => $orderId,
-                'product_id' => $orderInfo['product_id'],
-                'type_id'    => $typeId,
-                'status'     => self::Paid,
-                'area_id'    => $orderInfo['area_id'],
-                'order_no'   => $orderNo,
-                'user_id'    => $userId,
-                'from_user'  => $fromUser,
-                'price'      => $orderInfo['price'],
-                'popular_price'=> $price,
-                'fees'       => $fees,
-                'num'        => 1
+            'order_id'   => $orderId,
+            'product_id' => $orderInfo['product_id'],
+            'type_id'    => $typeId,
+            'status'     => self::Paid,
+            'area_id'    => $orderInfo['area_id'],
+            'order_no'   => $orderNo,
+            'user_id'    => $userId,
+            'from_user'  => $fromUser,
+            'price'      => $orderInfo['price'],
+            'popular_price' => $price,
+            'fees'       => $fees,
+            'num'        => 1
         ];
         //抢购订单
-        if($typeId == self::Popular) $data['is_popular'] = self::Paid;
+        if ($typeId == self::Popular) $data['is_popular'] = self::Paid;
         return self::create($data);
     }
 
     //判断是否购买持有产品
-    public function isBuyHoldProduct(array $productId, int $uid){
-        
+    public function isBuyHoldProduct(array $productId, int $uid)
+    {
+
         return self::where('user_id', $uid)->whereIn('product_id', $productId)->whereIn('status', [self::Paid, self::Transferred, self::Freeze])->count();
     }
 
     //获取持有持有产品数量
-    public static function getHoldProductNum(string $productId){
-        
+    public static function getHoldProductNum(string $productId)
+    {
+
         return self::whereIn('product_id', $productId)->whereIn('status', [self::Paid, self::Transferred, self::Freeze])
-        ->field('user_id,sum(num) as num')
-        ->group('user_id')
-        ->select();
+            ->field('user_id,sum(num) as num')
+            ->group('user_id')
+            ->select();
     }
 
     //获取抢购触发产品次数
     public static function getTripPopularProduct(string $productId)
-    {    
+    {
         return self::where('product_id', $productId)->where('is_popular', self::Paid)->whereTime('create_time', 'yesterday')->sum('num');
     }
 
@@ -159,7 +161,7 @@ class ProductOrder extends Model
     //获取持有产品数量
     public static function getByOrderProductNum($productId, $num, $uid)
     {
-        if(empty($productId)) return (object)[];
+        if (empty($productId)) return (object)[];
         foreach ($productId as &$item) {
             $item['num'] = $num;
             $count = self::getUserOrderNum($uid, $item['id']);
@@ -170,15 +172,16 @@ class ProductOrder extends Model
     }
 
     //空投产品向达到一定持有量的用户
-    public static function getUserOrderByProductId(int $rwaProductId, int $rwaNum){
+    public static function getUserOrderByProductId(int $rwaProductId, int $rwaNum)
+    {
         return ProductOrder::alias('a')
-                ->join('product_list b', 'a.product_id = b.id', 'left')
-                ->where('b.is_show', ProductLists::Normal)
-                ->whereIn('a.status', [self::Paid, self::Transferred, self::Freeze])
-                ->where('a.product_id', $rwaProductId)
-                ->group('a.user_id')->having('total_num>='.$rwaNum)
-                ->field('a.id,a.user_id,count(a.num) as total_num')
-                ->select();
+            ->join('product_list b', 'a.product_id = b.id', 'left')
+            ->where('b.is_show', ProductLists::Normal)
+            ->whereIn('a.status', [self::Paid, self::Transferred, self::Freeze])
+            ->where('a.product_id', $rwaProductId)
+            ->group('a.user_id')->having('total_num>=' . $rwaNum)
+            ->field('a.id,a.user_id,count(a.num) as total_num')
+            ->select();
     }
 
     //获取持有产品数量订单
@@ -191,22 +194,22 @@ class ProductOrder extends Model
     private static function getUserOrderNum(int $uid, int $product_id): int
     {
         return  self::alias('a')
-        ->join('product_list b', 'a.product_id = b.id', 'left')
-        ->where('b.is_show', ProductLists::Normal)
-        ->where('a.user_id', $uid)
-        ->where('a.product_id', $product_id)
-        ->where('a.status', '=', self::Paid)
-        ->sum('a.num');
+            ->join('product_list b', 'a.product_id = b.id', 'left')
+            ->where('b.is_show', ProductLists::Normal)
+            ->where('a.user_id', $uid)
+            ->where('a.product_id', $product_id)
+            ->where('a.status', '=', self::Paid)
+            ->sum('a.num');
     }
 
     // 获取订单状态
     public static function getProductOrder($orderId, $status, string $field)
     {
         return self::alias('a')->where('a.id', $orderId)
-                ->join("product_list b", "a.product_id = b.id", "left")
-                ->field('a.*,'.$field)
-                ->where('a.status', $status)
-                ->find();
+            ->join("product_list b", "a.product_id = b.id", "left")
+            ->field('a.*,' . $field)
+            ->where('a.status', $status)
+            ->find();
     }
 
     // 获取新人福利领取订单记录
@@ -225,30 +228,30 @@ class ProductOrder extends Model
     public static function getStatusAll()
     {
         return [
-            ['type_id'=>self::Popular,  'status'=> self::Paid,     'text' => __('已购买')],
-            ['type_id'=>self::Popular,  'status'=> self::Closure,  'text' => __('已关闭')],
-            ['type_id'=>self::Transfer, 'status'=> self::Transferred,'text' => __('转让中')],
-            ['type_id'=>self::Giveaway, 'status'=> self::Closure, 'text' => __('已赠送')],
-            ['type_id'=>self::Giveaway, 'status'=> self::Paid, 'text' => __('受赠')],
-            ['type_id'=>self::Transfer, 'status'=> self::Closure, 'text' => __('已转让')], 
-            ['type_id'=>self::Popular, 'status' => self::Shipped, 'text' => __('已提货')],
-            ['type_id'=>self::Transfer, 'status'=> self::Cancelled,'text' => __('已取消')],
-            ['type_id'=>self::Popular, 'status' => self::Freeze, 'text' => __('质押')],
-            ['type_id'=>self::Transfer, 'status' => self::Paid, 'text' => __('购买寄售')],
-            ['type_id'=>self::Airdrop, 'status' => self::Paid, 'text' => __('空投')],
-            ['type_id'=>self::Airdrop, 'status' => self::Closure, 'text' => __('空投关闭')],
-            ['type_id'=>self::Synthesi, 'status' => self::Paid, 'text' => __('合成')],
-            ['type_id'=>self::Synthesi, 'status' => self::Closure, 'text' => __('合成关闭')],
-            ['type_id'=>self::Buying, 'status' => self::Paid, 'text' => __('求购')],
-            ['type_id'=>self::Buying, 'status' => self::Closure, 'text' => __('求购关闭')],
-            ['type_id'=>self::Newbie, 'status' => self::Paid, 'text' => __('新人茶权')],
-            ['type_id'=>self::Newbie, 'status' => self::Closure, 'text' => __('新人茶权关闭')],
-            ['type_id'=>self::Super, 'status' => self::Paid, 'text' => __('新人福利')],
-            ['type_id'=>self::Super, 'status' => self::Closure, 'text' => __('新人福利关闭')],
-            ['type_id'=>self::RwaExchange, 'status' => self::Paid, 'text' => __('Rwa兑换')],
-            ['type_id'=>self::RwaExchange, 'status' => self::Closure, 'text' => __('Rwa兑换关闭')],
-            ['type_id'=>self::TeacExchange, 'status' => self::Paid, 'text' => __('Teac兑换')],
-            ['type_id'=>self::TeacExchange, 'status' => self::Closure, 'text' => __('Teac兑换关闭')],
+            ['type_id' => self::Popular,  'status' => self::Paid,     'text' => __('已购买')],
+            ['type_id' => self::Popular,  'status' => self::Closure,  'text' => __('已关闭')],
+            ['type_id' => self::Transfer, 'status' => self::Transferred, 'text' => __('转让中')],
+            ['type_id' => self::Giveaway, 'status' => self::Closure, 'text' => __('已赠送')],
+            ['type_id' => self::Giveaway, 'status' => self::Paid, 'text' => __('受赠')],
+            ['type_id' => self::Transfer, 'status' => self::Closure, 'text' => __('已转让')],
+            ['type_id' => self::Popular, 'status' => self::Shipped, 'text' => __('已提货')],
+            ['type_id' => self::Transfer, 'status' => self::Cancelled, 'text' => __('已取消')],
+            ['type_id' => self::Popular, 'status' => self::Freeze, 'text' => __('质押')],
+            ['type_id' => self::Transfer, 'status' => self::Paid, 'text' => __('购买寄售')],
+            ['type_id' => self::Airdrop, 'status' => self::Paid, 'text' => __('空投')],
+            ['type_id' => self::Airdrop, 'status' => self::Closure, 'text' => __('空投关闭')],
+            ['type_id' => self::Synthesi, 'status' => self::Paid, 'text' => __('合成')],
+            ['type_id' => self::Synthesi, 'status' => self::Closure, 'text' => __('合成关闭')],
+            ['type_id' => self::Buying, 'status' => self::Paid, 'text' => __('求购')],
+            ['type_id' => self::Buying, 'status' => self::Closure, 'text' => __('求购关闭')],
+            ['type_id' => self::Newbie, 'status' => self::Paid, 'text' => __('新人茶权')],
+            ['type_id' => self::Newbie, 'status' => self::Closure, 'text' => __('新人茶权关闭')],
+            ['type_id' => self::Super, 'status' => self::Paid, 'text' => __('新人福利')],
+            ['type_id' => self::Super, 'status' => self::Closure, 'text' => __('新人福利关闭')],
+            ['type_id' => self::RwaExchange, 'status' => self::Paid, 'text' => __('Rwa兑换')],
+            ['type_id' => self::RwaExchange, 'status' => self::Closure, 'text' => __('Rwa兑换关闭')],
+            ['type_id' => self::TeacExchange, 'status' => self::Paid, 'text' => __('Teac兑换')],
+            ['type_id' => self::TeacExchange, 'status' => self::Closure, 'text' => __('Teac兑换关闭')],
         ];
     }
 
@@ -270,8 +273,8 @@ class ProductOrder extends Model
     {
         return $this->hasOne('ProductArea', 'id', 'area_id', [], 'LEFT')->setEagerlyType(0);
     }
-    
-   
+
+
     //提货地址
     public function address()
     {
@@ -301,5 +304,28 @@ class ProductOrder extends Model
         return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
     }
 
-
+    //抢购下单:未选择地区购买
+    public static function setGiverwagoods($orderId, $price, $productId, $uid, $typeId): bool
+    {
+        //抢购订单
+        $time = time();
+        $is_popular = ($typeId == self::Giveaway) ? self::Closure : 0;
+        $data = [
+            'order_id' => $orderId,
+            'product_id' => $productId,
+            'type_id' => $typeId,
+            'status' => self::Closure,
+            'area_id' => 0,
+            'price' => $price,
+            'popular_price' => $price,
+            'fees'          => 0,
+            'num'           => 1,
+            'user_id'    => $uid,
+            'order_no'   => getOrderSN('R'),
+            'is_popular' => $is_popular,
+            'create_time' => $time,
+            'update_time' => $time
+        ];
+        return self::insert($data);
+    }
 }