| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?php
- // +----------------------------------------------------------------------
- // | LikeShop有特色的全开源社交分销电商系统
- // +----------------------------------------------------------------------
- // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
- // | 商业用途务必购买系统授权,以免引起不必要的法律纠纷
- // | 禁止对系统程序代码以任何目的,任何形式的再发布
- // | 微信公众号:好象科技
- // | 访问官网:http://www.likemarket.net
- // | 访问社区:http://bbs.likemarket.net
- // | 访问手册:http://doc.likemarket.net
- // | 好象科技开发团队 版权所有 拥有最终解释权
- // +----------------------------------------------------------------------
- // | Author: LikeShopTeam
- // +----------------------------------------------------------------------
- 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\facade\Db;
- use think\response\Json;
- /**
- * 充值逻辑层
- * Class RechargeLogic
- * @package app\shopapi\logic
- */
- class RechargeLogic extends BaseLogic
- {
- public static function recharge($params)
- {
- try {
- // 校验数据
- self::validateData($params);
- if(isset($params['template_id'])) {
- // 选择模板充值
- return self::rechargeByTemplate($params);
- }else{
- // 输入金额充值
- return self::rechargeByMoney($params);
- }
- } catch(\Exception $e) {
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * @notes 校验数据
- * @param $params
- * @throws \think\Exception
- * @author Tab
- * @date 2021/8/11 10:52
- */
- public static function validateData($params)
- {
- $open = ConfigService::get('recharge', 'open');
- if(!$open) {
- throw new \think\Exception('充值功能已关闭');
- }
- if(!isset($params['pay_way'])) {
- throw new \think\Exception('请选择支付方式');
- }
- if(!isset($params['template_id']) && !isset($params['money'])) {
- throw new \think\Exception('请输入充值金额');
- }
- }
- /**
- * @notes 输入金额充值
- * @param $params
- * @author Tab
- * @date 2021/8/11 11:27
- */
- public static function rechargeByMoney($params)
- {
- $params['template_id'] = 0;
- $params['award'] = [];
-
- // 输入金额 同样触发模板
- $templates = RechargeTemplate::field([ 'money', 'id', 'award' ])->select()->toArray();
-
- foreach ($templates as $key => $template) {
- $templates[$key]['money_int'] = (int) $template['money'] * 100;
- }
-
- array_multisort(array_column($templates, 'money_int'), SORT_ASC, SORT_NUMERIC, $templates);
-
- foreach ($templates as $key => $template) {
- // 满足金额
- if ($params['money'] >= $template['money']) {
- if (isset($templates[$key+1])) {
- if ($params['money'] < $templates[$key+1]['money']) {
- $params['template_id'] = $template['id'];
- $params['award'] = $template['award'];
- }
- } else {
- $params['template_id'] = $template['id'];
- $params['award'] = $template['award'];
- }
- }
- }
-
- return self::addRechargeOrder($params);
- }
- /**
- * @notes 选择模板充值
- * @param $params
- * @throws \think\Exception
- * @author Tab
- * @date 2021/8/11 11:25
- */
- public static function rechargeByTemplate($params)
- {
- $template = RechargeTemplate::findOrEmpty($params['template_id']);
- if($template->isEmpty()) {
- throw new \think\Exception('充值模板不存在');
- }
- $params['money'] = $template->money;
- $params['template_id'] = $template->id;
- $params['award'] = $template->award;
- return self::addRechargeOrder($params);
- }
- /**
- * @notes 添加充值订单
- * @param $params
- * @author Tab
- * @date 2021/8/11 11:23
- */
- public static function addRechargeOrder($params)
- {
- $minAmount = ConfigService::get('recharge', 'min_amount');
- if ($params['money'] < 0) {
- throw new \think\Exception("充值金额必须大于0");
- }
- if($minAmount > 0 && $params['money'] < $minAmount) {
- throw new \think\Exception('最低充值金额:' . $minAmount . "元");
- }
- $award = empty($params['award']) ? '' : json_encode($params['award'], JSON_UNESCAPED_UNICODE);
- $data = [
- 'sn' => generate_sn((new RechargeOrder()),'sn'),
- 'terminal' => $params['terminal'],
- 'user_id' => $params['user_id'],
- 'pay_status' => PayEnum::UNPAID,
- 'pay_way' => $params['pay_way'],
- 'order_amount' => $params['money'],
- 'template_id' => $params['template_id'],
- 'award' => $award
- ];
- $order = RechargeOrder::create($data);
- return [
- 'order_id' => $order->id,
- 'from' => 'recharge'
- ];
- }
- public static function exchange($params)
- {
- Db::startTrans();
- try {
- $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);
- if(!$order){
- Db::rollback();
- throw new \think\Exception('充值订单表插入失败!');
- }
- $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, '用户礼品卡兑换金额');
- // 提交事务
- Db::commit();
- return true;
- } catch (\Exception $e) {
- // 回滚事务
- Db::rollback();
- return $e->getMessage();
- }
- }
- }
|