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(); } } }