recipePage_ordinary.js 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. import getParams from './getParams.js';
  2. recipePage_ordinary = function () {
  3. //private menbers
  4. const ordinaryUrl_left = "./json/ordinaryData_left.json";
  5. const ordinaryUrl_right = "./json/ordinaryData_right.json";
  6. let ordinaryData_left;
  7. let ordinaryData_right;
  8. let ordinaryData = [];
  9. let targetData;
  10. let ingredientAllData;
  11. let ordinaryId;
  12. //private methods
  13. function init() {
  14. console.log('recipePage_ordinary is loaded.');
  15. // 取得是哪位素人
  16. if (getParams('ordinary') != null && !isNaN(getParams('ordinary'))){
  17. ordinaryId = getParams('ordinary')
  18. }else {
  19. ordinaryId = 1 // 預設kol
  20. }
  21. // console.log(!isNaN(getParams('ordinary')))
  22. console.log("ordinaryId: "+ordinaryId)
  23. // 取得素人資料
  24. getOrdinaryData();
  25. }
  26. function getOrdinaryData() {
  27. fetch(ordinaryUrl_left)
  28. .then(res => res.json())
  29. .then(data => {
  30. ordinaryData_left = data.sort((a, b) => {
  31. return -1
  32. });
  33. ordinaryData = ordinaryData.concat(ordinaryData_left)
  34. // console.log(ordinaryData)
  35. })
  36. .then(() => {
  37. fetch(ordinaryUrl_right)
  38. .then(res => res.json())
  39. .then(data => {
  40. ordinaryData_right = data.sort((a, b) => {
  41. return a.index - b.index
  42. });
  43. ordinaryData = ordinaryData.concat(ordinaryData_right)
  44. // console.log(ordinaryData)
  45. let temData = ordinaryData.filter(item=>{
  46. return item.id == ordinaryId;
  47. })
  48. targetData = temData[0];
  49. console.log(targetData)
  50. getIngredientData();
  51. })
  52. .then(() => {
  53. createOrdinaryHtml();
  54. buyCtaAni();
  55. })
  56. .then(() => {
  57. setTimeout(() => {
  58. initHandler();
  59. }, 200);
  60. })
  61. });
  62. }
  63. function newsticker(){
  64. $(".newsticker").eocjsNewsticker({
  65. speed: 20,
  66. divider: '  ',
  67. type: 'static'
  68. });
  69. }
  70. function recipeModal(){
  71. $(".recipe__open").click(function(){
  72. $(".recipe__modal").css("z-index",20)
  73. gsap.to(".recipe__modal", {
  74. duration: 0.3, autoAlpha: 1, onComplete: function () {
  75. $("body").addClass("modal-open");
  76. }})
  77. });
  78. $(".recipe__modal .close").click(function(){
  79. gsap.to(".recipe__modal", { duration: 0.3, autoAlpha: 0, onComplete: function(){
  80. $(".recipe__modal").css("z-index", -1);
  81. $("body").removeClass("modal-open");
  82. } })
  83. });
  84. }
  85. async function initHandler(){
  86. // console.log("initHandler")
  87. const recipeModalHandler = await recipeModal();
  88. // const newstickerStart = await newsticker();
  89. // const handlerInto = await intoPage();
  90. }
  91. // 把生成的HTML顯示到介面上
  92. function createOrdinaryHtml() {
  93. // kv 素人濾鏡照或菜色照
  94. $(".recipe__kv__ordinary").append(`
  95. <img src="./images/recipePage_ordinary/dish/${targetData.kv_img}" alt="">
  96. `)
  97. // 食譜名稱
  98. $(".recipe__kv__dish").html(`
  99. <span>民間料理救星 - ${targetData.name}</span>${targetData.text}
  100. `);
  101. // 食譜步驟
  102. $(".recipe__box-step").html(targetData.step);
  103. //放大的食譜步驟
  104. $(".recipe__modal .modal__name .newsticker").html(targetData.text);
  105. $(".recipe__modal .modal__content").html(targetData.step);
  106. // 食材列表
  107. for (let i = 0; i < ingredientAllData.length;i++) {
  108. $(".recipe__box-ingredients").append(`
  109. <div class="ingredients__box">
  110. <div class="ingredients__name">${ingredientAllData[i].ingredient_name}</div><div class="ingredients__space"></div><div class="ingredients__unit">${ingredientAllData[i].ingredient_unit}</div>
  111. </div>
  112. `);
  113. }
  114. // 分享
  115. $(".recipe__btn-share").attr("href", "./share.html?ordinary=" + targetData.id)
  116. //料理標籤
  117. let tagData = [];
  118. let tag_1 = targetData.tag_1.split(",")
  119. let tag_2 = targetData.tag_2.split(",")
  120. let tag_3 = targetData.tag_3.split(",")
  121. let tag_4 = targetData.tag_4.split(",")
  122. if (tag_1.length > 1) {
  123. for (let i = 0; i < tag_1.length; i++) {
  124. tagData.push(tag_1[i])
  125. }
  126. }else {
  127. if (targetData.tag_1 != "") {
  128. tagData.push(targetData.tag_1)
  129. }
  130. }
  131. if (tag_2.length > 1) {
  132. for (let i = 0; i < tag_2.length; i++) {
  133. tagData.push(tag_2[i])
  134. }
  135. } else {
  136. if (targetData.tag_2 != "") {
  137. tagData.push(targetData.tag_2)
  138. }
  139. }
  140. if (tag_3.length > 1) {
  141. for (let i = 0; i < tag_3.length; i++) {
  142. tagData.push(tag_3[i])
  143. }
  144. } else {
  145. if (targetData.tag_3 != "") {
  146. tagData.push(targetData.tag_3)
  147. }
  148. }
  149. if (tag_4.length > 1) {
  150. for (let i = 0; i < tag_4.length; i++) {
  151. tagData.push(tag_4[i])
  152. }
  153. } else {
  154. if (targetData.tag_4 != "") {
  155. tagData.push(targetData.tag_4)
  156. }
  157. }
  158. console.log(tagData)
  159. for(let i=0;i<tagData.length;i++) {
  160. $(".recipe__tag__list").append(`
  161. <a href="./search.html?search=${tagData[i]}" class="recipe__tag__item">${tagData[i]}</a>
  162. `)
  163. }
  164. let ctaUrl = getOrderURL(ingredientAllData);
  165. $(".recipe__buy").attr("href",ctaUrl);
  166. $(".recipe__buy").click(function(e){
  167. e.preventDefault();
  168. setTimeout(function(){
  169. location.href = ctaUrl
  170. },200)
  171. })
  172. }
  173. // 偵測上滑下滑
  174. function detectTouch(){
  175. var startX, startY, moveX, moveY;
  176. //here clientX, and clientY means X and Y coordinates
  177. function touchStart(e) {
  178. startX = e.touches[0].clientX;
  179. startY = e.touches[0].clientY;
  180. }
  181. function touchMove(e) {
  182. moveX = e.touches[0].clientX;
  183. moveY = e.touches[0].clientY;
  184. }
  185. function touchEnd() {
  186. if (startX + 100 < moveX) {
  187. console.log('right');
  188. } else if (startX - 100 > moveX) {
  189. console.log('left');
  190. }
  191. if (startY < moveY) {
  192. console.log('down');
  193. // $(".recipe__buy").css('bottom', '-29px')
  194. } else if (startY > moveY) {
  195. console.log('up');
  196. // $(".recipe__buy").css('bottom', '-49px')
  197. }
  198. }
  199. document.addEventListener('touchstart', touchStart,false);
  200. document.addEventListener('touchmove', touchMove, false);
  201. document.addEventListener('touchend', touchEnd, false);
  202. }
  203. // 一鍵下單動畫
  204. function buyCtaAni(){
  205. let buyCtaTl = gsap.timeline({ repeat: -1, repeatDelay: 3, delay: 0});
  206. buyCtaTl.to(".recipe__buy", {duration: 0.2,scale: 1, y:0, ease: "none" })
  207. buyCtaTl.to(".recipe__buy", { duration: 0.2,scale: 1.1, y:-10, ease: "none"})
  208. buyCtaTl.to(".recipe__buy", {duration: 0.2,scale: 1, y:0, ease: "none" })
  209. buyCtaTl.to(".recipe__buy", { duration: 0.2,scale: 1.1, y:-10, ease: "none" })
  210. buyCtaTl.to(".recipe__buy", {duration: 0.3,scale: 1, y:0, ease: "none" })
  211. }
  212. // 取得食材資料
  213. function getIngredientData(){
  214. let ingredientItem = []
  215. Object.entries(targetData).forEach(([key,value])=>{
  216. if (key.includes("ingredient") && value != "") {
  217. ingredientItem.push(value);
  218. }
  219. })
  220. // console.log(ingredientItem)
  221. ingredientAllData = ingredientItem.map(function(item){
  222. let itemArr = item.split(";")
  223. return {
  224. ingredient_name: itemArr[0],
  225. ingredient_unit: itemArr[1],
  226. salepage_url: itemArr[2],
  227. salepage_id: itemArr[3],
  228. sku_id: itemArr[4],
  229. qty: itemArr[5],
  230. }
  231. })
  232. }
  233. function getOrderURL(_data) {
  234. var _ingredientData = _data;
  235. var resuleData = [];
  236. _ingredientData.forEach(element => {
  237. // console.log(element);
  238. var _result = { "salepage_id": "", "sku_id": "", "qty": "" };
  239. _result.salepage_id = element.salepage_id;
  240. _result.sku_id = element.sku_id;
  241. _result.qty = element.qty;
  242. if(_result.sku_id !== null && _result.sku_id !== undefined){
  243. resuleData.push(_result);
  244. }
  245. });
  246. console.log(resuleData);
  247. var _utm = "&utm_campaign=ordinary&utm_medium=ordinarybuy&utm_source=web";
  248. var _url = 'http://shop.pxmart.com.tw/v2/ShoppingCart/BatchInsert?openExternalBrowser=1&data=' + encodeURIComponent(JSON.stringify(resuleData)) + _utm;
  249. console.log(_url);
  250. return _url;
  251. }
  252. function intoPage(){
  253. gsap.set(".recipeWrapper main",{y: 200})
  254. gsap.to(".recipeWrapper main",{y: 0,onComplete: function(){
  255. $(".recipeWrapper main").css("transform","none")
  256. // buyCtaAni();
  257. }})
  258. }
  259. {
  260. $(document).ready(function () {
  261. init();
  262. });
  263. }
  264. //public
  265. return {
  266. intoPage: function(){
  267. intoPage();
  268. }
  269. };
  270. };
  271. var recipePage_ordinary = new recipePage_ordinary();