123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- import lib from './library'
- import GameLife from './GameLife'
- function _renderIcon(gameName, adList, name, x, y, pNode) {
- _randomImage()
- /**
- * 随机渲染一张图片
- */
- function _randomImage() {
- let index = (new Date()).getTime() % adList.length
- let curAd = adList[index]
- let icon = curAd['small_icon'];
- // icon = 'http://ben.wiki.webdev2.duowan.com/house.png';
- let options = curAd['small_icon_options'] || {}
- options['pNode'] = pNode
- // options['rows'] = 4
- // options['cols'] = 2
- // options['num'] = 8
- // options['interval'] = 10
-
- lib.drawImage(name, icon, x, y, function () {
- // 点击后,更换图标广告
- _randomImage();
- // 打开这个广告
- _openMp(gameName, curAd)
- }, options);
- }
- }
- /**
- * 尝试直接访问,失败则打开图片的方式
- * @param {*} gameName
- * @param {*} curAd
- */
- function _openMp(gameName, curAd) {
- if (lib.wx) {
- // 优先直接打开小程序
- if (curAd.app_id) {
- wx.navigateToMiniProgram({
- appId: curAd.app_id,
- path: curAd.path,
- extraData: {},
- success() {
- // 上报跳转成功数据
- lib.get(`${lib.host}hz/static?field=navigate&name=${gameName}`)
- },
- fail() {
- // 打开失败,则打开图片
- _previewImage(gameName, curAd)
- }
- })
- } else {
- _previewImage(gameName, curAd)
- }
- } else {
- console.debug("点击了图标广告,显示图片:" + curAd['big_image'])
- // 上报打开图片数据
- lib.get(`${lib.host}hz/static?field=img&name=${gameName}`)
- window.open(curAd['big_image'])
- }
- }
- function _previewImage(gameName, curAd) {
- // 上报打开图片数据
- lib.get(`${lib.host}hz/static?field=img&name=${gameName}`)
- // 打开失败,则打开图片
- wx.previewImage({
- urls: [ curAd['big_image'] ]
- })
- }
- export default class DwSdk {
- constructor(name, debug = false) {
- this.name = name
- lib.setDebug(debug)
- // 游戏的生命周期
- this.objGameLife = new GameLife(name)
- this.objGameLife.initEvent()
- }
- /**
- * 显示小图标广告
- * @param {int} x X轴位置,默认0
- * @param {int} y Y轴位置,默认0
- * @param {object} node 可选参数(Cocos引擎的节点)
- */
- showAdIcon(x = 0, y = 0, node = null) {
- let self = this
- lib.get(`${lib.host}hz/showAd?name=${self.name}&ips=1` , function (ret) {
- _renderIcon(self.name, ret.data, "DwSdk_AdIcon", x, y, node)
- });
- }
- /**
- * 隐藏小图标广告
- * @param {object} node 可选参数(Cocos引擎的节点)
- */
- hideAdIcon(node = null) {
- lib.hideImage("DwSdk_AdIcon", node);
- }
- showHomeAd() {
- if (lib.cc) {
- // 加载 Prefab
- cc.loader.loadRes("duowansdk/prefab/homeAd", function (err, prefab) {
- var newNode = cc.instantiate(prefab);
- cc.director.getScene().addChild(newNode);
- let head = newNode.getChildByName('head')
- head.getChildByName('title').getComponent(cc.Label).string = "标题测试"
- newNode.getChildByName('content').getComponent(cc.Label).string = "内容测试"
- });
- }
- }
- /**
- * 打开多玩游戏中心
- */
- openGameCenter() {
- let app_id = 'wx582548bc3a843fed';
- let gameName = '多玩游戏中心';
- if (lib.wx) {
- wx.navigateToMiniProgram({
- appId: app_id,
- extraData: {},
- success() {
- // 上报跳转成功数据
- lib.get(`${lib.host}hz/static?field=navigate&name=${gameName}`)
- },
- fail() {
- // 打开失败,则打开图片
- lib.get(`${lib.host}hz/common`, function({data}) {
- // 上报打开图片数据
- lib.get(`${lib.host}hz/static?field=img&name=${gameName}`)
- wx.previewImage({
- urls: [data.hz_picture]
- })
- });
- }
- })
- } else {
- // 打开失败,则打开图片
- lib.get(`${lib.host}hz/common`, function({data}) {
- window.open(data.hz_picture)
- });
- }
- }
- setCanvasTranslate(dx, dy) {
- lib.setCanvasTranslate(dx, dy)
- }
- update() {
- lib.updateImage();
- }
- }
|