使用laya2.0发布的百度小游戏,目前只使用了登录,分享,播放广告,调试等功能
注意事项:下载好百度开发者工具,将自己的百度账号添加进管理人员,手机上面下载好百度APP,最好不要使用极速版,添加的账号是自己的用户名,不是昵称或者手机号
在百度小程序社区询问或者回复信息的时候,如果提示发布内容违规,不要去找哪里违规然后修改再次提交/回复,直接将文本截图,做成图片,使用图片去发布,不然发不了几次,你就会发现你被禁言了,解封还贼烦。就做成图片!做成图片!做成图片!
1.登录
登录时,要保证自己相关的配置已经完成:
1.将产品的App ID和App Key发给后端开发人员(在管理–基础设置–开发设置界面)
2.配置服务器域名(不要自己乱配,一个月只有5次修改机会,稍不注意就用完了,具体询问后端服务器开发人员)(在管理–基础设置–开发设置界面)
//方法里面也可以增加自己需要的回调函数
public login(): void {
this.swan.login({
success: function (res) {
console.log('百度平台登录code:' + res && res.code);
//接下来根据获取得到的code,向后端请求openid和token,具体操作根据后端接口的不同而定,具体操作询问后端对接开发人员
//根据得到的openid和token进行登录
//请求方法(自写),请求成功/失败,里面可以根据返回的结果进行相关操作或者回调
},
fail: () => {
console.log('百度登录失败');
this.swan.showModal({
title: "登录失败",
content: "是否重新登录?",
cancelText: "重新登录",
confirmText: "进入游戏",
success: (res) => {
if (res && res.cancel) {
console.log("点击了重新登录");
//再次发送登录请求(也就是再次调用登录方法)
this.login();
}
else {
console.log("点击了进入游戏");
//接下来根据获取得到的code,向后端请求openid和token,具体操作根据后端接口的不同而定,具体操作询问后端对接开发人员
//根据得到的openid和token进行登录
//请求方法(自写),请求成功/失败,里面可以根据返回的结果进行相关操作或者回调
}
}
})
},
complete: (res) => {
console.log('百度登录完成', res);
}
});
}
2.分享
public shareVideo(callBack: Handler): void {
console.log("开始调用分享功能");
if (Date.now() - this.lastShareTime < 1000) {
DebugLog.log("分享功能频率过高,请稍后再试");
return;// 防止瞬间调用多次
}
this.lastShareTime = Date.now();
if (!this.swan) {
this.swan.showToast({
title: '分享失败',
icon: 'none'
});
callBack != null && callBack.runWith(false);
return;
}
this.swan.openShare({
title: '********',//标题
content: "********",//内容
path: 'swan-api/open-share/open-share?key=value',//跳转页面
imageUrl: "https://b.bdstatic.com/miniapp/images/bgt_icon.png",//展示的图片地址
success: res => {
this.swan.showToast({
title: '分享成功',
icon: 'none'
});
console.log('分享成功openShare success', res);
callBack != null && callBack.runWith(true);
},
fail: err => {
console.log("分享成功失败!" + JSON.stringify(err));
this.swan.showToast({
title: '分享失败',
icon: 'none'
});
callBack != null && callBack.runWith(false);
}
});
this.swan.onShareAppMessage && this.swan.onShareAppMessage();
}
3.接广告(激励视频)
接广告有点麻烦了:
1.先保证百度账号已经开通了百青藤(经营–商业推广–流量主)
2.开通百青藤以后,开通应用合作,进入小程序管理(根据自己项目确定)–新建小程序应用
3.新建小程序应用以后,打开网盟推广的代码位管理–新建代码位–(里面数据根据自己需求添加,我选择的是代码位类型:激励视频,选择场景:激励视频)
4.代码位创建完毕以后,记录相关id:
//代码位id:adUnitId: 代码位名称下方对应的ID
//应用id:appSid: 所属应用下方对应的ID
//项目开始运行时可以先创建一个视频对象,也可以在播放广告的时候再创建,看自己需求
private videoAd; //视频广告对象
public videoAdReady: boolean; //视频广告是否准备好
private bdadUinitId: string = '******'; //百度广告位ID
private bdAppSid: string = '++++++'; //应用ID
private reloadNum: number = 2; //广告重新拉取次数
private swan;
/** 预先创建好广告体 */
public createAds(): void {
this.swan = Laya.Browser.window.swan;
if (this.swan) {
console.log('预先创建好广告体 ===adUnitId :' + this.bdadUinitId + ' appSid:' + this.bdAppSid);
this.videoAd = this.swan.createRewardedVideoAd({ adUnitId: this.bdadUinitId, appSid: this.bdAppSid });
if (this.videoAd) {
console.log('========百度广告体创建成功=====');
this.videoAd.onLoad(() => {
console.log('====百度广告拉取成功 onLoad ======');
this.videoAdReady = true;
this.reloadNum = 2;
});
this.videoAd.onError(err => {
console.log('====百度广告拉取失败 onError ======', err);
this.videoAdReady = false;
if (err.errCode) { //无合适的广告 这种情况重新拉取
if (this.reloadNum > 0) {
this.reloadNum--;
this.videoAd.load();
} else {
}
} else {
}
});
this.videoAd.load();
}
}
}
//播放视频广告
// 看激励视频广告 (视频)
public showVideoAd(closeHdr: Laya.Handler): void {
if (!this.videoAd) this.createAds();
if (!this.videoAd) {
this.ShowAdError('广告播放失败,请稍后再试');
console.error('广告体创建失败');
return;
}
if (!this.videoAdReady) {
this.ShowAdError('广告播放失败,请稍后再试');
console.log("广告没有准备好!!!")
return;
}
//广告播放失败,重新下载并播放
this.videoAd.show().then(() => {
console.log('广告播放成功 then ');
}).catch((err) => {
DebugLog.log("广告播放失败 ", err)
this.videoAd.load().then(() => {
DebugLog.log("广告播放失败,重新拉取成功")
this.videoAd.show().catch((err) => {
if (closeHdr) closeHdr.runWith(false);
});
});
});
DebugLog.log("广告展示成功")
this.videoAd.onClose((res) => {
this.videoAd.offClose();
DebugLog.log("关闭广告 ", res)
let isEnd: boolean = false;
if (res && res.isEnded || res === undefined) { //正常播放退出,下放奖励
isEnd = true;
res.isEnded = false;
} else {
console.log('广告中途被关闭');
}
if (closeHdr) closeHdr.runWith(isEnd);
});
}
public ShowAdError(_title: string) {
this.swan.showToast({
title: _title,
icon: 'none'
});
}
4.调试
在开发者工具上面调试时,注意自己项目的appid,在右上角的项目信息里面,注意查看id是否是正确的,调试基础库尽量高一点
在调试的时候,我这边使用真机调试时出现了问题,如下图所示:
提示我设置了在程序开头暂停,但是我没有找到具体设置的位置
我换了一种方式:点击预览(快速预览或者普通预览无所谓),手机使用百度APP扫码进入游戏,点击右上角的三个点,然后点击下方打开的项目名字,里面有一个打开/关闭sConsole调试按钮,打开调试以后就可以看log了