index.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * 获取动态广告栏属性
  3. * @param type
  4. * @returns {Object|*}
  5. */
  6. // url转base64 方法
  7. // base64 编码
  8. function getBase64Image(img) {
  9. var canvas = document.createElement('canvas')
  10. canvas.width = img.width
  11. canvas.height = img.height
  12. var ctx = canvas.getContext('2d')
  13. ctx.drawImage(img, 0, 0, img.width, img.height)
  14. var dataURL = canvas.toDataURL('image/png')
  15. return dataURL
  16. }
  17. function getImage64Url(url, ref) {
  18. // var that = this
  19. var image = document.createElement('img')
  20. image.setAttribute('crossorigin', 'anonymous')
  21. image.src = url + '?v=' + Math.random() // 处理缓存
  22. image.crossOrigin = 'anonymous'
  23. image.onload = function () {
  24. var base64 = getBase64Image(image)
  25. console.log(base64);
  26. }
  27. }
  28. // var ua = window.navigator.userAgent
  29. // let _isIOS = -1;
  30. // function isIOS() {
  31. // if (_isIOS === -1) {
  32. // _isIOS = /iPhone|iPod|iPad/i.test(ua) ? 1 : 0;
  33. // }
  34. // return _isIOS === 1;
  35. // }
  36. var getActive = {
  37. // 版本号
  38. version: '0.0.1',
  39. drawBase64Image: function (img) {
  40. var canvas = document.createElement('canvas')
  41. canvas.width = img.width
  42. canvas.height = img.height
  43. var ctx = canvas.getContext('2d')
  44. ctx.drawImage(img, 0, 0, img.width, img.height)
  45. var dataURL = canvas.toDataURL('image/png')
  46. return dataURL
  47. },
  48. /**
  49. * 获取后台广告方法
  50. * @param {Array|String} active_codes_map 广告id标识符--数组集合["jy-wx-course-pay-success"]
  51. * @param {String} active_tip_group 展示广告的容器demo节点
  52. * @param {String} isAppsion 请求环境、app、weixin、pc
  53. * @returns {Object|Array}
  54. */
  55. getActiveInfo: function (active_codes_map,isAppsion) {
  56. // 如果执行环境是app
  57. var isAppsionApp = isAppsion === 'App'
  58. $.ajax({
  59. url: '/publicapply/free/getJyAdList',
  60. type: 'POST',
  61. dataType: "json",
  62. traditional: true,
  63. //强制请求头数据格式 'application/json'
  64. headers: {'Content-Type': 'application/json'},
  65. data:active_codes_map,
  66. // JSON.stringify({codes:["jy-wx-course-pay-success","jy-pc-dataPackage-pay-success"] })
  67. // JSON.stringify()
  68. }).done(function (res) {
  69. // res.error_code =1
  70. // Object.value 兼容处理
  71. Object.values = function (obj) {
  72. return Object.keys(obj).map(function(e) {
  73. return obj[e]
  74. })
  75. }
  76. if (res.error_code === 0 && res.data && Object.values(res.data)[0] && Object.values(res.data)[0] !=null && Object.values(res.data)[0][0]) {
  77. // weixin、pc环境
  78. var img = new Image()
  79. var imgsrcArr = Object.values(res.data)[0][0]
  80. img.src = imgsrcArr.s_pic
  81. img.onload= function() {
  82. $(".active-tip-group").show()
  83. }
  84. $(".active-tip-group img").attr('src', imgsrcArr.s_pic + '?v=' + Math.random())
  85. // 如果是app环境 ,调用原生方法、执行保存
  86. // var isAppsionType = isAppsionApp || utils.isIos || utils.isAndroid
  87. var that = this
  88. var isAppsionType = isAppsionApp
  89. if (isAppsionType) {
  90. // APP 保存图片
  91. $('.active-tip-group').off('click').on('click', function () {
  92. if(!window.picImgUrl) {
  93. html2canvas(document.querySelector('.active-tip-group'), {
  94. // allowTaint: true,
  95. useCORS: true,
  96. backgroundColor: null,
  97. scale: 3,
  98. imageTimeout: 30000,
  99. allowTaint: true, // 允许跨域图片
  100. }).then(function(canvas) {
  101. var imgUrl = imgsrcArr.base64 || canvas.toDataURL('image/png')
  102. console.log(imgUrl)
  103. // $(".active-tip-group img").attr('src', imgUrl)
  104. imgUrl = imgUrl.replace('data:image/png;base64,', '')
  105. // console.log(imgUrl)
  106. window.picImgUrl = imgUrl
  107. try {
  108. window.JyObj.savePic(imgUrl)
  109. setTimeout(function() {
  110. weui.toast('图片已保存,快去扫码识别吧', {
  111. duration: 800,
  112. className: 'text-overflow100'
  113. })
  114. }, 2300)
  115. } catch (e) {
  116. weui.toast('保存失败', {
  117. duration: 800,
  118. className: 'j-toast'
  119. })
  120. console.log(e)
  121. // location.href=imgsrcArr.s_link
  122. }
  123. })
  124. }else {
  125. window.JyObj.savePic(window.picImgUrl)
  126. setTimeout(function() {
  127. weui.toast('图片已保存,快去扫码识别吧', {
  128. duration: 800,
  129. className: 'text-overflow100'
  130. })
  131. }, 2300)
  132. }
  133. })
  134. } // App打开新窗口等 用 @param {String} isAppsion === 'Applink'
  135. else if ((isAppsion === 'Applink') && imgsrcArr.o_extend && imgsrcArr.o_extend.linktype === 'abroad') {
  136. $(".active-tip-group").on('click', function () {
  137. try {
  138. JyObj.openExternalLink(imgsrcArr.s_link, imgsrcArr.o_extend.title || '剑鱼标讯');
  139. } catch (e) {
  140. console.log(e);
  141. }
  142. })
  143. if (isIOS()) {
  144. $('.rules').css('margin-top', '0.28rem')
  145. }
  146. } else {
  147. // weixin、pc环境 直接跳转
  148. if (imgsrcArr.s_link) {
  149. $(".active-tip-group").on('click', function () {
  150. try {
  151. location.href=imgsrcArr.s_link
  152. } catch (e) {
  153. console.log(e);
  154. }
  155. })
  156. }
  157. }
  158. } else {
  159. $('.rules').css('margin-top', '0.8rem')
  160. $('.ad_space').hide()
  161. $('.login-dig-box').css('left', 'calc(50vw - 181px)')
  162. }
  163. })
  164. }
  165. }