$(function(){
pushHistory();
window.addEventListener("popstate", function(e) {
location.href=base+"/mobile/index.jsp";
}, false);
});
function pushHistory() {
var state = {
title: "title",
url: "#"
};
if(window.history.length==1){
window.history.pushState(state, "title", "#");
}
}
var id=mobile.getUrlVars("id");
var cutId=mobile.getUrlVars("cutId");
var help=mobile.getUrlVars("help");
var data;
var custom;
var pageNumber = 1;
var pageSize=10;
mui.init();
/**
*初始化、添加监听
*/
mui.ready(function(){
getCustom();
if (cutId){
getCutInfo();
$(".cut-info").removeClass("mui-hidden");
$("#cut-list-div").removeClass("mui-hidden");
}else{
getProductDetail();
}
$("#sendCode").html(Math.floor(Math.random()*9000)+1000);
$(".header").on("tap",".guanzhu",function(e){
var html=[];
html.push('
');
mui.alert(html.join(""), '关注“'+custom.wechat_name+'”');
});
$(".footer").on("tap",function(e){
var html=[];
html.push('
' +
'电话:18797831026');
mui.alert(html.join(""), '长按二维码联系客服');
});
$(".merchant").on("tap",function(){
location.href=base+'/mobile/merchantDetail.jsp?id='+data.merchant.id;
});
$(".tel").on("tap",function(){
location.href = 'tel:'+data.merchant.mobile;
});
$(".map").on("tap",function(){
var locations=bd09togcj02(data.merchant.longitude,data.merchant.latitude);
wx.ready(function(){
wx.openLocation({
latitude: locations[1], // 纬度,浮点数,范围为90 ~ -90
longitude: locations[0], // 经度,浮点数,范围为180 ~ -180。
name: data.merchant.merchant_name, // 位置名
address: data.merchant.address, // 地址详情说明
scale: 25, // 地图缩放级别,整形值,范围从1~28。默认为最大
infoUrl: '' // 在查看位置界面底部显示的超链接,可点击跳转
});
});
});
$(".comment").on("tap",function(){
location.href = base+'/mobile/merchantComment.jsp?id='+data.merchant.id;
});
$("#login").on("tap",'.mui-icon-left-nav',function(){
$('#login').removeClass('mui-active');
});
$("#sendCode").on("tap",function(e){
$("#sendCode").html(Math.floor(Math.random()*9000)+1000);
});
$("#login-btn").on("tap",function(e){
var mobilePhone = $.trim($('#mobile').val());
var code = $.trim($('#code').val());
if(!mobile.checkMobile(mobilePhone)){
layer.open({content:'请输入正确手机号码',time:3});
return;
}
if(code!=$("#sendCode").html()){
layer.open({content:'验证码错误',time:3});
return;
}
user.mobile = mobilePhone;
mobile.ajaxPost(base+'/users/save',user, function(json) {
layer.open({content: '登录成功', time: 3});
setTimeout(function() {
$("#toOrder").html("立即抢购");
$('#login').removeClass('mui-active');
}, 1000);
});
});
$(".btns").on("tap",".baoming",function () {
if(!user.mobile){
$('#login').addClass('mui-active');
}else{
if(user.subscribe=="1"){
baoming();
}else{
var html=[];
html.push('
' +
'长按识别图中二维码');
mui.alert(html.join(""), '请先关注我们');
}
}
});
//我的砍价
$(".btns").on("tap",".my",function () {
if (!cutId){
location.href=base+"/mobile/cutProductDetail.jsp?id="+id+"&cutId="+data.cutInfo.id;
}else{
location.href=base+"/mobile/cutProductDetail.jsp?id="+id+"&cutId="+data.myCutInfo.id;
}
});
//其他砍价
$(".btns").on("tap",".qita",function () {
location.href=base+"/mobile/cutProductList.jsp";
});
//帮好友减价
$(".btns").on("tap",".cut",function () {
cut();
});
//邀请
$(".btns").on("tap",".yaoqing",function () {
$("#share").show();
});
//购买
$(".btns").on("tap",".buy",function () {
saveOrder();
});
//分享
$(".share-bg").on('tap',function(){
$("#share").hide();
});
//分享
$(".share-img").on('tap',function(){
$("#share").hide();
});
});
//获取商品信息
function getCustom(){
mobile.ajaxPost(base+"/sysCustom/getById",{},function(json){
custom = json.data;
});
}
//获取商品信息
function getProductDetail(){
mobile.ajaxPost(base+"/product/getProduct",{id:id},function(json){
if(json.success){
data = json.data;
if(data.time_status==0){
var interval= setInterval(function() {
var leftTime = (new Date(data.end_time.replace(/-/g, "/"))) - (new Date());
var days = parseInt(leftTime / 1000 / 60 / 60 / 24 , 10); //计算剩余的天数
var hours = parseInt(leftTime / 1000 / 60 / 60 % 24 , 10); //计算剩余的小时
var minutes = parseInt(leftTime / 1000 / 60 % 60, 10);//计算剩余的分钟
var seconds = parseInt(leftTime / 1000 % 60, 10);//计算剩余的秒数
hours = checkTime(hours);
minutes = checkTime(minutes);
seconds = checkTime(seconds);
$(".time").html("减价剩余"+days+"天" + hours+"小时" +
"" + minutes+"分"+seconds+"秒");
}, 1000);
if (data.cutInfo){
$(".btns").append('
去我的减价>>
');
}else{
$(".btns").append('我要报名
');
}
}else if(data.time_status==1){
$(".time").html("已抢完");
}else{
$(".time").html("已结束");
$(".btns").append('活动已结束,查询其他活动>>
');
}
share.title=data.product_name;
share.imgUrl=imgUrl+data.product_main_img;
var arr = [];
if(data.product_img_1){
arr.push(data.product_img_1);
$(".pic-box img").attr("src",imgUrl+data.product_img_1);
}
if(data.product_img_2){
arr.push(data.product_img_2);
}
if(data.product_img_3){
arr.push(data.product_img_3);
}
var unit = mobile.getDictVal("product_unit",data.product_unit);
$(".productName").html(data.product_name);
$(".product-price").html('底价 ¥ '+data.product_price+'');
$(".old_price").html("原价 ¥"+data.product_old_price+"元");
$(".visit_num").html(data.visit_num+"人查看");
$(".share_num").html(data.shareNum+"人分享");
$(".user_num").html(data.userNum+"人参与");
$(".use_time").html("兑换有效期至:"+data.use_time);
$(".product_num").html("仅剩"+(data.product_num-data.sale_num)+"/"+data.product_num+unit+"");
if(data.sale_num!=0){
$('.sale-num').html('已售'+data.sale_num+'份');
getOrderUser();
}else{
$('.sale-num-div').hide();
}
$(".merchant_name").html(data.merchant.merchant_name);
$(".merchant img").attr("src", imgUrl+data.merchant.merchant_img1);
$(".address").html(data.merchant.address);
if(data.merchant.comment_num!=0){
$(".comment_num").html(data.merchant.comment_num+"人评价")
}
if(data.product_detail){
$("#detail").html(data.product_detail);
$("img.lazy").lazyload({
threshold : 50,
effect: "fadeIn",
placeholder: base+'/static/mobile/image/500.gif',
skip_invisible : false
});
}else{
$("#detail").html('暂无信息');
}
$('.mui-content').removeClass('mui-hidden');
$('.mui-bar-tab').removeClass('mui-hidden');
initSlider(arr);
//获取评价
getUserMsg();
wx.ready(function(){
wx.getLocation({
type: 'wgs84', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
success: function (res) {
$.ajax({
url:'http://api.map.baidu.com/geoconv/v1/?coords='+res.longitude+','+res.latitude+'&from=1&to=5&ak=3Q4laSROftZI4chFMPVUno90',
dataType:'jsonp',
processData: false,
type:'get',
success:function(json){
if (json.status=='0'){
var latitude=json.result[0].y;
var longitude=json.result[0].x;
var map = new BMap.Map("allmap");
var pointA = new BMap.Point(longitude,latitude);
var pointB = new BMap.Point(data.merchant.longitude,data.merchant.latitude);
var distance = parseInt(map.getDistance(pointA,pointB));
$(".distance").html(getDistance(distance));
}
},
error:function(XMLHttpRequest, textStatus, errorThrown) {
layer.msg('坐标转换失败');
}
});
}
});
});
}else{
layer.open({content: json.msg,time: 3});
}
});
}
//获取商品信息
function getCutInfo(){
mobile.ajaxPost(base+"/product/getCutInfo",{cutId:cutId},function(json){
if(json.success){
data = json.data;
if(data.time_status==0){
var interval= setInterval(function() {
var leftTime = (new Date(data.end_time.replace(/-/g, "/"))) - (new Date());
var days = parseInt(leftTime / 1000 / 60 / 60 / 24 , 10); //计算剩余的天数
var hours = parseInt(leftTime / 1000 / 60 / 60 % 24 , 10); //计算剩余的小时
var minutes = parseInt(leftTime / 1000 / 60 % 60, 10);//计算剩余的分钟
var seconds = parseInt(leftTime / 1000 % 60, 10);//计算剩余的秒数
hours = checkTime(hours);
minutes = checkTime(minutes);
seconds = checkTime(seconds);
$(".time").html("减价剩余"+days+"天" + hours+"小时" +
"" + minutes+"分"+seconds+"秒");
}, 1000);
if(data.myCutInfo&&data.myCutInfo!=null){
if (data.myCutInfo.id==data.cutInfo.id){
$(".btns").append('自己减价
');
$(".btns").append('邀请好友减价
');
$(".btns").append('立即购买
');
}else{
$(".btns").append('帮好友减价
');
$(".btns").append('去我的减价>>
');
}
}else{
$(".btns").append('帮好友减价
');
$(".btns").append('我要报名
');
}
}else if(data.time_status==1){
$(".time").html("已抢完");
$(".btns").append('活动已经抢光,查询其他活动>>
');
}else{
$(".time").html("已结束");
$(".btns").append('活动已结束,查询其他活动>>
');
}
share.title=data.product_name;
share.imgUrl=imgUrl+data.product_main_img;
var arr = [];
if(data.product_img_1){
arr.push(data.product_img_1);
$(".pic-box img").attr("src",imgUrl+data.product_img_1);
}
if(data.product_img_2){
arr.push(data.product_img_2);
}
if(data.product_img_3){
arr.push(data.product_img_3);
}
var unit = mobile.getDictVal("product_unit",data.product_unit);
$(".productName").html(data.product_name);
$(".product-price").html('底价 ¥ '+data.product_price+'');
$(".cut-price").html(data.cutInfo.product_price+"元");
$("#userImg").attr("src",data.user.headimgurl);
$("#userName").html(data.user.nickname);
$(".old_price").html("原价 ¥"+data.product_old_price+"元");
$(".visit_num").html(data.visit_num+"人查看");
$(".share_num").html(data.shareNum+"人分享");
$(".user_num").html(data.userNum+"人参与");
$(".use_time").html("兑换有效期至:"+data.use_time);
$(".product_num").html("仅剩"+(data.product_num-data.sale_num)+"/"+data.product_num+unit+"");
if(data.sale_num!=0){
$('.sale-num').html('已售'+data.sale_num+'份');
getOrderUser();
}else{
$('.sale-num-div').hide();
}
if (data.cutDetail){
$(".help-num").html(data.cutDetail.length+"人");
$(".cut-list").empty();
if (data.cutDetail.length>0){
$.each(data.cutDetail,function (id,item) {
$(".cut-list").append('');
})
}else{
$(".cut-list").append('暂无');
}
}
$(".merchant_name").html(data.merchant.merchant_name);
$(".merchant img").attr("src", imgUrl+data.merchant.merchant_img1);
$(".address").html(data.merchant.address);
if(data.merchant.comment_num!=0){
$(".comment_num").html(data.merchant.comment_num+"人评价")
}
if(data.product_detail){
$("#detail").html(data.product_detail);
$("img.lazy").lazyload({
threshold : 50,
effect: "fadeIn",
placeholder: base+'/static/mobile/image/500.gif',
skip_invisible : false
});
}else{
$("#detail").html('暂无信息');
}
$('.mui-content').removeClass('mui-hidden');
$('.mui-bar-tab').removeClass('mui-hidden');
initSlider(arr);
//获取评价
getUserMsg();
wx.ready(function(){
wx.getLocation({
type: 'wgs84', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
success: function (res) {
$.ajax({
url:'http://api.map.baidu.com/geoconv/v1/?coords='+res.longitude+','+res.latitude+'&from=1&to=5&ak=3Q4laSROftZI4chFMPVUno90',
dataType:'jsonp',
processData: false,
type:'get',
success:function(json){
if (json.status=='0'){
var latitude=json.result[0].y;
var longitude=json.result[0].x;
var map = new BMap.Map("allmap");
var pointA = new BMap.Point(longitude,latitude);
var pointB = new BMap.Point(data.merchant.longitude,data.merchant.latitude);
var distance = parseInt(map.getDistance(pointA,pointB));
$(".distance").html(getDistance(distance));
}
},
error:function(XMLHttpRequest, textStatus, errorThrown) {
layer.msg('坐标转换失败');
}
});
}
});
});
}else{
layer.open({content: json.msg,time: 3});
}
});
}
function initSlider(arr){
$("#slider .mui-slider-group").append('');
if(arr.length>1){//数量大于1定时滚动
for(var i=0;i'+
''+
'
'+
''+
'');
if(i==0){
$("#slider .mui-slider-indicator").append('');
}else{
$("#slider .mui-slider-indicator").append('');
}
}
}else{//数量等于1
$("#slider .mui-slider-group").append('');
}
$("#slider .mui-slider-group").append('');
if(arr.length>1){
var slider = mui("#slider");
slider.slider({
interval: 3000
});
}
$("img.slider-lazy").lazyload({
threshold : 200,
placeholder: base+'/static/mobile/image/500.gif',
skip_invisible : false
});
}
function getOrderUser() {
mobile.ajaxPost(base+'/userOrder/getUserMsgList',{
productId: id,
pageSize: pageSize,
pageNumber: 1
},function(json){
if(json.list&&json.list.length>0){
$.each(json.list,function (id,item) {
if (id<9){
$(".sale-num-div").append('');
}
});
}
});
}
//获取评价
function getUserMsg(){
$('.userMsg-content').dropload({
scrollArea : window,
loadDownFn : function(me){
mobile.ajaxPost(base+'/userOrder/getUserMsgList',{
productId: id,
pageSize: pageSize,
pageNumber: pageNumber
},function(json){
if(json.list&&json.list.length>0){
if(pageNumber==json.totalPage){
me.lock();
me.noData();
}else{
pageNumber++;
}
var html=[];
jQuery.each(json.list,function(id,item){
html.push('');
html.push('');
html.push('
'+getName(item.nickname)+'
');
if (item.order_amt==data.product_price){
html.push('
减到低价
');
}else{
html.push('
'+item.order_amt+'元
');
}
html.push('
'+new Date(item.create_time.replace(/-/g, "/")).format("MM-dd hh:mm")+'
');
html.push('
');
html.push('');
})
$("#userMsg").append(html.join(""));
me.resetload();
}else{
me.lock();
me.noData();
me.resetload();
}
},"");
}
});
}
function checkTime(i){ // 将0-9的数字前面加上0,例1变为01
if(i<10) {
i = "0" + i;
}
return i;
}
function getName(name){
if(name){
if(name.length==1){
return "*";
}else if(name.length==2){
return name.substring(0,1)+"*";
}else{
return name.substring(0,1)+"*"+name.substring(name.length-1);
}
}else{
return "*";
}
}
function baoming() {
mobile.ajaxPost(base+'/cutInfo/save',{
productId: id
},function(json){
if(json.success){
layer.open({content:'报名成功',time:3});
setTimeout(function() {
location.href=base+"/mobile/cutProductDetail.jsp?id="+id+"&cutId="+json.data.id;
}, 1000);
}else{
layer.open({content:json.msg,time:3});
}
});
}
function cut() {
mobile.ajaxPost(base+'/cutDetail/save',{
cutId: cutId
},function(json){
if(json.success){
mui.alert('减价成功,帮忙减了'+mobile.toDecimal(json.data.cut_price)+'元', '提示信息');
$(".cut-price").html(mobile.toDecimal(json.data.product_price)+"元");
if (json.data.cutDetail){
$(".help-num").html(json.data.cutDetail.length+"人");
$(".cut-list").empty();
if (json.data.cutDetail.length>0){
$.each(json.data.cutDetail,function (id,item) {
$(".cut-list").append('');
})
}else{
$(".cut-list").append('暂无');
}
}
}else{
layer.open({content:json.msg,time:3});
}
});
}
function saveOrder(){
mobile.ajaxPost(base+'/userOrder/saveCut',{
productId: id,
cutId:cutId,
company: sessionStorage.getItem("company"),
shareUser: sessionStorage.getItem("shareUser")
},function(json){
if(json.success){
location.href=base+"/mobile/userOrderDetail.jsp?id="+json.data.id;
}else{
layer.open({content:json.msg,time:3});
}
});
}
function getDistance(distance){
var i= parseInt(distance);
if(i<1000){
return i+"米";
}else{
var f = parseFloat(i/1000);
if (isNaN(f)) {
return;
}
f = Math.round(i/1000 * 10) / 10;
return f+"公里";
}
}