123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- class oppoAdMange {
- public static readonly _instance: oppoAdMange = new oppoAdMange()
- /**激励视频广告对象 */
- private rewardVideoAd: any
- /**banner广告对象 */
- private bannerAd: any
- /**原生广告对象 */
- private nativeAd: any
- /**原生插屏广告对象 */
- private insertAd: any
- /**激励视频广告ID */
- private _rewardVideoAdUnitId = '388920';
- /**banner广告ID */
- private _banneradUnitId = '388926';
- /**原生广告ID */
- private nativeadUnitId = '388925';
- /**原生插屏广告ID */
- private insertAdUnitId = '';
- public set bannerAdUnitId(adUnitId: string) {
- this._banneradUnitId = adUnitId;
- }
- public set rewardedVideoAdUnitId(adUnitId: string) {
- this._rewardVideoAdUnitId = adUnitId;
- }
- //获取系统信息的同步版本
- get systemInfo() {
- return qg.getSystemInfoSync();
- }
- /**登陆接口 */
- login() {
- qg.login({
- success: res => {
- // 可全局储存 token,方便其他能力获取,注意 token 过期时间
- console.log(`登录成功: ${JSON.stringify(res.data)}`)
- },
- fail: res => {
- console.log(`登录失败: ${JSON.stringify(res)}`)
- }
- })
- }
- /**获取授权信息 */
- public getProfile(_token) {
- return new Promise((resolve, reject) => {
- qg.getProfile({
- token: _token,
- success: function (data) {
- resolve(data);
- },
- fail: function (data, code) {
- reject(data);
- }
- })
- });
- }
- /** 退出游戏,同步方法*/
- exitApplication() {
- qg.exitApplication();
- }
- /**使手机发生较短时间的振动(20 ms) */
- vibrateShort() {
- qg.vibrateShort({
- success: function (res) { },
- fail: function (res) { },
- complete: function (res) { }
- })
- }
- /**使手机发生较长时间的振动(400 ms) */
- vibrateLong() {
- qg.vibrateLong({
- success: function (res) { },
- fail: function (res) { },
- complete: function (res) { }
- })
- }
- //=============================广告
- /**展示激励视频 */
- showRewardVideoAd(suc: Function, fail: Function) {
- console.log("展示激励视频");
- // if(this.systemInfo.minPlatformVersion<1051)return
- // return new Promise<void>((resolve, reject) => {
- this.rewardVideoAd = qg.createRewardedVideoAd({
- adUnitId: this._rewardVideoAdUnitId,
- })
- console.log('加载广告')
- this.rewardVideoAd
- .load()
- .then(() => {
- console.log('promise 回调:加载成功')
- console.log('展示广告')
- // 调用 show 方法请求展示 banner,成功的时候回调 onShow,出错的时候回调 onError
- this.rewardVideoAd
- .show()
- .then(() => {
- console.log('promise 回调:展示成功')
- })
- .catch(err => {
- console.log(`promise 回调:展示失败 ${JSON.stringify(err)}`);
- })
- })
- .catch(err => {
- console.log(`promise 回调:加载失败 ${JSON.stringify(err)}`)
- })
- this.rewardVideoAd.onClose((res: any) => {
- if (res.isEnded) {
- console.log('激励视频广告完成,发放奖励')
- suc()
- this.rewardVideoAd.offClose()
- // resolve();
- } else {
- console.log('激励视频广告取消关闭,不发放奖励')
- fail();
- // reject();
- this.rewardVideoAd.offClose()
- }
- })
- // })
- }
- /**展示Banner广告 */
- showBannerAd() {
- console.log("展示banner视频");
- if (this.systemInfo.minPlatformVersion < 1051) return
- if (this.bannerAd) {
- return;
- }
- this.bannerAd = qg.createBannerAd({
- adUnitId: this._banneradUnitId,
- })
- console.log("创建成功");
- this.bannerAd.onLoad(() => {
- console.log('banner 广告加载成功')
- })
- this.bannerAd.show(() => {
- console.log('banner 广告显示')
- })
- this.bannerAd.onError(function (err) {
- console.error("banner 广告出错")
- console.error(JSON.stringify(err))
- this.bannerAd.destroy()
- this.bannerAd = null;
- })
- }
- /**隐藏Banner广告 */
- hideBannerAd() {
- if (this.bannerAd) {
- this.bannerAd.hide();
- this.bannerAd.destroy();
- this.bannerAd = null;
- }
- }
- /**展示原生插屏广告 */
- showInterstitialAd() {
- }
- /**原生插屏 */
- creatorInterstitialAd() {
- }
- /**创建原生广告 */
- createNativeAd(next: Function, fail: Function) {
- if (this.systemInfo.platformVersionCode < 1051) return
- // if (this.nativeAd) {
- // this.nativeAd.destroy();
- // this.nativeAd = null;
- // }
- this.nativeAd = qg.createNativeAd({
- posId: this.nativeadUnitId
- })
- this.nativeAd.onLoad((res) => {
- if (res && res.adList) {
- console.log("原生广告", res);
- this.reportAdShow(res.adList[0].adId);
- if (next) {
- next(res.adList[0]);
- }
- }
- })
- this.nativeAd.onError((res) => {
- console.log('原生广告加载失败', res)
- if (fail) {
- fail(res);
- }
- })
- let adLoad = this.nativeAd.load()
- adLoad && adLoad.then(() => {
- console.log("原生广告加载成功");
- }).catch((err) => {
- console.log('原生广告加载失败', JSON.stringify(err));
- if (fail) {
- fail();
- }
- });
- }
- /**上报原生点击 */
- clickNativeAd(adId) {
- if (!this.nativeAd) return;
- this.nativeAd.reportAdClick({
- adId: adId,
- });
- }
- /**上报广告曝光 */
- reportAdShow(adId) {
- if (!this.nativeAd) return;
- this.nativeAd.reportAdShow({
- adId: adId
- })
- }
- // 检查登录信息 5分钟后token 失效
- public getSetting(cbOK, cbFail = null) {
- }
- /**判断是否已经创建桌面图标 */
- hasShortcutInstalled() {
- this.login()
- qg.hasShortcutInstalled({
- success: function (res) {
- // 判断图标未存在时,创建图标
- if (res == false) {
- qg.installShortcut({
- success: function () {
- // 执行用户创建图标奖励
- },
- fail: function (err) { },
- complete: function () { }
- })
- }
- },
- fail: function (err) { },
- complete: function () { }
- })
- }
- /**设置游戏加载进度页面(该功能必须接入,否则会因为未接入,导致游戏加载页进度条缺失而被测试打回) */
- setLoadingProgress() {
- qg.setLoadingProgress({
- progress: 50
- });
- }
- /**隐藏游戏加载进度页面 */
- loadingComplete() {
- qg.loadingComplete({
- complete: function (res) { }
- });
- }
- /**跳转小游戏 */
- navigateToMiniGame() {
- qg.navigateToMiniGame({
- pkgName: 'com.sjmsj.nearme.gamecenter',
- path: '?page=pageB',
- extraData: {
- from: 'pageA'
- },
- success: function () {
- },
- fail: function (res) {
- console.log(JSON.stringify(res))
- }
- })
- }
- }
- export const OPPO = oppoAdMange._instance
|