TTSdk.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. declare var tt: any;
  2. import BaseSdk, { RecorderState } from "./BaseSdk";
  3. import { find, Node, Size, UITransform, v3, Vec3 } from "cc";
  4. export default class TTSdk extends BaseSdk {
  5. private videoAd: any;
  6. private callback: any;
  7. private target: any;
  8. private bannerAd: any;
  9. private interstitialAd: any;
  10. private customAd: any;
  11. private sidebarExist:boolean=false;
  12. private fromSidebar:boolean=false;
  13. /**
  14. * 初始化
  15. * @param callback 初始化成功后回调
  16. * @param target 监听对象
  17. */
  18. public init(callback: Function, target: Object): void {
  19. console.log("TTSdk init");
  20. // tt.onShow((res) => {
  21. // console.log("启动参数:", res?.query);
  22. // console.log("来源信息:", res?.refererInfo);
  23. // console.log("场景值:", res?.scene);
  24. // console.log("启动场景字段:", res.launch_from, ", ", res.location);
  25. // this.fromSidebar = res.launch_from=='homepage' && res.location=='sidebar_card';
  26. // callback?.call(target,res);
  27. // });
  28. // tt.checkScene({
  29. // scene: "sidebar",
  30. // success: (res) => {
  31. // console.log("check scene success: ", res);
  32. // //成功回调逻辑
  33. // this.sidebarExist=res.isExist;
  34. // },
  35. // fail: (res) => {
  36. // console.log("check scene fail:", res);
  37. // //失败回调逻辑
  38. // this.sidebarExist=false;
  39. // }
  40. // });
  41. }
  42. public goToSidebar():void{
  43. tt.navigateToScene({
  44. scene: "sidebar",
  45. success: (res) => {
  46. console.log("navigate to scene success");
  47. // 跳转成功回调逻辑
  48. },
  49. fail: (res) => {
  50. console.log("navigate to scene fail: ", res);
  51. // 跳转失败回调逻辑
  52. },
  53. });
  54. }
  55. public checkHasSidebar():boolean{
  56. return this.sidebarExist;
  57. }
  58. public checkFromSidebar():boolean{
  59. return this.fromSidebar;
  60. }
  61. /**
  62. * 登录
  63. * @param callback 登录成功后回调
  64. * @param target 监听对象
  65. */
  66. public login(callback: Function, target: Object): void {
  67. console.log("ttSdk login");
  68. }
  69. /**
  70. * 登出
  71. */
  72. public logout(): void {
  73. console.log("ttSdk logout");
  74. }
  75. /**
  76. * 退出
  77. */
  78. public exit(): void {
  79. console.log("ttSdk exit");
  80. }
  81. /**
  82. * 切换账号
  83. */
  84. public switchLogin(): void {
  85. console.log("ttSdk switchLogin");
  86. }
  87. public setOpenScore(score:number):void{
  88. }
  89. public getOpenRank():void{
  90. }
  91. /**
  92. * 数据上报
  93. * @param param 参数
  94. */
  95. public report(...param: any[]): void {
  96. console.log("ttSdk report");
  97. }
  98. /**
  99. * 数据上报
  100. * @param param 参数
  101. */
  102. public pay(...param: any[]): void {
  103. console.log("ttSdk pay");
  104. }
  105. showLoading(mess={title: "请稍后..."}){
  106. tt.showLoading(mess);
  107. }
  108. hideLoading(param ={
  109. success(res) {
  110. console.log(`${res}`);
  111. },
  112. fail(res) {
  113. console.log(`hideLoading调用失败`);
  114. },
  115. })
  116. {
  117. tt.hideLoading(param);
  118. }
  119. /**
  120. * 播放广告
  121. */
  122. playRewardAd(callback: Function, target: any): void {
  123. this.callback = callback;
  124. this.target = target;
  125. this.createRewardAd();
  126. //用户触发广告后,显示激励视频广告
  127. this.videoAd.show().catch(() => {
  128. // 失败重试
  129. this.videoAd.load()
  130. .then(() =>{
  131. this.videoAd.show()
  132. } )
  133. .catch(err => {
  134. tt.hideLoading({
  135. success(res) {
  136. console.log(`${res}`);
  137. },
  138. fail(res) {
  139. console.log(`hideLoading调用失败`);
  140. },
  141. });
  142. console.warn('激励视频 广告显示失败')
  143. this.onRewardAdCallback(false);
  144. })
  145. })
  146. }
  147. createRewardAd() {
  148. if (!this.videoAd) {
  149. this.videoAd = tt.createRewardedVideoAd({
  150. adUnitId: "2d5f6gan71lm1bjf1l",//广告位 id,后续可以在平台基于广告位id看数
  151. multiton:false,//是否开启再得广告模式(只支持安卓系统的抖音和抖音极速版)
  152. multitonRewardMsg:['更多奖励1', '更多奖励2', '更多奖励3'],//再得广告的奖励文案,玩家每看完一个广告都会展示multiton为true时必填
  153. multitonRewardTimes:0,//额外观看广告的次数,合法的数据范围为1-4,multiton为true时必填
  154. progressTip:false,//是否开启进度提醒,开启时广告文案为【再看N个获得xx】,关闭时为【 再看1个获得xx】。 N表示玩家当前还需额外观看广告的次数。
  155. })
  156. this.videoAd.onClose(res => {
  157. tt.hideLoading({
  158. success(res) {
  159. console.log(`${res}`);
  160. },
  161. fail(res) {
  162. console.log(`hideLoading调用失败`);
  163. },
  164. });
  165. // 用户点击了【关闭广告】按钮
  166. if (res && res.isEnded || res === undefined) {
  167. console.log(res);
  168. // 正常播放结束,可以下发游戏奖励
  169. this.onRewardAdCallback(true);
  170. //if (res.count) {
  171. //在支持多例模式的版本上会返回该字段,并且是否返回该字段与multiton是否为true无关
  172. //判断观看了几次广告
  173. //}
  174. }
  175. else {
  176. // 播放中途退出,不下发游戏奖励
  177. this.onRewardAdCallback(false);
  178. }
  179. })
  180. this.videoAd.onError(err => {
  181. console.warn(err);
  182. tt.hideLoading({
  183. success(res) {
  184. console.log(`${res}`);
  185. },
  186. fail(res) {
  187. console.log(`hideLoading调用失败`);
  188. },
  189. });
  190. this.onRewardAdCallback(false);
  191. })
  192. }
  193. }
  194. onRewardAdCallback(success) {
  195. if (this.callback) {
  196. this.callback.call(this.target, success);
  197. if (success) {
  198. this.videoAd.load();
  199. }
  200. this.callback = null;
  201. }
  202. }
  203. /**
  204. * 显示banner广告
  205. */
  206. showBannerAd(show: boolean): void {
  207. if (show) {
  208. // 在适合的场景显示 Banner 广告
  209. this.createBannerAd();
  210. this.bannerAd.show();
  211. }
  212. else {
  213. this.bannerAd.hide();
  214. this.bannerAd.destroy();
  215. this.bannerAd = null;
  216. this.createBannerAd();
  217. }
  218. }
  219. createBannerAd() {
  220. if (!this.bannerAd) {
  221. let sysInfo = tt.getSystemInfoSync();
  222. //创建 Banner 广告实例,提前初始化
  223. var width = 320;
  224. this.bannerAd = tt.createBannerAd({
  225. adUnitId: 'adunit-',
  226. adIntervals: 30, // 自动刷新频率不能小于30秒
  227. style: {
  228. left: sysInfo.screenWidth / 2 - width / 2,
  229. top: sysInfo.screenHeight,
  230. width: width
  231. }
  232. })
  233. this.bannerAd.onResize(res => {
  234. this.bannerAd.style.top = sysInfo.screenHeight - this.bannerAd.style.realHeight;
  235. })
  236. this.bannerAd.onError(err => {
  237. console.log(err)
  238. })
  239. }
  240. }
  241. createInterstitialAd() {
  242. // 创建插屏广告实例,提前初始化
  243. if (tt.createInterstitialAd && !this.interstitialAd) {
  244. this.interstitialAd = tt.createInterstitialAd({
  245. adUnitId: 'adunit-'
  246. })
  247. this.interstitialAd.onClose(res => {
  248. this.interstitialAd = null;
  249. this.createInterstitialAd();
  250. })
  251. }
  252. }
  253. showInterstitialAd() {
  254. // 在适合的场景显示插屏广告
  255. this.createInterstitialAd();
  256. if (this.interstitialAd) {
  257. this.interstitialAd.show().catch((err) => {
  258. console.error(err)
  259. })
  260. }
  261. }
  262. /**
  263. * 显示自定义广告
  264. */
  265. showCustomAd(show, top?, left?): void {
  266. // console.log(top, left);
  267. if (!this.customAd) {
  268. this.customAd = tt.createCustomAd({
  269. adIntervals: 30,
  270. adUnitId: "adunit-",
  271. style: {
  272. fixed: true,
  273. left: left,
  274. top: top,
  275. },
  276. })
  277. }
  278. if (show) {
  279. this.customAd.show();
  280. }
  281. else {
  282. this.customAd.destroy();
  283. this.customAd = null;
  284. }
  285. }
  286. /**
  287. * 主动拉起转发
  288. */
  289. shareAppMessage(title: string = "", imageUrlId: string = "", imageUrl: string = "", query: any = null, shareCallback: any = null): void {
  290. var data = {
  291. channel:"invite",
  292. title: title,
  293. imageUrlId: imageUrlId,
  294. imageUrl: imageUrl,
  295. query: "",
  296. success() {
  297. console.log("分享成功");
  298. tt.showModal({
  299. title: "分享成功",
  300. content: "",
  301. });
  302. },
  303. fail(e) {
  304. console.log("分享失败");
  305. },
  306. };
  307. tt.shareAppMessage(data);
  308. }
  309. //
  310. recorderStart(duration:number=30):void{
  311. if(this.recordState!=RecorderState.STOP) return;
  312. const recorder = tt.getGameRecorderManager();
  313. recorder.onStart((res) => {
  314. console.log("录屏开始");
  315. this._recordState=RecorderState.START;
  316. });
  317. recorder.onStop((res) => {
  318. // if(this.recordState != RecorderState.STOP){
  319. // recorder.start({
  320. // duration: duration,
  321. // isMarkOpen: false
  322. // });
  323. // }else{
  324. console.log("录屏结束");
  325. this._videoPath=res.videoPath||this._videoPath;
  326. console.log(res.videoPath);
  327. // }
  328. });
  329. recorder.start({
  330. duration: duration,
  331. isMarkOpen: false
  332. });
  333. }
  334. recorderResume():void{
  335. if(this._recordState !=RecorderState.PAUSE ) return;
  336. console.log("录屏继续");
  337. this._recordState = RecorderState.START
  338. const recorder = tt.getGameRecorderManager();
  339. recorder.resume();
  340. }
  341. private _videoPath:string="";
  342. recorderStop():void{
  343. if(this._recordState == RecorderState.STOP)return;
  344. console.log("录屏结束",tt.getGameRecorderManager);
  345. this._recordState = RecorderState.STOP
  346. if(tt.getGameRecorderManager){
  347. const recorder = tt.getGameRecorderManager();
  348. recorder.stop();
  349. }
  350. }
  351. recorderPause(){
  352. if(this._recordState != RecorderState.START ) return;
  353. console.log("录屏暂停");
  354. this._recordState=RecorderState.PAUSE
  355. const recorder = tt.getGameRecorderManager();
  356. recorder.pause();
  357. }
  358. shareRecord(title: string = "",desc:string="",path:string="",topics:string[]=["话题1","话题2"]):void{
  359. tt.shareAppMessage({
  360. channel: "video",
  361. title: title,
  362. desc: desc,
  363. imageUrl: "",
  364. templateId: "", // 替换成通过审核的分享ID
  365. query: "",
  366. extra: {
  367. videoPath:path=="" ? this._videoPath:path, // 可替换成录屏得到的视频地址
  368. videoTopics: topics,
  369. withVideoId: true,
  370. },
  371. success(res) {
  372. console.log(res.videoId);
  373. console.log("分享视频成功");
  374. /*tt.showModal({
  375. title: "分享成功",
  376. content: "",
  377. });*/
  378. },
  379. fail(e) {
  380. console.log("分享视频失败");
  381. /*tt.showModal({
  382. title: "分享失败",
  383. content: JSON.stringify(e),
  384. });*/
  385. },
  386. });
  387. }
  388. //
  389. vibrateShort(param: any) {
  390. tt.vibrateShort({
  391. success:param.success,
  392. fail:param.fail,
  393. complete:param.complete
  394. })
  395. }
  396. posMessge(messge?) {
  397. // let mess = { event: 'getRank', type: 'engine' }
  398. // if (messge) {
  399. // messge = { ...mess, ...messge }
  400. // }
  401. // messge = messge || mess
  402. // console.log("messge")
  403. // console.log(messge)
  404. // console.log(tt.postMessage)
  405. // console.log("messge__end")
  406. // const openDataContext = tt.getOpenDataContext();
  407. // console.log("openDataContext--->",openDataContext)
  408. // openDataContext.postMessage(messge)
  409. // let env = wx
  410. // if (env) {
  411. // console.log('Message posted');
  412. // env.getOpenDataContext().postMessage({
  413. // event:'setValue',type:'engine'
  414. // });
  415. // }
  416. }
  417. showRankList(param){
  418. tt.getImRankList(param);
  419. }
  420. setImRankData(param: any){
  421. tt.setImRankData(param);
  422. }
  423. getMenuButtonBoundingClientRect() {
  424. return tt.getMenuButtonLayout()
  425. }
  426. openSetting(res){
  427. // tt.showDouyinOpenAuth({
  428. // scopes: {
  429. // im: 0, // 用户无法取消
  430. // "im.media": 2,
  431. // },
  432. // success(res) {
  433. // console.log("success", res);
  434. // },
  435. // fail(res) {
  436. // console.log("fail", res);
  437. // },
  438. // complete(res) {
  439. // console.log("complete", res);
  440. // },
  441. // });
  442. tt.openSetting(res)
  443. }
  444. getUserInfo(reportFun:any, node?: Node,btnCb?: any) {
  445. console.log("======================")
  446. tt.getSetting({
  447. success(res) {
  448. if (res.authSetting['scope.userInfo'] === true) {
  449. tt.getUserInfo({
  450. success(res) {
  451. reportFun(res)
  452. },
  453. fail(res) {
  454. console.log(`getUserInfo 调用失败`, res.errMsg);
  455. },
  456. });
  457. } else {
  458. console.log(">>>>>>>>>>>>>>>>>>>")
  459. tt.authorize({
  460. scope: "scope.userInfo",
  461. success() {
  462. // 用户同意授权用户信息
  463. tt.getUserInfo({
  464. success(res) {
  465. reportFun(res)
  466. },
  467. fail(res) {
  468. console.log(`getUserInfo 调用失败`, res.errMsg);
  469. },
  470. });
  471. },
  472. });
  473. }
  474. },
  475. });
  476. }
  477. // url = "https://analytics.oceanengine.com/api/v2/conversion"
  478. // options
  479. // sendAnalytics(param:{event_type:string,outer_event_id?:number}){
  480. // let url = this.url
  481. // // 上报数据
  482. // if(!this.options){
  483. // this.options = tt.getLaunchOptionsSync();
  484. // }
  485. // var options = this.options||{}
  486. // console.log("options==>",options)
  487. // let body = {
  488. // "context": {
  489. // "ad": {
  490. // "callback": options?.query?.clickid,//callback 这里需要填写的就是从启动参数里获取的 clickid
  491. // }
  492. // },
  493. // "timestamp": ch.date.now(),
  494. // ...param,
  495. // }
  496. // request({url:url, method: 'POST',data:body}).then((res)=>{
  497. // console.log("res==>",res)
  498. // });
  499. // }
  500. awemeUserProfile(callback: Function){
  501. tt.openAwemeUserProfile({
  502. success: (res) => {
  503. callback(true,res)
  504. },
  505. fail: (res) => {
  506. callback(false,res)
  507. }
  508. })
  509. }
  510. checkFollowAwemeState(callback: Function){
  511. tt.checkFollowAwemeState({
  512. success(res) {
  513. callback(true,res)
  514. },
  515. fail(res) {
  516. callback(false,res)
  517. },
  518. });
  519. }
  520. getWindowInfo() {
  521. tt.getSystemInfoSync()
  522. }
  523. }