template.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <!doctype html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>模板</title>
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. }
  14. /* 搜索框样式 */
  15. .search-container {
  16. border: 1px solid #333;
  17. padding: 20px;
  18. border-radius: 8px;
  19. margin-bottom: 30px;
  20. }
  21. .search-box {
  22. position: relative;
  23. max-width: 100%;
  24. }
  25. .search-box input {
  26. width: 100%;
  27. padding: 12px 40px 12px 20px;
  28. border: 1px solid #ddd;
  29. border-radius: 4px;
  30. font-size: 14px;
  31. }
  32. .search-box button {
  33. position: absolute;
  34. right: 10px;
  35. top: 50%;
  36. transform: translateY(-50%);
  37. background: none;
  38. border: none;
  39. cursor: pointer;
  40. font-size: 16px;
  41. color: #666;
  42. }
  43. /* 模板区域样式 */
  44. .template-section {
  45. max-width: 1200px;
  46. margin: 0 auto;
  47. padding: 30px 20px;
  48. }
  49. .template-header {
  50. display: flex;
  51. justify-content: flex-start;
  52. align-items: flex-start;
  53. margin-bottom: 25px;
  54. flex-direction: column;
  55. }
  56. .filter-options {
  57. display: flex;
  58. margin-top: 15px;
  59. flex-direction: column;
  60. width: 100%;
  61. }
  62. .template-title {
  63. font-size: 18px;
  64. font-weight: bold;
  65. }
  66. .filter-group {
  67. display: flex;
  68. gap: 10px;
  69. flex-direction: row;
  70. align-items: center;
  71. padding: 10px;
  72. background-color: #f8f9fa;
  73. border-radius: 8px;
  74. width: fit-content;
  75. margin-bottom: 10px;
  76. }
  77. .filter-group label {
  78. font-size: 14px;
  79. color: #333;
  80. font-weight: bold;
  81. }
  82. .filter-group span {
  83. font-size: 14px;
  84. color: #333;
  85. cursor: pointer;
  86. padding: 5px 10px;
  87. border-radius: 4px;
  88. transition: background-color 0.2s, color 0.2s;
  89. }
  90. .filter-group span:hover {
  91. background-color: #e9ecef;
  92. }
  93. .template-grid {
  94. display: grid;
  95. grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  96. gap: 15px;
  97. }
  98. .template-card {
  99. background-color: #fff;
  100. border-radius: 8px;
  101. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  102. overflow: hidden;
  103. transition: transform 0.2s;
  104. }
  105. .template-card:hover {
  106. transform: translateY(-3px);
  107. box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  108. }
  109. .template-thumbnail {
  110. width: 100%;
  111. height: 120px;
  112. background-color: #f0f0f0;
  113. display: flex;
  114. align-items: center;
  115. justify-content: center;
  116. font-size: 12px;
  117. color: #666;
  118. overflow: hidden;
  119. }
  120. /* 图片和视频样式 */
  121. .template-thumbnail img,
  122. .template-thumbnail video {
  123. width: 100%;
  124. height: 100%;
  125. object-fit: cover;
  126. cursor: pointer;
  127. }
  128. /* 响应式图片 */
  129. img {
  130. max-width: 100%;
  131. height: auto;
  132. }
  133. .template-info {
  134. padding: 12px;
  135. }
  136. .template-name {
  137. font-size: 14px;
  138. font-weight: bold;
  139. margin-bottom: 6px;
  140. }
  141. .template-tags {
  142. display: flex;
  143. flex-wrap: wrap;
  144. gap: 5px;
  145. margin-bottom: 10px;
  146. }
  147. .template-tag {
  148. font-size: 12px;
  149. background-color: #e9ecef;
  150. color: #666;
  151. padding: 2px 8px;
  152. border-radius: 4px;
  153. }
  154. .template-meta {
  155. display: flex;
  156. justify-content: space-between;
  157. font-size: 12px;
  158. color: #999;
  159. }
  160. /* 放大模态框样式 */
  161. .modal {
  162. display: none;
  163. position: fixed;
  164. z-index: 9999;
  165. left: 0;
  166. top: 0;
  167. width: 100%;
  168. height: 100%;
  169. background-color: rgba(0, 0, 0, 0.8);
  170. overflow: auto;
  171. }
  172. .modal-content {
  173. position: relative;
  174. margin: 5% auto;
  175. max-width: 90%;
  176. max-height: 90%;
  177. }
  178. .modal-content img,
  179. .modal-content video {
  180. width: 100%;
  181. height: 100%;
  182. object-fit: contain;
  183. }
  184. .close-modal {
  185. position: absolute;
  186. top: -40px;
  187. right: 0;
  188. color: white;
  189. font-size: 35px;
  190. font-weight: bold;
  191. cursor: pointer;
  192. }
  193. </style>
  194. </head>
  195. <body>
  196. <section class="template-section">
  197. <!-- 搜索框 -->
  198. <div class="search-container">
  199. <div class="search-box">
  200. <input type="text" id="keywordInput" placeholder="搜索" value="{$keyword|default=''}">
  201. <input type="hidden" id="typeFilter" value="{$type|default=''}">
  202. <button id="searchBtn" type="button">查询</button>
  203. </div>
  204. </div>
  205. <div class="template-header">
  206. <h2 class="template-title">全部模板</h2>
  207. <div class="filter-options">
  208. <div class="filter-group">
  209. <label>风格:</label>
  210. <span {if condition="empty($style)"}style="background-color: #e9ecef;"{/if}>全部</span>
  211. <span>动漫</span>
  212. <span>油画</span>
  213. <span>风景</span>
  214. <span>人像</span>
  215. </div>
  216. <div class="filter-group">
  217. <label>模版类型:</label>
  218. <span id="typeAll" {if condition="empty($type)"}style="background-color: #e9ecef; color: #007bff;"{else}style="color: #333;"{/if} onclick="filterByType('')">全部</span>
  219. <span id="typeImage" {if condition="$type eq '文生图'"}style="background-color: #e9ecef; color: #007bff;"{else}style="color: #333;"{/if} onclick="filterByType('文生图')">图片</span>
  220. <span id="typeVideo" {if condition="$type eq '文生视频'"}style="background-color: #e9ecef; color: #007bff;"{else}style="color: #333;"{/if} onclick="filterByType('文生视频')">视频</span>
  221. </div>
  222. </div>
  223. </div>
  224. <div class="template-grid" id="templateGrid">
  225. <!-- 使用volist标签循环显示模板 -->
  226. {volist name="products" id="template"}
  227. <div class="template-card">
  228. <div class="template-thumbnail">
  229. {if condition="$template.type eq '文生图'"}
  230. <img src="{$template.template_image_url}" alt="{$template.template_name}" onclick="showMedia('{$template.template_image_url}', '{$template.type}')" onerror="this.src=''; this.alt='图片加载失败';">
  231. {elseif condition="$template.type eq '文生视频'"}
  232. <video src="{$template.template_image_url}" alt="{$template.template_name}" controls muted onclick="showMedia('{$template.template_image_url}', '{$template.type}')"></video>
  233. {/if}
  234. </div>
  235. <div class="template-info">
  236. <div class="template-name">{$template.template_name}</div>
  237. <div class="template-meta">
  238. <span>{if condition="$template.type eq '文生图'"}图片{else}视频{/if}</span>
  239. <span>免费</span>
  240. </div>
  241. </div>
  242. </div>
  243. {/volist}
  244. </div>
  245. </section>
  246. <!-- 放大模态框 -->
  247. <div id="mediaModal" class="modal">
  248. <span class="close-modal" onclick="closeModal()">&times;</span>
  249. <div class="modal-content">
  250. <div id="modalMediaContainer"></div>
  251. </div>
  252. </div>
  253. <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  254. <script>
  255. // 初始化变量
  256. let currentKeyword = '{$keyword|default=\'\'}';
  257. let currentType = '{$type|default=\'\'}';
  258. // 搜索函数
  259. function searchTemplates() {
  260. // 获取当前的关键词和类型
  261. const keyword = $('#keywordInput').val().trim();
  262. const type = $('#typeFilter').val();
  263. // 如果参数没有变化,不发送请求
  264. if (keyword === currentKeyword && type === currentType) {
  265. return;
  266. }
  267. // 更新当前参数
  268. currentKeyword = keyword;
  269. currentType = type;
  270. // 发送AJAX请求
  271. $.ajax({
  272. url: '{:url("index/user/template")}',
  273. type: 'GET',
  274. data: {
  275. keyword: keyword,
  276. type: type
  277. },
  278. dataType: 'json',
  279. success: function(response) {
  280. if (response.code === 0 && response.data) {
  281. // 更新模板列表
  282. updateTemplateGrid(response.data);
  283. } else {
  284. console.error('获取模板数据失败:', response.msg);
  285. }
  286. },
  287. error: function(xhr, status, error) {
  288. console.error('AJAX请求失败:', error);
  289. }
  290. });
  291. }
  292. // 更新模板网格
  293. function updateTemplateGrid(templates) {
  294. const templateGrid = $('#templateGrid');
  295. templateGrid.empty(); // 清空现有内容
  296. // 如果没有模板,显示提示
  297. if (templates.length === 0) {
  298. templateGrid.append('<div style="grid-column: 1 / -1; text-align: center; padding: 50px; color: #666;">暂无匹配的模板</div>');
  299. return;
  300. }
  301. // 循环生成模板卡片
  302. $.each(templates, function(index, template) {
  303. let mediaElement;
  304. // 根据类型创建媒体元素
  305. if (template.type === '文生图') {
  306. mediaElement = `<img src="${template.template_image_url}" alt="${template.template_name}" onclick="showMedia('${template.template_image_url}', '${template.type}')" onerror="this.src=''; this.alt='图片加载失败';">`;
  307. } else if (template.type === '文生视频') {
  308. mediaElement = `<video src="${template.template_image_url}" alt="${template.template_name}" controls muted onclick="showMedia('${template.template_image_url}', '${template.type}')"></video>`;
  309. }
  310. // 创建模板卡片
  311. const templateCard = `
  312. <div class="template-card">
  313. <div class="template-thumbnail">
  314. ${mediaElement}
  315. </div>
  316. <div class="template-info">
  317. <div class="template-name">${template.template_name}</div>
  318. <div class="template-meta">
  319. <span>${template.type === '文生图' ? '图片' : '视频'}</span>
  320. <span>免费</span>
  321. </div>
  322. </div>
  323. </div>
  324. `;
  325. templateGrid.append(templateCard);
  326. });
  327. }
  328. // 类型筛选函数
  329. function filterByType(type) {
  330. // 更新类型筛选器的值
  331. $('#typeFilter').val(type);
  332. // 更新类型筛选按钮的样式
  333. $('#typeAll, #typeImage, #typeVideo').css({'background-color': '', 'color': '#333'});
  334. if (type === '') {
  335. $('#typeAll').css({'background-color': '#e9ecef', 'color': '#007bff'});
  336. } else if (type === '文生图') {
  337. $('#typeImage').css({'background-color': '#e9ecef', 'color': '#007bff'});
  338. } else if (type === '文生视频') {
  339. $('#typeVideo').css({'background-color': '#e9ecef', 'color': '#007bff'});
  340. }
  341. // 执行搜索
  342. searchTemplates();
  343. }
  344. // 搜索按钮点击事件
  345. $('#searchBtn').click(function() {
  346. searchTemplates();
  347. });
  348. // 搜索框回车事件
  349. $('#keywordInput').keypress(function(e) {
  350. if (e.key === 'Enter') {
  351. searchTemplates();
  352. }
  353. });
  354. // 搜索框输入事件(可选:实现实时搜索)
  355. let searchTimer;
  356. $('#keywordInput').on('input', function() {
  357. clearTimeout(searchTimer);
  358. // 延迟300ms执行搜索,避免频繁请求
  359. searchTimer = setTimeout(searchTemplates, 300);
  360. });
  361. // 显示媒体放大模态框
  362. function showMedia(url, type) {
  363. const modal = document.getElementById('mediaModal');
  364. const container = document.getElementById('modalMediaContainer');
  365. // 清空容器
  366. container.innerHTML = '';
  367. // 创建媒体元素
  368. if (type === '文生图') {
  369. const img = document.createElement('img');
  370. img.src = url;
  371. img.alt = '放大图片';
  372. container.appendChild(img);
  373. } else if (type === '文生视频') {
  374. const video = document.createElement('video');
  375. video.src = url;
  376. video.controls = true;
  377. container.appendChild(video);
  378. }
  379. // 显示模态框
  380. modal.style.display = 'block';
  381. }
  382. // 关闭模态框
  383. function closeModal() {
  384. const modal = document.getElementById('mediaModal');
  385. modal.style.display = 'none';
  386. }
  387. // 点击模态框外部关闭
  388. window.onclick = function(event) {
  389. const modal = document.getElementById('mediaModal');
  390. if (event.target === modal) {
  391. closeModal();
  392. }
  393. }
  394. </script>
  395. </body>
  396. </html>