优惠券分享

index-01.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*封装一些公用的事件或者公用的方法*/
  2. /*定义的一个命名空间*/
  3. window.my = {};
  4. /* 获取url参数 */
  5. my.getURLCode = function (){
  6. var search = location.search.length > 0 ? location.search.substring(1) : '',
  7. args = {},
  8. items = search.length ? search.split('&') : [],
  9. item = null,
  10. name = null,
  11. value = null,
  12. i = 0,
  13. len = items.length,
  14. data = '';
  15. for (i = 0; i < len; i++) {
  16. item = items[i].split('=');
  17. name = decodeURIComponent(item[0]);
  18. value = decodeURIComponent(item[1]);
  19. if (item.length) {args[name] = value }
  20. }
  21. return args;
  22. }
  23. /*copy to clipboard*/
  24. function toCopy(copy01,copy02,cb) {
  25. if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) {
  26. //区分iPhone设备
  27. window.getSelection().removeAllRanges();//这段代码必须放在前面否则无效
  28. var Url2=document.getElementById(copy02);//要复制文字的节点
  29. var range = document.createRange();
  30. // 选中需要复制的节点
  31. range.selectNode(Url2);
  32. // 执行选中元素
  33. window.getSelection().addRange(range);
  34. // 执行 copy 操作
  35. var successful = document.execCommand('copy');
  36. // 移除选中的元素
  37. window.getSelection().removeAllRanges();
  38. cb();
  39. }else{
  40. var Url2=document.getElementById(copy01);//要复制文字的节点
  41. Url2.select(); // 选择对象
  42. document.execCommand("Copy"); // 执行浏览器复制命令
  43. cb()
  44. }
  45. }
  46. var args = my.getURLCode();
  47. var goods_id = args['goods_id'] || 0,
  48. is_coupon = args['is_coupon'] || 0,
  49. user_id = args['user_id'] || 0;
  50. union_id = args['union_id'] || 0;
  51. var btn = document.querySelector('.btn-wrapper .btn');
  52. window.onload = function () {
  53. writeLogH5({action:'SG-details',channel:'H5',url:window.location.href});//商品详情埋点
  54. _czc.push(["_trackEvent",'猎豆详情页','页面',]);
  55. axios.post('/api/v2/goods/tdetail', {
  56. goods_id: goods_id,
  57. is_coupon: is_coupon,
  58. user_id: user_id
  59. },{
  60. headers:{
  61. source:6000
  62. }
  63. })
  64. .then(function (response) {
  65. var res = response.data, data = null, html = '', imgList = [], swiperStr = '', pointStr = '', len = 0;
  66. var swiper = document.querySelector('.swiper-container .swiper-wrapper'),
  67. point = document.querySelector('.swiper-wrapper .swiper-point'),
  68. detailsWrapper = document.querySelector('.details-wrapper'),
  69. copy_2 = document.getElementById("copy_2"),
  70. copy_1 = document.getElementById("copy_1");
  71. copyName_1 = document.getElementById("copyName_1");
  72. copyName_2 = document.getElementById("copyName_2");
  73. // toast = document.getElementById("toast")
  74. // console.log(swiper);
  75. if (res.errno == 0) {
  76. data = res.rst.data;
  77. imgList = res.rst.data.small_img;
  78. len = imgList.length;
  79. if (len > 0) {
  80. for (var i = 0; i < len; i++) {
  81. swiperStr += '<div class="swiper-slide"><img src="'+ imgList[i] +'"></div>';
  82. }
  83. }
  84. if (is_coupon == 1) {
  85. html = '<h5 class="price"><span>券后 ¥</span><em>'+ data.discount_price +'</em><span class="icon icon-price">券&nbsp;&nbsp;&nbsp;'+ data.coupon_price
  86. +'元</span></h5><div class="info"><p class="before-price">价格 <span>'+ data.price +'</span></p><p class="quantity"> 月销 '+ data.volume +'</p></div><p class="elli desc">'+ data.title +'</p>';
  87. }else{
  88. html = '<h5 class="price"><span>折后 ¥</span><em>'+ data.discount_price +'</em></h5><div class="info"><p class="before-price">价格 <span>'+ data.price +'</span></p><p class="quantity"> 月销 '+ data.volume +'</p></div><p class="elli desc">'+ data.title +'</p>';
  89. }
  90. // if(data.commission_price){
  91. // var btn_yj = document.getElementsByClassName("btn2")[0].getElementsByTagName("span")[0];
  92. // btn_yj.innerHTML = "&nbsp;" + data.commission_price + "&nbsp;元"
  93. // }
  94. copyName_2.innerHTML = data.title;
  95. // console.log(copy_1);
  96. copyName_1.value = data.title;
  97. detailsWrapper.innerHTML = html;
  98. swiper.innerHTML = swiperStr;
  99. var mySwiper = new Swiper ('.swiper-container', {
  100. autoplay: 3000,
  101. loop: true,
  102. pagination: '.swiper-pagination',
  103. })
  104. var titleName = document.getElementsByClassName("details-wrapper")[0].getElementsByClassName("desc")[0];
  105. titleName.addEventListener('click', function () {
  106. toCopy("copyName_1","copyName_2",function () {
  107. showMsg("商品复制成功")
  108. });
  109. }, false)
  110. }
  111. })
  112. .catch(function (error) {
  113. console.log(error);
  114. });
  115. axios.post('/api/v2/adzoneCreate/h5CopyOfTheNaughtyPassword', {
  116. goods_id: goods_id,
  117. is_coupon: is_coupon,
  118. user_id: user_id
  119. },{
  120. headers:{
  121. source:6000
  122. }
  123. })
  124. .then(function (response) {
  125. console.log(response)
  126. var res = response.data, data = null;
  127. if (res.errno == 0) {
  128. data = res.rst.data;
  129. copy_2.innerHTML = data;
  130. // console.log(copy_1);
  131. copy_1.value = data;
  132. btn.addEventListener('click', function () {
  133. console.log('test');
  134. // toast.classList.add('show');
  135. toCopy("copy_1","copy_2",function () {
  136. var mask = document.getElementsByClassName('mask')[0];
  137. mask.style.visibility='visible';
  138. });
  139. // setTimeout(function () {
  140. // toast.classList.remove('show');
  141. // toast.classList.add('hide')
  142. // }, 2000)
  143. }, false)
  144. }
  145. })
  146. .catch(function (error) {
  147. console.log(error);
  148. });
  149. }
  150. //gxl
  151. var isClickFlag = true;//是否可以点击获取验证码
  152. var copyName = document.getElementsByClassName("copyName");
  153. var copyNameArr = Array.prototype.slice.call(copyName);
  154. copyNameArr.forEach(function (item, index) {
  155. item.addEventListener('click', function () {
  156. toCopy("copyName_1","copyName_2",function () {
  157. window.location.href="http://a.app.qq.com/o/simple.jsp?pkgname=com.kuxuan.coupon_liedou"
  158. });
  159. }, false)
  160. })
  161. //获取手机验证码
  162. function getYzm () {
  163. var phoneNumber = getIdHtml("phone").value;
  164. var ttl = new Date().getTime();
  165. var sign = hex_md5('phone=' + phoneNumber + '&ttl=' + ttl + 'cNHWj7pqBSXTi2DS4uvxqMTzuXOk5xvL');
  166. if(phoneNumber.length != 11){
  167. showMsg('请正确输入手机号');
  168. return;
  169. }
  170. if(!isClickFlag){
  171. return;
  172. }
  173. axios.post('/api/v2/users/sendCode', {
  174. phone: phoneNumber,
  175. ttl: ttl,
  176. sign: sign,
  177. union_id: union_id
  178. },{
  179. headers:{
  180. source:6000
  181. }
  182. })
  183. .then(function (response) {
  184. var res = response.data,wait_time;
  185. if (res.errno == 0 && res.rst.success) {
  186. wait_time = res.rst.wait_time;
  187. var time = setInterval(function () {
  188. wait_time--;
  189. getClassHtml('yzmButton').innerHTML = wait_time + 's';
  190. isClickFlag = false;//不可获取验证码
  191. if(wait_time == 0){
  192. clearInterval(time);
  193. getClassHtml('yzmButton').innerHTML = '获取验证码';
  194. isClickFlag = true;//可以获取验证码
  195. }
  196. },1000)
  197. }else if(res.errno == 0 && !res.rst.success){
  198. showMsg('请稍后再试哦~')
  199. }else if(res.errno == '1011'){//errno==1011 该手机号已绑定其他微信
  200. showMsg(res.err);
  201. }
  202. })
  203. .catch(function (error) {
  204. showMsg('请稍后再试哦~')
  205. });
  206. }
  207. //注册事件
  208. function registerEvent () {
  209. if(getIdHtml("phone").value.length != 11){
  210. showMsg('请正确输入手机号');
  211. return;
  212. }
  213. if((getIdHtml('yzm').value).trim().length == 0){
  214. showMsg('请输入手机验证码')
  215. return;
  216. }
  217. //验证注册
  218. axios.post('/api/v2/users/loginCode', {
  219. phone: getIdHtml("phone").value,
  220. code: getIdHtml('yzm').value,
  221. union_id: union_id
  222. },{
  223. headers:{
  224. source:6000
  225. }
  226. })
  227. .then(function (response) {
  228. var res = response.data;
  229. if (res.errno == 0) {
  230. writeLogH5({action:'SG-success',channel:'H5',url:window.location.href});//未注册手机注册成功埋点
  231. _czc.push(["_trackEvent",'猎豆详情页','手机注册成功',]);
  232. var mask = document.getElementById('registerMask');
  233. mask.style.visibility='hidden';
  234. document.getElementById('registerSuccess').style.visibility='visible';
  235. copyEvent(res.rst.user_id);//获取淘口令接口
  236. }else{
  237. showMsg(res.err);
  238. }
  239. })
  240. .catch(function (error) {
  241. showMsg('请稍后再试哦~')
  242. });
  243. }
  244. //获取淘口令接口
  245. function copyEvent (id) {
  246. axios.post('/api/v2/adzoneCreate/h5CopyOfTheNaughtyPassword', {
  247. goods_id: goods_id,
  248. is_coupon: is_coupon,
  249. user_id: id
  250. },{
  251. headers:{
  252. source:6000
  253. }
  254. })
  255. .then(function (response) {
  256. var res = response.data, data = null;
  257. if (res.errno == 0) {
  258. data = res.rst.data;
  259. var copy1 = getIdHtml("copy1");
  260. var copy2 = getIdHtml("copy2");
  261. copy2.innerHTML = data;
  262. copy1.value = data;
  263. getIdHtml("copyRegisterTkl").addEventListener('click', function () {
  264. toCopy("copy1","copy2",function () {
  265. showMsg('复制成功');
  266. });
  267. }, false)
  268. }
  269. })
  270. .catch(function (error) {
  271. console.log(error);
  272. });
  273. }
  274. //点击赚佣金购买
  275. function makeMoney(){
  276. if(union_id == 0){
  277. var mask = document.getElementsByClassName('mask')[1];
  278. mask.style.visibility='visible';
  279. return;//union_id为空
  280. }
  281. axios.get('/api/v2/user/h5ShareIfregist', {
  282. headers:{
  283. source:6000,
  284. unionid:union_id
  285. }
  286. })
  287. .then(function (response) {
  288. var res = response.data;
  289. if (res.errno == 0) {
  290. if (res.rst.code == 10000) {
  291. //未注册
  292. getClassHtml('alert-info').style.top='4.35rem';
  293. getIdHtml('registerMask').style.visibility='visible';
  294. writeLogH5({action:'SG-register',channel:'H5',url:window.location.href});//未注册手机注册埋点
  295. _czc.push(["_trackEvent",'猎豆详情页','手机注册弹框',]);
  296. }else{
  297. document.getElementsByClassName('mask')[1].style.visibility='visible';
  298. }
  299. }else{
  300. document.getElementsByClassName('mask')[1].style.visibility='visible';
  301. }
  302. })
  303. .catch(function (error) {
  304. document.getElementsByClassName('mask')[1].style.visibility='visible';
  305. });
  306. }
  307. function closeMake () {
  308. var mask = document.getElementById('registerMask');
  309. mask.style.visibility='hidden';
  310. document.getElementById('registerSuccess').style.visibility='hidden';
  311. getClassHtml('alert-info').style.top='9.35rem';
  312. }
  313. //封装 id标签
  314. function getIdHtml (name){
  315. return document.getElementById(name)
  316. }
  317. //封装 class标签
  318. function getClassHtml (name){
  319. return document.getElementsByClassName(name)[0]
  320. }
  321. /**
  322. * [showMsg 提示各种错误信息,3s后消失]
  323. */
  324. function showMsg(msg) {
  325. var msgBox = document.getElementsByClassName('alert-info')[0];
  326. msgBox.getElementsByTagName("p")[0].innerHTML=msg;
  327. msgBox.style.display="block";
  328. setTimeout(function() {
  329. msgBox.style.display="none";
  330. }, 1000);
  331. }
  332. //埋点
  333. function writeLogH5 (data) {
  334. axios.post('/api/v2/channel/writeLogH5', data,{
  335. headers:{
  336. source:6000
  337. }
  338. })
  339. .then(function (response) {
  340. })
  341. .catch(function (error) {
  342. });
  343. }