DwSdk.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import lib from './library'
  2. import GameLife from './GameLife'
  3. function _renderIcon(gameName, adList, name, x, y, pNode) {
  4. _randomImage()
  5. /**
  6. * 随机渲染一张图片
  7. */
  8. function _randomImage() {
  9. let index = (new Date()).getTime() % adList.length
  10. let curAd = adList[index]
  11. let icon = curAd['small_icon'];
  12. // icon = 'http://ben.wiki.webdev2.duowan.com/house.png';
  13. let options = curAd['small_icon_options'] || {}
  14. options['pNode'] = pNode
  15. // options['rows'] = 4
  16. // options['cols'] = 2
  17. // options['num'] = 8
  18. // options['interval'] = 10
  19. lib.drawImage(name, icon, x, y, function () {
  20. // 点击后,更换图标广告
  21. _randomImage();
  22. // 打开这个广告
  23. _openMp(gameName, curAd)
  24. }, options);
  25. }
  26. }
  27. /**
  28. * 尝试直接访问,失败则打开图片的方式
  29. * @param {*} gameName
  30. * @param {*} curAd
  31. */
  32. function _openMp(gameName, curAd) {
  33. if (lib.wx) {
  34. // 优先直接打开小程序
  35. if (curAd.app_id) {
  36. wx.navigateToMiniProgram({
  37. appId: curAd.app_id,
  38. path: curAd.path,
  39. extraData: {},
  40. success() {
  41. // 上报跳转成功数据
  42. lib.get(`${lib.host}hz/static?field=navigate&name=${gameName}`)
  43. },
  44. fail() {
  45. // 打开失败,则打开图片
  46. _previewImage(gameName, curAd)
  47. }
  48. })
  49. } else {
  50. _previewImage(gameName, curAd)
  51. }
  52. } else {
  53. console.debug("点击了图标广告,显示图片:" + curAd['big_image'])
  54. // 上报打开图片数据
  55. lib.get(`${lib.host}hz/static?field=img&name=${gameName}`)
  56. window.open(curAd['big_image'])
  57. }
  58. }
  59. function _previewImage(gameName, curAd) {
  60. // 上报打开图片数据
  61. lib.get(`${lib.host}hz/static?field=img&name=${gameName}`)
  62. // 打开失败,则打开图片
  63. wx.previewImage({
  64. urls: [ curAd['big_image'] ]
  65. })
  66. }
  67. export default class DwSdk {
  68. constructor(name, debug = false) {
  69. this.name = name
  70. lib.setDebug(debug)
  71. // 游戏的生命周期
  72. this.objGameLife = new GameLife(name)
  73. this.objGameLife.initEvent()
  74. }
  75. /**
  76. * 显示小图标广告
  77. * @param {int} x X轴位置,默认0
  78. * @param {int} y Y轴位置,默认0
  79. * @param {object} node 可选参数(Cocos引擎的节点)
  80. */
  81. showAdIcon(x = 0, y = 0, node = null) {
  82. let self = this
  83. lib.get(`${lib.host}hz/showAd?name=${self.name}&ips=1` , function (ret) {
  84. _renderIcon(self.name, ret.data, "DwSdk_AdIcon", x, y, node)
  85. });
  86. }
  87. /**
  88. * 隐藏小图标广告
  89. * @param {object} node 可选参数(Cocos引擎的节点)
  90. */
  91. hideAdIcon(node = null) {
  92. lib.hideImage("DwSdk_AdIcon", node);
  93. }
  94. showHomeAd() {
  95. if (lib.cc) {
  96. // 加载 Prefab
  97. cc.loader.loadRes("duowansdk/prefab/homeAd", function (err, prefab) {
  98. var newNode = cc.instantiate(prefab);
  99. cc.director.getScene().addChild(newNode);
  100. let head = newNode.getChildByName('head')
  101. head.getChildByName('title').getComponent(cc.Label).string = "标题测试"
  102. newNode.getChildByName('content').getComponent(cc.Label).string = "内容测试"
  103. });
  104. }
  105. }
  106. /**
  107. * 打开多玩游戏中心
  108. */
  109. openGameCenter() {
  110. let app_id = 'wx582548bc3a843fed';
  111. let gameName = '多玩游戏中心';
  112. if (lib.wx) {
  113. wx.navigateToMiniProgram({
  114. appId: app_id,
  115. extraData: {},
  116. success() {
  117. // 上报跳转成功数据
  118. lib.get(`${lib.host}hz/static?field=navigate&name=${gameName}`)
  119. },
  120. fail() {
  121. // 打开失败,则打开图片
  122. lib.get(`${lib.host}hz/common`, function({data}) {
  123. // 上报打开图片数据
  124. lib.get(`${lib.host}hz/static?field=img&name=${gameName}`)
  125. wx.previewImage({
  126. urls: [data.hz_picture]
  127. })
  128. });
  129. }
  130. })
  131. } else {
  132. // 打开失败,则打开图片
  133. lib.get(`${lib.host}hz/common`, function({data}) {
  134. window.open(data.hz_picture)
  135. });
  136. }
  137. }
  138. setCanvasTranslate(dx, dy) {
  139. lib.setCanvasTranslate(dx, dy)
  140. }
  141. update() {
  142. lib.updateImage();
  143. }
  144. }