productDetail.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. $("#kefu").on("tap",function(e){
  131. var html=[];
  132. html.push('<img src="'+mobile.getParam('customer_qrcode')+'" style="width:100%">' +
  133. '<a class="fs16" href="tel:'+mobile.getParam('customer_tel')+'">电话:'+mobile.getParam('customer_tel')+'</a>');
  134. mui.alert(html.join(""), '长按二维码联系客服');
  135. });
  136. //分享
  137. $(".share-bg").on('tap',function(){
  138. $("#share").hide();
  139. });
  140. //分享
  141. $(".share-img").on('tap',function(){
  142. $("#share").hide();
  143. });
  144. $("#saveOrder").on('tap',function(){
  145. saveOrder($('.mui-input-numbox').val());
  146. });
  147. $(".haibao").on("tap",function(){
  148. if($("#myImg").attr("src")){
  149. $(".poster").show();
  150. $(".qr-mask").show();
  151. $(".pic-box").hide();
  152. }else{
  153. var l = layer.open({type: 3,shadeClose:false,content: '生成中...'});
  154. $(".poster").show();
  155. html2canvas(document.querySelector("#pic-box"),{useCORS: true}).then(function(canvas) {
  156. // $("#myImg").css("height",$(window).height()-120);
  157. $("#myImg").css("width","100%");
  158. $("#myImg").attr("src",getBase64Image(canvas));
  159. $(".qr-mask").show();
  160. $(".pic-box").hide();
  161. layer.close(l);
  162. });
  163. }
  164. });
  165. $(".qr-mask").on("tap",function(){
  166. $(".poster").hide();
  167. $(".qr-mask").hide();
  168. $(".pic-box").show();
  169. })
  170. $('#myImg').on('tap', function(event) {
  171. event.preventDefault();
  172. event.stopPropagation();
  173. });
  174. });
  175. /**
  176. * 抢购
  177. * @returns
  178. */
  179. function buy(){
  180. if(!user.mobile){
  181. $('#login').addClass('mui-active');
  182. }else{
  183. if(data.limit_num!=1){
  184. mui('#popover').popover('toggle');
  185. }else{
  186. saveOrder(1);
  187. }
  188. }
  189. }
  190. function saveOrder(num){
  191. mobile.ajaxPost(base+'/userOrder/save',{
  192. productId: id,
  193. company: sessionStorage.getItem("company"),
  194. shareUser: sessionStorage.getItem("shareUser"),
  195. productNum: num
  196. },function(json){
  197. if(json.success){
  198. location.href=base+"/mobile/userOrderDetail.jsp?id="+json.data.id;
  199. }else{
  200. layer.open({content:json.msg,time:3});
  201. }
  202. });
  203. }
  204. function getDistance(distance){
  205. var i= parseInt(distance);
  206. if(i<1000){
  207. return i+"米";
  208. }else{
  209. var f = parseFloat(i/1000);
  210. if (isNaN(f)) {
  211. return;
  212. }
  213. f = Math.round(i/1000 * 10) / 10;
  214. return f+"公里";
  215. }
  216. }
  217. //获取商品信息
  218. function getCustom(){
  219. mobile.ajaxPost(base+"/sysCustom/getById",{},function(json){
  220. custom = json.data;
  221. $(".wechat_logo").attr("src",imgUrl+custom.wechat_logo);
  222. $(".wechat_name").html(custom.wechat_name);
  223. $(".wechat_summary").html(custom.wechat_summary);
  224. });
  225. }
  226. //获取商品信息
  227. function getProductDetail(){
  228. mobile.ajaxPost(base+"/product/getProduct",{id:id},function(json){
  229. if(json.success){
  230. data = json.data;
  231. if(data.time_status==3){
  232. $("#toOrder").html("已结束");
  233. $("#toOrder").removeClass("main-bgcolor").addClass("bgcolor3");
  234. }else{
  235. if (data.product_type=="10"){
  236. $(".time").removeClass("mui-hidden");
  237. var interval= setInterval(function() {
  238. var typeName;
  239. var time;
  240. var leftTime
  241. if(new Date(data.end_time.replace(/-/g, "/")) > new Date()){
  242. leftTime = (new Date(data.end_time.replace(/-/g, "/"))) - (new Date()); //计算剩余的毫秒数
  243. typeName = "结束";
  244. }else{
  245. leftTime = (new Date(data.use_time.replace(/-/g, "/"))) - (new Date()); //计算剩余的毫秒数
  246. typeName = "过期";
  247. }
  248. var days = parseInt(leftTime / 1000 / 60 / 60 / 24 , 10); //计算剩余的天数
  249. var hours = parseInt(leftTime / 1000 / 60 / 60 % 24 , 10); //计算剩余的小时
  250. var minutes = parseInt(leftTime / 1000 / 60 % 60, 10);//计算剩余的分钟
  251. var seconds = parseInt(leftTime / 1000 % 60, 10);//计算剩余的秒数
  252. hours = checkTime(hours);
  253. minutes = checkTime(minutes);
  254. seconds = checkTime(seconds);
  255. $(".time").html("距"+typeName+days+"天" + hours+"小时" + minutes+"分"+seconds+"秒");
  256. }, 1000);
  257. }
  258. if(data.time_status==0){
  259. var nbox = mui('.mui-numbox').numbox();
  260. //nbox.options["max"]=data.limit_num-data.orderNum;
  261. if(data.order){
  262. if(data.orderNum<data.limit_num){
  263. $("#toOrder").html("立即抢购");
  264. }else{
  265. if(data.order.order_status=="10"){
  266. $("#toOrder").html("去付款");
  267. }else if(data.order.order_status=="20"){
  268. $("#toOrder").html("去使用");
  269. }else if(data.order.order_status=="30"){
  270. if(data.order.comment_flag!="1"){
  271. $("#toOrder").html("去评价");
  272. }else{
  273. $("#toOrder").html("谢谢惠顾");
  274. $("#toOrder").removeClass("main-bgcolor").addClass("bgcolor3");
  275. }
  276. }
  277. }
  278. }else{
  279. if(user.mobile){
  280. $("#toOrder").html("立即抢购");
  281. }else{
  282. $("#toOrder").html("点击登录");
  283. }
  284. }
  285. }else if(data.time_status==1){
  286. $("#toOrder").html("已抢完");
  287. $("#toOrder").removeClass("main-bgcolor").addClass("bgcolor3");
  288. }else {
  289. $("#toOrder").html("已结束");
  290. $("#toOrder").removeClass("main-bgcolor").addClass("bgcolor3");
  291. }
  292. }
  293. share.title=data.product_name;
  294. share.imgUrl=imgUrl+data.product_main_img;
  295. $(".product-img-border img").attr("src",share.imgUrl);
  296. if(data.share_price&&user.is_share=="1"){
  297. $(".share_price").html("推广奖励:"+data.share_price+"元");
  298. $("#share-div").removeClass("mui-hidden");
  299. }
  300. var arr = [];
  301. if(data.product_img_1){
  302. arr.push(data.product_img_1);
  303. $(".pic-box img").attr("src",imgUrl+data.product_img_1);
  304. }
  305. if(data.product_img_2){
  306. arr.push(data.product_img_2);
  307. }
  308. if(data.product_img_3){
  309. arr.push(data.product_img_3);
  310. }
  311. var unit = mobile.getDictVal("product_unit",data.product_unit);
  312. $(".productName").html(data.product_name);
  313. $(".pic-title").html(data.product_name);
  314. $(".price .fs24").html(data.product_price);
  315. $(".product-price").html('<span class="fs16">¥</span><span class="fs24">'+data.product_price+'</span>'+
  316. '<span class="fs12 del-line color3 ml5">¥'+data.product_old_price+'</span>');
  317. $(".pic-price .del-line").html(data.product_old_price+"元");
  318. $(".pic-price .color-red").html(data.product_price+"元");
  319. $(".product_integral").html(parseInt(data.product_price)+"积分");
  320. $(".old_price").html(data.product_old_price+"元");
  321. $(".visit_num").html(data.visit_num+"人关注");
  322. $(".limit_num").html("限购<span class='color-red bold fs14'>"+data.limit_num+"</span>"+unit)
  323. $(".integral").html("返"+parseInt(data.product_price)+"积分");
  324. var left_num = data.product_num-data.sale_num-data.init_num;
  325. if (left_num<0){
  326. left_num=0;
  327. }
  328. if(data.payNum>0){
  329. $(".product_num").html("总"+data.product_num+unit+"/剩"+(left_num)+unit+"" +
  330. "<span class='main-color pl5'>"+data.payNum+"人正在付款</span> ");
  331. }else{
  332. $(".product_num").html("总"+data.product_num+unit+"/剩"+(left_num)+unit+"");
  333. }
  334. if(data.order_hour<=0){
  335. $(".order_hour").html("免预约");
  336. }else{
  337. $(".order_hour").html("提前"+data.order_hour+"小时预约");
  338. }
  339. if(data.is_pack==1){
  340. $(".pack").html("提供打包");
  341. }else{
  342. $(".pack").html("店内使用");
  343. }
  344. $(".merchant img").attr("src", imgUrl+data.merchant.merchant_img1);
  345. $(".merchant_name").html(data.merchant.merchant_name);
  346. $(".address").html(data.merchant.address);
  347. if(data.merchant.comment_num!=0){
  348. $(".comment_num").html(data.merchant.comment_num+"人评价")
  349. }
  350. var qrtext = location.href.replace('#','');
  351. if(qrtext.indexOf("shareUser")!=-1){
  352. qrtext = mobile.changeUrlParam(qrtext,"shareUser",user.openid);
  353. }else{
  354. qrtext+="&shareUser="+user.openid;
  355. }
  356. $("#qrcode img").attr("src",siteUrl+"/common/qrcode?width=80&height=80&content="+escape(qrtext));
  357. if(data.product_notice){
  358. $("#notice").html(data.product_notice);
  359. }else{
  360. $("#notice").html('<span class="fs14 color2">暂无信息</span>');
  361. }
  362. if(data.product_detail){
  363. $("#detail").html(data.product_detail);
  364. $("img.lazy").lazyload({
  365. threshold : 50,
  366. effect: "fadeIn",
  367. placeholder: base+'/static/mobile/image/500.gif',
  368. skip_invisible : false
  369. });
  370. }else{
  371. $("#detail").html('<span class="fs14 color2">暂无信息</span>');
  372. }
  373. $('.mui-content').removeClass('mui-hidden');
  374. $('.mui-bar-tab').removeClass('mui-hidden');
  375. initSlider(arr);
  376. //获取评价
  377. getHot();
  378. wx.ready(function(){
  379. wx.getLocation({
  380. type: 'wgs84', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
  381. success: function (res) {
  382. $.ajax({
  383. url:'http://api.map.baidu.com/geoconv/v1/?coords='+res.longitude+','+res.latitude+'&from=1&to=5&ak=3Q4laSROftZI4chFMPVUno90',
  384. dataType:'jsonp',
  385. processData: false,
  386. type:'get',
  387. success:function(json){
  388. if (json.status=='0'){
  389. var latitude=json.result[0].y;
  390. var longitude=json.result[0].x;
  391. var map = new BMap.Map("allmap");
  392. var pointA = new BMap.Point(longitude,latitude);
  393. var pointB = new BMap.Point(data.merchant.longitude,data.merchant.latitude);
  394. var distance = parseInt(map.getDistance(pointA,pointB));
  395. $(".distance").html(getDistance(distance));
  396. }
  397. },
  398. error:function(XMLHttpRequest, textStatus, errorThrown) {
  399. layer.msg('坐标转换失败');
  400. }
  401. });
  402. }
  403. });
  404. });
  405. }else{
  406. layer.open({content: json.msg,time: 3});
  407. }
  408. });
  409. }
  410. function initSlider(arr){
  411. $("#slider .mui-slider-group").append('<div class="mui-slider-item mui-slider-item-duplicate">'+
  412. '<a href="javascript:void(0)">'+
  413. '<img class="slider-lazy" data-original="'+imgUrl+arr[arr.length-1]+'" >'+
  414. '</a>'+
  415. '</div>');
  416. if(arr.length>1){//数量大于1定时滚动
  417. for(var i=0;i<arr.length;i++){
  418. $("#slider .mui-slider-group").append('<div class="mui-slider-item">'+
  419. '<a href="javascript:void(0)">'+
  420. '<img class="slider-lazy" data-original="'+imgUrl+arr[i]+'" >'+
  421. '</a>'+
  422. '</div>');
  423. if(i==0){
  424. $("#slider .mui-slider-indicator").append('<div class="mui-indicator mui-active"></div>');
  425. }else{
  426. $("#slider .mui-slider-indicator").append('<div class="mui-indicator"></div>');
  427. }
  428. }
  429. }else{//数量等于1
  430. $("#slider .mui-slider-group").append('<div class="mui-slider-item">'+
  431. '<a href="javascript:void(0)">'+
  432. '<img class="slider-lazy" data-original="'+imgUrl+arr[0]+'" >'+
  433. '</a>'+
  434. '</div>');
  435. }
  436. $("#slider .mui-slider-group").append('<div class="mui-slider-item mui-slider-item-duplicate">'+
  437. '<a href="javascript:void(0)">'+
  438. '<img src="'+imgUrl+arr[0]+'" >'+
  439. '</a>'+
  440. '</div>');
  441. if(arr.length>1){
  442. var slider = mui("#slider");
  443. slider.slider({
  444. interval: 3000
  445. });
  446. }
  447. $("img.slider-lazy").lazyload({
  448. threshold : 200,
  449. placeholder: base+'/static/mobile/image/500.gif',
  450. skip_invisible : false
  451. });
  452. }
  453. //获取评价
  454. function getUserMsg(){
  455. $('.userMsg-content').dropload({
  456. scrollArea : window,
  457. loadDownFn : function(me){
  458. mobile.ajaxPost(base+'/userOrder/getUserMsgList',{
  459. productId: id,
  460. pageSize: pageSize,
  461. pageNumber: pageNumber
  462. },function(json){
  463. if(json.list&&json.list.length>0){
  464. if(pageNumber==json.totalPage){
  465. me.lock();
  466. me.noData();
  467. }else{
  468. pageNumber++;
  469. }
  470. var html=[];
  471. jQuery.each(json.list,function(id,item){
  472. html.push('<li class="mui-table-view-cell">');
  473. html.push('<div class="mui-table">');
  474. html.push('<div class="mui-table-cell mui-col-xs-3 mui-ellipsis">'+getName(item.nickname)+'</div>');
  475. html.push('<div class="mui-table-cell mui-col-xs-6">'+item.user_msg+'</div>');
  476. html.push('<div class="mui-table-cell mui-col-xs-3">'+new Date(item.create_time.replace(/-/g, "/")).format("MM-dd hh:mm")+'</div>');
  477. html.push('</div>');
  478. html.push('</li>');
  479. })
  480. $("#userMsg").append(html.join(""));
  481. me.resetload();
  482. }else{
  483. me.lock();
  484. me.noData();
  485. me.resetload();
  486. }
  487. },"");
  488. }
  489. });
  490. }
  491. function checkTime(i){ // 将0-9的数字前面加上0,例1变为01
  492. if(i<10) {
  493. i = "0" + i;
  494. }
  495. return i;
  496. }
  497. function getName(name){
  498. if(name){
  499. if(name.length==1){
  500. return "*";
  501. }else if(name.length==2){
  502. return name.substring(0,1)+"*";
  503. }else{
  504. return name.substring(0,1)+"*"+name.substring(name.length-1);
  505. }
  506. }else{
  507. return "*";
  508. }
  509. }
  510. function getBase64Image(img) {
  511. var canvas = document.createElement("canvas");
  512. canvas.width = img.width;
  513. canvas.height = img.height;
  514. var ctx = canvas.getContext("2d");
  515. ctx.drawImage(img, 0, 0, img.width, img.height);
  516. var dataURL = canvas.toDataURL("image/png");
  517. return dataURL
  518. }
  519. function getHot() {
  520. mobile.ajaxPost(base+'/product/getHot',{id:id},function(json){
  521. if(json.data&&json.data.length>0){
  522. var html=[];
  523. jQuery.each(json.data,function(id,item){
  524. html.push('<a class="section-item" href="'+base+'/mobile/productDetail.jsp?id='+item.id+'">');
  525. html.push('<div class="image-box">');
  526. html.push('<img class="product-lazy" data-original="'+imgUrl+item.product_main_img+'" style="width: 100%; display: block;">');
  527. html.push('</div>');
  528. html.push('<div class="info-box">');
  529. html.push('<div class="info-title">'+item.product_name+'</div>');
  530. html.push('<div class="items">');
  531. html.push('<div class="product-price item"><em>¥'+item.product_price+'<span class="del-line color3 ml5">'+item.product_old_price+'元</span></em></div>')
  532. html.push('<div class="fs12 color3">关注:'+item.visit_num+'</div>')
  533. html.push('</div></div>');
  534. html.push('</a>');
  535. });
  536. $(".section-list").append(html.join(""));
  537. $("img.product-lazy").lazyload({
  538. threshold : 200,
  539. effect: "fadeIn",
  540. placeholder: base+'/static/mobile/image/100.gif',
  541. skip_invisible : false
  542. });
  543. }else{
  544. }
  545. });
  546. }