RechargeLogic.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LikeShop有特色的全开源社交分销电商系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 商业用途务必购买系统授权,以免引起不必要的法律纠纷
  7. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  8. // | 微信公众号:好象科技
  9. // | 访问官网:http://www.likemarket.net
  10. // | 访问社区:http://bbs.likemarket.net
  11. // | 访问手册:http://doc.likemarket.net
  12. // | 好象科技开发团队 版权所有 拥有最终解释权
  13. // +----------------------------------------------------------------------
  14. // | Author: LikeShopTeam
  15. // +----------------------------------------------------------------------
  16. namespace app\shopapi\logic;
  17. use app\common\enum\PayEnum;
  18. use app\common\logic\BaseLogic;
  19. use app\common\model\Config;
  20. use app\common\model\RechargeOrder;
  21. use app\common\model\RechargeTemplate;
  22. use app\common\service\ConfigService;
  23. use think\response\Json;
  24. /**
  25. * 充值逻辑层
  26. * Class RechargeLogic
  27. * @package app\shopapi\logic
  28. */
  29. class RechargeLogic extends BaseLogic
  30. {
  31. public static function recharge($params)
  32. {
  33. try {
  34. // 校验数据
  35. self::validateData($params);
  36. if(isset($params['template_id'])) {
  37. // 选择模板充值
  38. return self::rechargeByTemplate($params);
  39. }else{
  40. // 输入金额充值
  41. return self::rechargeByMoney($params);
  42. }
  43. } catch(\Exception $e) {
  44. self::setError($e->getMessage());
  45. return false;
  46. }
  47. }
  48. /**
  49. * @notes 校验数据
  50. * @param $params
  51. * @throws \think\Exception
  52. * @author Tab
  53. * @date 2021/8/11 10:52
  54. */
  55. public static function validateData($params)
  56. {
  57. $open = ConfigService::get('recharge', 'open');
  58. if(!$open) {
  59. throw new \think\Exception('充值功能已关闭');
  60. }
  61. if(!isset($params['pay_way'])) {
  62. throw new \think\Exception('请选择支付方式');
  63. }
  64. if(!isset($params['template_id']) && !isset($params['money'])) {
  65. throw new \think\Exception('请输入充值金额');
  66. }
  67. }
  68. /**
  69. * @notes 输入金额充值
  70. * @param $params
  71. * @author Tab
  72. * @date 2021/8/11 11:27
  73. */
  74. public static function rechargeByMoney($params)
  75. {
  76. $params['template_id'] = 0;
  77. $params['award'] = [];
  78. // 输入金额 同样触发模板
  79. $templates = RechargeTemplate::field([ 'money', 'id', 'award' ])->select()->toArray();
  80. foreach ($templates as $key => $template) {
  81. $templates[$key]['money_int'] = (int) $template['money'] * 100;
  82. }
  83. array_multisort(array_column($templates, 'money_int'), SORT_ASC, SORT_NUMERIC, $templates);
  84. foreach ($templates as $key => $template) {
  85. // 满足金额
  86. if ($params['money'] >= $template['money']) {
  87. if (isset($templates[$key+1])) {
  88. if ($params['money'] < $templates[$key+1]['money']) {
  89. $params['template_id'] = $template['id'];
  90. $params['award'] = $template['award'];
  91. }
  92. } else {
  93. $params['template_id'] = $template['id'];
  94. $params['award'] = $template['award'];
  95. }
  96. }
  97. }
  98. return self::addRechargeOrder($params);
  99. }
  100. /**
  101. * @notes 选择模板充值
  102. * @param $params
  103. * @throws \think\Exception
  104. * @author Tab
  105. * @date 2021/8/11 11:25
  106. */
  107. public static function rechargeByTemplate($params)
  108. {
  109. $template = RechargeTemplate::findOrEmpty($params['template_id']);
  110. if($template->isEmpty()) {
  111. throw new \think\Exception('充值模板不存在');
  112. }
  113. $params['money'] = $template->money;
  114. $params['template_id'] = $template->id;
  115. $params['award'] = $template->award;
  116. return self::addRechargeOrder($params);
  117. }
  118. /**
  119. * @notes 添加充值订单
  120. * @param $params
  121. * @author Tab
  122. * @date 2021/8/11 11:23
  123. */
  124. public static function addRechargeOrder($params)
  125. {
  126. $minAmount = ConfigService::get('recharge', 'min_amount');
  127. if ($params['money'] < 0) {
  128. throw new \think\Exception("充值金额必须大于0");
  129. }
  130. if($minAmount > 0 && $params['money'] < $minAmount) {
  131. throw new \think\Exception('最低充值金额:' . $minAmount . "元");
  132. }
  133. $award = empty($params['award']) ? '' : json_encode($params['award'], JSON_UNESCAPED_UNICODE);
  134. $data = [
  135. 'sn' => generate_sn((new RechargeOrder()),'sn'),
  136. 'terminal' => $params['terminal'],
  137. 'user_id' => $params['user_id'],
  138. 'pay_status' => PayEnum::UNPAID,
  139. 'pay_way' => $params['pay_way'],
  140. 'order_amount' => $params['money'],
  141. 'template_id' => $params['template_id'],
  142. 'award' => $award
  143. ];
  144. $order = RechargeOrder::create($data);
  145. return [
  146. 'order_id' => $order->id,
  147. 'from' => 'recharge'
  148. ];
  149. }
  150. }