shopCar.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /**
  2. * Created by chen on 2017/5/12.
  3. */
  4. window.onload=function () {
  5. var aFooterLi=document.getElementsByClassName("footNav")[0].getElementsByTagName("li"),
  6. // aSelectBtn=document.getElementsByClassName("selectBtn"),
  7. oTotalBtn=document.getElementsByClassName("totalBtn")[0],
  8. remindDiv=document.getElementsByClassName("remindDiv")[0],
  9. oShopList=document.getElementsByClassName("carList")[0],
  10. aLi=oShopList.getElementsByTagName("li"),
  11. // oTotalLe=document.getElementsByClassName("totalLe")[0],
  12. // oRemindDiv=document.getElementById("remindDiv"),
  13. oTotalNum=document.getElementsByClassName("totalNum")[0],
  14. waitDiv=document.getElementsByClassName("waitDiv")[0],
  15. confirmationDiv=document.getElementsByClassName("confirmationDiv")[0];
  16. var pageList=["index.html","shopPage.html","shopCar.html","order.html","my.html"],
  17. activeNum=0,
  18. oRemindTime,
  19. imgModel="<img src='{{img}}' class='sImg'>",
  20. onePrice=0,
  21. ulModel=oShopList.getElementsByTagName("ul")[0].innerHTML;
  22. /*获取购物车数量*/
  23. Ajax('get',pageUrl+"/Api/Index/shopCar","",function (data) {
  24. if(data.status=="999"){
  25. window.location.href="./login.html";
  26. }
  27. var getData=data.data,shopId=[],update={};
  28. if(!getData.length) remindMes("购物车中还没有商品");
  29. shopId=initData(ulModel,getData,oShopList,waitDiv);
  30. totalPrice(aLi,oTotalNum);
  31. oTotalBtn.onclick=function () {
  32. if(aLi.length==0){
  33. remindMes("尚未选择商品!");
  34. }else {
  35. var reData=[];
  36. for(var i=0,len=aLi.length;i<len;i++){
  37. if(aLi[i].getElementsByClassName("selectBtn")[0].className.indexOf("active")!=-1){
  38. reData[reData.length]={
  39. shopid:shopId[i],
  40. shopNum:aLi[i].getElementsByClassName("shopZS")[0].value
  41. }
  42. }
  43. }
  44. // Ajax("post",pageUrl+"",reData,function (data) {
  45. // if(data.status==200){
  46. location.href="./shopCar/shopAffirm.html";
  47. // }
  48. // })
  49. }
  50. }
  51. activeNum=aLi.length;
  52. // oTotalLe.onclick=function () {
  53. // if(getComputedStyle(this.childNodes[1],null)['background'].indexOf("ed")==-1){
  54. // addCls(this.childNodes[1],"active");
  55. // for(var i=0,len=aSelectBtn.length;i<len;i++){
  56. // addCls(aSelectBtn[i],"active");
  57. // }
  58. // activeNum=aLi.length;
  59. // }else {
  60. // removeCls(this.childNodes[1],"active");
  61. // for(var i=0,len=aSelectBtn.length;i<len;i++){
  62. // removeCls(aSelectBtn[i],"active");
  63. // }
  64. // activeNum=0;
  65. // }
  66. // totalPrice(aLi,oTotalNum);
  67. // }
  68. oShopList.onclick=function (e) {
  69. var className="",ClsArr=[];
  70. if(e.target.className.indexOf("sI")!=-1){
  71. // if(e.target.tagName.toLowerCase()=="div"){
  72. // className=e.target.childNodes[1].className;
  73. // ClsArr=className.split(" ");
  74. // if(ClsArr.indexOf("active")==-1){
  75. // addCls(e.target.childNodes[1],"active");
  76. // activeNum++;
  77. // if(activeNum==aLi.length){
  78. // addCls(oTotalLe.childNodes[1],"active");
  79. // }
  80. // }else {
  81. // removeCls(e.target.childNodes[1],"active");
  82. // removeCls(oTotalLe.childNodes[1],"active");
  83. // activeNum--;
  84. // }
  85. // }else if(e.target.tagName.toLowerCase()=="i"){
  86. // className=e.target.className;
  87. // ClsArr=className.split(" ");
  88. // if(ClsArr.indexOf("active")==-1){
  89. // addCls(e.target,"active");
  90. // activeNum++;
  91. // if(activeNum==aLi.length){
  92. // addCls(oTotalLe.childNodes[1],"active");
  93. // }
  94. // }else {
  95. // removeCls(e.target,"active");
  96. // removeCls(oTotalLe.childNodes[1],"active");
  97. // activeNum--;
  98. // }
  99. // }
  100. // totalPrice(aLi,oTotalNum);
  101. }else if(e.target.className.indexOf("minus")!=-1){
  102. onePrice=(e.target.parentNode.previousSibling.previousSibling.innerText.slice(1)-0)/(e.target.nextSibling.value-0);
  103. if(e.target.nextSibling.value-1<=1){
  104. addCls(e.target,"active");
  105. }
  106. if(e.target.nextSibling.value-1<=0) return;
  107. e.target.nextSibling.value=parseInt(e.target.nextSibling.value)-1;
  108. e.target.parentNode.previousSibling.previousSibling.innerText="¥"+(onePrice*(e.target.nextSibling.value-0)).toFixed(2);
  109. update.shopid=shopId[e.target.parentNode.parentNode.parentNode.parentNode.dataset.n];
  110. update.num=e.target.nextSibling.value;
  111. Ajax("post",pageUrl+"/Api/Index/shopCar",update,function () {})
  112. totalPrice(aLi,oTotalNum);
  113. }else if(e.target.className.indexOf("plus")!=-1){
  114. var kc=e.target.parentNode.parentNode.parentNode.getElementsByClassName("kc")[0].innerText;
  115. onePrice=(e.target.parentNode.previousSibling.previousSibling.innerText.slice(1)-0)/(e.target.previousSibling.value-0);
  116. if(e.target.previousSibling.value-0>=1&&e.target.previousSibling.value<(kc.replace("库存:","")-0)){
  117. removeCls(e.target.previousSibling.previousSibling,"active");
  118. e.target.previousSibling.value=(e.target.previousSibling.value-0)+1;
  119. e.target.parentNode.previousSibling.previousSibling.innerText="¥"+(onePrice*(e.target.previousSibling.value-0)).toFixed(2);
  120. update.shopid=shopId[e.target.parentNode.parentNode.parentNode.parentNode.dataset.n];
  121. update.num=e.target.previousSibling.value;
  122. Ajax("post",pageUrl+"/Api/Index/shopCar",update,function () {})
  123. totalPrice(aLi,oTotalNum);
  124. }else if(e.target.previousSibling.value>=(kc.replace("库存:","")-0)) {
  125. addCls(this.nextSibling, "active");
  126. }
  127. }else if(e.target.className.indexOf("delBtn")!=-1){
  128. showConfirmationDiv(confirmationDiv,oShopList.getElementsByTagName("ul")[0],e.target.parentNode.parentNode.parentNode.parentNode,shopId);
  129. }else if(e.target.className.indexOf("shopZS")!=-1){
  130. var kc=e.target.parentNode.parentNode.parentNode.getElementsByClassName("kc")[0].innerText;
  131. onePrice=(e.target.parentNode.previousSibling.previousSibling.innerText.slice(1)-0)/(e.target.value-0);
  132. e.target.oninput=function () {
  133. if(/\D/g.test(this.value)||!this.value||this.value==1){
  134. this.value=1;
  135. this.parentNode.previousSibling.previousSibling.innerText="¥"+onePrice.toFixed(2);
  136. addCls(e.target.previousSibling,"active");
  137. }else if(this.value>=(kc.replace("库存:","")-0)){
  138. this.value=(kc.replace("库存:","")-0);
  139. var thisPrice=onePrice*(this.value-0);
  140. this.parentNode.previousSibling.previousSibling.innerText="¥"+thisPrice.toFixed(2);
  141. addCls(e.target.nextSibling,"active");
  142. }else {
  143. var thisPrice=onePrice*(this.value-0);
  144. this.parentNode.previousSibling.previousSibling.innerText="¥"+thisPrice.toFixed(2);
  145. removeCls(e.target.previousSibling,"active");
  146. }
  147. update.shopid=shopId[e.target.parentNode.parentNode.parentNode.parentNode.dataset.n];
  148. update.num=e.target.value;
  149. Ajax("post",pageUrl+"/Api/Index/shopCar",update,function () {})
  150. totalPrice(aLi,oTotalNum);
  151. }
  152. }
  153. }
  154. })
  155. /*我的未读消息*/
  156. Ajax("get",pageUrl+"/Api/Index/getmsg","",function (data) {
  157. var mesTag=document.getElementsByClassName("mesTag")[0];
  158. if(!data.msgcount) return;
  159. if((data.msgcount-0)>0){
  160. mesTag.style.display="block";
  161. }
  162. })
  163. if(is_weixin()){
  164. var mainPB=document.getElementsByClassName("mainPB")[0];
  165. mainPB.style.display="none";
  166. }
  167. for(var i=0,len=aFooterLi.length;i<len;i++){
  168. !function (i) {
  169. aFooterLi[i].onclick=function () {
  170. var url=location.href;
  171. if(url.indexOf("?")!=-1){
  172. if(pageList[i]==url.slice(url.lastIndexOf("/")+1,url.indexOf("?"))) return;
  173. }else {
  174. if(pageList[i]==url.slice(url.lastIndexOf("/")+1)) return;
  175. }
  176. if(!i){
  177. location.href="../index.html";
  178. }else {
  179. location.href="./"+pageList[i];
  180. }
  181. }
  182. }(i)
  183. }
  184. /*删除商品函数*/
  185. function showConfirmationDiv(confirmationDiv,parentUl,li,shopId) {
  186. confirmationDiv.style.display="block";
  187. confirmationDiv.getElementsByClassName("confirmationCancel")[0].onclick=function () {
  188. confirmationDiv.style.display="none";
  189. }
  190. confirmationDiv.getElementsByClassName("confirmationDel")[0].onclick=function () {
  191. var aLi=parentUl.getElementsByTagName("li"),
  192. oTotalNum=document.getElementsByClassName("totalNum")[0];
  193. var dataList={},arr=[];
  194. dataList.shopid=shopId[li.dataset.n];
  195. dataList.num="0";
  196. /*发送删除商品id*/
  197. Ajax("post",pageUrl+"/Api/Index/shopCar",dataList,function (data) {
  198. if(data.status==200){
  199. remindMes(data.message);
  200. sessionStorage.setItem("carNum",sessionStorage.getItem("carNum")-1);
  201. }
  202. })
  203. parentUl.removeChild(li);
  204. /* for(var j=0;j<aLi.length;j++){
  205. aLi[j].dataset.n=j;
  206. }*/
  207. /* if(li.getElementsByClassName("selectBtn")[0].className.indexOf("active")!=-1){
  208. activeNum--;
  209. }*/
  210. confirmationDiv.style.display="none";
  211. /* if(activeNum==aLi.length){
  212. addCls(oTotalLe.childNodes[1],"active");
  213. }*/
  214. totalPrice(aLi,oTotalNum);
  215. }
  216. }
  217. function remindMes(val) {
  218. if(oRemindTime) clearTimeout(oRemindTime);
  219. remindDiv.innerText=val;
  220. addCls(remindDiv,'active');
  221. oRemindTime=setTimeout(function () {
  222. removeCls(remindDiv,'active');
  223. oRemindTime=undefined;
  224. },2000)
  225. }
  226. /*加载购物车商品*/
  227. function initData(model,data,div,waitDiv) {
  228. var ulStr="<ul>",arr=[];
  229. for(var shopNum in data){
  230. ulStr+=model.replace("{{i}}",shopNum).replace("{{img}}",imgModel.replace("{{img}}",pageImg+data[shopNum]["imgurl"])).replace("{{name}}",data[shopNum]["shopname"])
  231. .replace("{{guiG}}",data[shopNum]["kcnum"]).replace("{{oneP}}","¥"+(data[shopNum]["price"]-0).toFixed(2)).replace("{{price}}","¥"+(data[shopNum]["price"]*data[shopNum]["shopnum"]).toFixed(2))
  232. .replace("{{value}}",data[shopNum]["shopnum"])
  233. arr[shopNum]=data[shopNum]["id"]
  234. }
  235. ulStr+="</ul>";
  236. waitDiv.style.display="none";
  237. document.getElementsByClassName("waitDiv")[0].style.display="none";
  238. div.innerHTML=ulStr;
  239. return arr
  240. }
  241. }
  242. function toBack() {
  243. window.history.back();
  244. }
  245. /*计算总价*/
  246. function totalPrice(aLi,totalDiv) {
  247. var totalPrice=0,oneTotal=0;
  248. for(var i=0,len=aLi.length;i<len;i++){
  249. if(aLi[i].getElementsByClassName("selectBtn")[0].className.indexOf("active")!=-1){
  250. oneTotal=(aLi[i].getElementsByClassName("sPrice")[0].innerText.slice(1)-0);
  251. totalPrice+=oneTotal;
  252. }
  253. }
  254. totalDiv.innerText="¥"+totalPrice.toFixed(2);
  255. }
  256. function is_weixin(){
  257. var ua = navigator.userAgent;
  258. if(/(MicroMessenger)/i.test(ua)) {
  259. return true;
  260. } else {
  261. return false;
  262. }
  263. }