* @since 2021-08-23 */ namespace App\Http\Controllers\Wap\TemplateLibraryApi; use App\Http\Controllers\Controller; use App\Http\Models\Site; use App\Http\Services\TemplateLibraryApiService; use Illuminate\Http\Request; use Illuminate\http\JsonResponse; use Illuminate\Support\Facades\DB; use itbdw\Ip\IpLocation; class TemplateLibraryApiController extends Controller { private $templateLibraryApiService; /** * 模版服务类 * TemplateLibraryApiController constructor. * @param TemplateLibraryApiService $templateLibraryApiService */ public function __construct(TemplateLibraryApiService $templateLibraryApiService) { $this->templateLibraryApiService = $templateLibraryApiService; } /** * 获取所有页面的关联模版以及变量(弃用,量大拖速度) * @param Request $request * @return JsonResponse */ public function getSitesTemplateList(Request $request) { try { $result = $request->all(); if (!empty($result['siteId'])) { [$siteInfo, $setting] = $this->templateLibraryApiService->getSettingList($result['siteId']); return $this->returnApiSuccess($this->templateLibraryApiService->getContentList($result['siteId']), $siteInfo, $setting); } return $this->returnApiError(); } catch (\Exception $exception) { return $this->returnApiError($exception->getMessage()); } } /** * 获取所有页面内容 * @param Request $request * @return JsonResponse */ public function getWebsitePageList(Request $request) { try { $result = $request->all(); if (!empty($result['siteId'])) { [$siteInfo, $setting] = $this->templateLibraryApiService->getSettingList($result['siteId']); $advertiseList = $this->templateLibraryApiService->getAdvertiseList($result['siteId']); $result = $this->templateLibraryApiService->getWebsitePageList($result['siteId'], $result['tplId'] ?? 0, $result['notTplId'] ?? 0); return $this->returnApiSuccess($result, $siteInfo, $setting, $advertiseList); } return $this->returnApiError(); } catch (\Exception $exception) { return $this->returnApiError($exception->getMessage()); } } /** * 根据页面id获取详情 * @param Request $request * @return JsonResponse */ public function getWebsitePageDetailsByUri(Request $request) { try { $result = $request->all(); if (((!empty($result['siteId']) && !empty($result['pageId']))) || ((!empty($result['siteId']) && !empty($result['uri'])))) { [$siteInfo, $setting] = $this->templateLibraryApiService->getSettingList($result['siteId']); return $this->returnApiSuccess($this->templateLibraryApiService->getWebsitePageDetailsByUri($result['siteId'], $result['pageId'] ?? 0, $result['uri'] ?? ''), $siteInfo, $setting); } return $this->returnApiError(); } catch (\Exception $exception) { return $this->returnApiError($exception->getMessage()); } } /** * 筛选父节点下面的子页面列表 * @param Request $request * @return JsonResponse */ public function getChildWebsitePageListByPageId(Request $request) { try { $result = $request->all(); if (!empty($result['siteId'])) { [$siteInfo, $setting] = $this->templateLibraryApiService->getSettingList($result['siteId']); $result = $this->templateLibraryApiService->getChildWebsitePageListByPageId($result['siteId'], $result['pid'] ?? 0, $result['tplId'] ?? 0, $result['notTplId'] ?? 0, $result['sortBy'] ?? false, $result['all'] ?? false, $result['pageSize'] ?? TABLE_PAGE_SIZE); return $this->returnApiSuccess($result, $siteInfo, $setting); } return $this->returnApiError(); } catch (\Exception $exception) { return $this->returnApiError($exception->getMessage()); } } /** * 页面关键词查询 * @param Request $request * @return JsonResponse */ public function search(Request $request) { $result = $request->all(); try { if (!empty($result['siteId']) && !empty($result['keyword'])) { [$siteInfo, $setting] = $this->templateLibraryApiService->getSettingList($result['siteId']); $result = $this->templateLibraryApiService->search($result['siteId'], $result['keyword'] ?? '', $result['pageSize'] ?? TABLE_PAGE_SIZE, $result['tplId'] ?? 0, $result['notTplId'] ?? 0); return $this->returnApiSuccess($result, $siteInfo, $setting); } return $this->returnApiError(); } catch (\Exception $exception) { return $this->returnApiError($exception->getMessage()); } } /** * 表单提交(弃用) * @param Request $request * @return JsonResponse */ public function formSubmission(Request $request) { $request = $request->all(); if (empty($request['siteId']) && empty($request['name']) && empty($request['email'] && empty($request['phone'] && empty($request['sign'])))) { return $this->returnApiError(['info' => '缺少必要参数']); } $pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/"; if (!preg_match($pattern, $request['email'])) { return $this->returnApiError(['info' => '邮箱不匹配']); } $sign = strtolower(md5($request['siteId'] . $request['name'] . $request['email'] . $request['phone'])); if ($request['sign'] == $sign) { $this->templateLibraryApiService->formSubmission($request); return $this->returnApiSuccess([ 'info' => '提交成功', ]); } return $this->returnApiError([ 'info' => '无效的签名', ]); } /** * 表单提交 * @param Request $request * @return array|JsonResponse */ public function formSubmission1(Request $request) { try { $formData = $request->all(); $clientURL = $formData['url'] ?? ''; $data = [ 'user_id' => '1', 'notice' => '询盘', 'url' => $clientURL ?? '', 'request' => serialize($formData), 'cookie' => serialize($request->cookie()), 'client_ip' => ip2long($request->ip()), ]; $website = DB::connection('template') ->table('template_library_project_config') ->where('site_id', $formData['siteId'])->value('cn_title') ?? ''; $connection = DB::connection($this->templateLibraryApiService->connection($formData['siteId'])); $connection->table('user_log')->insert($data); if (empty($formData['name'])) { return $this->returnApiError([ 'info' => 'Please write your name', ]); } if (empty($formData['email'])) { return $this->returnApiError([ 'info' => 'Please write your the e-mail', ]); } $pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/"; if (!preg_match($pattern, $formData['email'])) { return $this->returnApiError([ 'info' => 'Please write your the e-mail', ]); } if ($request->hasFile('files')) { foreach ($request->file('files') as $key => $file) { $fileUrl = $file->storeAs(date('Ymd') . '/' . $file->getClientOriginalExtension(), $file->getClientOriginalName(), 'public'); $fileUrl = sprintf('/storage/%s', $fileUrl); $formData['attach'][$key] = '' . $file->getClientOriginalName() . ''; } } $data = [ '姓名' => $formData['name'] ?? '-', '邮箱' => $formData['email'] ?? '-', '电话' => $formData['phone'] ?? '-', '国家' => $formData['country'] ?? '-' ]; if (!empty($formData['attach'])) { $data['附件'] = implode(', ', $formData['attach']); } $content = ''; if (!empty($formData['content'])) { $content = $formData['content']; } $country = IpLocation::getLocation($request->ip()); if (!empty($country)) { $country = $country['country'] ?? '' . $country['province'] ?? '' . $country['city'] ?? ''; } else { $country = ''; } $html = '
![]() | 上市公司海外营销第一选择! 访问官网 |
您好!
您的网站 ' . $website . ' 于 ' . date('Y-m-d H:i:s') . ' 收到一封来自 IP:' . $request->ip() . ' (' . $country . ') 的询盘消息,请及时处理。详细信息如下:
' . $key . ': | '; $html .= '' . $val . ' | '; $i++; } if (!empty($formData['ext'])) { foreach ($formData['ext'] as $key => $ext) { $html .= '||
' . $ext['name'] . ': | '; $html .= '' . $ext['value'] . ' | '; $html .= '||
留言: | '; $html .= '';
$html .= ' ' . $content . ' | ||
URL: | '; $html .= ''; $html .= ' |