From e682ab1b83597e691fd8b201b8670f83d1880bce Mon Sep 17 00:00:00 2001 From: song Date: Wed, 21 Dec 2022 10:11:14 +0800 Subject: [PATCH] 视频多例 --- sdk/shareTools.ts | 12 +++++++++++- wxsdk/base/SDKConst.ts | 2 +- wxsdk/service/GameService.ts | 11 ++++++++--- wxsdk/service/ShareVideoService.ts | 272 +++++++++++++++++--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- wxsdk/share/SDKVideo.ts | 49 ++++++++++++++++++++++++++++++------------------- 5 files changed, 67 insertions(+), 279 deletions(-) diff --git a/sdk/shareTools.ts b/sdk/shareTools.ts index 26ba687..10a8aa7 100644 --- a/sdk/shareTools.ts +++ b/sdk/shareTools.ts @@ -13,7 +13,7 @@ export class ShareTools { * 验证分享:可处理成功、失败 * @param shareKey * @param params params.fail 有就不处理,没有自动处理 - * @param opts 目前支持4个key 1,title自定义分享标题 2,img_url自定义分享图片 3,share_type(不走后台配置写死走视频or分享。1分享2视频3无视频则分享)4,closeSimulate是否关闭模拟分享 + * @param opts 目前支持5个key 1,title自定义分享标题 2,img_url自定义分享图片 3,share_type(不走后台配置写死走视频or分享。1分享2视频3无视频则分享)4,closeSimulate是否关闭模拟分享 5:multiton重新创建视频实例 */ static share(shareKey: string, params?: { success?: Function, fail?: Function, context?: any }, opts?: any) { if (typeof wx === 'undefined' || this.isTest) { @@ -37,6 +37,16 @@ export class ShareTools { } })//this.buildParams(params) } + + /** + * 重新创建视频实例 + * @param shareKey + * @param params + * @param opts multiton重新创建视频实例 + */ + static multitonShare(shareKey: string, params?: { success?: Function, fail?: Function, context?: any }, opts?: any) { + this.share(shareKey, params, { multiton: true, ...opts }); + } /** * 纯净分享 不处理回调 * @param shareKey diff --git a/wxsdk/base/SDKConst.ts b/wxsdk/base/SDKConst.ts index c56e3be..f6dfa62 100644 --- a/wxsdk/base/SDKConst.ts +++ b/wxsdk/base/SDKConst.ts @@ -35,7 +35,7 @@ export const GAMEDATA = { // sdk版本 -export const SDKVersion = 'v1.0.15'; +export const SDKVersion = 'v1.0.16'; // 是否打印 export const __LOG__ = false; // 是否mock diff --git a/wxsdk/service/GameService.ts b/wxsdk/service/GameService.ts index bb7ce0f..8d9b047 100644 --- a/wxsdk/service/GameService.ts +++ b/wxsdk/service/GameService.ts @@ -159,16 +159,21 @@ export default class GameService { }) } /** - * 文本检测 + * 更换昵称和头像 */ - updateNickname(nickname: string, headurl: string) { + updateNickname(nickname: string, headurl: string) { + let gameid = GAMEDATA.game_id; + let uid = DataService.I.UserId; + let token = DataService.I.Token; + let pkv = SDKVersion; return SDKApi.updateNickname({ - ...this.buildParams(), + gameid,uid,token,pkv, nickname, headurl }) } + /** * 构建登录/弱登录公用参数 */ diff --git a/wxsdk/service/ShareVideoService.ts b/wxsdk/service/ShareVideoService.ts index 95ed14a..91f3fa5 100644 --- a/wxsdk/service/ShareVideoService.ts +++ b/wxsdk/service/ShareVideoService.ts @@ -1,11 +1,11 @@ -import { ShareVideoType, ShareVideoFrom } from "../base/SDKEnum"; -import RandomUtils from "../utils/RandomUtils"; -import ShareData from "./entity/ShareData"; -import SDKShare from "../share/SDKShare"; -import { __LOG__, GAMEDATA } from "../base/SDKConst"; +import { GAMEDATA } from "../base/SDKConst"; +import { ShareVideoType } from "../base/SDKEnum"; import { SDKApi } from "../http/SDKApi"; -import DataService from "./DataService"; +import SDKShare from "../share/SDKShare"; import SDKVideo from "../share/SDKVideo"; +import RandomUtils from "../utils/RandomUtils"; +import DataService from "./DataService"; +import ShareData from "./entity/ShareData"; export default class ShareVideoService { private forwardKey?: string; @@ -27,26 +27,19 @@ export default class ShareVideoService { this.forward(this.forwardKey); } - private cnt: number = 1 - async initList() { //初始化List + async init() { let data = await SDKApi.ShareList(); - if (data.code) { - console.log('拉取ShareList失败!') - setTimeout(this.initList.bind(this), 200 * this.cnt) - this.cnt++; - return - } this.setShareVideoData(data); - if (this.forwardKey) this.forward(this.forwardKey); - } - async init() { - this.initList(); + // console.log(JSON.stringify(data)) SDKShare.I.updateShareMenu(true);//打开群分享 + if (cc.sys.platform === cc.sys.WECHAT_GAME) { if (GAMEDATA.shareMessageToFriend.scene > 0) { wx.setMessageToFriendQuery({ shareMessageToFriendScene: GAMEDATA.shareMessageToFriend.scene }) } } + + if (this.forwardKey) this.forward(this.forwardKey); } preload = true; @@ -69,22 +62,26 @@ export default class ShareVideoService { share_id: id, query: this.createQuery({ share_key: key, share_id: id, query: opts.query }), }; + let multiton = false; if (opts) { if (opts.title) _params.title = opts.title; if (opts.img_url) _params.imageUrl = opts.img_url; if (opts.share_type) typ = opts.share_type; + if (opts.hasOwnProperty('multiton')) { + multiton = opts.multiton; + }; } // console.log("share_query",JSON.stringify(_params), _params.query); switch (+typ) { case ShareVideoType.Video: - SDKVideo.I.show(shareKey, videoid).then(success => { + SDKVideo.I.show(shareKey, videoid, multiton).then(success => { resolve(success) }).catch(err => { reject(err); }) break; case ShareVideoType.VideoToShare: - SDKVideo.I.show(shareKey, videoid).then(success => { + SDKVideo.I.show(shareKey, videoid, multiton).then(success => { resolve(success) }).catch(err => { if (err.code !== 1000 && err.code !== 1003) {//1000关闭1003正在播放 @@ -110,46 +107,6 @@ export default class ShareVideoService { resolve({}) break; } - // share_desc = opts.shareTitle || share_desc; - // share_desc = StringUtils.stringFormat(share_desc, opts.formater); - // let shareMsg = { - // title: share_desc, - // imageUrl: opts.shareImg || share_icon, - // query - // }; - // // __LOG__ && console.error(`SDK ShareVideoService ${opts.shareForward ? 'forward 右上角分享:' : 'share 主动拉起分享:'}`); - // // __LOG__ && console.error(`SDK ShareVideoService 分享参数对象shareMsg: ${JSON.stringify(shareMsg)}`); - // // __LOG__ && console.error('SDK ShareVideoService 分享扩展参数', opts); - // DebugUtils.I.dynamic(`====> PCSDK ShareVideoService share 分享query: ${query}`); - // let success = (ret: any) => { - // // __LOG__ && console.error(`SDK ShareVideoService ${opts.shareForward ? 'forward 右上角分享:' : 'share 主动拉起分享:'} 成功回调: ${share_id}`); - // if (!opts.__GROUP) { - // // 不是群分享的情况下,进行统计,群分享需要验证 - // TempService.I.add(TempDataKeys.ShareSuccess, 1); - // share_id && LogService.I.tacticShare(share_id, share_key, TacticType.ShareSuccess); - // } - // resolve({ ...ret, share_id, share_key: shareKey }); - // }; - // let fail = (err: any) => { - // // __LOG__ && console.error(`SDK ShareVideoService ${opts.shareForward ? 'forward 右上角分享:' : 'share 主动拉起分享:'} 失败回调: ${share_id}`); - // if (!opts.__GROUP) { - // share_id && LogService.I.tacticShare(share_id, share_key, TacticType.ShareInterrupt); - // } - // reject({ ...err, share_id, share_key: shareKey }); - // }; - // if (opts.shareForward) - // // 右上角转发 - // Platform.I.forward(shareMsg, { - // ...opts, - // success, - // fail, - // context: this - // }); - // else - // // 普通分享 - // Platform.I.share(shareMsg, opts) - // .then((ret: any) => success(ret)) - // .catch((err: any) => fail(err)); }); } @@ -166,129 +123,6 @@ export default class ShareVideoService { // console.log("forward", JSON.stringify(params)); SDKShare.I.forward(params); } - - /** - * 分享自动入口 - * @param shareKey - * @param opts { - * type: 当shareType为VideoAndShare时候,该参数有用,0:分享 1:视频 - * context: 函数执行上下文 - * fail: Function 失败函数 - * success: Function 成功函数 - * } - */ - // dispatch(shareKey: string, opts: any = {}, queryObj: { [key: string]: number | string } = {}) { - // let shareData = this.getShareVideoData(shareKey); - // let { share_open, share_wxad_id } = shareData; - // opts = { - // ...opts, - // share_wxad_id, - // __ShareData__: shareData - // }; - // this.dispatchType(+share_open, shareKey, opts, queryObj); - // } - - shareDispatch(shareKey: string, opts: any = {}, queryObj: { [key: string]: number | string } = {}) { - // this.dispatch(shareKey, opts, queryObj); - } - - /** - * 根据分享类型进行处理 - * @param shareType - * @param shareKey - * @param opts { - * type: 当shareType为VideoAndShare时候,该参数有用,0:分享 1:视频 - * context: 函数执行上下文 - * fail: Function 失败函数 - * success: Function 成功函数 - * } - */ - dispatchType(shareType: ShareVideoType, shareKey: string, opts: any = {}, queryObj: { [key: string]: number | string } = {}) { - // if (Platform.IsWx && OnlineService.I.getParamsInt(OnlineKeys.ShareUnlock, 1) !== 1) - // shareType = ShareVideoType.None; - - // let share_wxad_id = opts.share_wxad_id || (opts.__ShareData__ || this.getShareVideoData(shareKey) || {}).share_wxad_id; - // switch (shareType) { - // case ShareVideoType.None: // 无分享 - // this.handleSuccess(opts, shareType, ShareVideoFrom.None, null); - // break; - - // case ShareVideoType.Share: // 同步分享 - // // 同步分享点:是否开启后台配置中如果存在视频ID,则自动切换为视频点 - // if (opts.shareAutoVideo && share_wxad_id) { - // this.dispatchType(ShareVideoType.VideoToShare, shareKey, opts, queryObj); - // break; - // } - // this.group(shareKey, queryObj, opts) - // .then(ret => this.handleSuccess(opts, shareType, ShareVideoFrom.Share, ret)) - // .catch(err => this.handleFail(opts, shareType, ShareVideoFrom.Share, err)); - // break; - - // case ShareVideoType.ShareAysnc: // 异步分享 - // break; - - // case ShareVideoType.ShareIntegral: // 分享积分 - // // 是否开启分享积分,且配置为分享积分 - // if (IntegralService.I.IsOpen) { - // let data = IntegralService.I.convert(); - // if (data) { - // opts.shareIntegralData = data; - // this.dispatchType(data.shareType, shareKey, opts, queryObj); - // } else { - // // 超过了现在,直接执行失败:今日已达分享上限次数,请明日再来 - // this.handleFail(opts, shareType, ShareVideoFrom.Share, { ...ShareVideoError.ShareOverLimit }); - // } - // } else { - // // 如果配置了分享积分,但是未开启开关,则推:无视频则分享 - // this.dispatchType(ShareVideoType.VideoToShare, shareKey, opts, queryObj); - // } - // break; - - // case ShareVideoType.Video: // 看视频 - // Platform.I.video(shareKey, share_wxad_id) - // .then(ret => this.handleSuccess(opts, shareType, ShareVideoFrom.Video, ret)) - // .catch((err: any) => this.handleFail(opts, shareType, ShareVideoFrom.Video, err)); - // break; - - // case ShareVideoType.VideoToShare: // 无视频则分享 - // if (!share_wxad_id) { - // this.group(shareKey, queryObj, opts) - // .then(ret => this.handleSuccess(opts, shareType, ShareVideoFrom.Share, ret)) - // .catch(err => this.handleFail(opts, shareType, ShareVideoFrom.Share, err)); - // return; - // } - // Platform.I.video(shareKey, share_wxad_id) - // .then((ret) => this.handleSuccess(opts, shareType, ShareVideoFrom.Video, ret)) - // .catch((err: any) => { - // // 拉取视频失败/视频UID不存在/微信版本过低,暂不支持看视频,自动切换到分享 - // let { VideoFail, VideoInvalid, VideoNotOpen } = ShareVideoError; - // if (err && (err.code === VideoFail.code || err.code === VideoInvalid.code || err.code === VideoNotOpen.code)) - // this.group(shareKey, queryObj, opts) - // .then(ret => this.handleSuccess(opts, shareType, ShareVideoFrom.Share, ret)) - // .catch(err => this.handleFail(opts, shareType, ShareVideoFrom.Share, err)); - // else - // this.handleFail(opts, shareType, ShareVideoFrom.Video, err); - // }); - // break; - - // case ShareVideoType.VideoAndShare: // 看视频 - // if (share_wxad_id && opts.type === 1) { - // Platform.I.video(shareKey, share_wxad_id) - // .then(ret => this.handleSuccess(opts, shareType, ShareVideoFrom.Video, ret)) - // .catch((err: any) => this.handleFail(opts, shareType, ShareVideoFrom.Video, err)); - // } else { - // this.group(shareKey, queryObj, opts) - // .then(ret => this.handleSuccess(opts, shareType, ShareVideoFrom.Share, ret)) - // .catch(err => this.handleFail(opts, shareType, ShareVideoFrom.Share, err)); - // } - // break; - // } - } - - shareWithType(shareType: ShareVideoType, shareKey: string, opts: any = {}, queryObj: { [key: string]: number | string } = {}) { - this.dispatchType(shareType, shareKey, opts, queryObj); - } - /** * 分享id * @param shareKey @@ -309,25 +143,6 @@ export default class ShareVideoService { getShareVideoType(shareKey: string): ShareVideoType { let shareData = this.getShareVideoData(shareKey); let shareType: ShareVideoType = +shareData.typ; - // // 木有开启分享,直接返回None - // if (Platform.IsWx && OnlineService.I.getParamsInt(OnlineKeys.ShareUnlock, 1) !== 1) - // return ShareVideoType.None; - - // // 后台配置视频,判断是否加载出错过,出错返回分享,否则直接返回 - // if (shareType === ShareVideoType.Video || shareType === ShareVideoType.VideoAndShare || shareType === ShareVideoType.VideoToShare) { - // // 检测视频加载失败 - // if (Platform.I.isVideoErrored()) - // return ShareVideoType.Share; - // else - // return shareType; - // } - - // // 是否开启分享积分,且配置为分享积分 - // if (shareType === ShareVideoType.ShareIntegral && IntegralService.I.IsOpen) { - // let data = IntegralService.I.convert(); - // if (data) - // return data.shareType; - // } return shareType; } @@ -339,55 +154,6 @@ export default class ShareVideoService { return this.getShareVideoType(shareKey); } - private handleSuccess(opts: any, shareType: ShareVideoType, from: ShareVideoFrom, ret: any) { - // // 判断分享规则 - // if (IntegralService.I.IsOpen && opts.shareIntegralData && from === ShareVideoFrom.Share) { - // let shareNum = IntegralService.I.IntegralShareNum; - // if (shareNum === 0) { - // // 第1次分享 - // IntegralService.I.setIntegralShareNum(); - // IntegralService.I.resetShareRatio(); - // __LOG__ && console.error(`SDK ShareVideoService 分享积分 第1次分享: 初始来源:${shareType}, 类型:${from}, 分享次数:${shareNum}`); - // // 设定:第一次强制失败 - // // return this.handleFail(opts, shareType, from, { ...ShareVideoError.ShareRuleFail }, true); - // } else { - // // 第n次分享 - // IntegralService.I.setIntegralShareNum(); - // IntegralService.I.addShareRatio(); - - // let shareRatio = IntegralService.I.ShareRatio; - // let radomRatio = +Math.random().toFixed(2); - // __LOG__ && console.error(`SDK ShareVideoService 分享积分 第${shareNum}次分享: 初始来源:${shareType}, 类型:${from}, 分享次数:${shareNum}, 随机概率:${radomRatio}, 失败概率:${shareRatio}`); - // if (radomRatio <= shareRatio) { - // __LOG__ && console.error(`SDK ShareVideoService 分享积分 触发失败规则:分享强制失败, radomRatio <= shareRatio: ${radomRatio} <= ${shareRatio}`); - // return this.handleFail(opts, shareType, from, { ...ShareVideoError.ShareRuleFail }, true); - // } - // } - - // // 是否同步分享积分数据 - // let { intergral, isAsyncNum } = opts.shareIntegralData; - // isAsyncNum && LocalService.I.saveVideoOverShareNum(intergral); - // __LOG__ && isAsyncNum && console.error(`SDK ShareVideoService 分享积分 触发视频看完后,再次分享次数限制更新成功,成功后数量:${LocalService.I.getVideoOverShareNum(intergral)}`); - // } - - // let success = opts.success || function () { }; - // let context = opts.context || this; - // typeof success === 'function' && success.call(context, from, ret); - // (from === ShareVideoFrom.Share) && TempService.I.add(TempDataKeys.ShareSuccess, 1); - } - - private handleFail(opts: any, shareType: ShareVideoType, from: ShareVideoFrom, err: any, isFromIntegral?: boolean) { - // if (err instanceof Error) return; - // // 判断来自分享积分规则,则重置分享概率 - // if (IntegralService.I.IsOpen && opts.shareIntegralData && from === ShareVideoFrom.Share) { - // __LOG__ && console.error(`SDK ShareVideoService 强制失败分享,回滚到: ${IntegralService.I.ShareRatioInit}`); - // IntegralService.I.resetShareRatio(); - // } - // let fail = opts.fail || function () { }; - // let context = opts.context || this; - // fail && fail.call(context, from, err); - } - private setShareVideoData(data: any) { (data.data || []).forEach((item: any) => { this.shareObjs[item.key] = this.shareObjs[item.key] || []; @@ -399,10 +165,6 @@ export default class ShareVideoService { let list: Array = this.shareObjs[shareKey]; if (!list) { list = this.shareObjs.default; - // let shareData: ShareData = CfgManager.I.config.ShareData; - // if (!shareData) throw new TypeError('SDK ShareVideoService - 请在config.js中配置ShareData'); - // let share_open = shareData.share_wxad_id ? ShareVideoType.Video : ShareVideoType.Share; - // return { ...shareData, share_id: 99999 + '', share_key: shareKey, share_open }; } let index = RandomUtils.rand(0, list.length); return list[index]; diff --git a/wxsdk/share/SDKVideo.ts b/wxsdk/share/SDKVideo.ts index 00dcf0c..29ca22d 100644 --- a/wxsdk/share/SDKVideo.ts +++ b/wxsdk/share/SDKVideo.ts @@ -8,6 +8,7 @@ import LogService from "../service/LogService"; export default class SDKVideo { private _isPlaying: boolean; private _isErrored: boolean; + private _isMultiton: boolean; private resolve: any; private reject: any; private videoAd: any; @@ -19,6 +20,7 @@ export default class SDKVideo { this.videoKey = ''; this._isPlaying = false; this._isErrored = false; + this._isMultiton = false; } get isErrored() { @@ -35,7 +37,7 @@ export default class SDKVideo { if (this.preloadVideoAd || this.isPreload) return this.isPreload = true; let ad = wx.createRewardedVideoAd({ - adUnitId + adUnitId, }); ad.onError(this.preError); ad.load().then(this.handleLoaded2).catch(() => { @@ -54,8 +56,6 @@ export default class SDKVideo { that.preloadVideoAd = that.videoAd2; that.isPreload = false __LOG__ && console.warn("视频预加载成功", that.preloadVideoAd) - // that.preloadVideoAd.ttttttt = '111111' - // console.warn(that.preloadVideoAd.isReady()); } offPreload() { @@ -63,7 +63,14 @@ export default class SDKVideo { that.preloadVideoAd.offError(that.preError); } - async show(videoKey: string, adUnitId: string): Promise { + /** + * + * @param videoKey + * @param adUnitId + * @param multiton 多例 + * @returns + */ + async show(videoKey: string, adUnitId: string, multiton: boolean = false): Promise { return new Promise(async (resolve, reject) => { if (this.isPlaying) return reject({ ...ShareVideoError.VideoPlaying }); @@ -72,26 +79,23 @@ export default class SDKVideo { return reject({ ...ShareVideoError.VideoInvalid }); LogService.I.adStat(videoKey, this.adUnitId, DOT_AD_TYPE.video, DOT_AD_STATUS.request); - let videoAd - let hasVideo - if (this.preloadVideoAd) { + let videoAd; + let hasVideo; + if (this.preloadVideoAd && !multiton) { __LOG__ && console.log("使用预加载视频", this.preloadVideoAd) hasVideo = true - this.offPreload() + this.offPreload(); videoAd = this.preloadVideoAd; - this.preloadVideoAd = null; - // this.preloadVideo(adUnitId); } else { __LOG__ && console.log("不使用预加载视频") - hasVideo = false + hasVideo = false; videoAd = wx.createRewardedVideoAd({ - adUnitId + adUnitId, + multiton: true }); // this.preloadVideo(adUnitId); } - // let videoAd = wx.createRewardedVideoAd({ - // adUnitId - // }); + this._isMultiton = !hasVideo; if (!videoAd) return reject({ ...ShareVideoError.VideoNotOpen }); @@ -101,7 +105,6 @@ export default class SDKVideo { this.adUnitId = adUnitId; this.resolve = resolve; this.reject = reject; - this.videoAd = videoAd; videoAd.onClose(this.handleClose); videoAd.onError(this.onError); @@ -123,7 +126,6 @@ export default class SDKVideo { } } - this.preloadVideo(adUnitId); __LOG__ && console.warn('====> PCSDK WxVideo 请求视频adUnitId', adUnitId); }); } @@ -140,8 +142,8 @@ export default class SDKVideo { // __LOG__ && console.warn("handleLoaded2", that.videoAd, that.videoAd.isReady()); LogService.I.adStat(this.videoKey, this.adUnitId, DOT_AD_TYPE.video, DOT_AD_STATUS.show) - if (cc.sys.platform === cc.sys.WECHAT_GAME && typeof qq == 'undefined') { - if (that.videoAd.isReady()) {//抖音、qq 没有 isReady + if (cc.sys.platform === cc.sys.WECHAT_GAME) { + if (that.videoAd.isReady()) {//抖音没有 isReady that.show_time = Date.now(); await that.videoAd.show(); } else { @@ -178,6 +180,15 @@ export default class SDKVideo { that.reject && that.reject({ ...ShareVideoError.VideoQuit }); } that.videoAd.offClose(that.handleClose); + if (that._isMultiton) { + that.videoAd.destroy(); + that.videoAd = null; + } + // if (that.preloadVideoAd) { + // that.preloadVideoAd.destroy(); + // that.preloadVideoAd = null; + // } + that.preloadVideo(that.adUnitId); that._isPlaying = false; that._isErrored = false; } -- libgit2 0.21.0