No Description

saveLetters.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. headers:{
  209. source:6001
  210. }
  211. })
  212. .then(function (response) {
  213. var res = response.data, data = null, html = '', imgList = [], swiperStr = '', pointStr = '', len = 0;
  214. var swiper = document.querySelector('.swiper-container .swiper-wrapper'),
  215. point = document.querySelector('.swiper-wrapper .swiper-point'),
  216. detailsWrapper = document.querySelector('.details-wrapper'),
  217. copy_2 = document.getElementById("copy_2"),
  218. copy_1 = document.getElementById("copy_1");
  219. copyName_1 = document.getElementById("copyName_1");
  220. copyName_2 = document.getElementById("copyName_2");
  221. // toast = document.getElementById("toast")
  222. // console.log(swiper);
  223. if (res.errno == 0) {
  224. data = res.rst.data;
  225. imgList = res.rst.data.small_img;
  226. len = imgList.length;
  227. if (len > 0) {
  228. for (var i = 0; i < len; i++) {
  229. swiperStr += '<div class="swiper-slide"><img src="'+ imgList[i] +'"></div>';
  230. }
  231. }
  232. if (is_coupon == 1) {
  233. html = '<h5 class="price"><span>券后 ¥</span><em>'+ data.discount_price +'</em><span class="icon icon-price">券&nbsp;&nbsp;&nbsp;'+ data.coupon_price
  234. +'元</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>';
  235. }else{
  236. 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>';
  237. }
  238. if(data.commission_price){
  239. var btn_yj = document.getElementsByClassName("btn2")[0].getElementsByTagName("span")[0];
  240. btn_yj.innerHTML = "&nbsp;" + data.commission_price + "&nbsp;元"
  241. }
  242. copyName_2.innerHTML = data.title;
  243. // console.log(copy_1);
  244. copyName_1.value = data.title;
  245. detailsWrapper.innerHTML = html;
  246. swiper.innerHTML = swiperStr;
  247. var mySwiper = new Swiper ('.swiper-container', {
  248. autoplay: 3000,
  249. loop: true,
  250. pagination: '.swiper-pagination',
  251. })
  252. var titleName = document.getElementsByClassName("details-wrapper")[0].getElementsByClassName("desc")[0];
  253. titleName.addEventListener('click', function () {
  254. toCopy("copyName_1","copyName_2",function () {
  255. showMsg("商品复制成功")
  256. });
  257. }, false)
  258. }
  259. })
  260. .catch(function (error) {
  261. console.log(error);
  262. });
  263. axios.post('/api/v2/adzoneCreate/h5CopyOfTheNaughtyPassword', {
  264. goods_id: goods_id,
  265. is_coupon: is_coupon,
  266. user_id: user_id
  267. },{
  268. headers:{
  269. source:6001
  270. }
  271. })
  272. .then(function (response) {
  273. console.log(response)
  274. var res = response.data, data = null;
  275. if (res.errno == 0) {
  276. data = res.rst.data;
  277. copy_2.innerHTML = data;
  278. // console.log(copy_1);
  279. copy_1.value = data;
  280. btn.addEventListener('click', function () {
  281. console.log('test');
  282. // toast.classList.add('show');
  283. toCopy("copy_1","copy_2",function () {
  284. var mask = document.getElementsByClassName('mask')[0];
  285. mask.style.visibility='visible';
  286. });
  287. // setTimeout(function () {
  288. // toast.classList.remove('show');
  289. // toast.classList.add('hide')
  290. // }, 2000)
  291. }, false)
  292. }
  293. })
  294. .catch(function (error) {
  295. console.log(error);
  296. });
  297. }
  298. //gxl
  299. var copyName = document.getElementsByClassName("copyName");
  300. var copyNameArr = Array.prototype.slice.call(copyName);
  301. copyNameArr.forEach(function (item, index) {
  302. item.addEventListener('click', function () {
  303. toCopy("copyName_1","copyName_2",function () {
  304. window.location.href="http://a.app.qq.com/o/simple.jsp?pkgname=com.kuxuan.coupon_liedou"
  305. });
  306. }, false)
  307. })
  308. /**
  309. * [showMsg 提示各种错误信息,3s后消失]
  310. */
  311. function showMsg(msg) {
  312. var msgBox = document.getElementsByClassName('alert-info')[0];
  313. msgBox.getElementsByTagName("p")[0].innerHTML=msg;
  314. msgBox.style.display="block";
  315. setTimeout(function() {
  316. msgBox.style.display="none";
  317. }, 1500);
  318. }