public function store(Request $request) { $web = trans("language::web"); $str = trans("language::system.no_null"); $config = Setting::where('lang', 'single')->lists('value', 'name'); $messageVerifyType = $config['message_verify_type'] ?? 1; $systems = trans("language::system"); if ($messageVerifyType == 1) { $this->validate($request, [ // 'subject' => 'required', 'email' => 'required|email', //'company' => 'required', // 新增的 'company' 字段及其验证规则 //'country' => 'required', // 新增的 'country' 字段及其验证规则 // 'captcha' => 'required|captcha:' . $request->get('key', 'default') . ',math', ],[ 'subject.required' => (isset($web['subject']) ? $web['subject'] : '' . 'Subject') . ' ' . $str, //'company.required' => (isset($web['company']) ? $web['company'] : '' . 'Company') . ' ' . $str, // 对 'company' 的错误消息定义 //'country.required' => (isset($web['country']) ? $web['country'] : '' . 'Country') . ' ' . $str, // 对 'country' 的错误消息定义 'email.required' => (isset($web['email']) ? $web['email'] : '' . 'E-mail') . ' ' . $str, 'captcha.required' => (isset($web['captcha']) ? $web['captcha'] : '' . 'Captcha') . ' ' . $str, 'email.email' => 'E-mail format is wrong', // 'captcha.captcha' => 'Captcha is wrong', ]); } else { $this->validate($request, [ 'subject' => 'required', 'email' => 'required|email', //'company' => 'required', // 新增的 'company' 字段及其验证规则 //'country' => 'required', // 新增的 'country' 字段及其验证规则 'g-recaptcha-response' => 'required', ],[ 'subject.required' => (isset($web['subject']) ? $web['subject'] : '' . 'Subject') . ' ' . $str, //'company.required' => (isset($web['company']) ? $web['company'] : '' . 'Company') . ' ' . $str, // 对 'company' 的错误消息定义 //'country.required' => (isset($web['country']) ? $web['country'] : '' . 'Country') . ' ' . $str, // 对 'country' 的错误消息定义 'email.required' => (isset($web['email']) ? $web['email'] : '' . 'E-mail') . ' ' . $str, 'g-recaptcha-response.required' => 'Please verify with the machine', 'email.email' => 'E-mail format is wrong', ]); $reCaptchaSecret = $config['re_captcha_secret'] ?? null; if (!$reCaptchaSecret) { return back()->withInput()->with('error', $systems['message_error']); } $client = new \GuzzleHttp\Client(['timeout' => 10.0]); try { $res = $client->request('POST', config('system.re_captcha_url').'/recaptcha/api/siteverify', [ 'form_params' => [ 'secret' => $reCaptchaSecret, 'response' => $request->input('g-recaptcha-response'), 'remoteip' => $request->getClientIp() ] ]); $content = json_decode($res->getBody()->getContents(), true); if (!$content['success']) { return back()->withInput()->with('error', $systems['message_error']); } } catch (\Exception $e) {} } $cacheKey = Carbon::now()->format('Y-m-d') . $request->getClientIp(); $cacheNum = Cache::get($cacheKey) ?: 0; // 单个 ip 每天超过 4 次不允许继续提交留言 if ($cacheNum >= 4) { return back()->withInput()->with('error', $systems['message_error']); } $request->merge(['lang' => config('lang')]); $data = $request->all(); $data['ip'] = $request->getClientIp(); $result = Message::create($data); if ($result) { Cache::put($cacheKey, $cacheNum + 1, 60 * 24); event(new \App\Events\MessageEvent($result)); return redirect('message')->with('success', $systems['message_success']); } return back()->withInput()->with('error', $systems['message_error']); }
上一篇:外贸易列表排序
下一篇:已经是最后一篇