优惠券分享

saveMoney.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. /*封装一个事件 过渡结束事件*/
  24. my.transitionEnd = function(dom,callback){
  25. //1.给谁加事件
  26. //2.事件触发后处理什么业务
  27. if(!dom || typeof dom != 'object'){
  28. //没dom的时候或者不是一个对象的时候 程序停止
  29. return false;
  30. }
  31. dom.addEventListener('transitionEnd', function(){
  32. callback && callback();
  33. });
  34. dom.addEventListener('webkitTransitionEnd', function(){
  35. callback && callback();
  36. });
  37. }
  38. //封装一个tap事件
  39. my.tap = function(dom,callback){
  40. if(!dom || typeof dom != 'object'){
  41. //没dom的时候或者不是一个对象的时候 程序停止
  42. return false;
  43. }
  44. var isMove = false; //是否滑动过
  45. var time = 0; //刚刚触摸屏幕的事件 touchstart的触发事件
  46. dom.addEventListener('touchstart',function(){
  47. //记录触发这个事件的时间
  48. time = Date.now(); //时间戳 毫秒
  49. });
  50. dom.addEventListener('touchmove',function(){
  51. isMove = true;
  52. });
  53. window.addEventListener('touchend',function(e){
  54. //1.没有滑动过
  55. //2.响应事件在150ms以内 要求比click要响应快
  56. if(!isMove && (Date.now()-time)<150 ){
  57. callback && callback(e);
  58. }
  59. //重置参数
  60. isMove = false;
  61. time = 0;
  62. });
  63. }
  64. /* to swiper */
  65. function startSwiper(imageCount) {
  66. var banner = document.querySelector('.swiper-wrapper'); //轮播图大盒子
  67. var imageBox = banner.querySelector('.swiper-wrapper .swiper-item'); //图片盒子
  68. var pointBox = banner.querySelector('.swiper-wrapper .swiper-point'); //点盒子
  69. var imageCount = imageCount; //页面中用来轮播的图片有5张不同的
  70. var width = banner.offsetWidth; //图片的宽度
  71. var points = pointBox.querySelectorAll('li'); //所有的点
  72. //加过渡
  73. var addTransition = function(){
  74. imageBox.style.transition = "all 0.3s";
  75. imageBox.style.webkitTransition = "all 0.3s";/*做兼容*/
  76. };
  77. //清除过渡
  78. var removeTransition = function(){
  79. imageBox.style.transition = "none";
  80. imageBox.style.webkitTransition = "none";
  81. }
  82. //定位
  83. var setTranslateX = function(translateX){
  84. var translateX = translateX + 375;
  85. imageBox.style.transform = "translateX("+translateX+"px)";
  86. imageBox.style.webkitTransform = "translateX("+translateX+"px)";
  87. }
  88. //自动轮播 定时器 无缝衔接 动画结束瞬间定位
  89. var index = 1;
  90. var timer = setInterval(function(){
  91. index++ ; //自动轮播到下一张
  92. //改变定位 动画的形式去改变 transition transform translate
  93. addTransition(); //加过渡动画
  94. // console.log(index, -index * width);
  95. setTranslateX(-index * width); //定位
  96. }, 3000);
  97. //等过渡结束之后来做无缝衔接
  98. my.transitionEnd(imageBox, function(){
  99. //处理事件结束后的业务逻辑
  100. if(index > imageCount ){
  101. index = 1;
  102. }else if(index <= 0){
  103. index = imageCount;
  104. }
  105. removeTransition(); //清除过渡
  106. setTranslateX(-index * width); //定位
  107. setPoint(); //设置底部显示当前图片对应的圆角
  108. });
  109. //改变当前样式 当前图片的索引
  110. var setPoint = function(){
  111. //清除上一次的now
  112. for(var i = 0 ; i < points.length ; i++){
  113. points[i].className = " ";
  114. }
  115. //给图片对应的点加上样式
  116. points[index-1].className = "now";
  117. }
  118. /*
  119. 手指滑动的时候让轮播图滑动 touch事件 记录坐标轴的改变 改变轮播图的定位(位移css3)
  120. 当滑动的距离不超过一定的距离的时候 需要吸附回去 过渡的形式去做
  121. 当滑动超过了一定的距离 需要 跳到 下一张或者上一张 (滑动的方向) 一定的距离(屏幕的三分之一)
  122. */
  123. //touch事件
  124. var startX = 0; //记录起始 刚刚触摸的点的位置 x的坐标
  125. var moveX = 0; //滑动的时候x的位置
  126. var distanceX = 0; //滑动的距离
  127. var isMove = false; //是否滑动过
  128. imageBox.addEventListener('touchstart', function(e){
  129. clearInterval(timer); //清除定时器
  130. startX = e.touches[0].clientX; //记录起始X
  131. });
  132. imageBox.addEventListener('touchmove',function(e){
  133. moveX = e.touches[0].clientX; //滑动时候的X
  134. distanceX = moveX - startX; //计算移动的距离
  135. //计算当前定位 -index*width+distanceX
  136. removeTransition(); //清除过渡
  137. setTranslateX(-index * width + distanceX); //实时的定位
  138. isMove = true; //证明滑动过
  139. });
  140. //在模拟器上模拟的滑动会有问题 丢失的情况 最后在模拟器的时候用window
  141. imageBox.addEventListener('touchend', function(e){
  142. // 滑动超过 1/3 即为滑动有效,否则即为无效,则吸附回去
  143. if(isMove && Math.abs(distanceX) > width/3){
  144. //5.当滑动超过了一定的距离 需要 跳到 下一张或者上一张 (滑动的方向)*/
  145. if(distanceX > 0){ //上一张
  146. index --;
  147. }
  148. else{ //下一张
  149. index ++;
  150. }
  151. }
  152. addTransition(); //加过渡动画
  153. setTranslateX(-index * width); //定位
  154. if(index > imageCount ){
  155. index = 1;
  156. }else if(index <= 0){
  157. index = imageCount;
  158. }
  159. setPoint();
  160. //重置参数
  161. startX = 0;
  162. moveX = 0;
  163. distanceX = 0;
  164. isMove = false;
  165. //加定时器
  166. clearInterval(timer); //严谨 再清除一次定时器
  167. timer= setInterval(function(){
  168. index++ ; //自动轮播到下一张
  169. addTransition(); //加过渡动画
  170. setTranslateX(-index * width); //定位
  171. },3000);
  172. });
  173. }
  174. /*copy to clipboard*/
  175. function toCopy(copy01,copy02,cb) {
  176. if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) {
  177. //区分iPhone设备
  178. window.getSelection().removeAllRanges();//这段代码必须放在前面否则无效
  179. var Url2=document.getElementById(copy02);//要复制文字的节点
  180. var range = document.createRange();
  181. // 选中需要复制的节点
  182. range.selectNode(Url2);
  183. // 执行选中元素
  184. window.getSelection().addRange(range);
  185. // 执行 copy 操作
  186. var successful = document.execCommand('copy');
  187. // 移除选中的元素
  188. window.getSelection().removeAllRanges();
  189. cb();
  190. }else{
  191. var Url2=document.getElementById(copy01);//要复制文字的节点
  192. Url2.select(); // 选择对象
  193. document.execCommand("Copy"); // 执行浏览器复制命令
  194. cb()
  195. }
  196. }
  197. window.onload = function () {
  198. var args = my.getURLCode();
  199. var goods_id = args['goods_id'] || 0,
  200. is_coupon = args['is_coupon'] || 0,
  201. user_id = args['user_id'] || 0;
  202. var btn = document.querySelector('.btn-wrapper .btn');
  203. axios.post('/api/v2/goods/tdetail', {
  204. goods_id: goods_id,
  205. is_coupon: is_coupon,
  206. user_id: user_id
  207. })
  208. .then(function (response) {
  209. var res = response.data, data = null, html = '', imgList = [], swiperStr = '', pointStr = '', len = 0;
  210. var swiper = document.querySelector('.swiper-container .swiper-wrapper'),
  211. point = document.querySelector('.swiper-wrapper .swiper-point'),
  212. detailsWrapper = document.querySelector('.details-wrapper'),
  213. copy_2 = document.getElementById("copy_2"),
  214. copy_1 = document.getElementById("copy_1");
  215. copyName_1 = document.getElementById("copyName_1");
  216. copyName_2 = document.getElementById("copyName_2");
  217. // toast = document.getElementById("toast")
  218. // console.log(swiper);
  219. if (res.errno == 0) {
  220. data = res.rst.data;
  221. imgList = res.rst.data.small_img;
  222. len = imgList.length;
  223. if (len > 0) {
  224. for (var i = 0; i < len; i++) {
  225. swiperStr += '<div class="swiper-slide"><img src="'+ imgList[i] +'"></div>';
  226. }
  227. }
  228. if (is_coupon == 1) {
  229. html = '<h5 class="price"><span>券后 ¥</span><em>'+ data.discount_price +'</em><span class="icon icon-price">券&nbsp;&nbsp;&nbsp;'+ data.coupon_price
  230. +'元</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>';
  231. }else{
  232. 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>';
  233. }
  234. if(data.commission_price){
  235. var btn_yj = document.getElementsByClassName("btn2")[0].getElementsByTagName("span")[0];
  236. btn_yj.innerHTML = "&nbsp;" + data.commission_price + "&nbsp;元"
  237. }
  238. copyName_2.innerHTML = data.title;
  239. // console.log(copy_1);
  240. copyName_1.value = data.title;
  241. detailsWrapper.innerHTML = html;
  242. swiper.innerHTML = swiperStr;
  243. var mySwiper = new Swiper ('.swiper-container', {
  244. autoplay: 3000,
  245. loop: true,
  246. pagination: '.swiper-pagination',
  247. })
  248. var titleName = document.getElementsByClassName("details-wrapper")[0].getElementsByClassName("desc")[0];
  249. titleName.addEventListener('click', function () {
  250. toCopy("copyName_1","copyName_2",function () {
  251. showMsg("商品复制成功")
  252. });
  253. }, false)
  254. }
  255. })
  256. .catch(function (error) {
  257. console.log(error);
  258. });
  259. axios.post('/api/v2/adzoneCreate/h5CopyOfTheNaughtyPassword', {
  260. goods_id: goods_id,
  261. is_coupon: is_coupon,
  262. user_id: user_id
  263. })
  264. .then(function (response) {
  265. var res = response.data, data = null;
  266. if (res.errno == 0) {
  267. data = res.rst.data;
  268. copy_2.innerHTML = data;
  269. // console.log(copy_1);
  270. copy_1.value = data;
  271. btn.addEventListener('click', function () {
  272. // console.log('test');
  273. // toast.classList.add('show');
  274. toCopy("copy_1","copy_2",function () {
  275. var mask = document.getElementsByClassName('mask')[0];
  276. mask.style.visibility='visible';
  277. });
  278. // setTimeout(function () {
  279. // toast.classList.remove('show');
  280. // toast.classList.add('hide')
  281. // }, 2000)
  282. }, false)
  283. }
  284. })
  285. .catch(function (error) {
  286. console.log(error);
  287. });
  288. }
  289. //gxl
  290. var copyName = document.getElementsByClassName("copyName");
  291. var copyNameArr = Array.prototype.slice.call(copyName);
  292. copyNameArr.forEach(function (item, index) {
  293. item.addEventListener('click', function () {
  294. toCopy("copyName_1","copyName_2",function () {
  295. window.location.href="http://a.app.qq.com/o/simple.jsp?pkgname=com.kuxuan.coupon"
  296. });
  297. }, false)
  298. })
  299. /**
  300. * [showMsg 提示各种错误信息,3s后消失]
  301. */
  302. function showMsg(msg) {
  303. var msgBox = document.getElementsByClassName('alert-info')[0];
  304. msgBox.getElementsByTagName("p")[0].innerHTML=msg;
  305. msgBox.style.display="block";
  306. setTimeout(function() {
  307. msgBox.style.display="none";
  308. }, 1500);
  309. }