Step_4.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. <script setup>
  2. import { ref, computed, onMounted } from "vue";
  3. import { useMainStore } from "@/stores/store";
  4. import { useRouter } from "vue-router";
  5. import { useI18n } from "vue-i18n";
  6. import "animate.css";
  7. import axios from "axios";
  8. import Footer from "../components/Footer.vue";
  9. const { t } = useI18n();
  10. const router = useRouter();
  11. const store = useMainStore();
  12. const apiUrl = import.meta.env.VITE_API_URL;
  13. const imgUrl = import.meta.env.VITE_API_IMG_URL;
  14. console.log("VITE_API_URL", apiUrl);
  15. let race = store.assignRace; // 種族
  16. let gender = store.assignGender; // 性別
  17. let imgLoading = ref(false);
  18. let parameter = ref([]);
  19. onMounted(() => {
  20. getLandmark();
  21. getTargetImageList();
  22. // getParameters();
  23. });
  24. // 取得背景清單
  25. async function getTargetImageList() {
  26. imgLoading.value = true;
  27. let url = `http://172.104.93.163:3219/fs/target-image-list/${gender}/${race}`;
  28. console.log("url", url);
  29. try {
  30. let response = await axios.get(url);
  31. console.log("取得背景清單", response);
  32. let imagePromises = response.data.map((item, index) => {
  33. let imageUrl = `http://172.104.93.163:3219/fs/target-image/${gender}/${race}/${item}`;
  34. return getTargetImage(imageUrl, index); // 取得種族圖片
  35. });
  36. await Promise.all(imagePromises);
  37. console.log("全部完成");
  38. // 依照檔名進行排序 (0-9)
  39. parameter.value.sort((a, b) => {
  40. const aNum = parseInt(a.name);
  41. const bNum = parseInt(b.name);
  42. return aNum - bNum;
  43. });
  44. // 組合陣列
  45. parameter.value = parameter.value.map((item, index) => ({
  46. imgUrl: item.imgUrl,
  47. name: `${index}.jpg`,
  48. title: landmarkNames.value[index],
  49. }));
  50. console.log("items >>>", parameter.value);
  51. imgLoading.value = false;
  52. } catch (error) {
  53. console.log("error", error);
  54. }
  55. }
  56. const landmarkNames = ref([]);
  57. // 取得景點名稱
  58. async function getLandmark() {
  59. let url = "http://172.104.93.163:3219/fslandmark-order";
  60. try {
  61. let response = await axios.get(url);
  62. landmarkNames.value = response.data;
  63. console.log("getLandmark", landmarkNames.value);
  64. } catch (error) {
  65. console.log("error", error);
  66. }
  67. }
  68. // 取得背景圖片
  69. async function getTargetImage(url, index) {
  70. try {
  71. // 設定 responseType 為 arraybuffer 以取得二進位數據
  72. let response = await axios.get(url, { responseType: "arraybuffer" });
  73. let blob = new Blob([response.data], { type: "image/png" }); // 創建 blob
  74. let imageUrl = URL.createObjectURL(blob); // 創建圖片 URL
  75. console.log("imageUrl", imageUrl);
  76. parameter.value.push({ imgUrl: imageUrl, name: `${index}.jpg` });
  77. console.log("parameter.value", parameter.value);
  78. } catch (error) {
  79. console.log("error", error);
  80. }
  81. }
  82. function handleBgImg(item) {
  83. console.log("item", item);
  84. store.assignBgImg = item.name;
  85. store.assignBgImgUrl = item.imgUrl;
  86. store.assignBgImgTitle = item.title;
  87. console.log("store.assignBgImg", store.assignBgImg);
  88. console.log("store.assignBgImgUrl", store.assignBgImgUrl);
  89. }
  90. const currentPhotos = computed(() => {
  91. const start = currentIndex.value;
  92. const end = start + perPage.value;
  93. return parameter.value.slice(start, end);
  94. });
  95. console.log("currentPhotos", currentPhotos);
  96. let currentIndex = ref(0);
  97. let perPage = ref(2);
  98. function prev() {
  99. if (currentIndex.value > 0) {
  100. currentIndex.value -= perPage.value;
  101. }
  102. }
  103. function next() {
  104. if (currentIndex.value + perPage.value < parameter.value.length) {
  105. currentIndex.value += perPage.value;
  106. }
  107. }
  108. // 計算頁數
  109. const totalPages = computed(() =>
  110. Math.ceil(parameter.value.length / perPage.value)
  111. );
  112. const currentPage = computed(
  113. () => Math.floor(currentIndex.value / perPage.value) + 1
  114. );
  115. let parameterList = ref([]);
  116. // 背景清單
  117. // let parameter = ref([
  118. // {
  119. // bg_img: "臺北陽明山-母親節.png",
  120. // title: "taipei_yangmingshan",
  121. // description: "taipei_yangmingshan_description",
  122. // },
  123. // {
  124. // bg_img: "台南孔廟.png",
  125. // title: "tainan_confucius_temple",
  126. // description: "tainan_confucius_temple_description",
  127. // },
  128. // {
  129. // bg_img: "臺北中正紀念堂-2.png",
  130. // title: "taipei_chiang_kai_shek_memorial_hall_2",
  131. // description: "taipei_chiang_kai_shek_memorial_hall_2_description",
  132. // },
  133. // {
  134. // bg_img: "台東嘉明湖.png",
  135. // title: "taitung_jiaming_lake",
  136. // description: "taitung_jiaming_lake_description",
  137. // },
  138. // {
  139. // bg_img: "中秋節.png",
  140. // title: "mid_autumn_festival",
  141. // description: "mid_autumn_festival_description",
  142. // },
  143. // {
  144. // bg_img: "新北野柳女王頭.png",
  145. // title: "new_taipei_yehliu_queen_head",
  146. // description: "new_taipei_yehliu_queen_head_description",
  147. // },
  148. // {
  149. // bg_img: "基隆和平島公園.png",
  150. // title: "keelung_heping_island_park",
  151. // description: "keelung_heping_island_park_description",
  152. // },
  153. // {
  154. // bg_img: "南投日月潭.png",
  155. // title: "taichung_sun_moon_lake",
  156. // description: "taichung_sun_moon_lake_description",
  157. // },
  158. // {
  159. // bg_img: "臺北中正紀念堂.png",
  160. // title: "taipei_chiang_kai_shek_memorial_hall",
  161. // description: "taipei_chiang_kai_shek_memorial_hall_description",
  162. // },
  163. // {
  164. // bg_img: "台南鹽田.png",
  165. // title: "tainan_salt_field",
  166. // description: "tainan_salt_field_description",
  167. // },
  168. // {
  169. // bg_img: "高雄美麗島.png",
  170. // title: "kaohsiung_formosa_boulevard_station",
  171. // description: "kaohsiung_formosa_boulevard_station_description",
  172. // },
  173. // {
  174. // bg_img: "新北十分瀑布.png",
  175. // title: "new_taipei_shifen_waterfall",
  176. // description: "new_taipei_shifen_waterfall_description",
  177. // },
  178. // {
  179. // bg_img: "臺北故宮.png",
  180. // title: "taipei_national_palace_museum",
  181. // description: "taipei_national_palace_museum_description",
  182. // },
  183. // {
  184. // bg_img: "臺北故宮-2.png",
  185. // title: "taipei_national_palace_museum_2",
  186. // description: "taipei_national_palace_museum_2_description",
  187. // },
  188. // {
  189. // bg_img: "台中歌劇院.png",
  190. // title: "national_taichung_theater",
  191. // description: "national_taichung_theater_description",
  192. // },
  193. // {
  194. // bg_img: "嘉義森林之歌.png",
  195. // title: "chiayi_song_of_forest",
  196. // description: "chiayi_song_of_forest_description",
  197. // },
  198. // {
  199. // bg_img: "基隆八斗子鐵路.png",
  200. // title: "keelung_baduzi_railway",
  201. // description: "keelung_baduzi_railway_description",
  202. // },
  203. // {
  204. // bg_img: "花蓮清水斷崖.png",
  205. // title: "hualien_qingshui_cliff",
  206. // description: "hualien_qingshui_cliff_description",
  207. // },
  208. // {
  209. // bg_img: "澎湖.png",
  210. // title: "penghu",
  211. // description: "penghu_description",
  212. // },
  213. // {
  214. // bg_img: "南投清境農場.png",
  215. // title: "nantou_qingjing_farm",
  216. // description: "nantou_qingjing_farm_description",
  217. // },
  218. // {
  219. // bg_img: "高雄流行音樂中心.png",
  220. // title: "kaohsiung_music_center",
  221. // description: "kaohsiung_music_center_description",
  222. // },
  223. // {
  224. // bg_img: "花蓮金針花山.png",
  225. // title: "hualien_daylily_mountain",
  226. // description: "hualien_daylily_mountain_description",
  227. // },
  228. // {
  229. // bg_img: "新北九份老街.png",
  230. // title: "new_taipei_jiufen_old_street",
  231. // description: "new_taipei_jiufen_old_street_description",
  232. // },
  233. // {
  234. // bg_img: "嘉義阿里山小火車.png",
  235. // title: "chiayi_alishan_forest_railways",
  236. // description: "chiayi_alishan_forest_railways_description",
  237. // },
  238. // {
  239. // bg_img: "台中高美濕地.png",
  240. // title: "taichung_gaomei_wetland",
  241. // description: "taichung_gaomei_wetland_description",
  242. // },
  243. // ]);
  244. // console.log("parameter", parameter.value);
  245. async function getParameters() {
  246. let url = `${apiUrl}/sd/parameters`;
  247. try {
  248. let response = await axios.get(url);
  249. parameterList.value = response.data;
  250. console.log("parameterList", parameterList.value);
  251. } catch (error) {
  252. console.log("error", error);
  253. }
  254. }
  255. let alertShow = ref(false);
  256. function checkImg() {
  257. if (store.assignBgImg && store.assignBgImg !== "") {
  258. alertShow.value = false;
  259. router.push("/step5");
  260. } else {
  261. alertShow.value = true;
  262. setTimeout(() => {
  263. alertShow.value = false;
  264. }, 2000);
  265. }
  266. }
  267. </script>
  268. <template>
  269. <div class="content">
  270. <p class="title">{{ t("postcard.step2.text_2") }}</p>
  271. <div
  272. v-if="imgLoading"
  273. class="d-flex flex-column align-center justify-center pt-15"
  274. >
  275. <v-progress-circular
  276. :size="70"
  277. :width="7"
  278. color="white"
  279. indeterminate
  280. ></v-progress-circular>
  281. </div>
  282. <div v-else class="img-content">
  283. <div class="slider-btn">
  284. <button class="prev" @click="prev">
  285. <img class="arrow" src="../assets/img/arrow_l.png" alt="" />
  286. </button>
  287. <button class="next" @click="next">
  288. <img class="arrow" src="../assets/img/arrow_r.png" alt="" />
  289. </button>
  290. </div>
  291. <div
  292. @click="handleBgImg(item)"
  293. v-for="item in currentPhotos"
  294. class="bg-img"
  295. >
  296. <!-- <v-img
  297. cover
  298. class="cover"
  299. :lazy-src="`http://172.104.93.163:3219/static/assets/img/bg/${item.bg_img}`"
  300. :src="`http://172.104.93.163:3219/static/assets/img/bg/${item.bg_img}`"
  301. >
  302. <template v-slot:placeholder>
  303. <div class="d-flex align-center justify-center fill-height">
  304. <v-progress-circular
  305. color="grey-lighten-4"
  306. indeterminate
  307. ></v-progress-circular>
  308. </div>
  309. </template>
  310. </v-img> -->
  311. <v-img cover class="cover" :lazy-src="item.imgUrl" :src="item.imgUrl">
  312. <template v-slot:placeholder>
  313. <div class="d-flex align-center justify-center fill-height">
  314. <v-progress-circular
  315. color="grey-lighten-4"
  316. indeterminate
  317. ></v-progress-circular>
  318. </div>
  319. </template>
  320. </v-img>
  321. <p>{{ item.title }}</p>
  322. <img
  323. v-if="item.name === store.assignBgImg"
  324. class="icon active"
  325. src="../assets/img/confirm.png"
  326. alt=""
  327. />
  328. <img v-else class="icon" src="../assets/img/confirm-solid.png" alt="" />
  329. </div>
  330. <span class="page-num">{{ currentPage }} / {{ totalPages }}</span>
  331. <a @click="checkImg()" href="javascript:;" class="main-btn">
  332. {{ t("next_step") }}
  333. </a>
  334. <div v-if="alertShow" class="alert-item">
  335. <v-alert border="top" type="warning" variant="outlined" class="mt-5">
  336. 尚未選擇背景
  337. </v-alert>
  338. </div>
  339. </div>
  340. <Footer url="/step3" />
  341. </div>
  342. </template>
  343. <style lang="scss" scoped>
  344. .img-content {
  345. // height: 80vh;
  346. padding: 0 2rem;
  347. display: flex;
  348. flex-direction: column;
  349. align-items: center;
  350. @media (max-width: 600px) {
  351. height: 100%;
  352. padding: 0 4rem;
  353. }
  354. .bg-img {
  355. margin-bottom: 2rem;
  356. cursor: pointer;
  357. position: relative;
  358. .cover {
  359. max-width: 100%;
  360. width: 75vw;
  361. height: 50vw;
  362. object-fit: cover;
  363. }
  364. }
  365. p {
  366. margin-top: 0.5rem;
  367. }
  368. .icon {
  369. width: 3.5rem;
  370. position: absolute;
  371. top: 0.1rem;
  372. right: 0.1rem;
  373. }
  374. }
  375. .slider-btn {
  376. width: 100%;
  377. position: absolute;
  378. z-index: 100;
  379. top: 50vh;
  380. img {
  381. width: 100px;
  382. transition: all 0.2s;
  383. @media (max-width: 600px) {
  384. width: 50px;
  385. }
  386. }
  387. .prev,
  388. .next {
  389. position: absolute;
  390. cursor: pointer;
  391. border: none;
  392. background-color: transparent;
  393. &:hover {
  394. img {
  395. opacity: 0.7;
  396. }
  397. }
  398. }
  399. .prev {
  400. left: 0;
  401. }
  402. .next {
  403. right: 0;
  404. }
  405. }
  406. .page-num {
  407. margin: auto auto 2.2rem;
  408. color: white;
  409. letter-spacing: 0.2rem;
  410. }
  411. .content {
  412. @media (max-width: 600px) {
  413. min-height: 100vh;
  414. }
  415. }
  416. .alert-item {
  417. position: absolute;
  418. top: 50%;
  419. left: 50%;
  420. transform: translate(-50%, -50%);
  421. .v-alert {
  422. background-color: var(--sub-color);
  423. }
  424. .text-warning {
  425. color: var(--main-color) !important;
  426. }
  427. }
  428. </style>