productDetail.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. $(function(){
  2. pushHistory();
  3. window.addEventListener("popstate", function(e) {
  4. location.href=base+"/mobile/index.jsp";
  5. }, false);
  6. });
  7. function pushHistory() {
  8. var state = {
  9. title: "title",
  10. url: "#"
  11. };
  12. if(window.history.length==1){
  13. window.history.pushState(state, "title", "#");
  14. }
  15. }
  16. var id=mobile.getUrlVars("id");
  17. var data;
  18. var custom;
  19. var pageNumber = 1;
  20. var pageSize=10;
  21. mui.init();
  22. /**
  23. *初始化、添加监听
  24. */
  25. mui.ready(function(){
  26. getCustom();
  27. getProductDetail();
  28. $("#sendCode").html(Math.floor(Math.random()*9000)+1000);
  29. $(".header").on("tap",".icon-cha",function(e){
  30. $(".header .mui-table-view").hide();
  31. $(".mui-content").css("padding-top","0");
  32. });
  33. $(".header").on("tap",".guanzhu",function(e){
  34. var html=[];
  35. html.push('<img src="'+imgUrl+custom.wechat_qrcode+'" style="width:100%">');
  36. mui.alert(html.join(""), '关注“<span class="main-color">'+custom.wechat_name+'</span>”');
  37. });
  38. $("#custom").on("tap",function(){
  39. var html=[];
  40. html.push('<img src="'+imgUrl+custom.custom_qrcode+'" style="width:100%">'+
  41. '<div class="main-color">客服电话'+custom.custom_mobile+'</div>');
  42. mui.alert(html.join(""), '客服二维码');
  43. });
  44. $("#user").on("tap",function(){
  45. location.href=base+'/mobile/userCenter.jsp';
  46. });
  47. $(".merchant").on("tap",function(){
  48. location.href=base+'/mobile/merchantDetail.jsp?id='+data.merchant.id;
  49. });
  50. $("#merchant").on("tap",function(){
  51. location.href=base+'/mobile/merchantDetail.jsp?id='+data.merchant.id;
  52. });
  53. $(".tel").on("tap",function(){
  54. location.href = 'tel:'+data.merchant.mobile;
  55. });
  56. $(".map").on("tap",function(){
  57. var locations=bd09togcj02(data.merchant.longitude,data.merchant.latitude);
  58. wx.ready(function(){
  59. wx.openLocation({
  60. latitude: locations[1], // 纬度,浮点数,范围为90 ~ -90
  61. longitude: locations[0], // 经度,浮点数,范围为180 ~ -180。
  62. name: data.merchant.merchant_name, // 位置名
  63. address: data.merchant.address, // 地址详情说明
  64. scale: 25, // 地图缩放级别,整形值,范围从1~28。默认为最大
  65. infoUrl: '' // 在查看位置界面底部显示的超链接,可点击跳转
  66. });
  67. });
  68. });
  69. $(".comment").on("tap",function(){
  70. location.href = base+'/mobile/merchantComment.jsp?id='+data.merchant.id;
  71. });
  72. $(".complain").on("tap",function(){
  73. location.href = base+'/mobile/userComplain.jsp?id='+id;
  74. });
  75. $(".mui-bar-tab").on("tap",".main-bgcolor",function(e){
  76. e.stopPropagation();
  77. if($("#toOrder").html()=="去评价"){
  78. location.href=base+"/mobile/userOrderComment.jsp?id="+data.order.id;
  79. }else if($("#toOrder").html()=="立即抢购"){
  80. buy();
  81. }else if($("#toOrder").html()=="点击登录"){
  82. $('#login').addClass('mui-active');
  83. }else{
  84. location.href=base+"/mobile/userOrderDetail.jsp?id="+data.order.id;
  85. }
  86. });
  87. $("#login").on("tap",'.mui-icon-left-nav',function(){
  88. $('#login').removeClass('mui-active');
  89. });
  90. $("#sendCode").on("tap",function(e){
  91. $("#sendCode").html(Math.floor(Math.random()*9000)+1000);
  92. });
  93. $("#login-btn").on("tap",function(e){
  94. var mobilePhone = $.trim($('#mobile').val());
  95. var code = $.trim($('#code').val());
  96. if(!mobile.checkMobile(mobilePhone)){
  97. layer.open({content:'请输入正确手机号码',time:3});
  98. return;
  99. }
  100. if(code!=$("#sendCode").html()){
  101. layer.open({content:'验证码错误',time:3});
  102. return;
  103. }
  104. user.mobile = mobilePhone;
  105. mobile.ajaxPost(base+'/users/save',user, function(json) {
  106. layer.open({content: '登录成功', time: 3});
  107. setTimeout(function() {
  108. $("#toOrder").html("立即抢购");
  109. $('#login').removeClass('mui-active');
  110. }, 1000);
  111. });
  112. });
  113. $("#use .icon-chucuo1").on("click",function(){
  114. $("#use").remove();
  115. });
  116. $("#use .main").on("tap",function(){
  117. location.href=base+"/mobile/index.jsp";
  118. });
  119. $("#use .order").on("tap",function(){
  120. location.href=base+"/mobile/userOrderDetail.jsp?id="+data.order.id;
  121. });
  122. //推广
  123. $(".tuiguang").on('tap',function(){
  124. if(user.is_share=='1'){
  125. $("#share").show();
  126. }else{
  127. location.href=base+'/mobile/userInfo.jsp?flag=1';
  128. }
  129. });
  130. //分享
  131. $(".share-bg").on('tap',function(){
  132. $("#share").hide();
  133. });
  134. //分享
  135. $(".share-img").on('tap',function(){
  136. $("#share").hide();
  137. });
  138. $("#saveOrder").on('tap',function(){
  139. saveOrder($('.mui-input-numbox').val());
  140. });
  141. $(".haibao").on("tap",function(){
  142. if($("#myImg").attr("src")){
  143. $(".poster").show();
  144. $(".qr-mask").show();
  145. $(".pic-box").hide();
  146. }else{
  147. var l = layer.open({type: 3,shadeClose:false,content: '生成中...'});
  148. $(".poster").show();
  149. html2canvas(document.querySelector("#pic-box"),{useCORS: true}).then(function(canvas) {
  150. $("#myImg").css("height",$(window).height()-120);
  151. $("#myImg").css("width","100%");
  152. $("#myImg").attr("src",getBase64Image(canvas));
  153. $(".qr-mask").show();
  154. $(".pic-box").hide();
  155. layer.close(l);
  156. });
  157. }
  158. });
  159. $(".qr-mask").on("tap",function(){
  160. $(".poster").hide();
  161. $(".qr-mask").hide();
  162. $(".pic-box").show();
  163. })
  164. $('#myImg').on('tap', function(event) {
  165. event.preventDefault();
  166. event.stopPropagation();
  167. });
  168. });
  169. /**
  170. * 抢购
  171. * @returns
  172. */
  173. function buy(){
  174. if(!user.mobile){
  175. $('#login').addClass('mui-active');
  176. }else{
  177. if(data.limit_num!=1){
  178. mui('#popover').popover('toggle');
  179. }else{
  180. saveOrder(1);
  181. }
  182. }
  183. }
  184. function saveOrder(num){
  185. mobile.ajaxPost(base+'/userOrder/save',{
  186. productId: id,
  187. company: sessionStorage.getItem("company"),
  188. shareUser: sessionStorage.getItem("shareUser"),
  189. productNum: num
  190. },function(json){
  191. if(json.success){
  192. location.href=base+"/mobile/userOrderDetail.jsp?id="+json.data.id;
  193. }else{
  194. layer.open({content:json.msg,time:3});
  195. }
  196. });
  197. }
  198. function getDistance(distance){
  199. var i= parseInt(distance);
  200. if(i<1000){
  201. return i+"米";
  202. }else{
  203. var f = parseFloat(i/1000);
  204. if (isNaN(f)) {
  205. return;
  206. }
  207. f = Math.round(i/1000 * 10) / 10;
  208. return f+"公里";
  209. }
  210. }
  211. //获取商品信息
  212. function getCustom(){
  213. mobile.ajaxPost(base+"/sysCustom/getById",{},function(json){
  214. custom = json.data;
  215. $(".wechat_logo").attr("src",imgUrl+custom.wechat_logo);
  216. $(".wechat_name").html(custom.wechat_name);
  217. $(".wechat_summary").html(custom.wechat_summary);
  218. });
  219. }
  220. //获取商品信息
  221. function getProductDetail(){
  222. mobile.ajaxPost(base+"/product/getProduct",{id:id},function(json){
  223. if(json.success){
  224. data = json.data;
  225. if(data.time_status==3){
  226. $("#toOrder").html("已结束");
  227. $("#toOrder").removeClass("main-bgcolor").addClass("bgcolor3");
  228. }else{
  229. if (data.product_type=="10"){
  230. $(".time").removeClass("mui-hidden");
  231. var interval= setInterval(function() {
  232. var typeName;
  233. var time;
  234. var leftTime
  235. if(new Date(data.end_time.replace(/-/g, "/")) > new Date()){
  236. leftTime = (new Date(data.end_time.replace(/-/g, "/"))) - (new Date()); //计算剩余的毫秒数
  237. typeName = "结束";
  238. }else{
  239. leftTime = (new Date(data.use_time.replace(/-/g, "/"))) - (new Date()); //计算剩余的毫秒数
  240. typeName = "过期";
  241. }
  242. var days = parseInt(leftTime / 1000 / 60 / 60 / 24 , 10); //计算剩余的天数
  243. var hours = parseInt(leftTime / 1000 / 60 / 60 % 24 , 10); //计算剩余的小时
  244. var minutes = parseInt(leftTime / 1000 / 60 % 60, 10);//计算剩余的分钟
  245. var seconds = parseInt(leftTime / 1000 % 60, 10);//计算剩余的秒数
  246. hours = checkTime(hours);
  247. minutes = checkTime(minutes);
  248. seconds = checkTime(seconds);
  249. $(".time").html("距"+typeName+days+"天" + hours+"小时" + minutes+"分"+seconds+"秒");
  250. }, 1000);
  251. }
  252. if(data.time_status==0){
  253. var nbox = mui('.mui-numbox').numbox();
  254. //nbox.options["max"]=data.limit_num-data.orderNum;
  255. if(data.order){
  256. if(data.orderNum<data.limit_num){
  257. $("#toOrder").html("立即抢购");
  258. }else{
  259. if(data.order.order_status=="10"){
  260. $("#toOrder").html("去付款");
  261. }else if(data.order.order_status=="20"){
  262. $("#toOrder").html("去使用");
  263. }else if(data.order.order_status=="30"){
  264. if(data.order.comment_flag!="1"){
  265. $("#toOrder").html("去评价");
  266. }else{
  267. $("#toOrder").html("谢谢惠顾");
  268. $("#toOrder").removeClass("main-bgcolor").addClass("bgcolor3");
  269. }
  270. }
  271. }
  272. }else{
  273. if(user.mobile){
  274. $("#toOrder").html("立即抢购");
  275. }else{
  276. $("#toOrder").html("点击登录");
  277. }
  278. }
  279. }else if(data.time_status==1){
  280. $("#toOrder").html("已抢完");
  281. $("#toOrder").removeClass("main-bgcolor").addClass("bgcolor3");
  282. }else {
  283. $("#toOrder").html("已结束");
  284. $("#toOrder").removeClass("main-bgcolor").addClass("bgcolor3");
  285. }
  286. }
  287. share.title=data.product_name;
  288. share.imgUrl=imgUrl+data.product_main_img;
  289. $(".product-img-border img").attr("src",share.imgUrl);
  290. if(data.share_price&&user.is_share=="1"){
  291. $(".share_price").html("推广奖励:"+data.share_price+"元");
  292. $("#share-div").removeClass("mui-hidden");
  293. }
  294. var arr = [];
  295. if(data.product_img_1){
  296. arr.push(data.product_img_1);
  297. $(".pic-box img").attr("src",imgUrl+data.product_img_1);
  298. }
  299. if(data.product_img_2){
  300. arr.push(data.product_img_2);
  301. }
  302. if(data.product_img_3){
  303. arr.push(data.product_img_3);
  304. }
  305. var unit = mobile.getDictVal("product_unit",data.product_unit);
  306. $(".productName").html(data.product_name);
  307. $(".pic-title").html(data.product_name);
  308. $(".price .fs24").html(data.product_price);
  309. $(".product-price").html('<span class="fs16">¥</span><span class="fs24">'+data.product_price+'</span>'+
  310. '<span class="fs12 del-line color3 ml5">¥'+data.product_old_price+'</span>');
  311. $(".pic-price .del-line").html(data.product_old_price+"元");
  312. $(".pic-price .color-red").html(data.product_price+"元");
  313. $(".product_integral").html(parseInt(data.product_price)+"积分");
  314. $(".old_price").html(data.product_old_price+"元");
  315. $(".visit_num").html(data.visit_num+"人关注");
  316. $(".limit_num").html("限购<span class='color-red bold fs14'>"+data.limit_num+"</span>"+unit)
  317. $(".integral").html("返"+parseInt(data.product_price)+"积分");
  318. if(data.payNum>0){
  319. $(".product_num").html("总"+data.product_num+unit+"/剩"+(data.product_num-data.sale_num)+unit+"" +
  320. "<span class='main-color pl5'>"+data.payNum+"人正在付款</span> ");
  321. }else{
  322. $(".product_num").html("总"+data.product_num+unit+"/剩"+(data.product_num-data.sale_num)+unit+"");
  323. }
  324. if(data.order_hour<=0){
  325. $(".order_hour").html("免预约");
  326. }else{
  327. $(".order_hour").html("提前"+data.order_hour+"小时预约");
  328. }
  329. if(data.is_pack==1){
  330. $(".pack").html("提供打包");
  331. }else{
  332. $(".pack").html("店内使用");
  333. }
  334. $(".merchant img").attr("src", imgUrl+data.merchant.merchant_img1);
  335. $(".merchant_name").html(data.merchant.merchant_name);
  336. $(".address").html(data.merchant.address);
  337. if(data.merchant.comment_num!=0){
  338. $(".comment_num").html(data.merchant.comment_num+"人评价")
  339. }
  340. var qrtext = location.href.replace('#','');
  341. if(qrtext.indexOf("shareUser")!=-1){
  342. qrtext = mobile.changeUrlParam(qrtext,"shareUser",user.openid);
  343. }else{
  344. qrtext+="&shareUser="+user.openid;
  345. }
  346. $("#qrcode img").attr("src",base+"/common/qrcode?width=200&height=200&content="+escape(qrtext));
  347. if(data.product_notice){
  348. $("#notice").html(data.product_notice);
  349. }else{
  350. $("#notice").html('<span class="fs14 color2">暂无信息</span>');
  351. }
  352. if(data.product_detail){
  353. $("#detail").html(data.product_detail);
  354. $("img.lazy").lazyload({
  355. threshold : 50,
  356. effect: "fadeIn",
  357. placeholder: base+'/static/mobile/image/500.gif',
  358. skip_invisible : false
  359. });
  360. }else{
  361. $("#detail").html('<span class="fs14 color2">暂无信息</span>');
  362. }
  363. $('.mui-content').removeClass('mui-hidden');
  364. $('.mui-bar-tab').removeClass('mui-hidden');
  365. initSlider(arr);
  366. //获取评价
  367. getHot();
  368. wx.ready(function(){
  369. wx.getLocation({
  370. type: 'wgs84', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
  371. success: function (res) {
  372. $.ajax({
  373. url:'http://api.map.baidu.com/geoconv/v1/?coords='+res.longitude+','+res.latitude+'&from=1&to=5&ak=3Q4laSROftZI4chFMPVUno90',
  374. dataType:'jsonp',
  375. processData: false,
  376. type:'get',
  377. success:function(json){
  378. if (json.status=='0'){
  379. var latitude=json.result[0].y;
  380. var longitude=json.result[0].x;
  381. var map = new BMap.Map("allmap");
  382. var pointA = new BMap.Point(longitude,latitude);
  383. var pointB = new BMap.Point(data.merchant.longitude,data.merchant.latitude);
  384. var distance = parseInt(map.getDistance(pointA,pointB));
  385. $(".distance").html(getDistance(distance));
  386. }
  387. },
  388. error:function(XMLHttpRequest, textStatus, errorThrown) {
  389. layer.msg('坐标转换失败');
  390. }
  391. });
  392. }
  393. });
  394. });
  395. }else{
  396. layer.open({content: json.msg,time: 3});
  397. }
  398. });
  399. }
  400. function initSlider(arr){
  401. $("#slider .mui-slider-group").append('<div class="mui-slider-item mui-slider-item-duplicate">'+
  402. '<a href="javascript:void(0)">'+
  403. '<img class="slider-lazy" data-original="'+imgUrl+arr[arr.length-1]+'" >'+
  404. '</a>'+
  405. '</div>');
  406. if(arr.length>1){//数量大于1定时滚动
  407. for(var i=0;i<arr.length;i++){
  408. $("#slider .mui-slider-group").append('<div class="mui-slider-item">'+
  409. '<a href="javascript:void(0)">'+
  410. '<img class="slider-lazy" data-original="'+imgUrl+arr[i]+'" >'+
  411. '</a>'+
  412. '</div>');
  413. if(i==0){
  414. $("#slider .mui-slider-indicator").append('<div class="mui-indicator mui-active"></div>');
  415. }else{
  416. $("#slider .mui-slider-indicator").append('<div class="mui-indicator"></div>');
  417. }
  418. }
  419. }else{//数量等于1
  420. $("#slider .mui-slider-group").append('<div class="mui-slider-item">'+
  421. '<a href="javascript:void(0)">'+
  422. '<img class="slider-lazy" data-original="'+imgUrl+arr[0]+'" >'+
  423. '</a>'+
  424. '</div>');
  425. }
  426. $("#slider .mui-slider-group").append('<div class="mui-slider-item mui-slider-item-duplicate">'+
  427. '<a href="javascript:void(0)">'+
  428. '<img src="'+imgUrl+arr[0]+'" >'+
  429. '</a>'+
  430. '</div>');
  431. if(arr.length>1){
  432. var slider = mui("#slider");
  433. slider.slider({
  434. interval: 3000
  435. });
  436. }
  437. $("img.slider-lazy").lazyload({
  438. threshold : 200,
  439. placeholder: base+'/static/mobile/image/500.gif',
  440. skip_invisible : false
  441. });
  442. }
  443. //获取评价
  444. function getUserMsg(){
  445. $('.userMsg-content').dropload({
  446. scrollArea : window,
  447. loadDownFn : function(me){
  448. mobile.ajaxPost(base+'/userOrder/getUserMsgList',{
  449. productId: id,
  450. pageSize: pageSize,
  451. pageNumber: pageNumber
  452. },function(json){
  453. if(json.list&&json.list.length>0){
  454. if(pageNumber==json.totalPage){
  455. me.lock();
  456. me.noData();
  457. }else{
  458. pageNumber++;
  459. }
  460. var html=[];
  461. jQuery.each(json.list,function(id,item){
  462. html.push('<li class="mui-table-view-cell">');
  463. html.push('<div class="mui-table">');
  464. html.push('<div class="mui-table-cell mui-col-xs-3 mui-ellipsis">'+getName(item.nickname)+'</div>');
  465. html.push('<div class="mui-table-cell mui-col-xs-6">'+item.user_msg+'</div>');
  466. html.push('<div class="mui-table-cell mui-col-xs-3">'+new Date(item.create_time.replace(/-/g, "/")).format("MM-dd hh:mm")+'</div>');
  467. html.push('</div>');
  468. html.push('</li>');
  469. })
  470. $("#userMsg").append(html.join(""));
  471. me.resetload();
  472. }else{
  473. me.lock();
  474. me.noData();
  475. me.resetload();
  476. }
  477. },"");
  478. }
  479. });
  480. }
  481. function checkTime(i){ // 将0-9的数字前面加上0,例1变为01
  482. if(i<10) {
  483. i = "0" + i;
  484. }
  485. return i;
  486. }
  487. function getName(name){
  488. if(name){
  489. if(name.length==1){
  490. return "*";
  491. }else if(name.length==2){
  492. return name.substring(0,1)+"*";
  493. }else{
  494. return name.substring(0,1)+"*"+name.substring(name.length-1);
  495. }
  496. }else{
  497. return "*";
  498. }
  499. }
  500. function getBase64Image(img) {
  501. var canvas = document.createElement("canvas");
  502. canvas.width = img.width;
  503. canvas.height = img.height;
  504. var ctx = canvas.getContext("2d");
  505. ctx.drawImage(img, 0, 0, img.width, img.height);
  506. var dataURL = canvas.toDataURL("image/png");
  507. return dataURL
  508. }
  509. function getHot() {
  510. mobile.ajaxPost(base+'/product/getHot',{id:id},function(json){
  511. if(json.data&&json.data.length>0){
  512. var html=[];
  513. jQuery.each(json.data,function(id,item){
  514. html.push('<a class="section-item" href="'+base+'/mobile/productDetail.jsp?id='+item.id+'">');
  515. html.push('<div class="image-box">');
  516. html.push('<img class="product-lazy" data-original="'+imgUrl+item.product_main_img+'" style="width: 100%; display: block;">');
  517. html.push('</div>');
  518. html.push('<div class="info-box">');
  519. html.push('<div class="info-title">'+item.product_name+'</div>');
  520. html.push('<div class="items">');
  521. html.push('<div class="product-price item"><em>¥'+item.product_price+'<span class="del-line color3 ml5">'+item.product_old_price+'元</span></em></div>')
  522. html.push('<div class="fs12 color3">关注:'+item.visit_num+'</div>')
  523. html.push('</div></div>');
  524. html.push('</a>');
  525. });
  526. $(".section-list").append(html.join(""));
  527. $("img.product-lazy").lazyload({
  528. threshold : 200,
  529. effect: "fadeIn",
  530. placeholder: base+'/static/mobile/image/100.gif',
  531. skip_invisible : false
  532. });
  533. }else{
  534. }
  535. });
  536. }