DwSdk.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import lib from './library'
  2. import GameLife from './GameLife'
  3. import md5 from './md5'
  4. let timer
  5. function _renderIcon(gameName, adList, name, x, y, pNode) {
  6. var curAd = { id: -1 }
  7. //定时切换
  8. _randomImage()
  9. timer = setInterval(() => {
  10. _randomImage()
  11. }, 3000)
  12. /**
  13. * 随机渲染一张图片
  14. */
  15. function _randomImage() {
  16. let index = (new Date()).getTime() % adList.length
  17. //不渲染和上一个一样的图标
  18. if (curAd.id === adList[index].id) {
  19. index = (index + 1) % adList.length
  20. }
  21. curAd = adList[index]
  22. let icon = curAd['small_icon'];
  23. // icon = 'http://ben.wiki.webdev2.duowan.com/house.png';
  24. let options = curAd['small_icon_options'] || {}
  25. options['pNode'] = pNode
  26. // options['rows'] = 4
  27. // options['cols'] = 2
  28. // options['num'] = 8
  29. // options['interval'] = 10
  30. lib.drawImage(name, icon, x, y, function () {
  31. // 点击后,更换图标广告
  32. // _randomImage();
  33. // 打开这个广告
  34. _openMp(gameName, curAd)
  35. }, options);
  36. }
  37. }
  38. //加载Prefab
  39. function _loadPrefab(data,gameName) {
  40. cc.loader.loadRes("duowansdk/prefab/homeAd", function (err, prefab) {
  41. var newNode = cc.instantiate(prefab);
  42. cc.director.getScene().addChild(newNode);
  43. let head = newNode.getChildByName('head')
  44. head.getComponent(cc.Label).string = data.title
  45. newNode.getChildByName('content').getChildByName('desc').getComponent(cc.Label).string = data.content
  46. newNode.getChildByName('content').getChildByName('time').getComponent(cc.Label).string = `时间:${data.start_time}-${data.end_time}`
  47. let closeBtn = newNode.getChildByName('icon-close').addComponent(cc.Button)
  48. closeBtn.node.on(cc.Node.EventType.TOUCH_START,() => {
  49. newNode.destroy()
  50. })
  51. let detailBtn = newNode.getChildByName('btn').addComponent(cc.Button)
  52. detailBtn.node.on(cc.Node.EventType.TOUCH_START,() => {
  53. wx.navigateToMiniProgram({
  54. appId: data.app_id,
  55. path: '',
  56. extraData: {}
  57. })
  58. })
  59. });
  60. }
  61. /**
  62. * 尝试直接访问,失败则打开图片的方式
  63. * @param {*} gameName
  64. * @param {*} curAd
  65. */
  66. function _openMp(gameName, curAd) {
  67. console.log(curAd)
  68. if (lib.wx) {
  69. // 优先直接打开小程序
  70. if (curAd.app_id) {
  71. wx.navigateToMiniProgram({
  72. appId: curAd.app_id,
  73. path: curAd.path,
  74. extraData: {},
  75. success() {
  76. // 上报跳转成功数据
  77. lib.get(`${lib.host}hz/static?field=navigate&name=${gameName}`)
  78. },
  79. fail() {
  80. // 打开失败,则打开图片
  81. _previewImage(gameName, curAd)
  82. }
  83. })
  84. } else {
  85. _previewImage(gameName, curAd)
  86. }
  87. } else {
  88. console.debug("点击了图标广告,显示图片:" + curAd['big_image'])
  89. // 上报打开图片数据
  90. lib.get(`${lib.host}hz/static?field=img&name=${curAd.name}`)
  91. window.open(curAd['big_image'])
  92. }
  93. }
  94. function _previewImage(gameName, curAd) {
  95. if(!curAd) {
  96. return
  97. }
  98. // 上报打开图片数据
  99. lib.get(`${lib.host}hz/static?field=img&name=${curAd.name}`)
  100. // 打开失败,则打开图片
  101. wx.previewImage({
  102. urls: [curAd['big_image']]
  103. })
  104. }
  105. export default class DwSdk {
  106. constructor(name, debug = false) {
  107. this.name = name
  108. lib.setDebug(debug)
  109. // 游戏的生命周期
  110. this.objGameLife = new GameLife(name)
  111. this.objGameLife.initEvent()
  112. }
  113. /**
  114. * 显示小图标广告
  115. * @param {int} x X轴位置,默认0
  116. * @param {int} y Y轴位置,默认0
  117. * @param {object} node 可选参数(Cocos引擎的节点)
  118. */
  119. showAdIcon(x = 0, y = 0, node = null) {
  120. let self = this
  121. lib.get(`${lib.host}hz/showAd?name=${self.name}&ips=1`, function (ret) {
  122. _renderIcon(self.name, ret.data, "DwSdk_AdIcon", x, y, node)
  123. });
  124. }
  125. /**
  126. * 隐藏小图标广告
  127. * @param {object} node 可选参数(Cocos引擎的节点)
  128. */
  129. hideAdIcon(node = null) {
  130. lib.hideImage("DwSdk_AdIcon", node);
  131. clearInterval(timer)
  132. }
  133. showHomeAd() {
  134. let self = this
  135. if (lib.cc) {
  136. lib.get(`${lib.host}hz/mainAd?name=${self.name}`, (ret) => {
  137. let data = ret.data
  138. //无数据
  139. if (!data.app_id) {
  140. return
  141. }
  142. //根据时长判断是否加载首屏广告
  143. let lastTime = lib.getStorage("showAdTime");
  144. let interval = data.interval_time * 60 * 1000;
  145. let ts = new Date().getTime();
  146. if (lastTime && interval > ts - lastTime) {
  147. return
  148. } else {
  149. _loadPrefab(data,'多玩游戏中心')
  150. lib.setStorage("showAdTime",ts)
  151. }
  152. })
  153. }
  154. }
  155. /**
  156. * 打开多玩游戏中心
  157. */
  158. openGameCenter() {
  159. let app_id = 'wx582548bc3a843fed';
  160. let gameName = '多玩游戏中心';
  161. if (lib.wx) {
  162. wx.navigateToMiniProgram({
  163. appId: app_id,
  164. extraData: {},
  165. success() {
  166. // 上报跳转成功数据
  167. lib.get(`${lib.host}hz/static?field=navigate&name=${gameName}`)
  168. },
  169. fail() {
  170. // 打开失败,则打开图片
  171. lib.get(`${lib.host}hz/common`, function ({ data }) {
  172. // 上报打开图片数据
  173. // lib.get(`${lib.host}hz/static?field=img&name=${gameName}`)
  174. wx.previewImage({
  175. urls: [data.hz_picture]
  176. })
  177. });
  178. }
  179. })
  180. } else {
  181. // 打开失败,则打开图片
  182. lib.get(`${lib.host}hz/common`, function ({ data }) {
  183. window.open(data.hz_picture)
  184. });
  185. }
  186. }
  187. setCanvasTranslate(dx, dy) {
  188. lib.setCanvasTranslate(dx, dy)
  189. }
  190. update() {
  191. lib.updateImage();
  192. }
  193. //获取广告图标列表
  194. getAdList(cb) {
  195. lib.get(`${lib.host}hz/showAd?name=${self.name}&ips=1`, (ret) => {
  196. if(ret.data) {
  197. cb(ret.data)
  198. }
  199. })
  200. }
  201. /**
  202. * 点击图标上报
  203. * @param {string} name 被点击的图标的游戏的名称
  204. */
  205. reportClickAdIcon(name) {
  206. lib.get(`${lib.host}hz/static?field=img&name=${name}`)
  207. }
  208. }