RechargeLogic.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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\AccountLogEnum;
  18. use app\common\enum\PayEnum;
  19. use app\common\logic\AccountLogLogic;
  20. use app\common\logic\BaseLogic;
  21. use app\common\model\Config;
  22. use app\common\model\GiftCardInfo;
  23. use app\common\model\RechargeOrder;
  24. use app\common\model\RechargeTemplate;
  25. use app\common\model\User;
  26. use app\common\service\ConfigService;
  27. use think\response\Json;
  28. /**
  29. * 充值逻辑层
  30. * Class RechargeLogic
  31. * @package app\shopapi\logic
  32. */
  33. class RechargeLogic extends BaseLogic
  34. {
  35. public static function recharge($params)
  36. {
  37. try {
  38. // 校验数据
  39. self::validateData($params);
  40. if(isset($params['template_id'])) {
  41. // 选择模板充值
  42. return self::rechargeByTemplate($params);
  43. }else{
  44. // 输入金额充值
  45. return self::rechargeByMoney($params);
  46. }
  47. } catch(\Exception $e) {
  48. self::setError($e->getMessage());
  49. return false;
  50. }
  51. }
  52. /**
  53. * @notes 校验数据
  54. * @param $params
  55. * @throws \think\Exception
  56. * @author Tab
  57. * @date 2021/8/11 10:52
  58. */
  59. public static function validateData($params)
  60. {
  61. $open = ConfigService::get('recharge', 'open');
  62. if(!$open) {
  63. throw new \think\Exception('充值功能已关闭');
  64. }
  65. if(!isset($params['pay_way'])) {
  66. throw new \think\Exception('请选择支付方式');
  67. }
  68. if(!isset($params['template_id']) && !isset($params['money'])) {
  69. throw new \think\Exception('请输入充值金额');
  70. }
  71. }
  72. /**
  73. * @notes 输入金额充值
  74. * @param $params
  75. * @author Tab
  76. * @date 2021/8/11 11:27
  77. */
  78. public static function rechargeByMoney($params)
  79. {
  80. $params['template_id'] = 0;
  81. $params['award'] = [];
  82. // 输入金额 同样触发模板
  83. $templates = RechargeTemplate::field([ 'money', 'id', 'award' ])->select()->toArray();
  84. foreach ($templates as $key => $template) {
  85. $templates[$key]['money_int'] = (int) $template['money'] * 100;
  86. }
  87. array_multisort(array_column($templates, 'money_int'), SORT_ASC, SORT_NUMERIC, $templates);
  88. foreach ($templates as $key => $template) {
  89. // 满足金额
  90. if ($params['money'] >= $template['money']) {
  91. if (isset($templates[$key+1])) {
  92. if ($params['money'] < $templates[$key+1]['money']) {
  93. $params['template_id'] = $template['id'];
  94. $params['award'] = $template['award'];
  95. }
  96. } else {
  97. $params['template_id'] = $template['id'];
  98. $params['award'] = $template['award'];
  99. }
  100. }
  101. }
  102. return self::addRechargeOrder($params);
  103. }
  104. /**
  105. * @notes 选择模板充值
  106. * @param $params
  107. * @throws \think\Exception
  108. * @author Tab
  109. * @date 2021/8/11 11:25
  110. */
  111. public static function rechargeByTemplate($params)
  112. {
  113. $template = RechargeTemplate::findOrEmpty($params['template_id']);
  114. if($template->isEmpty()) {
  115. throw new \think\Exception('充值模板不存在');
  116. }
  117. $params['money'] = $template->money;
  118. $params['template_id'] = $template->id;
  119. $params['award'] = $template->award;
  120. return self::addRechargeOrder($params);
  121. }
  122. /**
  123. * @notes 添加充值订单
  124. * @param $params
  125. * @author Tab
  126. * @date 2021/8/11 11:23
  127. */
  128. public static function addRechargeOrder($params)
  129. {
  130. $minAmount = ConfigService::get('recharge', 'min_amount');
  131. if ($params['money'] < 0) {
  132. throw new \think\Exception("充值金额必须大于0");
  133. }
  134. if($minAmount > 0 && $params['money'] < $minAmount) {
  135. throw new \think\Exception('最低充值金额:' . $minAmount . "元");
  136. }
  137. $award = empty($params['award']) ? '' : json_encode($params['award'], JSON_UNESCAPED_UNICODE);
  138. $data = [
  139. 'sn' => generate_sn((new RechargeOrder()),'sn'),
  140. 'terminal' => $params['terminal'],
  141. 'user_id' => $params['user_id'],
  142. 'pay_status' => PayEnum::UNPAID,
  143. 'pay_way' => $params['pay_way'],
  144. 'order_amount' => $params['money'],
  145. 'template_id' => $params['template_id'],
  146. 'award' => $award
  147. ];
  148. $order = RechargeOrder::create($data);
  149. return [
  150. 'order_id' => $order->id,
  151. 'from' => 'recharge'
  152. ];
  153. }
  154. public static function exchange($params){
  155. $gift_card_info = GiftCardInfo::where(['card_pass'=>$params['pass']])->findOrEmpty();
  156. if($gift_card_info->isEmpty()){
  157. throw new \think\Exception('卡密有误,请检查!');
  158. }
  159. $data = [
  160. 'sn' => generate_sn((new RechargeOrder()),'sn'),
  161. 'terminal' => 0,
  162. 'user_id' => $params['user_id'],
  163. 'pay_status' => PayEnum::ISPAID,
  164. 'pay_time' => time(),
  165. 'pay_way' => 7,
  166. 'order_amount' => $gift_card_info['card_money'],
  167. 'template_id' => 0,
  168. 'award' => 0
  169. ];
  170. $order = RechargeOrder::create($data);
  171. $user_info = User::find($params['user_id']);
  172. $user_info->user_money = $user_info->user_money + $gift_card_info['card_money'];
  173. $user_info->save();
  174. $gift_card_info->is_used = 1;
  175. $gift_card_info->user_id = $params['user_id'];
  176. $gift_card_info->used_time = time();
  177. $gift_card_info->save();
  178. // 记录账户流水
  179. AccountLogLogic::add($params['user_id'], AccountLogEnum::EXCHANGE_INC_RECHARGE, AccountLogEnum::INC,$gift_card_info['card_money'], $order->sn, '用户礼品卡兑换金额');
  180. return true;
  181. }
  182. }