DwSdk.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. //装配启动参数
  122. let lunchData = wx.getLaunchOptionsSync()
  123. let sdkParam = {}
  124. try {
  125. lunchData.query = JSON.parse(lunchData.query)
  126. lunchData.referrerInfo = JSON.parse(lunchData.referrerInfo)
  127. delete lunchData.__public
  128. } catch (error) {
  129. }
  130. sdkParam.query = lunchData.query
  131. sdkParam.referrerInfo = lunchData.referrerInfo
  132. let sdkStr = JSON.stringify(sdkParam)
  133. console.log(sdkStr)
  134. lib.get(`${lib.host}hz/showAd?name=${self.name}&ips=1&sdk_param=${sdkStr}`, function (ret) {
  135. _renderIcon(self.name, ret.data, "DwSdk_AdIcon", x, y, node)
  136. });
  137. }
  138. /**
  139. * 隐藏小图标广告
  140. * @param {object} node 可选参数(Cocos引擎的节点)
  141. */
  142. hideAdIcon(node = null) {
  143. lib.hideImage("DwSdk_AdIcon", node);
  144. clearInterval(timer)
  145. }
  146. showHomeAd() {
  147. let self = this
  148. if (lib.cc) {
  149. lib.get(`${lib.host}hz/mainAd?name=${self.name}`, (ret) => {
  150. let data = ret.data
  151. //无数据
  152. if (!data.app_id) {
  153. return
  154. }
  155. //根据时长判断是否加载首屏广告
  156. let lastTime = lib.getStorage("showAdTime");
  157. let interval = data.interval_time * 60 * 1000;
  158. let ts = new Date().getTime();
  159. if (lastTime && interval > ts - lastTime) {
  160. return
  161. } else {
  162. _loadPrefab(data,'多玩游戏中心')
  163. lib.setStorage("showAdTime",ts)
  164. }
  165. })
  166. }
  167. }
  168. /**
  169. * 打开多玩游戏中心
  170. */
  171. openGameCenter() {
  172. let app_id = 'wxa72e774364b4e06c';
  173. let gameName = '多玩游戏中心';
  174. if (lib.wx) {
  175. wx.navigateToMiniProgram({
  176. appId: app_id,
  177. extraData: {},
  178. success() {
  179. // 上报跳转成功数据
  180. lib.get(`${lib.host}hz/static?field=navigate&name=${gameName}`)
  181. },
  182. fail() {
  183. // 打开失败,则打开图片
  184. lib.get(`${lib.host}hz/common`, function ({ data }) {
  185. // 上报打开图片数据
  186. // lib.get(`${lib.host}hz/static?field=img&name=${gameName}`)
  187. wx.previewImage({
  188. urls: [data.hz_picture]
  189. })
  190. });
  191. }
  192. })
  193. } else {
  194. // 打开失败,则打开图片
  195. lib.get(`${lib.host}hz/common`, function ({ data }) {
  196. window.open(data.hz_picture)
  197. });
  198. }
  199. }
  200. setCanvasTranslate(dx, dy) {
  201. lib.setCanvasTranslate(dx, dy)
  202. }
  203. update() {
  204. lib.updateImage();
  205. }
  206. //获取广告图标列表
  207. getAdList(cb) {
  208. lib.get(`${lib.host}hz/showAd?name=${self.name}&ips=1`, (ret) => {
  209. if(ret.data) {
  210. cb(ret.data)
  211. }
  212. })
  213. }
  214. /**
  215. * 点击图标上报
  216. * @param {string} name 被点击的图标的游戏的名称
  217. */
  218. reportClickAdIcon(name) {
  219. lib.get(`${lib.host}hz/static?field=img&name=${name}`)
  220. }
  221. }