123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- /**
- * Created by chen on 2017/5/12.
- */
- window.onload=function () {
- var aFooterLi=document.getElementsByClassName("footNav")[0].getElementsByTagName("li"),
- // aSelectBtn=document.getElementsByClassName("selectBtn"),
- oTotalBtn=document.getElementsByClassName("totalBtn")[0],
- remindDiv=document.getElementsByClassName("remindDiv")[0],
- oShopList=document.getElementsByClassName("carList")[0],
- aLi=oShopList.getElementsByTagName("li"),
- // oTotalLe=document.getElementsByClassName("totalLe")[0],
- // oRemindDiv=document.getElementById("remindDiv"),
- oTotalNum=document.getElementsByClassName("totalNum")[0],
- waitDiv=document.getElementsByClassName("waitDiv")[0],
- confirmationDiv=document.getElementsByClassName("confirmationDiv")[0];
- var pageList=["index.html","shopPage.html","shopCar.html","order.html","my.html"],
- activeNum=0,
- oRemindTime,
- imgModel="<img src='{{img}}' class='sImg'>",
- onePrice=0,
- ulModel=oShopList.getElementsByTagName("ul")[0].innerHTML;
- /*获取购物车数量*/
- Ajax('get',pageUrl+"/Api/Index/shopCar","",function (data) {
- if(data.status=="999"){
- window.location.href="./login.html";
- }
- var getData=data.data,shopId=[],update={};
- if(!getData.length) remindMes("购物车中还没有商品");
- shopId=initData(ulModel,getData,oShopList,waitDiv);
- totalPrice(aLi,oTotalNum);
- oTotalBtn.onclick=function () {
- if(aLi.length==0){
- remindMes("尚未选择商品!");
- }else {
- var reData=[];
- for(var i=0,len=aLi.length;i<len;i++){
- if(aLi[i].getElementsByClassName("selectBtn")[0].className.indexOf("active")!=-1){
- reData[reData.length]={
- shopid:shopId[i],
- shopNum:aLi[i].getElementsByClassName("shopZS")[0].value
- }
- }
- }
- // Ajax("post",pageUrl+"",reData,function (data) {
- // if(data.status==200){
- location.href="./shopCar/shopAffirm.html";
- // }
- // })
- }
- }
- activeNum=aLi.length;
- // oTotalLe.onclick=function () {
- // if(getComputedStyle(this.childNodes[1],null)['background'].indexOf("ed")==-1){
- // addCls(this.childNodes[1],"active");
- // for(var i=0,len=aSelectBtn.length;i<len;i++){
- // addCls(aSelectBtn[i],"active");
- // }
- // activeNum=aLi.length;
- // }else {
- // removeCls(this.childNodes[1],"active");
- // for(var i=0,len=aSelectBtn.length;i<len;i++){
- // removeCls(aSelectBtn[i],"active");
- // }
- // activeNum=0;
- // }
- // totalPrice(aLi,oTotalNum);
- // }
- oShopList.onclick=function (e) {
- var className="",ClsArr=[];
- if(e.target.className.indexOf("sI")!=-1){
- // if(e.target.tagName.toLowerCase()=="div"){
- // className=e.target.childNodes[1].className;
- // ClsArr=className.split(" ");
- // if(ClsArr.indexOf("active")==-1){
- // addCls(e.target.childNodes[1],"active");
- // activeNum++;
- // if(activeNum==aLi.length){
- // addCls(oTotalLe.childNodes[1],"active");
- // }
- // }else {
- // removeCls(e.target.childNodes[1],"active");
- // removeCls(oTotalLe.childNodes[1],"active");
- // activeNum--;
- // }
- // }else if(e.target.tagName.toLowerCase()=="i"){
- // className=e.target.className;
- // ClsArr=className.split(" ");
- // if(ClsArr.indexOf("active")==-1){
- // addCls(e.target,"active");
- // activeNum++;
- // if(activeNum==aLi.length){
- // addCls(oTotalLe.childNodes[1],"active");
- // }
- // }else {
- // removeCls(e.target,"active");
- // removeCls(oTotalLe.childNodes[1],"active");
- // activeNum--;
- // }
- // }
- // totalPrice(aLi,oTotalNum);
- }else if(e.target.className.indexOf("minus")!=-1){
- onePrice=(e.target.parentNode.previousSibling.previousSibling.innerText.slice(1)-0)/(e.target.nextSibling.value-0);
- if(e.target.nextSibling.value-1<=1){
- addCls(e.target,"active");
- }
- if(e.target.nextSibling.value-1<=0) return;
- e.target.nextSibling.value=parseInt(e.target.nextSibling.value)-1;
- e.target.parentNode.previousSibling.previousSibling.innerText="¥"+(onePrice*(e.target.nextSibling.value-0)).toFixed(2);
- update.shopid=shopId[e.target.parentNode.parentNode.parentNode.parentNode.dataset.n];
- update.num=e.target.nextSibling.value;
- Ajax("post",pageUrl+"/Api/Index/shopCar",update,function () {})
- totalPrice(aLi,oTotalNum);
- }else if(e.target.className.indexOf("plus")!=-1){
- var kc=e.target.parentNode.parentNode.parentNode.getElementsByClassName("kc")[0].innerText;
- onePrice=(e.target.parentNode.previousSibling.previousSibling.innerText.slice(1)-0)/(e.target.previousSibling.value-0);
- if(e.target.previousSibling.value-0>=1&&e.target.previousSibling.value<(kc.replace("库存:","")-0)){
- removeCls(e.target.previousSibling.previousSibling,"active");
- e.target.previousSibling.value=(e.target.previousSibling.value-0)+1;
- e.target.parentNode.previousSibling.previousSibling.innerText="¥"+(onePrice*(e.target.previousSibling.value-0)).toFixed(2);
- update.shopid=shopId[e.target.parentNode.parentNode.parentNode.parentNode.dataset.n];
- update.num=e.target.previousSibling.value;
- Ajax("post",pageUrl+"/Api/Index/shopCar",update,function () {})
- totalPrice(aLi,oTotalNum);
- }else if(e.target.previousSibling.value>=(kc.replace("库存:","")-0)) {
- addCls(this.nextSibling, "active");
- }
- }else if(e.target.className.indexOf("delBtn")!=-1){
- showConfirmationDiv(confirmationDiv,oShopList.getElementsByTagName("ul")[0],e.target.parentNode.parentNode.parentNode.parentNode,shopId);
- }else if(e.target.className.indexOf("shopZS")!=-1){
- var kc=e.target.parentNode.parentNode.parentNode.getElementsByClassName("kc")[0].innerText;
- onePrice=(e.target.parentNode.previousSibling.previousSibling.innerText.slice(1)-0)/(e.target.value-0);
- e.target.oninput=function () {
- if(/\D/g.test(this.value)||!this.value||this.value==1){
- this.value=1;
- this.parentNode.previousSibling.previousSibling.innerText="¥"+onePrice.toFixed(2);
- addCls(e.target.previousSibling,"active");
- }else if(this.value>=(kc.replace("库存:","")-0)){
- this.value=(kc.replace("库存:","")-0);
- var thisPrice=onePrice*(this.value-0);
- this.parentNode.previousSibling.previousSibling.innerText="¥"+thisPrice.toFixed(2);
- addCls(e.target.nextSibling,"active");
- }else {
- var thisPrice=onePrice*(this.value-0);
- this.parentNode.previousSibling.previousSibling.innerText="¥"+thisPrice.toFixed(2);
- removeCls(e.target.previousSibling,"active");
- }
- update.shopid=shopId[e.target.parentNode.parentNode.parentNode.parentNode.dataset.n];
- update.num=e.target.value;
- Ajax("post",pageUrl+"/Api/Index/shopCar",update,function () {})
- totalPrice(aLi,oTotalNum);
- }
- }
- }
- })
- /*我的未读消息*/
- Ajax("get",pageUrl+"/Api/Index/getmsg","",function (data) {
- var mesTag=document.getElementsByClassName("mesTag")[0];
- if(!data.msgcount) return;
- if((data.msgcount-0)>0){
- mesTag.style.display="block";
- }
- })
- if(is_weixin()){
- var mainPB=document.getElementsByClassName("mainPB")[0];
- mainPB.style.display="none";
- }
- for(var i=0,len=aFooterLi.length;i<len;i++){
- !function (i) {
- aFooterLi[i].onclick=function () {
- var url=location.href;
- if(url.indexOf("?")!=-1){
- if(pageList[i]==url.slice(url.lastIndexOf("/")+1,url.indexOf("?"))) return;
- }else {
- if(pageList[i]==url.slice(url.lastIndexOf("/")+1)) return;
- }
- if(!i){
- location.href="../index.html";
- }else {
- location.href="./"+pageList[i];
- }
- }
- }(i)
- }
- /*删除商品函数*/
- function showConfirmationDiv(confirmationDiv,parentUl,li,shopId) {
- confirmationDiv.style.display="block";
- confirmationDiv.getElementsByClassName("confirmationCancel")[0].onclick=function () {
- confirmationDiv.style.display="none";
- }
- confirmationDiv.getElementsByClassName("confirmationDel")[0].onclick=function () {
- var aLi=parentUl.getElementsByTagName("li"),
- oTotalNum=document.getElementsByClassName("totalNum")[0];
- var dataList={},arr=[];
- dataList.shopid=shopId[li.dataset.n];
- dataList.num="0";
- /*发送删除商品id*/
- Ajax("post",pageUrl+"/Api/Index/shopCar",dataList,function (data) {
- if(data.status==200){
- remindMes(data.message);
- sessionStorage.setItem("carNum",sessionStorage.getItem("carNum")-1);
- }
- })
- parentUl.removeChild(li);
- /* for(var j=0;j<aLi.length;j++){
- aLi[j].dataset.n=j;
- }*/
- /* if(li.getElementsByClassName("selectBtn")[0].className.indexOf("active")!=-1){
- activeNum--;
- }*/
- confirmationDiv.style.display="none";
- /* if(activeNum==aLi.length){
- addCls(oTotalLe.childNodes[1],"active");
- }*/
- totalPrice(aLi,oTotalNum);
- }
- }
- function remindMes(val) {
- if(oRemindTime) clearTimeout(oRemindTime);
- remindDiv.innerText=val;
- addCls(remindDiv,'active');
- oRemindTime=setTimeout(function () {
- removeCls(remindDiv,'active');
- oRemindTime=undefined;
- },2000)
- }
- /*加载购物车商品*/
- function initData(model,data,div,waitDiv) {
- var ulStr="<ul>",arr=[];
- for(var shopNum in data){
- ulStr+=model.replace("{{i}}",shopNum).replace("{{img}}",imgModel.replace("{{img}}",pageImg+data[shopNum]["imgurl"])).replace("{{name}}",data[shopNum]["shopname"])
- .replace("{{guiG}}",data[shopNum]["kcnum"]).replace("{{oneP}}","¥"+(data[shopNum]["price"]-0).toFixed(2)).replace("{{price}}","¥"+(data[shopNum]["price"]*data[shopNum]["shopnum"]).toFixed(2))
- .replace("{{value}}",data[shopNum]["shopnum"])
- arr[shopNum]=data[shopNum]["id"]
- }
- ulStr+="</ul>";
- waitDiv.style.display="none";
- document.getElementsByClassName("waitDiv")[0].style.display="none";
- div.innerHTML=ulStr;
- return arr
- }
- }
- function toBack() {
- window.history.back();
- }
- /*计算总价*/
- function totalPrice(aLi,totalDiv) {
- var totalPrice=0,oneTotal=0;
- for(var i=0,len=aLi.length;i<len;i++){
- if(aLi[i].getElementsByClassName("selectBtn")[0].className.indexOf("active")!=-1){
- oneTotal=(aLi[i].getElementsByClassName("sPrice")[0].innerText.slice(1)-0);
- totalPrice+=oneTotal;
- }
- }
- totalDiv.innerText="¥"+totalPrice.toFixed(2);
- }
- function is_weixin(){
- var ua = navigator.userAgent;
- if(/(MicroMessenger)/i.test(ua)) {
- return true;
- } else {
- return false;
- }
- }
|