RechargeLogic.php 7.3 KB

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