Geen omschrijving

detail.js 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //获取地址栏参数
  2. function parseQueryString(url) {
  3. var urlObj = {};
  4. var reg = /([^?=&]+)=([^?=&]+)/g;
  5. url.replace(reg, ($0, $1, $2) => {
  6. urlObj[$1] = decodeURIComponent($2);
  7. })
  8. return urlObj;
  9. }
  10. var qs = parseQueryString(window.location.href);
  11. var page =1;
  12. var maxPage =1;
  13. var loadingFlag = false;
  14. var goodsData = [];//商品列表
  15. var goods_id, is_coupon, coupon_price, price, discount_price, token;
  16. $().ready(function () {
  17. token = qs.token;
  18. goods_id = qs.goods_id;
  19. is_coupon = qs.is_coupon;
  20. coupon_price = qs.coupon_price;
  21. price = qs.price;
  22. discount_price = qs.discount_price;
  23. tdetail();
  24. recommendlist(1);//相关推荐
  25. $(window).scroll(function(){
  26. if($(window).scrollTop()>= $(document).height()-$(window).height() - 20){
  27. //到达底部加载数据
  28. if(loadingFlag){
  29. page ++ ;
  30. recommendlist(page);//相关推荐
  31. }
  32. }
  33. });
  34. })
  35. //详情页接口
  36. function tdetail () {
  37. $.ajax({
  38. type:"post",
  39. url:"/api/v2/goods/detail",
  40. dataType:'json',
  41. data:{
  42. goods_id:goods_id,
  43. is_coupon:is_coupon,
  44. coupon_price:coupon_price,
  45. price:price,
  46. discount_price:discount_price
  47. },
  48. success:function (res) {
  49. console.log(res)
  50. if(res && res.errno == 0){
  51. var commodityDetailsHtml;
  52. var shop = res.rst.data.shop,shopHtml;//商铺信息
  53. var bannerHtml = '';//banner
  54. res.rst.data.small_img.forEach(function (item, index) {
  55. bannerHtml += `<div class="swiper-slide">
  56. <img width="100%" src="${item}" />
  57. </div>`
  58. })
  59. var is_coupon = res.rst.data.is_coupon,is_coupon_html = "";//是否有券
  60. if(is_coupon == 1){
  61. is_coupon_html = `<span class="commodity_coupon"><em>券</em><em>${res.rst.data.coupon_price}元</em></span>`
  62. }
  63. var shop_type = res.rst.data.shop_type, shop_type_img = '';//店铺类型,0淘宝,1天猫
  64. if(shop_type == 1){
  65. shop_type_img = `<img src="imgs/tianmao.png" class="icon" />`
  66. }
  67. var commodityHtml = `<div class="swiper-container">
  68. <div class="swiper-wrapper">
  69. ${bannerHtml}
  70. </div>
  71. <!-- 如果需要分页器 -->
  72. <div class="swiper-pagination"></div>
  73. </div>
  74. <div class="commodity_info">
  75. <p class="info_top">
  76. <span class="commodity_new_price">券后<em>¥${res.rst.data.discount_price}</em></span>
  77. ${is_coupon_html}
  78. </p>
  79. <p class="info_cen">
  80. <span class="info_cen_old_price">原价<em>¥${res.rst.data.price}</em></span>
  81. <span >月销${res.rst.data.volume}</span>
  82. </p>
  83. <p class="info_name">
  84. ${shop_type_img}
  85. ${res.rst.data.title}
  86. </p>
  87. </div>`;
  88. if(shop){//商品详情
  89. shopHtml = `<div class="shop">
  90. <img src="${shop.pic_path}" alt="" class="shop_img">
  91. <p class="shop_info">
  92. <span class="shop_name">${shop.title}</span>
  93. <span class="shop_about"><em>宝贝描述 ${shop.item_score}</em><em>卖家服务 ${shop.service_score}</em><em>物流服务 ${shop.delivery_score}</em></span>
  94. </p>
  95. </div>`
  96. }
  97. commodityDetails = ` <div class='commodityDetails'>
  98. <a class='title' href='${res.rst.data.detail_url}'>宝贝详情 <img src='https://small-app.oss-cn-beijing.aliyuncs.com/youhuiquan/%E5%8F%B3%403x.png'/></a>
  99. </div>`
  100. $("#commodityDetails").html(commodityDetails)
  101. $("#shop").html(shopHtml)
  102. $(".commodity").html(commodityHtml);
  103. swiperEvent();//swiper banner动画
  104. }
  105. }
  106. });
  107. }
  108. //swiper
  109. function swiperEvent () {
  110. var mySwiper = new Swiper ('.swiper-container', {
  111. autoplay: 3000,
  112. loop: true,
  113. pagination: '.swiper-pagination',
  114. })
  115. }
  116. //相关推荐
  117. function recommendlist (pageIndex) {
  118. loadingFlag = false;
  119. if(maxPage < page){
  120. $(".loading").html("没有更多了")
  121. return;
  122. }
  123. $.ajax({
  124. type:"post",
  125. url:"/api/v2/goods/recommendlist",
  126. dataType:'json',
  127. data:{
  128. goods_id:goods_id,
  129. page:pageIndex,
  130. },
  131. success:function (res) {
  132. console.log(res)
  133. if(res.errno == "0" && res.rst){
  134. htmlEvent(res)
  135. }
  136. }
  137. })
  138. }
  139. function htmlEvent (res) {
  140. var data = res.rst.data;
  141. var conHtml= '';
  142. var quan = '';
  143. maxPage = res.rst.pageInfo.maxPage;
  144. //商品
  145. if( data.length > 0){
  146. if(goodsData.length != 0){
  147. goodsData = goodsData.concat(data);
  148. goodsData.forEach(function (item, index) {
  149. var shop_type = item.shop_type, shop_type_img = '';//店铺类型,0淘宝,1天猫
  150. if(shop_type == 1){ shop_type_img = '<img src="imgs/tianmao.png" class="icon"/>' }
  151. var is_coupon = item.is_coupon, quan = ''; //有没有券
  152. if(is_coupon == 1){ quan = `<div class="goods_coupon" ><span>券</span><span>${item.coupon_price}元</span></div>` }
  153. conHtml +=`<li class="goods_item" onclick="toDetails({goods_id:${item.goods_id},coupon_price:${item.coupon_price},is_coupon:${item.is_coupon},price:${item.price},discount_price:${item.discount_price}})">
  154. <img src="${item.img}" alt="" class="goods_img">
  155. <div class="goods_name">
  156. ${shop_type_img}
  157. ${item.title}
  158. </div>
  159. <div class="quan">
  160. ${quan}
  161. </div>
  162. <div class="goods_price">
  163. <span>券后</span>
  164. <span class="new_price">¥${item.discount_price}</span>
  165. <span class="old_price">¥${item.price}</span>
  166. </div>
  167. </li>`
  168. })
  169. }else{
  170. goodsData = res.rst.data;
  171. goodsData.forEach(function (item, index) {
  172. var shop_type = item.shop_type, shop_type_img = '';//店铺类型,0淘宝,1天猫
  173. if(shop_type == 1){ shop_type_img = '<img src="imgs/tianmao.png" class="icon"/>' }
  174. var is_coupon = item.is_coupon, quan = ''; //有没有券
  175. if(is_coupon == 1){ quan = `<div class="goods_coupon" ><span>券</span><span>${item.coupon_price}元</span></div>` }
  176. conHtml +=`<li class="goods_item" onclick="toDetails({goods_id:${item.goods_id},coupon_price:${item.coupon_price},is_coupon:${item.is_coupon},price:${item.price},discount_price:${item.discount_price}})">
  177. <img src="${item.img}" alt="" class="goods_img">
  178. <div class="goods_name">
  179. ${shop_type_img}
  180. ${item.title}
  181. </div>
  182. <div class="quan">
  183. ${quan}
  184. </div>
  185. <div class="goods_price">
  186. <span>券后</span>
  187. <span class="new_price">¥${item.discount_price}</span>
  188. <span class="old_price">¥${item.price}</span>
  189. </div>
  190. </li>`
  191. })
  192. }
  193. loadingFlag = true;
  194. $('#tuijian ul').html(conHtml)
  195. $(".loading").html("努力加载中...")
  196. }else{
  197. $(".loading").html('暂无数据')
  198. }
  199. }
  200. //跳转详情页
  201. function toDetails (res) {
  202. window.location.href="details.html?coupon_price=" + res.coupon_price + "&discount_price=" + res.discount_price + "&goods_id=" + res.goods_id + "&is_coupon=" + res.is_coupon + "&price=" + res.price + "&token=" + token;
  203. $(window).scrollTop(0);
  204. }
  205. //用户下单接口
  206. function orderDown () {
  207. $.ajax({
  208. type:"post",
  209. url:"/api/v2/adzoneCreate/orderDown",
  210. headers:{
  211. token:token,
  212. source:6002
  213. },
  214. data:{
  215. goods_id:goods_id,
  216. is_coupon:is_coupon
  217. },
  218. success: function (res){
  219. if(res && res.errno == 0){
  220. openTaobao(res.rst.url)
  221. }
  222. }
  223. });
  224. }
  225. // 打开淘宝
  226. function openTaobao (url) {
  227. // alert(url)
  228. var param = {"taobaoUrl" : url}
  229. param = JSON.stringify(param)
  230. try{
  231. if(navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)){
  232. window.webkit.messageHandlers.app_h5_open_taobao_url.postMessage(param);
  233. }
  234. if(navigator.userAgent.match(/android/i)){
  235. obj4H5.app_h5_open_taobao_url(param);
  236. }
  237. }catch(e){
  238. //TODO handle the exception
  239. alert('请更新版本就行购买')
  240. }
  241. }