index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // lionfish_comshop/components/ad-alert/index.js
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. * pop_page 投放页面类型:0、商城首页,1、商品分类,2、商城购物车,3、商城个人中心
  6. */
  7. properties: {
  8. pop_page: {
  9. type: Number,
  10. value: ''
  11. }
  12. },
  13. data: {
  14. imgheights: [],//所有图片的高度
  15. current:0,
  16. visible: false,
  17. adv_list: []
  18. },
  19. attached: function() {
  20. // this.getData();
  21. },
  22. pageLifetimes: {
  23. show: function () {
  24. this.getData();
  25. }
  26. },
  27. methods: {
  28. getData: function(){
  29. let token = wx.getStorageSync('token');
  30. let pop_page = this.data.pop_page;
  31. getApp().util.ProReq('popadv.popadv_list', {token, pop_page}).catch(res=>{
  32. console.log(res.popadvs)
  33. if(res.popadvs) {
  34. let adv_list = res.popadvs.adv_list || [];
  35. this.setData({
  36. adv_list,
  37. visible: adv_list.length>0
  38. })
  39. }
  40. })
  41. },
  42. imageLoad(e){
  43. //当图片载入完毕时
  44. var imgwidth = e.detail.width,
  45. imgheight = e.detail.height,
  46. //宽高比
  47. ratio = imgwidth / imgheight;
  48. console.log(imgwidth, imgheight)
  49. // 计算的高度值
  50. var viewHeight = 600 / ratio;
  51. var imgheight = viewHeight;
  52. var imgheights = this.data.imgheights;
  53. //把每一张图片的对应的高度记录到数组里
  54. imgheights[e.target.dataset.id] = imgheight;
  55. console.log('图片的高度:'+imgheights)
  56. this.setData({
  57. imgheights: imgheights
  58. })
  59. },
  60. bindchange: function (e) {
  61. // current 改变时会触发 change 事件
  62. this.setData({ current: e.detail.current })
  63. },
  64. close: function() {
  65. this.setData({ visible: false })
  66. },
  67. popclick: function(e){
  68. let idx = e.currentTarget.dataset.idx;
  69. if(idx!=='') {
  70. let adv_list = this.data.adv_list;
  71. let linktype = adv_list[idx].linktype;
  72. let url = adv_list[idx].link;
  73. let ad_id = adv_list[idx].ad_id;
  74. if (linktype == 0) {
  75. // 跳转webview
  76. wx.navigateTo({
  77. url: '/lionfish_comshop/pages/web-view?url=' + encodeURIComponent(url),
  78. })
  79. } else if (linktype == 1) {
  80. let tabUrls = ['/lionfish_comshop/pages/index/index', '/lionfish_comshop/pages/order/shopCart', '/lionfish_comshop/pages/user/me', '/lionfish_comshop/pages/type/index'];
  81. if (tabUrls.indexOf(url) != -1) {
  82. url && wx.switchTab({ url: url })
  83. } else {
  84. url && wx.navigateTo({ url: url })
  85. }
  86. } else if (linktype == 2) {
  87. // 跳转小程序
  88. let appId = adv_list[idx].appid;
  89. appId && wx.navigateToMiniProgram({
  90. appId,
  91. path: url,
  92. extraData: {},
  93. envVersion: 'release',
  94. success(res) {
  95. console.log(`弹窗广告:打开外链 appId:${appId} url:${url} 成功`)
  96. },
  97. fail(error) {
  98. console.log(`弹窗广告:打开外链 appId:${appId} url:${url} 失败`)
  99. console.log('失败原因:' + error.errMsg)
  100. if(error.errMsg == 'navigateToMiniProgram:fail invalid appid') {
  101. wx.showToast({
  102. icon: 'none',
  103. title: 'appid验证失败,请检查!',
  104. })
  105. }
  106. }
  107. })
  108. }
  109. //添加点击量
  110. this.addview(ad_id);
  111. }
  112. },
  113. addview: function(id){
  114. getApp().util.ProReq('popadv.popadv_click', {id})
  115. }
  116. }
  117. })