Index.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php
  2. namespace addons\wwh\controller;
  3. use think\Db;
  4. use think\Request;
  5. use think\Exception;
  6. class Index extends Base
  7. {
  8. /**
  9. * 首页
  10. */
  11. public function index()
  12. {
  13. // 获取URL名称
  14. $diyname = $this->request->param('diyname/s', '');
  15. // 获取插件配置
  16. $config = get_addon_config("wwh");
  17. $assignConfig = [
  18. 'product_show' => $config['product_show'] ?? 1,
  19. 'about_show' => $config['about_show'] ?? 1,
  20. 'cases_show' => $config['cases_show'] ?? 1,
  21. 'partner_show' => $config['partner_show'] ?? 1,
  22. 'news_show' => $config['news_show'] ?? 1
  23. ];
  24. // 使用闭包方式批量查询
  25. $data = [
  26. // 首页Banner
  27. 'banner' => Db::name('wwh_banner')
  28. ->where('lang', $this->lang)
  29. ->order('weigh desc')
  30. ->limit(6)
  31. ->select(),
  32. // 首页推荐产品
  33. 'product' => Db::name('wwh_archives')
  34. ->where([
  35. 'rec_data' => '1',
  36. 'status' => '1',
  37. 'classify' => 'product',
  38. 'lang' => $this->lang
  39. ])
  40. ->order('weigh desc')
  41. ->limit(4)
  42. ->select(),
  43. // 首页推荐产品大图
  44. 'productBig' => Db::name('wwh_archives')
  45. ->where([
  46. 'rec_data' => '1,10',
  47. 'status' => '1',
  48. 'classify' => 'product',
  49. 'lang' => $this->lang
  50. ])
  51. ->order('weigh desc')
  52. ->limit(1)
  53. ->select(), // 确保返回数组
  54. // 首页解决方案
  55. 'cases' => Db::name('wwh_archives')
  56. ->where([
  57. 'rec_data' => '1',
  58. 'status' => '1',
  59. 'classify' => 'cases',
  60. 'lang' => $this->lang
  61. ])
  62. ->order('weigh desc')
  63. ->limit(3)
  64. ->select(),
  65. // 首页合作伙伴
  66. 'partner' => Db::name('wwh_partner')
  67. ->where([
  68. 'is_rec' => '1',
  69. 'lang' => $this->lang
  70. ])
  71. ->order('weigh desc')
  72. ->limit(18)
  73. ->select(),
  74. // 首页新闻中心
  75. 'news' => Db::name('wwh_archives')
  76. ->where([
  77. 'rec_data' => '1',
  78. 'status' => '1',
  79. 'classify' => 'news',
  80. 'lang' => $this->lang
  81. ])
  82. ->order('weigh desc')
  83. ->limit(6)
  84. ->select(),
  85. // 公司简介
  86. 'home' => Db::name('wwh_home')
  87. ->where('lang', $this->lang)
  88. ->limit(1)
  89. ->find() ?: [],
  90. // 当前活动栏目
  91. 'navCur' => Db::name('wwh_column')
  92. ->where('diyname', $diyname)
  93. ->find() ?: ['classify' => 'none']
  94. ];
  95. //date 下的 news 处理时间
  96. foreach ($data['news'] as $key => $value) {
  97. $data['news'][$key]['time'] = strtotime($value['time']);
  98. }
  99. // 批量分配变量到模板
  100. $this->assign(array_merge($data, $assignConfig));
  101. return $this->view->fetch('/index');
  102. }
  103. /**
  104. * 简历提交处理
  105. */
  106. public function injoin()
  107. {
  108. // token验证
  109. $token = $this->request->post('__token__');
  110. if (!$token || !\think\Validate::is($token, "token", ['__token__' => $token])) {
  111. return json(['code' => 0, 'msg' => __lang('Token verification failed')]);
  112. }
  113. // 获取并验证验证码
  114. $verify2 = $this->request->post('verify2');
  115. if (!captcha_check($verify2)) {
  116. return json(['code' => 0, 'msg' => __lang('Verification code error, please try again')]);
  117. }
  118. // 获取并处理输入数据
  119. $realname2 = htmlspecialchars(trim($this->request->post('realname2')), ENT_QUOTES);
  120. $tel2 = htmlspecialchars(trim($this->request->post('tel2')), ENT_QUOTES);
  121. $gangwei2 = htmlspecialchars(trim($this->request->post('gangwei2')), ENT_QUOTES);
  122. // 验证必填字段
  123. if (empty($realname2) || empty($tel2) || empty($gangwei2)) {
  124. return json(['code' => 0, 'msg' => __lang('Please fill in all required fields')]);
  125. }
  126. // 验证姓名格式
  127. if (!preg_match('/^[\p{L}\s]{2,50}$/u', $realname2)) {
  128. return json(['code' => 0, 'msg' => __lang('Please provide a valid name')]);
  129. }
  130. // 验证手机号格式
  131. if (!preg_match('/^1[3-9]\d{9}$/', $tel2)) {
  132. return json(['code' => 0, 'msg' => __lang('Please provide a valid phone number')]);
  133. }
  134. // 获取上传文件
  135. $file = $this->request->file('url');
  136. if (!$file) {
  137. return json(['code' => 0, 'msg' => __lang('Please upload your resume')]);
  138. }
  139. // 文件验证规则(包含MIME类型)
  140. $validate = [
  141. 'size' => 10000000, // 10MB
  142. 'ext' => 'doc,docx,xls,xlsx,ppt,pptx,pdf',
  143. 'type' => 'application/msword,' .
  144. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document,' .
  145. 'application/vnd.ms-excel,' .
  146. 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,' .
  147. 'application/vnd.ms-powerpoint,' .
  148. 'application/vnd.openxmlformats-officedocument.presentationml.presentation,' .
  149. 'application/pdf'
  150. ];
  151. try {
  152. // 开启事务
  153. Db::startTrans();
  154. // 创建上传目录
  155. $uploadPath = ROOT_PATH . 'public/uploads/wwh/resume/';
  156. if (!is_dir($uploadPath)) {
  157. mkdir($uploadPath, 0755, true);
  158. }
  159. // 安全上传文件
  160. $info = $file->validate($validate)->rule('uniqid')->move($uploadPath);
  161. if (!$info) {
  162. throw new Exception($file->getError());
  163. }
  164. // 获取文件MIME类型进行二次验证
  165. $filePath = $uploadPath . $info->getSaveName();
  166. $finfo = finfo_open(FILEINFO_MIME_TYPE);
  167. $mime = finfo_file($finfo, $filePath);
  168. finfo_close($finfo);
  169. $allowedMimes = [
  170. 'application/msword',
  171. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  172. 'application/vnd.ms-excel',
  173. 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  174. 'application/vnd.ms-powerpoint',
  175. 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  176. 'application/pdf'
  177. ];
  178. if (!in_array($mime, $allowedMimes)) {
  179. @unlink($filePath);
  180. throw new Exception(__lang('Upload file format not allowed or file too large'));
  181. }
  182. $url = '/uploads/wwh/resume/' . str_replace("\\", "/", $info->getSaveName());
  183. $fullUrl = $this->request->domain() . $url;
  184. // 准备数据
  185. $data = [
  186. 'realname2' => $realname2,
  187. 'tel2' => $tel2,
  188. 'gangwei2' => $gangwei2,
  189. 'url' => $url,
  190. 'lang' => $this->lang,
  191. 'createtime' => time()
  192. ];
  193. // 构建邮件内容
  194. $html = "<p><strong>".__lang('Full Name').":</strong>{$realname2}</p>
  195. <p><strong>".__lang('Contact Number').":</strong>{$tel2}</p>
  196. <p><strong>".__lang('Position').":</strong>{$gangwei2}</p>
  197. <p><strong>".__lang('Resume').":</strong><a href=\"{$fullUrl}\">{$fullUrl}</a></p>";
  198. // 获取配置
  199. $config = get_addon_config("wwh");
  200. if (empty($config['hr'])) {
  201. throw new Exception(__lang('Resume recipient email not configured'));
  202. }
  203. // 发送邮件
  204. $emailResult = \app\common\library\Email::instance()
  205. ->to($config['hr'])
  206. ->subject(__lang('Job Application Resume'))
  207. ->message($html)
  208. ->send();
  209. if (!$emailResult) {
  210. throw new Exception(__lang('Failed to send email, Please check the backend email configuration'));
  211. }
  212. // 保存到数据库
  213. $dbResult = Db::name('wwh_injoin')->insert($data);
  214. if (!$dbResult) {
  215. throw new Exception(__lang('Database operation failed'));
  216. }
  217. // 提交事务
  218. Db::commit();
  219. return json(['code' => 1, 'msg' => __lang('Submitted Successfully')]);
  220. } catch (Exception $e) {
  221. // 回滚事务
  222. Db::rollback();
  223. // 删除已上传的文件
  224. if (isset($url) && file_exists(ROOT_PATH . 'public' . $url)) {
  225. @unlink(ROOT_PATH . 'public' . $url);
  226. }
  227. return json(['code' => 0, 'msg' => $e->getMessage()]);
  228. }
  229. }
  230. /**
  231. * 客户留言
  232. */
  233. public function message()
  234. {
  235. // token验证
  236. $token = $this->request->post('__token__');
  237. if (!$token || !\think\Validate::is($token, "token", ['__token__' => $token])) {
  238. return json(['code' => 0, 'msg' => __lang('Token verification failed')]);
  239. }
  240. // 验证码检查
  241. // $verify = input("verify");
  242. // if (!captcha_check($verify)) {
  243. // session('captcha', null);
  244. // return json(['code' => 0, 'msg' => __lang('Verification code error, please try again')]);
  245. // }
  246. // session('captcha', null);
  247. // 数据收集与过滤
  248. $data = [
  249. 'realname' => htmlspecialchars(trim(input('realname', ''))),
  250. 'tel' => htmlspecialchars(trim(input('tel', ''))),
  251. 'email' => htmlspecialchars(trim(input('email', ''))),
  252. 'content' => htmlspecialchars(trim(input('content', ''))),
  253. 'lang' => $this->lang,
  254. 'createtime' => time()
  255. ];
  256. // 数据验证
  257. $validate = new \think\Validate([
  258. 'realname' => 'require|max:50',
  259. 'tel' => 'require|regex:/^1[3-9]\d{9}$/',
  260. 'email' => 'require|email',
  261. 'content' => 'require|max:1000'
  262. ]);
  263. if (!$validate->check($data)) {
  264. return json(['code' => 0, 'msg' => $validate->getError()]);
  265. }
  266. // 构建邮件内容
  267. // $html = "<p><strong>".__lang('Full Name').":</strong>".htmlspecialchars($data['realname'])."</p>
  268. // <p><strong>".__lang('Email').":</strong>".htmlspecialchars($data['email'])."</p>
  269. // <p><strong>".__lang('Phone').":</strong>".htmlspecialchars($data['tel'])."</p>
  270. // <p><strong>".__lang('Content').":</strong>".htmlspecialchars($data['content'])."</p>";
  271. // 获取配置
  272. // $config = get_addon_config("wwh");
  273. // $receive = $config['receive'] ?? '';
  274. // if (empty($receive)) {
  275. // return json(['code' => 0, 'msg' => __lang('Message recipient email not configured')]);
  276. // }
  277. // 发送邮件
  278. // $emailResult = \app\common\library\Email::instance()
  279. // ->to($receive)
  280. // ->subject(__lang('Customer message'))
  281. // ->message($html)
  282. // ->send();
  283. $db = Db::name('wwh_message')->insert($data);
  284. return json(['code' => 1, 'msg' => __lang('Submitted Successfully')]);
  285. // 数据库操作
  286. // if ($emailResult) {
  287. // $db = Db::name('wwh_message')->insert($data);
  288. // return json(['code' => 1, 'msg' => __lang('Submitted Successfully')]);
  289. // }
  290. return json(['code' => 0, 'msg' => __lang('Failed to send email, Please check the backend email configuration')]);
  291. }
  292. /**
  293. * 验证码
  294. */
  295. public function verify()
  296. {
  297. $captcha = new \think\captcha\Captcha();
  298. //验证码过期时间(s)
  299. $captcha->expire =1800;
  300. //验证码位数
  301. $captcha->length = 4;
  302. //验证成功后是否重置
  303. $captcha->reset = true;
  304. return $captcha->entry();
  305. }
  306. /**
  307. * 设置语言
  308. */
  309. public function switchLang()
  310. {
  311. $url = (string)input('param.url');
  312. $lang = (int)input('param.lang');
  313. // 验证 lang 参数
  314. if (!$lang) {
  315. $this->error('lang 不能为空');
  316. }
  317. // 验证 URL 参数
  318. if (empty($url)) {
  319. $this->error('URL 参数不能为空');
  320. }
  321. try {
  322. $decodedUrl = base64_decode($url, true);
  323. if (!$decodedUrl) {
  324. $this->error('URL 解码失败,请稍后重试');
  325. }
  326. } catch (\Exception $e) {
  327. $this->error('URL 解码失败,请稍后重试');
  328. }
  329. // 设置语言 cookie
  330. cookie('lang', $lang, 3600 * 24 * 30);
  331. // 重定向到目标 URL
  332. $this->redirect($decodedUrl);
  333. }
  334. }