123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- <?php
- namespace addons\wwh\controller;
- use think\Db;
- use think\Request;
- use think\Exception;
- class Index extends Base
- {
- /**
- * 首页
- */
- public function index()
- {
- // 获取URL名称
- $diyname = $this->request->param('diyname/s', '');
- // 获取插件配置
- $config = get_addon_config("wwh");
-
- $assignConfig = [
- 'product_show' => $config['product_show'] ?? 1,
- 'about_show' => $config['about_show'] ?? 1,
- 'cases_show' => $config['cases_show'] ?? 1,
- 'partner_show' => $config['partner_show'] ?? 1,
- 'news_show' => $config['news_show'] ?? 1
- ];
- // 使用闭包方式批量查询
- $data = [
- // 首页Banner
- 'banner' => Db::name('wwh_banner')
- ->where('lang', $this->lang)
- ->order('weigh desc')
- ->limit(6)
- ->select(),
- // 首页推荐产品
- 'product' => Db::name('wwh_archives')
- ->where([
- 'rec_data' => '1',
- 'status' => '1',
- 'classify' => 'product',
- 'lang' => $this->lang
- ])
- ->order('weigh desc')
- ->limit(4)
- ->select(),
- // 首页推荐产品大图
- 'productBig' => Db::name('wwh_archives')
- ->where([
- 'rec_data' => '1,10',
- 'status' => '1',
- 'classify' => 'product',
- 'lang' => $this->lang
- ])
- ->order('weigh desc')
- ->limit(1)
- ->select(), // 确保返回数组
- // 首页解决方案
- 'cases' => Db::name('wwh_archives')
- ->where([
- 'rec_data' => '1',
- 'status' => '1',
- 'classify' => 'cases',
- 'lang' => $this->lang
- ])
- ->order('weigh desc')
- ->limit(3)
- ->select(),
- // 首页合作伙伴
- 'partner' => Db::name('wwh_partner')
- ->where([
- 'is_rec' => '1',
- 'lang' => $this->lang
- ])
- ->order('weigh desc')
- ->limit(18)
- ->select(),
- // 首页新闻中心
- 'news' => Db::name('wwh_archives')
- ->where([
- 'rec_data' => '1',
- 'status' => '1',
- 'classify' => 'news',
- 'lang' => $this->lang
- ])
- ->order('weigh desc')
- ->limit(6)
- ->select(),
-
-
- // 公司简介
- 'home' => Db::name('wwh_home')
- ->where('lang', $this->lang)
- ->limit(1)
- ->find() ?: [],
- // 当前活动栏目
- 'navCur' => Db::name('wwh_column')
- ->where('diyname', $diyname)
- ->find() ?: ['classify' => 'none']
- ];
- //date 下的 news 处理时间
- foreach ($data['news'] as $key => $value) {
- $data['news'][$key]['time'] = strtotime($value['time']);
- }
- // 批量分配变量到模板
- $this->assign(array_merge($data, $assignConfig));
- return $this->view->fetch('/index');
- }
- /**
- * 简历提交处理
- */
- public function injoin()
- {
- // token验证
- $token = $this->request->post('__token__');
- if (!$token || !\think\Validate::is($token, "token", ['__token__' => $token])) {
- return json(['code' => 0, 'msg' => __lang('Token verification failed')]);
- }
- // 获取并验证验证码
- $verify2 = $this->request->post('verify2');
- if (!captcha_check($verify2)) {
- return json(['code' => 0, 'msg' => __lang('Verification code error, please try again')]);
- }
- // 获取并处理输入数据
- $realname2 = htmlspecialchars(trim($this->request->post('realname2')), ENT_QUOTES);
- $tel2 = htmlspecialchars(trim($this->request->post('tel2')), ENT_QUOTES);
- $gangwei2 = htmlspecialchars(trim($this->request->post('gangwei2')), ENT_QUOTES);
- // 验证必填字段
- if (empty($realname2) || empty($tel2) || empty($gangwei2)) {
- return json(['code' => 0, 'msg' => __lang('Please fill in all required fields')]);
- }
- // 验证姓名格式
- if (!preg_match('/^[\p{L}\s]{2,50}$/u', $realname2)) {
- return json(['code' => 0, 'msg' => __lang('Please provide a valid name')]);
- }
- // 验证手机号格式
- if (!preg_match('/^1[3-9]\d{9}$/', $tel2)) {
- return json(['code' => 0, 'msg' => __lang('Please provide a valid phone number')]);
- }
- // 获取上传文件
- $file = $this->request->file('url');
- if (!$file) {
- return json(['code' => 0, 'msg' => __lang('Please upload your resume')]);
- }
- // 文件验证规则(包含MIME类型)
- $validate = [
- 'size' => 10000000, // 10MB
- 'ext' => 'doc,docx,xls,xlsx,ppt,pptx,pdf',
- 'type' => 'application/msword,' .
- 'application/vnd.openxmlformats-officedocument.wordprocessingml.document,' .
- 'application/vnd.ms-excel,' .
- 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,' .
- 'application/vnd.ms-powerpoint,' .
- 'application/vnd.openxmlformats-officedocument.presentationml.presentation,' .
- 'application/pdf'
- ];
- try {
- // 开启事务
- Db::startTrans();
- // 创建上传目录
- $uploadPath = ROOT_PATH . 'public/uploads/wwh/resume/';
- if (!is_dir($uploadPath)) {
- mkdir($uploadPath, 0755, true);
- }
- // 安全上传文件
- $info = $file->validate($validate)->rule('uniqid')->move($uploadPath);
- if (!$info) {
- throw new Exception($file->getError());
- }
- // 获取文件MIME类型进行二次验证
- $filePath = $uploadPath . $info->getSaveName();
- $finfo = finfo_open(FILEINFO_MIME_TYPE);
- $mime = finfo_file($finfo, $filePath);
- finfo_close($finfo);
- $allowedMimes = [
- 'application/msword',
- 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
- 'application/vnd.ms-excel',
- 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
- 'application/vnd.ms-powerpoint',
- 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
- 'application/pdf'
- ];
- if (!in_array($mime, $allowedMimes)) {
- @unlink($filePath);
- throw new Exception(__lang('Upload file format not allowed or file too large'));
- }
- $url = '/uploads/wwh/resume/' . str_replace("\\", "/", $info->getSaveName());
- $fullUrl = $this->request->domain() . $url;
- // 准备数据
- $data = [
- 'realname2' => $realname2,
- 'tel2' => $tel2,
- 'gangwei2' => $gangwei2,
- 'url' => $url,
- 'lang' => $this->lang,
- 'createtime' => time()
- ];
- // 构建邮件内容
- $html = "<p><strong>".__lang('Full Name').":</strong>{$realname2}</p>
- <p><strong>".__lang('Contact Number').":</strong>{$tel2}</p>
- <p><strong>".__lang('Position').":</strong>{$gangwei2}</p>
- <p><strong>".__lang('Resume').":</strong><a href=\"{$fullUrl}\">{$fullUrl}</a></p>";
- // 获取配置
- $config = get_addon_config("wwh");
- if (empty($config['hr'])) {
- throw new Exception(__lang('Resume recipient email not configured'));
- }
- // 发送邮件
- $emailResult = \app\common\library\Email::instance()
- ->to($config['hr'])
- ->subject(__lang('Job Application Resume'))
- ->message($html)
- ->send();
- if (!$emailResult) {
- throw new Exception(__lang('Failed to send email, Please check the backend email configuration'));
- }
- // 保存到数据库
- $dbResult = Db::name('wwh_injoin')->insert($data);
- if (!$dbResult) {
- throw new Exception(__lang('Database operation failed'));
- }
- // 提交事务
- Db::commit();
- return json(['code' => 1, 'msg' => __lang('Submitted Successfully')]);
- } catch (Exception $e) {
- // 回滚事务
- Db::rollback();
- // 删除已上传的文件
- if (isset($url) && file_exists(ROOT_PATH . 'public' . $url)) {
- @unlink(ROOT_PATH . 'public' . $url);
- }
- return json(['code' => 0, 'msg' => $e->getMessage()]);
- }
- }
- /**
- * 客户留言
- */
- public function message()
- {
- // token验证
- $token = $this->request->post('__token__');
- if (!$token || !\think\Validate::is($token, "token", ['__token__' => $token])) {
- return json(['code' => 0, 'msg' => __lang('Token verification failed')]);
- }
- // 验证码检查
- // $verify = input("verify");
- // if (!captcha_check($verify)) {
- // session('captcha', null);
- // return json(['code' => 0, 'msg' => __lang('Verification code error, please try again')]);
- // }
- // session('captcha', null);
- // 数据收集与过滤
- $data = [
- 'realname' => htmlspecialchars(trim(input('realname', ''))),
- 'tel' => htmlspecialchars(trim(input('tel', ''))),
- 'email' => htmlspecialchars(trim(input('email', ''))),
- 'content' => htmlspecialchars(trim(input('content', ''))),
- 'lang' => $this->lang,
- 'createtime' => time()
- ];
- // 数据验证
- $validate = new \think\Validate([
- 'realname' => 'require|max:50',
- 'tel' => 'require|regex:/^1[3-9]\d{9}$/',
- 'email' => 'require|email',
- 'content' => 'require|max:1000'
- ]);
- if (!$validate->check($data)) {
- return json(['code' => 0, 'msg' => $validate->getError()]);
- }
- // 构建邮件内容
- // $html = "<p><strong>".__lang('Full Name').":</strong>".htmlspecialchars($data['realname'])."</p>
- // <p><strong>".__lang('Email').":</strong>".htmlspecialchars($data['email'])."</p>
- // <p><strong>".__lang('Phone').":</strong>".htmlspecialchars($data['tel'])."</p>
- // <p><strong>".__lang('Content').":</strong>".htmlspecialchars($data['content'])."</p>";
- // 获取配置
- // $config = get_addon_config("wwh");
- // $receive = $config['receive'] ?? '';
- // if (empty($receive)) {
- // return json(['code' => 0, 'msg' => __lang('Message recipient email not configured')]);
- // }
- // 发送邮件
- // $emailResult = \app\common\library\Email::instance()
- // ->to($receive)
- // ->subject(__lang('Customer message'))
- // ->message($html)
- // ->send();
- $db = Db::name('wwh_message')->insert($data);
- return json(['code' => 1, 'msg' => __lang('Submitted Successfully')]);
- // 数据库操作
- // if ($emailResult) {
- // $db = Db::name('wwh_message')->insert($data);
- // return json(['code' => 1, 'msg' => __lang('Submitted Successfully')]);
- // }
- return json(['code' => 0, 'msg' => __lang('Failed to send email, Please check the backend email configuration')]);
- }
- /**
- * 验证码
- */
- public function verify()
- {
- $captcha = new \think\captcha\Captcha();
- //验证码过期时间(s)
- $captcha->expire =1800;
- //验证码位数
- $captcha->length = 4;
- //验证成功后是否重置
- $captcha->reset = true;
- return $captcha->entry();
- }
- /**
- * 设置语言
- */
- public function switchLang()
- {
- $url = (string)input('param.url');
- $lang = (int)input('param.lang');
- // 验证 lang 参数
- if (!$lang) {
- $this->error('lang 不能为空');
- }
- // 验证 URL 参数
- if (empty($url)) {
- $this->error('URL 参数不能为空');
- }
- try {
- $decodedUrl = base64_decode($url, true);
- if (!$decodedUrl) {
- $this->error('URL 解码失败,请稍后重试');
- }
- } catch (\Exception $e) {
- $this->error('URL 解码失败,请稍后重试');
- }
- // 设置语言 cookie
- cookie('lang', $lang, 3600 * 24 * 30);
- // 重定向到目标 URL
- $this->redirect($decodedUrl);
- }
- }
|