moonsflyer 6 месяцев назад
Родитель
Сommit
e49538bd27

+ 3 - 0
app/common/enum/AccountLogEnum.php

@@ -81,6 +81,7 @@ class AccountLogEnum
     const BNW_INC_LOTTERY = 207;
     const BNW_INC_INTEGRAL_ORDER = 208;
     const BNW_INC_USER_REGISTER = 209;
+    const EXCHANGE_INC_RECHARGE = 210;
 
     /**
      * 可提现余额减少类型
@@ -155,6 +156,7 @@ class AccountLogEnum
         self::BNW_INC_LOTTERY,
         self::BNW_INC_INTEGRAL_ORDER,
         self::BNW_INC_USER_REGISTER,
+        self::EXCHANGE_INC_RECHARGE,
     ];
 
     /**
@@ -291,6 +293,7 @@ class AccountLogEnum
             self::INTEGRAL_INC_AWARD => '消费赠送积分',
             self::INTEGRAL_INC_CANCEL_INTEGRAL => '取消积分订单返还积分',
             self::INTEGRAL_INC_USER_REGISTER    => '用户注册赠送积分',
+            self::EXCHANGE_INC_RECHARGE => '礼品卡兑换增加余额',
         ];
         if($flag) {
             return $desc;

+ 20 - 0
app/shopapi/controller/RechargeController.php

@@ -18,6 +18,8 @@
 // +----------------------------------------------------------------------
 namespace app\shopapi\controller;
 
+use app\adminapi\logic\gift_card\GiftCardLogic;
+use app\shopapi\validate\GiftCardValidate;
 use app\shopapi\lists\RechargeTemplateLists;
 use app\shopapi\logic\RechargeLogic;
 
@@ -67,4 +69,22 @@ class RechargeController extends BaseShopController
     {
         return $this->dataLists();
     }
+
+    public function giftCardExchange(){
+        if($this->request->isPost()) {
+
+            $params = (new GiftCardValidate())->post()->goCheck('exchange');
+            $params['user_id'] = $this->userId;
+            $result = (new RechargeLogic())->exchange($params);
+            var_dump($result);
+            if($result == 'true'){
+                return $this->success('礼品卡兑换成功');
+            }else{
+                return $this->fail(RechargeLogic::getError());
+            }
+
+        }else{
+            return $this->fail('请求方式错误');
+        }
+    }
 }

+ 42 - 0
app/shopapi/logic/RechargeLogic.php

@@ -16,11 +16,15 @@
 
 namespace app\shopapi\logic;
 
+use app\common\enum\AccountLogEnum;
 use app\common\enum\PayEnum;
+use app\common\logic\AccountLogLogic;
 use app\common\logic\BaseLogic;
 use app\common\model\Config;
+use app\common\model\GiftCardInfo;
 use app\common\model\RechargeOrder;
 use app\common\model\RechargeTemplate;
+use app\common\model\User;
 use app\common\service\ConfigService;
 use think\response\Json;
 
@@ -160,4 +164,42 @@ class RechargeLogic extends BaseLogic
             'from' => 'recharge'
         ];
     }
+
+    public static function exchange($params){
+
+        $gift_card_info = GiftCardInfo::where(['card_pass'=>$params['pass']])->findOrEmpty();
+        if($gift_card_info->isEmpty()){
+            throw new \think\Exception('卡密有误,请检查!');
+        }
+
+        $data = [
+            'sn'      => generate_sn((new RechargeOrder()),'sn'),
+            'terminal'      => 0,
+            'user_id'       => $params['user_id'],
+            'pay_status'    => PayEnum::ISPAID,
+            'pay_time'       => time(),
+            'pay_way'       => 7,
+            'order_amount'  => $gift_card_info['card_money'],
+            'template_id'   => 0,
+            'award'         => 0
+        ];
+
+        $order = RechargeOrder::create($data);
+
+        $user_info = User::find($params['user_id']);
+        $user_info->user_money =  $user_info->user_money + $gift_card_info['card_money'];
+        $user_info->save();
+
+        $gift_card_info->is_used = 1;
+        $gift_card_info->user_id = $params['user_id'];
+        $gift_card_info->used_time = time();
+        $gift_card_info->save();
+
+
+        // 记录账户流水
+        AccountLogLogic::add($params['user_id'], AccountLogEnum::EXCHANGE_INC_RECHARGE, AccountLogEnum::INC,$gift_card_info['card_money'], $order->sn, '用户礼品卡兑换金额');
+
+
+        return true;
+    }
 }

+ 57 - 0
app/shopapi/validate/GiftCardValidate.php

@@ -0,0 +1,57 @@
+<?php
+// +----------------------------------------------------------------------
+// | likeshop开源商城系统
+// +----------------------------------------------------------------------
+// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
+// | gitee下载:https://gitee.com/likeshop_gitee
+// | github下载:https://github.com/likeshop-github
+// | 访问官网:https://www.likeshop.cn
+// | 访问社区:https://home.likeshop.cn
+// | 访问手册:http://doc.likeshop.cn
+// | 微信公众号:likeshop技术社区
+// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
+// |  likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
+// | 禁止对系统程序代码以任何目的,任何形式的再发布
+// | likeshop团队版权所有并拥有最终解释权
+// +----------------------------------------------------------------------
+// | author: likeshop.cn.team
+// +----------------------------------------------------------------------
+
+namespace app\shopapi\validate;
+
+use app\common\model\GiftCardInfo;
+use app\common\validate\BaseValidate;
+
+class GiftCardValidate extends BaseValidate
+{
+    protected $rule = [
+        'pass' => 'require',
+
+    ];
+
+    protected $message = [
+        'pass.require' => '卡密必传!',
+    ];
+
+
+    public function sceneExchange()
+    {
+        return $this->only(['pass'])
+            ->append('pass','checkPass');
+    }
+
+    public function checkPass($value,$rule,$data){
+        $result = GiftCardInfo::where(['card_pass'=>$value])->findOrEmpty();
+        if ($result->isEmpty()) {
+            return '礼品卡信息不存在!';
+        }
+        if($result['is_used']){
+            return '礼品卡已被使用!';
+        }
+        return true;
+    }
+
+
+
+
+}