chanpin_details.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. var flag = true,
  2. param = null,
  3. pStatus = true,
  4. goodStatus = true,
  5. colorId = '',
  6. shareOpenid = '',
  7. productId = '',
  8. yuanjia = '';
  9. mui.ready(function() {
  10. var nowUrl = location.href;
  11. productId = getParam("productId");
  12. colorId = getParam("colorId");
  13. param = nowUrl.split("?")[1];
  14. //springFestivalPop();
  15. marquee_Show();
  16. selectProductInfo(productId);
  17. selectProductImageText(productId);
  18. // mui('body').on('tap', '#add-cart-div', function() {
  19. // if(!pStatus) {
  20. // mui.alert("只有在售商品才可以添加购物车!");
  21. // return false
  22. // }
  23. // if(!goodStatus) {
  24. // mui.alert("该商品暂时缺货,不能购买!");
  25. // return false
  26. // }
  27. // var btnArray = ['否', '是'];
  28. // $.ajax(base_path + '/cart/addProductToCart?' + param + '&dates=' + new Date().getTime(), {
  29. // dataType: 'json',
  30. // type: 'GET',
  31. // timeout: 15000,
  32. // xhrFields: {
  33. // withCredentials: true
  34. // },
  35. // crossDomain: true,
  36. // success: function(dt) {
  37. // if(dt.isRedirect) {
  38. // location.href = dt.redirectURL;
  39. // } else {
  40. // if(dt.status) {
  41. // getCartNum()
  42. // } else {
  43. // mui.confirm(dt.message + '是否刷新页面?', '提示', btnArray, function(e) {
  44. // if(e.index == 1) {
  45. // location.reload(true)
  46. // }
  47. // })
  48. // }
  49. // }
  50. // },
  51. // error: function(xhr, type, errorThrown) {
  52. // console.log(xhr)
  53. // }
  54. // })
  55. // });
  56. mui('body').on('tap', '#click-product-desc', function() {
  57. if(flag) {
  58. selectProductImageText(productId);
  59. flag = false
  60. }
  61. });
  62. mui('body').on('tap', '.go_gwc', function() {
  63. mui.openWindow({
  64. url: 'shopping_cart.html'
  65. })
  66. });
  67. // mui('body').on('tap', '#shopp-div', function() {
  68. // if(!pStatus) {
  69. // mui.alert("该商品已经下架,请重新挑选商品购买!");
  70. // return false
  71. // }
  72. // if(!goodStatus) {
  73. // mui.alert("该商品暂时缺货,不能购买!");
  74. // return false
  75. // }
  76. // var btnArray = ['否', '是'];
  77. // $.ajax(base_path + '/cart/addProductToCart?' + param + '&dates=' + new Date().getTime(), {
  78. // dataType: 'json',
  79. // type: 'GET',
  80. // timeout: 15000,
  81. // xhrFields: {
  82. // withCredentials: true
  83. // },
  84. // crossDomain: true,
  85. // success: function(dt) {
  86. // if(dt.status) {
  87. // window.location.href = 'clearing.html?cartId=' + dt.data + "&tiemstamp=" + new Date().getTime()
  88. // } else {
  89. // mui.confirm(dt.message, '提示', btnArray, function(e) {
  90. // if(e.index == 1) {}
  91. // })
  92. // }
  93. // },
  94. // error: function(xhr, type, errorThrown) {
  95. // console.log(xhr)
  96. // }
  97. // })
  98. // });
  99. fenxiang();
  100. getCartNum();
  101. document.querySelector('.loading-bg').style.display = 'none'
  102. });
  103. function selectProductInfo(productId) {
  104. var btnArray = ['否', '是'];
  105. $.ajax(base_path + '/product/getProductByProductId?productId=' + productId + '&colorId=' + colorId + '&dates=' + new Date().getTime(), {
  106. dataType: 'json',
  107. async: false,
  108. type: 'GET',
  109. timeout: 15000,
  110. xhrFields: {
  111. withCredentials: true
  112. },
  113. crossDomain: true,
  114. success: function(dt) {
  115. if(dt.isRedirect) {
  116. location.href = dt.redirectURL;
  117. } else {
  118. if(dt.status) {
  119. console.log(dt.data.product);
  120. if(dt.data.product.productId == 28) {
  121. goodStatus = false
  122. }
  123. shareOpenid = dt.data.shareOpenid;
  124. var imageNote = "<div class='mui-slider-item mui-slider-item-duplicate'><a href='#'><img src='" + dt.data.product.pictureList[0].pictureUrl + "'></a></div>";
  125. for(var i = 0; i < dt.data.product.pictureList.length; i += 1) {
  126. imageNote += "<div class='mui-slider-item'><a href='#'><img src='" + dt.data.product.pictureList[i].pictureUrl + "'></a></div>";
  127. if(i == 0) {
  128. $("#active-div").append("<div class='mui-indicator mui-active'></div>")
  129. } else {
  130. $("#active-div").append("<div class='mui-indicator'></div>")
  131. }
  132. }
  133. imageNote += "<div class='mui-slider-item mui-slider-item-duplicate'><a href='#'><img src='" + dt.data.product.pictureList[0].pictureUrl + "'></a></div>";
  134. $("#group-images-div").append(imageNote);
  135. var banner = mui('#detail-slider');
  136. banner.slider({
  137. interval: 5000
  138. });
  139. $("#product-name-font").html(dt.data.product.productName);
  140. if(marquee != '' && dt.data.product.productDiscount == '328000') {
  141. $('<span class="mui-h6">购买即赠3年滤芯</span>').insertAfter('.price-sp')
  142. }
  143. $("#product-price-span").html("¥" + accDiv(dt.data.product.productDiscount, 100));
  144. if(yuanjia == '') {
  145. yuanjia = '<s>原价¥' + accDiv(dt.data.product.productPrice, 100) + '</s>'
  146. }
  147. $("#product-price-s").html(yuanjia);
  148. $("#product-access-span").html(dt.data.product.productSalesnum + "人已购买");
  149. if(dt.data.product.productStatus == 1) {
  150. pStatus = true
  151. } else {
  152. pStatus = false
  153. }
  154. //添加颜色列表弹窗
  155. var btn = '<div class="mui-table"><div class="mui-table-cell mui-col-xs-6 table-middle mui-hidden"><button class="mui-btn my-btn-block my-btn-add-cart">加入购物车</button></div><div class="mui-table-cell mui-col-xs-6 table-middle mui-hidden"><button class="mui-btn my-btn-block my-btn-buy-know">立即购买</button></div><div class="mui-table-cell mui-col-xs-12 table-middle"><button class="mui-btn my-btn-block my-btn-buy-know">确定</button></div></div>';
  156. var colorList = '',
  157. pic = '',
  158. price = '';
  159. mui.each(dt.data.colorList, function() { //遍历产品颜色集合
  160. var active = '';
  161. if(colorId == this.colorId) {
  162. active = 'active'; //弹窗选中当前颜色
  163. $(".select-color span").text(this.colorName); //当前颜色
  164. pic = this.colorImg; //当前图片
  165. price = '¥' + (this.colorDiscount / 100).toFixed(2);
  166. $(".price").text(price); //当前价格
  167. colorName = this.colorName;
  168. }
  169. colorList += '<span class="pro-select-color ' + active + '" name="' + this.colorId + '" src="' + this.colorImg + '" price="' + this.colorDiscount + '">' + this.colorName + '</span>';
  170. });
  171. $("body").append('<div class="my-picker"><ul class="mui-table-view mui-table-view-striped mui-table-view-condensed my-grid-view-no"><li class="mui-table-view-cell"><div class="mui-table"><div class="mui-table-cell mui-col-xs-3 table-middle"><img class="dalog-proPic" style="background: #f8f8f8;" width="90%" src="' + pic + '" /></div><div class="mui-table-cell mui-col-xs-8 table-middle"><div>' + dt.data.product.productName + '</div><h6 class="dalog-colorPresent" style="line-height: 14px;"></h6><div class="dalog-proPrice">' + price + '</div></div></div></li><li class="mui-table-view-cell">选择颜色</li><li style="padding: 0 15px;"><div class="mui-table" ><div class="mui-table-cell mui-col-xs-12">' + colorList + '</div></div></li><li class="mui-table-view-cell"><div class="mui-table"><div class="mui-table-cell mui-col-xs-6">选择数量</div><div class="mui-table-cell mui-text-right mui-col-xs-6"><div class="mui-numbox" data-numbox-min="0" data-numbox-max="99" name="0"><button class="mui-btn mui-btn-numbox-minus" type="button">-</button><input class="mui-input-numbox" type="number" value="0" readonly="readonly" disabled="disabled"><button class="mui-btn mui-btn-numbox-plus" type="button">+</button></div></div></div></li></ul><br />' + btn + '<span class="iconfont icon-guanbi"></span></div>');
  172. if(colorId == 310) {
  173. $(".dalog-colorPresent").css("color", "red");
  174. }
  175. getColorNumber(); //获取单个购物项id数量
  176. mui(".mui-numbox").numbox(); //激活加减按钮组合
  177. } else {
  178. mui.confirm(dt.message + '是否重新加载页面?', '提示', btnArray, function(e) {
  179. if(e.index == 1) {
  180. location.reload(true)
  181. }
  182. })
  183. }
  184. }
  185. },
  186. error: function(xhr, type, errorThrown) {
  187. console.log(xhr)
  188. }
  189. })
  190. };
  191. function selectProductImageText(productId) {
  192. var btnArray = ['否', '是'];
  193. $.ajax(base_path + '/product/getPictureByProductId?productId=' + productId, {
  194. dataType: 'json',
  195. type: 'POST',
  196. timeout: 15000,
  197. xhrFields: {
  198. withCredentials: true
  199. },
  200. crossDomain: true,
  201. success: function(dt) {
  202. if(dt.isRedirect) {
  203. location.href = dt.redirectURL;
  204. } else {
  205. if(dt.status) {
  206. if(dt.data != null) {
  207. $("#product-param-img").html(dt.data.imagetextContext)
  208. } else {
  209. $("#item2").empty();
  210. mui.alert('商品详情获取失败')
  211. }
  212. } else {
  213. mui.confirm(dt.message + '是否刷新页面?', '提示', btnArray, function(e) {
  214. if(e.index == 1) {
  215. location.reload(true)
  216. }
  217. })
  218. }
  219. }
  220. },
  221. error: function(xhr, type, errorThrown) {
  222. mui.confirm('您的网络开小差了,是否重新加载页面?', '提示', btnArray, function(e) {
  223. if(e.index == 1) {
  224. location.reload(true)
  225. }
  226. })
  227. }
  228. })
  229. };
  230. //跳转购物车
  231. $(document).on('tap', '.cart', function() {
  232. location.href = 'shopping_cart.html';
  233. });
  234. $(window).on("scroll", function() {
  235. if($(window).scrollTop() > 120) {
  236. $(".return").show()
  237. } else {
  238. $(".return").hide()
  239. }
  240. });
  241. //callback为用户点击蒙版时自动执行的回调;
  242. var mask = mui.createMask(function() {
  243. $(".my-picker").removeClass("active"); //隐藏颜色列表
  244. });
  245. //加入购物车弹出层
  246. var btnTap = '';
  247. var cartStatus = "";
  248. $(document).on('tap', '.mui-bar-tab .div-btn-buy-cart,.mui-bar-tab .div-btn-buy-know,.select-color', function() {
  249. if($(".my-picker").length > 0) { //判断是否已有弹窗
  250. var th = $(this);
  251. mask.show(); //显示遮罩层
  252. //判断点击进来的按钮
  253. if(th.hasClass("select-color")) {
  254. $(".my-picker>.mui-table .mui-col-xs-12").addClass("mui-hidden");
  255. $(".my-picker>.mui-table .mui-col-xs-6").removeClass("mui-hidden");
  256. } else {
  257. $(".my-picker>.mui-table .mui-col-xs-6").addClass("mui-hidden");
  258. $(".my-picker>.mui-table .mui-col-xs-12").removeClass("mui-hidden");
  259. if(th.hasClass("div-btn-buy-cart")) {
  260. btnTap = 'addCart';
  261. cartStatus = null;
  262. } else {
  263. btnTap = 'buyknow';
  264. cartStatus = 1;
  265. }
  266. }
  267. $(".my-picker").addClass("active"); //显示颜色列表弹窗
  268. }
  269. });
  270. //切换颜色
  271. $(document).on('tap', '.pro-select-color', function() {
  272. var th = $(this);
  273. colorId = th.attr("name");
  274. th.addClass("active").siblings().removeClass("active"); //选中自己再把前后的兄弟元素取消选中
  275. $(".select-color span").text(th.text());
  276. getColorNumber(); //重新获取单个购物项id数量
  277. $(".dalog-proPic").attr("src", th.attr("src")); //替换当前选择颜色的图片
  278. colorName = th.text();
  279. fenxiang();
  280. });
  281. //弹窗的确定
  282. $(document).on('tap', '.my-picker .mui-col-xs-12 .my-btn-buy-know', function() {
  283. //mui.toast('加入购物车成功!');
  284. var number = $(".mui-input-numbox").val();
  285. if(number != 0) {
  286. console.log(btnTap)
  287. add();
  288. if(btnTap == 'addCart') {
  289. mui.toast('加入购物车成功!');
  290. }
  291. } else {
  292. mui.toast('请选择商品数量!');
  293. }
  294. });
  295. //判断是加减删,ajax分别调用不同的URLclearing.html
  296. function add() {
  297. var parameter = {}; //ajax参数json集合
  298. var src = '/cart/addProductToCart';
  299. if(typeof($(".my-picker .pro-select-color.active").attr("name")) != "undefined") {
  300. parameter["productId"] = productId; //产品id
  301. parameter["colorId"] = $(".my-picker .pro-select-color.active").attr("name"); //颜色id
  302. if($(".mui-input-numbox").val() - $(".mui-numbox").attr("name") < 0) {
  303. if($(".mui-input-numbox").val() == 0) {
  304. src = '/cart/deleteCart';
  305. addCart(parameter, src)
  306. } else {
  307. parameter["cartNum"] = Math.abs($(".mui-input-numbox").val() - $(".mui-numbox").attr("name"));
  308. src = '/cart/deleteCart';
  309. addCart(parameter, src)
  310. }
  311. getCartNum();
  312. } else if($(".mui-input-numbox").val() - $(".mui-numbox").attr("name") > 0) {
  313. parameter["cartNum"] = Math.abs($(".mui-input-numbox").val() - $(".mui-numbox").attr("name"));
  314. addCart(parameter, src);
  315. getCartNum();
  316. } else {
  317. if($(".mui-input-numbox").val() < 1) {
  318. mui.toast("请选择商品数量!"); //如果数量为0提示用户去选
  319. } else {
  320. $(".my-picker").removeClass("active"); //隐藏颜色列表
  321. $(".mui-backdrop").remove(); //移除遮罩层
  322. if(btnTap == 'buyknow') {
  323. if(!window.sessionStorage) {
  324. mui.alert("您的浏览器不支持sessionStorage");
  325. } else {
  326. getColorNumber();
  327. //自定义json数据
  328. var obj = {
  329. text: [$(".mui-numbox").attr("cartid")]
  330. };
  331. //json格式化插入 购物项id集合
  332. sessionStorage.setItem('pay_data', JSON.stringify(obj));
  333. location.href = 'clearing.html?cartId=' + $(".mui-numbox").attr("cartid") + "&tiemstamp=" + new Date().getTime()
  334. }
  335. }
  336. }
  337. }
  338. } else {
  339. mui.alert("没有获取到产品颜色!");
  340. }
  341. };
  342. //商品加减删三个ajax接口集合,parameter参数,src接口的路径
  343. function addCart(parameter, src) {
  344. $.ajax(base_path + src + '?dates=' + new Date().getTime(), {
  345. data: parameter,
  346. dataType: 'json',
  347. xhrFields: {
  348. withCredentials: true
  349. },
  350. crossDomain: true,
  351. type: 'get',
  352. timeout: 15000,
  353. success: function(dt) {
  354. if(dt.isRedirect) {
  355. location.href = dt.redirectURL;
  356. } else {
  357. if(dt.status) {
  358. getColorNumber(); //重新获取单个购物项id数量
  359. getCartNum(); //重新获取购物车总数量
  360. if(btnTap == 'buyknow') {
  361. if($(".mui-input-numbox").val() > 0) {
  362. $(".my-picker").removeClass("active"); //隐藏颜色列表
  363. $(".mui-backdrop").remove(); //移除遮罩层
  364. if(!window.sessionStorage) {
  365. mui.alert("您的浏览器不支持sessionStorage");
  366. } else {
  367. getColorNumber();
  368. setTimeout(function() {
  369. //自定义json数据
  370. var obj = {
  371. text: [$(".mui-numbox").attr("cartid")]
  372. };
  373. //json格式化插入 购物项id集合
  374. sessionStorage.setItem('pay_data', JSON.stringify(obj));
  375. location.href = 'clearing.html?cartId=' + $(".mui-numbox").attr("cartid") + "&tiemstamp=" + new Date().getTime()
  376. }, 500);
  377. }
  378. } else {
  379. mui.toast("请选择商品数量!");
  380. }
  381. } else {
  382. $(".my-picker").removeClass("active"); //隐藏颜色列表
  383. $(".mui-backdrop").remove(); //移除遮罩层
  384. }
  385. } else {
  386. mui.alert("添加失败!");
  387. }
  388. }
  389. },
  390. error: function(xhr, type, errorThrown) {
  391. console.log(xhr);
  392. mui.alert("添加失败!网络错误");
  393. }
  394. });
  395. };
  396. //获取单个购物项id数量
  397. function getColorNumber() {
  398. $.ajax(base_path + '/cart/getCartColorNum?dates=' + new Date().getTime(), {
  399. data: {
  400. "colorId": colorId
  401. },
  402. dataType: 'json',
  403. xhrFields: {
  404. withCredentials: true
  405. },
  406. crossDomain: true,
  407. type: 'get', //减少数量后台限制用get
  408. timeout: 15000,
  409. success: function(dt) {
  410. if(dt.isRedirect) {
  411. location.href = dt.redirectURL;
  412. } else {
  413. if(dt.status) {
  414. if(typeof(dt.data) != "undefined") {
  415. $(".mui-input-numbox").val(dt.data.cartNum);
  416. $(".mui-numbox").attr("name", dt.data.cartNum);
  417. $(".mui-numbox").attr("cartid", dt.data.cartId);
  418. if(dt.data.cartNum > 0) {
  419. $(".mui-btn-numbox-minus").removeAttr("disabled");
  420. } else {
  421. $(".mui-btn-numbox-minus").attr("disabled", "disabled");
  422. }
  423. } else {
  424. $(".mui-numbox").removeAttr("cartid");
  425. $(".mui-numbox").attr("name", "0");
  426. $(".mui-input-numbox").val(1);
  427. $(".mui-btn-numbox-minus").attr("disabled", "disabled");
  428. }
  429. } else {
  430. console.log("失败!");
  431. }
  432. }
  433. },
  434. error: function(xhr, type, errorThrown) {
  435. console.log(xhr);
  436. }
  437. });
  438. };
  439. //关闭弹窗
  440. $(document).on('tap', '.icon-guanbi', function() {
  441. $(".mui-backdrop").remove(); //移除遮罩层
  442. $(".my-picker").removeClass("active"); //隐藏弹窗
  443. });
  444. //分享弹窗
  445. $(document).on('tap', '.icon-fenxiang', function() {
  446. $(".share-dalog").show();
  447. });
  448. //关闭分享弹窗
  449. $(document).on('tap', '.share-dalog', function() {
  450. $(this).hide();
  451. });
  452. //分享
  453. function fenxiang() {
  454. var url = domanUrl + "/watero/wechat/chanpin_details.html?productId=" + productId + '&colorId=' + colorId + "&shareOpenid=" + shareOpenid;
  455. window.repleFlag = false;
  456. window.wxFriend = {
  457. "appId": "",
  458. "imgUrl": domanUrl + '/watero/wechat/images/index.jpg',
  459. "link": url,
  460. "desc": "美国标准,4级净化,5档水温,废水比3:1,水质TDS智能显示。",
  461. "title": "Watero,美国高端净水专家,4级净化,5档水温,免安装饮水一体机。",
  462. "netError": "您的网络异常,请刷新重试!如多次刷新无效,请向我们反应 ~",
  463. "shareTimeSuccess": "分享成功",
  464. "shareTimeCancel": "已取消分享",
  465. "shareAppSuccess": "分享成功!",
  466. "shareAppCancel": "已取消分享"
  467. };
  468. getConfig();
  469. }