/*封装一些公用的事件或者公用的方法*/ /*定义的一个命名空间*/ window.my = {}; /* 获取url参数 */ my.getURLCode = function (){ var search = location.search.length > 0 ? location.search.substring(1) : '', args = {}, items = search.length ? search.split('&') : [], item = null, name = null, value = null, i = 0, len = items.length, data = ''; for (i = 0; i < len; i++) { item = items[i].split('='); name = decodeURIComponent(item[0]); value = decodeURIComponent(item[1]); if (item.length) {args[name] = value } } return args; } /*copy to clipboard*/ function toCopy(copy01,copy02,cb) { if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) { //区分iPhone设备 window.getSelection().removeAllRanges();//这段代码必须放在前面否则无效 var Url2=document.getElementById(copy02);//要复制文字的节点 var range = document.createRange(); // 选中需要复制的节点 range.selectNode(Url2); // 执行选中元素 window.getSelection().addRange(range); // 执行 copy 操作 var successful = document.execCommand('copy'); // 移除选中的元素 window.getSelection().removeAllRanges(); cb(); }else{ var Url2=document.getElementById(copy01);//要复制文字的节点 Url2.select(); // 选择对象 document.execCommand("Copy"); // 执行浏览器复制命令 cb() } } var args = my.getURLCode(); var goods_id = args['goods_id'] || 0, is_coupon = args['is_coupon'] || 0, user_id = args['user_id'] || 0; union_id = args['union_id'] || 0; var btn = document.querySelector('.btn-wrapper .btn'); window.onload = function () { writeLogH5({action:'SG-details',channel:'H5',url:window.location.href});//商品详情埋点 _czc.push(["_trackEvent",'猎豆详情页','页面',]); axios.post('/api/v2/goods/tdetail', { goods_id: goods_id, is_coupon: is_coupon, user_id: user_id },{ headers:{ source:6000 } }) .then(function (response) { var res = response.data, data = null, html = '', imgList = [], swiperStr = '', pointStr = '', len = 0; var swiper = document.querySelector('.swiper-container .swiper-wrapper'), point = document.querySelector('.swiper-wrapper .swiper-point'), detailsWrapper = document.querySelector('.details-wrapper'), copy_2 = document.getElementById("copy_2"), copy_1 = document.getElementById("copy_1"); copyName_1 = document.getElementById("copyName_1"); copyName_2 = document.getElementById("copyName_2"); // toast = document.getElementById("toast") // console.log(swiper); if (res.errno == 0) { data = res.rst.data; imgList = res.rst.data.small_img; len = imgList.length; if (len > 0) { for (var i = 0; i < len; i++) { swiperStr += '
'; } } if (is_coupon == 1) { html = '
券后 ¥'+ data.discount_price +'券   '+ data.coupon_price +'元

价格 '+ data.price +'

月销 '+ data.volume +'

'+ data.title +'

'; }else{ html = '
折后 ¥'+ data.discount_price +'

价格 '+ data.price +'

月销 '+ data.volume +'

'+ data.title +'

'; } // if(data.commission_price){ // var btn_yj = document.getElementsByClassName("btn2")[0].getElementsByTagName("span")[0]; // btn_yj.innerHTML = " " + data.commission_price + " 元" // } copyName_2.innerHTML = data.title; // console.log(copy_1); copyName_1.value = data.title; detailsWrapper.innerHTML = html; swiper.innerHTML = swiperStr; var mySwiper = new Swiper ('.swiper-container', { autoplay: 3000, loop: true, pagination: '.swiper-pagination', }) var titleName = document.getElementsByClassName("details-wrapper")[0].getElementsByClassName("desc")[0]; titleName.addEventListener('click', function () { toCopy("copyName_1","copyName_2",function () { showMsg("商品复制成功") }); }, false) } }) .catch(function (error) { console.log(error); }); axios.post('/api/v2/adzoneCreate/h5CopyOfTheNaughtyPassword', { goods_id: goods_id, is_coupon: is_coupon, user_id: user_id },{ headers:{ source:6000 } }) .then(function (response) { console.log(response) var res = response.data, data = null; if (res.errno == 0) { data = res.rst.data; copy_2.innerHTML = data; // console.log(copy_1); copy_1.value = data; btn.addEventListener('click', function () { console.log('test'); // toast.classList.add('show'); toCopy("copy_1","copy_2",function () { var mask = document.getElementsByClassName('mask')[0]; mask.style.visibility='visible'; }); // setTimeout(function () { // toast.classList.remove('show'); // toast.classList.add('hide') // }, 2000) }, false) } }) .catch(function (error) { console.log(error); }); } //gxl var isClickFlag = true;//是否可以点击获取验证码 var copyName = document.getElementsByClassName("copyName"); var copyNameArr = Array.prototype.slice.call(copyName); copyNameArr.forEach(function (item, index) { item.addEventListener('click', function () { toCopy("copyName_1","copyName_2",function () { window.location.href="http://a.app.qq.com/o/simple.jsp?pkgname=com.kuxuan.coupon_liedou" }); }, false) }) //获取手机验证码 function getYzm () { var phoneNumber = getIdHtml("phone").value; var ttl = new Date().getTime(); var sign = hex_md5('phone=' + phoneNumber + '&ttl=' + ttl + 'cNHWj7pqBSXTi2DS4uvxqMTzuXOk5xvL'); if(phoneNumber.length != 11){ showMsg('请正确输入手机号'); return; } if(!isClickFlag){ return; } axios.post('/api/v2/users/sendCode', { phone: phoneNumber, ttl: ttl, sign: sign, union_id: union_id },{ headers:{ source:6000 } }) .then(function (response) { var res = response.data,wait_time; if (res.errno == 0 && res.rst.success) { wait_time = res.rst.wait_time; var time = setInterval(function () { wait_time--; getClassHtml('yzmButton').innerHTML = wait_time + 's'; isClickFlag = false;//不可获取验证码 if(wait_time == 0){ clearInterval(time); getClassHtml('yzmButton').innerHTML = '获取验证码'; isClickFlag = true;//可以获取验证码 } },1000) }else if(res.errno == 0 && !res.rst.success){ showMsg('请稍后再试哦~') }else if(res.errno == '1011'){//errno==1011 该手机号已绑定其他微信 showMsg(res.err); } }) .catch(function (error) { showMsg('请稍后再试哦~') }); } //注册事件 function registerEvent () { if(getIdHtml("phone").value.length != 11){ showMsg('请正确输入手机号'); return; } if((getIdHtml('yzm').value).trim().length == 0){ showMsg('请输入手机验证码') return; } //验证注册 axios.post('/api/v2/users/loginCode', { phone: getIdHtml("phone").value, code: getIdHtml('yzm').value, union_id: union_id },{ headers:{ source:6000 } }) .then(function (response) { var res = response.data; if (res.errno == 0) { writeLogH5({action:'SG-success',channel:'H5',url:window.location.href});//未注册手机注册成功埋点 _czc.push(["_trackEvent",'猎豆详情页','手机注册成功',]); var mask = document.getElementById('registerMask'); mask.style.visibility='hidden'; document.getElementById('registerSuccess').style.visibility='visible'; copyEvent(res.rst.user_id);//获取淘口令接口 }else{ showMsg(res.err); } }) .catch(function (error) { showMsg('请稍后再试哦~') }); } //获取淘口令接口 function copyEvent (id) { axios.post('/api/v2/adzoneCreate/h5CopyOfTheNaughtyPassword', { goods_id: goods_id, is_coupon: is_coupon, user_id: id },{ headers:{ source:6000 } }) .then(function (response) { var res = response.data, data = null; if (res.errno == 0) { data = res.rst.data; var copy1 = getIdHtml("copy1"); var copy2 = getIdHtml("copy2"); copy2.innerHTML = data; copy1.value = data; getIdHtml("copyRegisterTkl").addEventListener('click', function () { toCopy("copy1","copy2",function () { showMsg('复制成功'); }); }, false) } }) .catch(function (error) { console.log(error); }); } //点击赚佣金购买 function makeMoney(){ if(union_id == 0){ var mask = document.getElementsByClassName('mask')[1]; mask.style.visibility='visible'; return;//union_id为空 } axios.get('/api/v2/user/h5ShareIfregist', { headers:{ source:6000, unionid:union_id } }) .then(function (response) { var res = response.data; if (res.errno == 0) { if (res.rst.code == 10000) { //未注册 getClassHtml('alert-info').style.top='4.35rem'; getIdHtml('registerMask').style.visibility='visible'; writeLogH5({action:'SG-register',channel:'H5',url:window.location.href});//未注册手机注册埋点 _czc.push(["_trackEvent",'猎豆详情页','手机注册弹框',]); }else{ document.getElementsByClassName('mask')[1].style.visibility='visible'; } }else{ document.getElementsByClassName('mask')[1].style.visibility='visible'; } }) .catch(function (error) { document.getElementsByClassName('mask')[1].style.visibility='visible'; }); } function closeMake () { var mask = document.getElementById('registerMask'); mask.style.visibility='hidden'; document.getElementById('registerSuccess').style.visibility='hidden'; getClassHtml('alert-info').style.top='9.35rem'; } //封装 id标签 function getIdHtml (name){ return document.getElementById(name) } //封装 class标签 function getClassHtml (name){ return document.getElementsByClassName(name)[0] } /** * [showMsg 提示各种错误信息,3s后消失] */ function showMsg(msg) { var msgBox = document.getElementsByClassName('alert-info')[0]; msgBox.getElementsByTagName("p")[0].innerHTML=msg; msgBox.style.display="block"; setTimeout(function() { msgBox.style.display="none"; }, 1000); } //埋点 function writeLogH5 (data) { axios.post('/api/v2/channel/writeLogH5', data,{ headers:{ source:6000 } }) .then(function (response) { }) .catch(function (error) { }); }