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' ]; } }