TemplateLibraryApiController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. /**
  3. * 模版站api
  4. * @copyright 2021-浙江引擎力营销策划有限公司
  5. * @author Lc<sunshinecc1@163.com>
  6. * @since 2021-08-23
  7. */
  8. namespace App\Http\Controllers\Wap\TemplateLibraryApi;
  9. use App\Http\Controllers\Controller;
  10. use App\Http\Models\Site;
  11. use App\Http\Services\TemplateLibraryApiService;
  12. use Illuminate\Http\Request;
  13. use Illuminate\http\JsonResponse;
  14. use Illuminate\Support\Facades\DB;
  15. use itbdw\Ip\IpLocation;
  16. class TemplateLibraryApiController extends Controller
  17. {
  18. private $templateLibraryApiService;
  19. /**
  20. * 模版服务类
  21. * TemplateLibraryApiController constructor.
  22. * @param TemplateLibraryApiService $templateLibraryApiService
  23. */
  24. public function __construct(TemplateLibraryApiService $templateLibraryApiService)
  25. {
  26. $this->templateLibraryApiService = $templateLibraryApiService;
  27. }
  28. /**
  29. * 获取所有页面的关联模版以及变量(弃用,量大拖速度)
  30. * @param Request $request
  31. * @return JsonResponse
  32. */
  33. public function getSitesTemplateList(Request $request)
  34. {
  35. try {
  36. $result = $request->all();
  37. if (!empty($result['siteId'])) {
  38. [$siteInfo, $setting] = $this->templateLibraryApiService->getSettingList($result['siteId']);
  39. return $this->returnApiSuccess($this->templateLibraryApiService->getContentList($result['siteId']), $siteInfo, $setting);
  40. }
  41. return $this->returnApiError();
  42. } catch (\Exception $exception) {
  43. return $this->returnApiError($exception->getMessage());
  44. }
  45. }
  46. /**
  47. * 获取所有页面内容
  48. * @param Request $request
  49. * @return JsonResponse
  50. */
  51. public function getWebsitePageList(Request $request)
  52. {
  53. try {
  54. $result = $request->all();
  55. if (!empty($result['siteId'])) {
  56. [$siteInfo, $setting] = $this->templateLibraryApiService->getSettingList($result['siteId']);
  57. $advertiseList = $this->templateLibraryApiService->getAdvertiseList($result['siteId']);
  58. $result = $this->templateLibraryApiService->getWebsitePageList($result['siteId'], $result['tplId'] ?? 0, $result['notTplId'] ?? 0);
  59. return $this->returnApiSuccess($result, $siteInfo, $setting, $advertiseList);
  60. }
  61. return $this->returnApiError();
  62. } catch (\Exception $exception) {
  63. return $this->returnApiError($exception->getMessage());
  64. }
  65. }
  66. /**
  67. * 根据页面id获取详情
  68. * @param Request $request
  69. * @return JsonResponse
  70. */
  71. public function getWebsitePageDetailsByUri(Request $request)
  72. {
  73. try {
  74. $result = $request->all();
  75. if (((!empty($result['siteId']) && !empty($result['pageId']))) || ((!empty($result['siteId']) && !empty($result['uri'])))) {
  76. [$siteInfo, $setting] = $this->templateLibraryApiService->getSettingList($result['siteId']);
  77. return $this->returnApiSuccess($this->templateLibraryApiService->getWebsitePageDetailsByUri($result['siteId'], $result['pageId'] ?? 0, $result['uri'] ?? ''), $siteInfo, $setting);
  78. }
  79. return $this->returnApiError();
  80. } catch (\Exception $exception) {
  81. return $this->returnApiError($exception->getMessage());
  82. }
  83. }
  84. /**
  85. * 筛选父节点下面的子页面列表
  86. * @param Request $request
  87. * @return JsonResponse
  88. */
  89. public function getChildWebsitePageListByPageId(Request $request)
  90. {
  91. try {
  92. $result = $request->all();
  93. if (!empty($result['siteId'])) {
  94. [$siteInfo, $setting] = $this->templateLibraryApiService->getSettingList($result['siteId']);
  95. $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);
  96. return $this->returnApiSuccess($result, $siteInfo, $setting);
  97. }
  98. return $this->returnApiError();
  99. } catch (\Exception $exception) {
  100. return $this->returnApiError($exception->getMessage());
  101. }
  102. }
  103. /**
  104. * 页面关键词查询
  105. * @param Request $request
  106. * @return JsonResponse
  107. */
  108. public function search(Request $request)
  109. {
  110. $result = $request->all();
  111. try {
  112. if (!empty($result['siteId']) && !empty($result['keyword'])) {
  113. [$siteInfo, $setting] = $this->templateLibraryApiService->getSettingList($result['siteId']);
  114. $result = $this->templateLibraryApiService->search($result['siteId'], $result['keyword'] ?? '', $result['pageSize'] ?? TABLE_PAGE_SIZE, $result['tplId'] ?? 0, $result['notTplId'] ?? 0);
  115. return $this->returnApiSuccess($result, $siteInfo, $setting);
  116. }
  117. return $this->returnApiError();
  118. } catch (\Exception $exception) {
  119. return $this->returnApiError($exception->getMessage());
  120. }
  121. }
  122. /**
  123. * 表单提交(弃用)
  124. * @param Request $request
  125. * @return JsonResponse
  126. */
  127. public function formSubmission(Request $request)
  128. {
  129. $request = $request->all();
  130. if (empty($request['siteId'])
  131. && empty($request['name'])
  132. && empty($request['email']
  133. && empty($request['phone']
  134. && empty($request['sign'])))) {
  135. return $this->returnApiError(['info' => '缺少必要参数']);
  136. }
  137. $pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/";
  138. if (!preg_match($pattern, $request['email'])) {
  139. return $this->returnApiError(['info' => '邮箱不匹配']);
  140. }
  141. $sign = strtolower(md5($request['siteId'] . $request['name'] . $request['email'] . $request['phone']));
  142. if ($request['sign'] == $sign) {
  143. $this->templateLibraryApiService->formSubmission($request);
  144. return $this->returnApiSuccess([
  145. 'info' => '提交成功',
  146. ]);
  147. }
  148. return $this->returnApiError([
  149. 'info' => '无效的签名',
  150. ]);
  151. }
  152. /**
  153. * 表单提交
  154. * @param Request $request
  155. * @return array|JsonResponse
  156. */
  157. public function formSubmission1(Request $request)
  158. {
  159. try {
  160. $formData = $request->all();
  161. $clientURL = $formData['url'] ?? '';
  162. $data = [
  163. 'user_id' => '1',
  164. 'notice' => '询盘',
  165. 'url' => $clientURL ?? '',
  166. 'request' => serialize($formData),
  167. 'cookie' => serialize($request->cookie()),
  168. 'client_ip' => ip2long($request->ip()),
  169. ];
  170. $website = DB::connection('template')
  171. ->table('template_library_project_config')
  172. ->where('site_id', $formData['siteId'])->value('cn_title') ?? '';
  173. $connection = DB::connection($this->templateLibraryApiService->connection($formData['siteId']));
  174. $connection->table('user_log')->insert($data);
  175. if (empty($formData['name'])) {
  176. return $this->returnApiError([
  177. 'info' => 'Please write your name',
  178. ]);
  179. }
  180. if (empty($formData['email'])) {
  181. return $this->returnApiError([
  182. 'info' => 'Please write your the e-mail',
  183. ]);
  184. }
  185. $pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/";
  186. if (!preg_match($pattern, $formData['email'])) {
  187. return $this->returnApiError([
  188. 'info' => 'Please write your the e-mail',
  189. ]);
  190. }
  191. if ($request->hasFile('files')) {
  192. foreach ($request->file('files') as $key => $file) {
  193. $fileUrl = $file->storeAs(date('Ymd') . '/' . $file->getClientOriginalExtension(), $file->getClientOriginalName(), 'public');
  194. $fileUrl = sprintf('/storage/%s', $fileUrl);
  195. $formData['attach'][$key] = '<a href="' . $fileUrl . '" target="_blank">' . $file->getClientOriginalName() . '</a>';
  196. }
  197. }
  198. $data = [
  199. '姓名' => $formData['name'] ?? '-',
  200. '邮箱' => $formData['email'] ?? '-',
  201. '电话' => $formData['phone'] ?? '-',
  202. '国家' => $formData['country'] ?? '-'
  203. ];
  204. if (!empty($formData['attach'])) {
  205. $data['附件'] = implode(', ', $formData['attach']);
  206. }
  207. $content = '';
  208. if (!empty($formData['content'])) {
  209. $content = $formData['content'];
  210. }
  211. $country = IpLocation::getLocation($request->ip());
  212. if (!empty($country)) {
  213. $country = $country['country'] ?? '' . $country['province'] ?? '' . $country['city'] ?? '';
  214. } else {
  215. $country = '';
  216. }
  217. $html = '<div class="sep-email" style="max-width:960px; color:#333; margin: auto; background: #f9f9f9; line-height: 1.8em; font-size: 14px; font-family: \'Lucida Grande\', \'Microsoft Yahei\';"><table align="center" bgcolor="#373d41" border="0" cellpadding="0" cellspacing="0" height="48" style="width:100%;"><tbody><tr><td align="center" border="0" height="48" style="padding-left:8px;height:48px;" valign="middle" width="74"><a href="https://www.yinqingli.com/" target="_blank" rel="noopener"><img border="0" height="20" src="https://www.yinqingli.com/uploads/image/logo2.png" ></a></td><td align="right" colspan="2" height="48" style="color:#ffffff; padding-right:12px;height:48px;" valign="middle" width="703"> <a href="javascript:;" style="color:#ffffff;text-decoration:none;font-family:\'Microsoft YaHei\';font-size: 12px;" target="_blank" rel="noopener">上市公司海外营销第一选择!</a>&nbsp; &nbsp; &nbsp; &nbsp; <a href="https://www.yinqingli.com/" style="color:#ffffff;text-decoration:none;font-family:\'Microsoft YaHei\';font-size: 12px;" target="_blank" rel="noopener">访问官网</a></td></tr> </tbody></table>';
  218. $html .= '<div style="position: relative; padding: 20px 30px 50px 30px;"><b>尊敬的 ' . $formData['email'] . ' 用户:</b><div style="color:#555; border-bottom: solid 1px #ccc; padding-bottom: 10px; margin-bottom: 25px;"><p style="text-indent: 2em;">您好!</p><p style="text-indent: 2em;">您的网站 ' . $website . ' 于 ' . date('Y-m-d H:i:s') . ' 收到一封来自 IP:' . $request->ip() . ' (' . $country . ') 的询盘消息,请及时处理。详细信息如下:</p></div>';
  219. $html .= '<table style="line-height:2em;color:#333;font-size:14px;width:90%"><tr>';
  220. $i = 0;
  221. foreach ($data as $key => $val) {
  222. if ($i > 0 && $i % 2 == 0) {
  223. $html .= '</tr><tr>';
  224. }
  225. $html .= '<td style="width:100px;text-align:right">' . $key . ':</td>';
  226. $html .= '<td style="border-bottom:solid 1px #ccc;color:#666;">' . $val . '</td>';
  227. $i++;
  228. }
  229. if (!empty($formData['ext'])) {
  230. foreach ($formData['ext'] as $key => $ext) {
  231. $html .= '<tr>';
  232. $html .= '<td style="width:100px;text-align:right">' . $ext['name'] . ':</td>';
  233. $html .= '<td colspan="3" style="border-bottom:solid 1px #ccc; color:#666;">' . $ext['value'] . '</td>';
  234. $html .= '</tr>';
  235. }
  236. }
  237. $html .= '</tr><tr><td style="text-align:right">留言:</td>';
  238. $html .= '<td colspan="3" style="border-bottom:solid 1px #ccc;color:#666;">';
  239. $html .= '<div style="line-height:1.8em;padding:2px 0;">' . $content . '</div></td></tr>';
  240. $html .= '<tr><td style="text-align:right">URL:</td>';
  241. $html .= '<td colspan="3" style="border-bottom: solid 1px #ccc; color:#666;">';
  242. $html .= '<div style="line-height:1.8em;padding: 2px 0;"><a href="' . $clientURL . '">' . $clientURL . '</a></div></td></tr>';
  243. $html .= '</table></div><div style="padding: 20px 30px; background: #e8e8e8; position: relative; font-size: 13px;"><div><strong style="color: #f60">温馨提示:</strong>此邮件由系统自动发出,请勿直接回复!<br />如有疑问或建议,可发送邮件至 <u>hina@yinqingli.com</u>,或致电400-865-6067.<br />为了您能够正常收到来自引擎力云平台的询盘邮件提醒,请将 <u>service@googleseo.com.cn</u> 添加进您的 <b>域白名单。</b><br /></div></div></div>';
  244. $data = [
  245. 'sender_name' => $request['name'],
  246. 'sender_email' => $request['email'],
  247. 'content' => $html,
  248. 'user_id' => 0,
  249. 'sender_uid' => 0,
  250. 'title' => '',
  251. 'client_ip' => ip2long($request->ip()),
  252. 'is_read' => 0,
  253. 'is_delete' => 0,
  254. 'mail_time' => 0,
  255. 'mail_response' => 0,
  256. 'create_time' => time(),
  257. ];
  258. $connection->table('user_msg')->insert($data);
  259. return $this->returnApiSuccess([
  260. 'info' => '提交成功',
  261. ]);
  262. } catch (\Throwable $exception) {
  263. return $this->returnApiError([
  264. 'info' => '提交失败,服务器遇到了未知的错误~[' . $exception->getMessage() . ']',
  265. ]);
  266. }
  267. }
  268. /**
  269. * 引擎力案例短信验证
  270. * @param Request $request
  271. * @return JsonResponse
  272. */
  273. public function getVerifyResultBySiteId(Request $request)
  274. {
  275. $siteId = $request->input('siteId');
  276. $code = $request->input('code');
  277. if (empty($siteId) || empty($code)) {
  278. return $this->returnApiError([
  279. 'info' => '缺少必要参数',
  280. ]);
  281. }
  282. $siteInfo = Site::query()->where('id', $siteId)->first();
  283. $time = strtotime(date('Y-m-d H:i:s')) - strtotime($siteInfo->create_time_code);
  284. if ($code == $siteInfo->code && $time < 300) {
  285. return $this->returnApiSuccess([
  286. 'info' => '验证成功,有效期还剩' . (300 - $time) . '秒',
  287. ]);
  288. } else {
  289. return $this->returnApiError([
  290. 'info' => '验证失败,验证码错误或过期',
  291. ]);
  292. }
  293. }
  294. }