template.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. .template-thumbnail {
  162. position: relative;
  163. overflow: hidden;
  164. }
  165. .template-thumbnail img,
  166. .template-thumbnail video {
  167. width: 100%;
  168. height: auto;
  169. cursor: pointer;
  170. }
  171. /* 放大查看容器 */
  172. #largeViewContainer {
  173. display: none;
  174. position: fixed;
  175. top: 0;
  176. left: 0;
  177. width: 100vw;
  178. height: 100vh;
  179. background-color: rgba(0, 0, 0, 0.9);
  180. z-index: 9999;
  181. justify-content: center;
  182. align-items: center;
  183. overflow: hidden;
  184. }
  185. #largeViewContainer img,
  186. #largeViewContainer video {
  187. max-width: 90vw;
  188. max-height: 90vh;
  189. object-fit: contain;
  190. }
  191. /* 背景遮罩 */
  192. .overlay {
  193. display: none;
  194. position: fixed;
  195. top: 0;
  196. left: 0;
  197. width: 100%;
  198. height: 100%;
  199. background-color: rgba(0, 0, 0, 0.7);
  200. z-index: 9998;
  201. }
  202. </style>
  203. </head>
  204. <body>
  205. <section class="template-section">
  206. <!-- 搜索框 -->
  207. <div class="search-container">
  208. <div class="search-box">
  209. <input type="text" id="keywordInput" placeholder="搜索" value="{$keyword|default=''}">
  210. <input type="hidden" id="typeFilter" value="{$type|default=''}">
  211. <button id="searchBtn" type="button">查询</button>
  212. </div>
  213. </div>
  214. <div class="template-header">
  215. <h2 class="template-title">全部模板</h2>
  216. <div class="filter-options">
  217. <div class="filter-group">
  218. <label>风格:</label>
  219. <span {if condition="empty($style)"}style="background-color: #e9ecef;"{/if}>全部</span>
  220. <span>动漫</span>
  221. <span>油画</span>
  222. <span>风景</span>
  223. <span>人像</span>
  224. </div>
  225. <div class="filter-group">
  226. <label>模版类型:</label>
  227. <span id="typeAll" {if condition="empty($type)"}style="background-color: #e9ecef; color: #007bff;"{else}style="color: #333;"{/if} onclick="filterByType('')">全部</span>
  228. <span id="typeImage" {if condition="$type eq '文生图'"}style="background-color: #e9ecef; color: #007bff;"{else}style="color: #333;"{/if} onclick="filterByType('文生图')">图片</span>
  229. <span id="typeVideo" {if condition="$type eq '文生视频'"}style="background-color: #e9ecef; color: #007bff;"{else}style="color: #333;"{/if} onclick="filterByType('文生视频')">视频</span>
  230. </div>
  231. </div>
  232. </div>
  233. <div class="template-grid" id="templateGrid">
  234. <!-- 使用volist标签循环显示模板 -->
  235. {volist name="products" id="template"}
  236. <div class="template-card">
  237. <div class="template-thumbnail">
  238. {if condition="$template.type eq '文生图'"}
  239. <img src="{$template.template_image_url}" alt="{$template.template_name}" onclick="showMedia('{$template.template_image_url}', '{$template.type}')" onerror="this.src=''; this.alt='图片加载失败';">
  240. {elseif condition="$template.type eq '文生视频'"}
  241. <video src="{$template.template_image_url}" alt="{$template.template_name}" controls muted onclick="showMedia('{$template.template_image_url}', '{$template.type}')"></video>
  242. {/if}
  243. </div>
  244. <div class="template-info">
  245. <div class="template-name">{$template.template_name}</div>
  246. <div class="template-meta">
  247. <span>{if condition="$template.type eq '文生图'"}图片{else}视频{/if}</span>
  248. <span>免费</span>
  249. </div>
  250. </div>
  251. </div>
  252. {/volist}
  253. </div>
  254. </section>
  255. <!-- 大图查看容器 -->
  256. <div id="largeViewContainer">
  257. <div id="largeMediaContent"></div>
  258. </div>
  259. <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  260. <script>
  261. // 初始化变量
  262. let currentKeyword = '{$keyword|default=\'\'}';
  263. let currentType = '{$type|default=\'\'}';
  264. // 搜索函数
  265. function searchTemplates() {
  266. // 获取当前的关键词和类型
  267. const keyword = $('#keywordInput').val().trim();
  268. const type = $('#typeFilter').val();
  269. // 如果参数没有变化,不发送请求
  270. if (keyword === currentKeyword && type === currentType) {
  271. return;
  272. }
  273. // 更新当前参数
  274. currentKeyword = keyword;
  275. currentType = type;
  276. // 发送AJAX请求
  277. $.ajax({
  278. url: '{:url("index/user/template")}',
  279. type: 'GET',
  280. data: {
  281. keyword: keyword,
  282. type: type
  283. },
  284. dataType: 'json',
  285. success: function(response) {
  286. if (response.code === 0 && response.data) {
  287. // 更新模板列表
  288. updateTemplateGrid(response.data);
  289. } else {
  290. console.error('获取模板数据失败:', response.msg);
  291. }
  292. },
  293. error: function(xhr, status, error) {
  294. console.error('AJAX请求失败:', error);
  295. }
  296. });
  297. }
  298. // 更新模板网格
  299. function updateTemplateGrid(templates) {
  300. const templateGrid = $('#templateGrid');
  301. templateGrid.empty(); // 清空现有内容
  302. // 如果没有模板,显示提示
  303. if (templates.length === 0) {
  304. templateGrid.append('<div style="grid-column: 1 / -1; text-align: center; padding: 50px; color: #666;">暂无匹配的模板</div>');
  305. return;
  306. }
  307. // 循环生成模板卡片
  308. $.each(templates, function(index, template) {
  309. let mediaElement;
  310. // 根据类型创建媒体元素
  311. if (template.type === '文生图') {
  312. mediaElement = `<img src="${template.template_image_url}" alt="${template.template_name}" onclick="showMedia('${template.template_image_url}', '${template.type}')" onerror="this.src=''; this.alt='图片加载失败';">`;
  313. } else if (template.type === '文生视频') {
  314. mediaElement = `<video src="${template.template_image_url}" alt="${template.template_name}" controls muted onclick="showMedia('${template.template_image_url}', '${template.type}')"></video>`;
  315. }
  316. // 创建模板卡片
  317. const templateCard = `
  318. <div class="template-card">
  319. <div class="template-thumbnail">
  320. ${mediaElement}
  321. </div>
  322. <div class="template-info">
  323. <div class="template-name">${template.template_name}</div>
  324. <div class="template-meta">
  325. <span>${template.type === '文生图' ? '图片' : '视频'}</span>
  326. <span>免费</span>
  327. </div>
  328. </div>
  329. </div>
  330. `;
  331. templateGrid.append(templateCard);
  332. });
  333. }
  334. // 类型筛选函数
  335. function filterByType(type) {
  336. // 更新类型筛选器的值
  337. $('#typeFilter').val(type);
  338. // 更新类型筛选按钮的样式
  339. $('#typeAll, #typeImage, #typeVideo').css({'background-color': '', 'color': '#333'});
  340. if (type === '') {
  341. $('#typeAll').css({'background-color': '#e9ecef', 'color': '#007bff'});
  342. } else if (type === '文生图') {
  343. $('#typeImage').css({'background-color': '#e9ecef', 'color': '#007bff'});
  344. } else if (type === '文生视频') {
  345. $('#typeVideo').css({'background-color': '#e9ecef', 'color': '#007bff'});
  346. }
  347. // 执行搜索
  348. searchTemplates();
  349. }
  350. // 搜索按钮点击事件
  351. $('#searchBtn').click(function() {
  352. searchTemplates();
  353. });
  354. // 搜索框回车事件
  355. $('#keywordInput').keypress(function(e) {
  356. if (e.key === 'Enter') {
  357. searchTemplates();
  358. }
  359. });
  360. // 搜索框输入事件(可选:实现实时搜索)
  361. let searchTimer;
  362. $('#keywordInput').on('input', function() {
  363. clearTimeout(searchTimer);
  364. // 延迟300ms执行搜索,避免频繁请求
  365. searchTimer = setTimeout(searchTemplates, 300);
  366. });
  367. // 图片/视频放大查看功能
  368. function showMedia(url, type) {
  369. const container = document.getElementById('largeViewContainer');
  370. const content = document.getElementById('largeMediaContent');
  371. // 清空容器
  372. content.innerHTML = '';
  373. // 创建媒体元素
  374. if (type === '文生图') {
  375. const img = document.createElement('img');
  376. img.src = url;
  377. img.alt = '大图查看';
  378. content.appendChild(img);
  379. } else if (type === '文生视频') {
  380. const video = document.createElement('video');
  381. video.src = url;
  382. video.controls = true;
  383. content.appendChild(video);
  384. }
  385. // 显示大图查看容器
  386. container.style.display = 'flex';
  387. }
  388. // 关闭大图查看
  389. function closeLargeView() {
  390. document.getElementById('largeViewContainer').style.display = 'none';
  391. }
  392. // 点击大图容器关闭
  393. document.getElementById('largeViewContainer').addEventListener('click', function(event) {
  394. // 只有点击容器本身才关闭,避免点击媒体元素也关闭
  395. if (event.target === this) {
  396. closeLargeView();
  397. }
  398. });
  399. // 按ESC键关闭大图查看
  400. document.addEventListener('keydown', function(event) {
  401. if (event.key === 'Escape') {
  402. closeLargeView();
  403. }
  404. });
  405. </script>
  406. </body>
  407. </html>