SdkUtil.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. /**
  2. * 广告sdk & 网络控制
  3. * ts 版本
  4. * 单例
  5. */
  6. import utils from "./Utils";
  7. export default class SdkUtil {
  8. public static _instance:SdkUtil = null
  9. isvideoend: boolean;
  10. checkVideo: boolean;
  11. private _type: any;
  12. isdebug = false //是测试模式 该模式 无广告 无 网络
  13. public static get Instance(){
  14. if (null == this._instance) {
  15. this._instance = new SdkUtil();
  16. }
  17. return this._instance
  18. }
  19. //platform:string = 'tt'
  20. private _platform:string
  21. get platform():string {
  22. if(cc.sys.platform == cc.sys.BYTEDANCE_GAME){
  23. this._platform = 'tt'
  24. return this._platform
  25. }else if(cc.sys.platform == cc.sys.WECHAT_GAME){
  26. this._platform = 'wx'
  27. return this._platform
  28. }
  29. }
  30. set plaform(str){
  31. this._platform = str
  32. }
  33. videoRecordingState:any = 0;
  34. video_recorder:any = null;
  35. videoStartTime:any = null;
  36. videoPath:any = null;
  37. videoIsExist:any = 0;//视频是否存在 默认存在
  38. recorderTime:any = 120; //视频录制时间
  39. videoAd:any = null; //激励视频
  40. chaPingErr:any = false; //插屏报错(触发频繁限制等)
  41. chaPing:any = null
  42. bannerAd:any = null; //横屏广告
  43. videoCallBack = null //激励视频成功回调
  44. videoFailCallBack = null //激励视频失败回调
  45. isshowbanner = false
  46. //platformSdk:any = tt //字节跳动sdk
  47. tt_info:any = {
  48. gamename:"字节游戏名称",
  49. shareTitle:'',
  50. appId:'appid',
  51. videoId: 'videoid',
  52. chapingId: 'chapingid',
  53. bannerId: "bannerid",
  54. templateId: ['','','',''],
  55. videoTopics:['',''],
  56. appSecret:'',
  57. }
  58. ks_info:any = {
  59. gamename:"",
  60. shareTitle:'',
  61. appId:'',
  62. bannerId: "",
  63. videoId: '',
  64. chapingId: '',
  65. templateId: ['','','',''],
  66. videoTopics:['',''],
  67. appSecret:'',
  68. }
  69. wx_info:any = {
  70. gamename:"微信游戏名称",//游戏名称
  71. shareTitle:'',
  72. appId:'appid', //appid
  73. bannerId: "bannerid",//横幅 已接入
  74. videoId:'videoid', //6---30s 激励
  75. videoId2:'videoid', //6---60s 激励 // 主要用这个 已接入
  76. chapingId:'chapingid', //插屏已接入
  77. gezi:'geziid', //格子未接入
  78. NativeAd:{ // 原生 格子未接入
  79. juzhen:'juzhenid',
  80. duogezi:'duogeziid',
  81. dangezi:'dangeziid'
  82. },
  83. templateId: ['','','',''],
  84. videoTopics:['','']
  85. }
  86. url:string = 'https://www.xxxxx.top/sgame'
  87. web_url = ' https://www.xxxxx.top/pv'
  88. /**
  89. * 获取平台 类
  90. */
  91. getPlatformClass(){
  92. var p_class = null
  93. switch(this.platform){
  94. case 'tt':
  95. //@ts-ignore
  96. p_class = tt
  97. break
  98. case 'op':
  99. //@ts-ignore
  100. p_class = qg
  101. break
  102. case 'wx':
  103. //@ts-ignore
  104. p_class = wx
  105. break
  106. case 'ks':
  107. //@ts-ignore
  108. p_class = ks
  109. break
  110. }
  111. return p_class
  112. }
  113. /**
  114. * 获取分享标题
  115. */
  116. getShareTitle(){
  117. var title = ''
  118. switch(this.platform){
  119. case 'tt':
  120. title = this.tt_info.shareTitle
  121. break
  122. case 'ks':
  123. title = this.ks_info.shareTitle
  124. break
  125. case 'wx':
  126. title = this.wx_info.shareTitle
  127. break
  128. }
  129. return title
  130. }
  131. /**
  132. * 获取名字
  133. */
  134. getName(){
  135. var name = ''
  136. switch(this.platform){
  137. case 'tt':
  138. name = this.tt_info.gamename
  139. break
  140. case 'ks':
  141. name = this.ks_info.gamename
  142. break
  143. case 'wx':
  144. name = this.wx_info.gamename
  145. break
  146. }
  147. return name
  148. }
  149. /**
  150. * 获取appid
  151. */
  152. getAppId(){
  153. var appid = ''
  154. switch(this.platform){
  155. case 'tt':
  156. appid = this.tt_info.appId
  157. break
  158. case 'ks':
  159. appid = this.ks_info.appId
  160. break
  161. case 'wx':
  162. appid = this.wx_info.appId
  163. break
  164. }
  165. return appid
  166. }
  167. /**
  168. * 获取AppSecret
  169. */
  170. getAppSecret(){
  171. var appSecret = ''
  172. switch(this.platform){
  173. case 'tt':
  174. appSecret = this.tt_info.appSecret
  175. break
  176. case 'ks':
  177. break
  178. }
  179. return appSecret
  180. }
  181. /**
  182. * 获取bannerid
  183. */
  184. getBannerId(){
  185. var bannerID = ''
  186. switch(this.platform){
  187. case 'tt':
  188. bannerID = this.tt_info.bannerId
  189. break
  190. case 'ks':
  191. bannerID = this.ks_info.bannerId
  192. break
  193. case 'wx':
  194. bannerID = this.wx_info.bannerId
  195. break
  196. }
  197. return bannerID
  198. }
  199. /**
  200. * 获取videoid
  201. */
  202. getVideoId(){
  203. var videoId = ''
  204. switch(this.platform){
  205. case 'tt':
  206. videoId = this.tt_info.videoId
  207. break
  208. case 'ks':
  209. videoId = this.ks_info.videoId
  210. break
  211. case 'wx':
  212. videoId = this.wx_info.videoId
  213. break
  214. }
  215. return videoId
  216. }
  217. getVideoId2(){
  218. if(this.platform == 'wx'){
  219. var videoId = ''
  220. videoId = this.wx_info.videoId2
  221. return videoId
  222. }
  223. }
  224. /**
  225. * 获取id
  226. * @param {*012} type
  227. */
  228. getNativeAd(type){
  229. var id = ''
  230. switch(this.platform){
  231. case 'tt':
  232. break
  233. case 'wx':
  234. if(type == 0){
  235. id = this.wx_info.NativeAd.juzhen
  236. }else if(type == 1){
  237. id = this.wx_info.NativeAd.duogezi
  238. }else if(type == 2){
  239. id = this.wx_info.NativeAd.dangezi
  240. console.log('广告id', id)
  241. }
  242. break
  243. }
  244. return id
  245. }
  246. /**
  247. * 获取插屏广告id
  248. */
  249. getChaPingId(){
  250. var chapingId = ''
  251. switch(this.platform){
  252. case 'tt':
  253. chapingId = this.tt_info.chapingId
  254. break
  255. case 'ks':
  256. chapingId = this.ks_info.chapingId
  257. break
  258. case 'wx':
  259. chapingId = this.wx_info.chapingId
  260. break
  261. }
  262. return chapingId
  263. }
  264. /**
  265. * 获取分享审核id
  266. */
  267. getTemplateId(){
  268. var templateId = ''
  269. var id_lenth = this.tt_info.templateId.length
  270. var randIndex = this.getRangeRandom(0, id_lenth)
  271. switch(this.platform){
  272. case 'tt':
  273. templateId = this.tt_info.templateId[randIndex]
  274. break
  275. case 'wx':
  276. templateId = this.wx_info.templateId[randIndex]
  277. break
  278. }
  279. return templateId
  280. }
  281. /**
  282. * 游戏分享
  283. */
  284. game_share(callBack){
  285. if(this.isdebug) return callBack()
  286. if (cc.sys.platform === cc.sys.WECHAT_GAME || cc.sys.platform == cc.sys.BYTEDANCE_GAME) {
  287. //console.log('游戏分享')
  288. var title = this.getShareTitle()
  289. var templateId = this.getTemplateId()
  290. this.getPlatformClass().shareAppMessage({
  291. templateId: templateId, // 替换成通过审核的分享ID
  292. query: "",
  293. success() {
  294. //console.log("分享成功");
  295. callBack()
  296. },
  297. fail(e) {
  298. //console.log("分享失败");
  299. }
  300. });
  301. }else if(this.platform == 'ks'){
  302. }
  303. }
  304. /**
  305. * 分享到好友
  306. * @param callback
  307. */
  308. shareGameToFriend(callback, note?:string){
  309. if(this.isdebug) return callback()
  310. let notestr = '你的好友邀请你一起战斗'
  311. if(note) notestr = note
  312. var self = this
  313. if (cc.sys.platform === cc.sys.WECHAT_GAME || cc.sys.platform == cc.sys.BYTEDANCE_GAME) {
  314. let title = this.getName()
  315. this.getPlatformClass().shareAppMessage({
  316. channel: "invite", // 拉起邀请面板分享游戏好友
  317. title: title,
  318. desc: notestr,
  319. imageUrl: "",
  320. query: "",
  321. success() {
  322. //console.log("分享成功");
  323. },
  324. fail(e) {
  325. //console.log("分享失败");
  326. },
  327. });
  328. }
  329. }
  330. /**
  331. * banner 广告显示
  332. */
  333. bannerAds_Show(){
  334. if(this.isdebug) return
  335. var self = this
  336. if (cc.sys.platform == cc.sys.BYTEDANCE_GAME) {
  337. if (self.bannerAd == null) {
  338. let winSize = this.getPlatformClass().getSystemInfoSync();
  339. let bannerWidth = winSize.windowWidth;
  340. let bannerHeight = 100;
  341. // // 广告
  342. let bannerID = this.getBannerId()
  343. self.bannerAd = this.getPlatformClass().createBannerAd({
  344. adUnitId: bannerID,
  345. style: {
  346. left: (winSize.windowWidth - bannerWidth) / 2,
  347. top: winSize.windowHeight - bannerHeight ,
  348. width: bannerWidth,
  349. height: bannerHeight,
  350. }
  351. })
  352. self.bannerAd.onResize(res => {
  353. self.bannerAd.style.left = (winSize.windowWidth - res.width) / 2;
  354. self.bannerAd.style.top = winSize.windowHeight - res.height ;
  355. })
  356. self.bannerAd.onError(function (res) {
  357. //console.log(res);
  358. })
  359. }
  360. if (self.bannerAd != null) {
  361. self.bannerAd.show();
  362. }
  363. }else if(cc.sys.platform === cc.sys.WECHAT_GAME){
  364. let sysData = this.getPlatformClass().getSystemInfoSync()
  365. if(sysData.brand == 'devtools') return console.log('开发工具不操作')
  366. if (self.bannerAd == null) {
  367. self.isshowbanner = false
  368. let winSize = this.getPlatformClass().getSystemInfoSync();
  369. let bannerWidth:number = winSize.windowWidth;
  370. let bannerHeight = 100;
  371. // // 广告
  372. let bannerID = this.getBannerId()
  373. self.bannerAd = this.getPlatformClass().createBannerAd({
  374. adUnitId: bannerID,
  375. adIntervals:30, //自动刷新时间 最少30 。 无,不自动
  376. style: {
  377. left: (winSize.windowWidth - bannerWidth) / 2,
  378. top: winSize.windowHeight - bannerHeight ,
  379. width: bannerWidth,
  380. height: bannerHeight,
  381. }
  382. })
  383. self.bannerAd.onResize(res => {
  384. self.bannerAd.style.left = (winSize.windowWidth - res.width) / 2;
  385. self.bannerAd.style.top = winSize.windowHeight - res.height ;
  386. })
  387. self.bannerAd.onError(function (res) {
  388. console.log('banner失败',res);
  389. self.bannerAd = null
  390. //self.bannerAds_Load()
  391. })
  392. self.bannerAd.onLoad(() => {
  393. console.log('banner 广告加载成功')
  394. //gameConfig.isshowbanner = true
  395. })
  396. }
  397. if (self.bannerAd != null) {
  398. self.bannerAd.show();
  399. }
  400. }
  401. }
  402. /**
  403. * banner 广告隐藏
  404. */
  405. bannerAds_Hide(){
  406. if(this.isdebug) return
  407. var self = this
  408. if (cc.sys.platform === cc.sys.WECHAT_GAME || cc.sys.platform == cc.sys.BYTEDANCE_GAME) {
  409. let sysData = this.getPlatformClass().getSystemInfoSync()
  410. if(sysData.brand == 'devtools' || sysData.appName == 'devtools') return console.log('开发工具不操作')
  411. if (self.bannerAd != null) {
  412. self.bannerAd.hide();
  413. }
  414. }else if(this.platform == 'ks'){
  415. }
  416. }
  417. /**
  418. * 插屏广告加载
  419. */
  420. chaPingAds_Load(){
  421. if(cc.sys.platform == cc.sys.WECHAT_GAME){
  422. let sysData = this.getPlatformClass().getSystemInfoSync()
  423. if(sysData.brand == 'devtools') return console.log('开发工具不操作')
  424. var chapingId = this.getChaPingId()
  425. this.chaPing = this.getPlatformClass().createInterstitialAd({ adUnitId: chapingId })
  426. this.chaPing.onError(err => {
  427. console.log('插屏广告拉取失败',err)
  428. //gameConfig.chaPing = wx.createInterstitialAd({ adUnitId: chapingId })
  429. })
  430. }
  431. }
  432. /**
  433. * 插屏广告显示
  434. */
  435. chaPingAds_Show(callback?){
  436. if(this.isdebug) return
  437. var self = this
  438. if(cc.sys.platform == cc.sys.WECHAT_GAME){
  439. let sysData = this.getPlatformClass().getSystemInfoSync()
  440. if(sysData.brand == 'devtools') return console.log('开发工具不操作')
  441. if(self.chaPingErr == true){
  442. if(callback){
  443. this.showToast('资源未准备好...')
  444. return callback()
  445. }
  446. }
  447. self.chaPingErr = true
  448. setTimeout(()=>{
  449. self.chaPingErr = false
  450. },45000)
  451. self.chaPing.show()
  452. .then(()=>{
  453. if(callback){
  454. callback()
  455. }
  456. })
  457. .catch((err) => {
  458. console.error(err)
  459. self.showToast('资源未准备好...')
  460. console.log('展示广告失败')
  461. if(callback){
  462. callback()
  463. }
  464. //gameConfig.chaPing = wx.createInterstitialAd({ adUnitId: this.getChaPingId() })
  465. })
  466. }
  467. else if(cc.sys.platform == cc.sys.BYTEDANCE_GAME){
  468. let sysData = this.getPlatformClass().getSystemInfoSync()
  469. if(sysData.appName == 'devtools') return console.log('插屏开发工具不操作')
  470. if(self.chaPingErr == true) return
  471. self.chaPingErr = true
  472. setTimeout(()=>{
  473. self.chaPingErr = false
  474. },60000)
  475. var chapingId = this.getChaPingId()
  476. if (this.getPlatformClass().createInterstitialAd) {
  477. const interstitialAd = this.getPlatformClass().createInterstitialAd({
  478. adUnitId: chapingId
  479. });
  480. interstitialAd
  481. .load()
  482. .then(() => {
  483. interstitialAd.show();
  484. })
  485. .catch(err => {
  486. console.log(err);
  487. setTimeout(() => {
  488. interstitialAd.load().then(()=>{interstitialAd.show})
  489. }, 5);
  490. });
  491. interstitialAd.onClose(()=>{
  492. if(interstitialAd){
  493. interstitialAd.destroy()
  494. }
  495. })
  496. }
  497. }else if(this.platform == 'ks'){
  498. }
  499. }
  500. /**
  501. * 激励广告加载
  502. */
  503. motivational_Video_Load(){
  504. if(this.isdebug) return
  505. if(cc.sys.platform == cc.sys.WECHAT_GAME){
  506. let sysData = this.getPlatformClass().getSystemInfoSync()
  507. if(sysData.brand == 'devtools') return console.log('开发工具不操作')
  508. var self = this
  509. var videoId = this.getVideoId2()
  510. self.videoAd = this.getPlatformClass().createRewardedVideoAd({
  511. adUnitId: videoId,
  512. //multiton: true
  513. })
  514. self.videoAd.onError((res)=>{
  515. console.log('激励视频错误',res)
  516. })
  517. self.videoAd.load()
  518. var endCallBack = function(res){
  519. if (res.isEnded == true) {
  520. if(self.videoCallBack != null){
  521. console.log('获取奖励')
  522. self.videoCallBack()
  523. }
  524. if(this._type != -1){
  525. self.showToast('已获得')
  526. }
  527. } else {
  528. if (self.videoFailCallBack != null) self.videoFailCallBack()
  529. }
  530. self.isvideoend = true
  531. setTimeout(()=>{
  532. self.checkVideo = false
  533. },60000)
  534. }
  535. self.videoAd.onClose(endCallBack); //绑定结束回调
  536. }
  537. else if(cc.sys.platform == cc.sys.BYTEDANCE_GAME){
  538. let sysData = this.getPlatformClass().getSystemInfoSync()
  539. if(sysData.appName == 'devtools') return console.log('开发工具不操作')
  540. var self = this
  541. var videoId = this.getVideoId()
  542. this.videoAd = this.getPlatformClass().createRewardedVideoAd({
  543. adUnitId: videoId,
  544. //multiton: true
  545. })
  546. this.videoAd.onError((res)=>{
  547. //console.log('激励视频错误',res)
  548. })
  549. this.videoAd.load()
  550. var endCallBack = function(res){
  551. if (res.isEnded == true) {
  552. if(self.videoCallBack != null){
  553. //console.log('获取奖励')
  554. self.videoCallBack()
  555. }
  556. if(this._type != -1){
  557. self.showToast('已获得')
  558. }
  559. } else {
  560. if (self.videoFailCallBack != null) self.videoFailCallBack()
  561. }
  562. self.isvideoend = true
  563. }
  564. self.videoAd.onClose(endCallBack); //绑定结束回调
  565. }else if(this.platform == 'ks'){
  566. }
  567. }
  568. /**
  569. * 展示激励视频
  570. * 参数1 成功回调
  571. * 参数2 失败回调 可以省略
  572. */
  573. motivational_Video_Show(success, faile, type?){
  574. if(this.isdebug) return success()
  575. var self = this
  576. if(this.checkVideo == true) return this.showToast('勿频繁点击')
  577. setTimeout(()=>{
  578. self.checkVideo = false
  579. },1000)
  580. this.videoCallBack = success
  581. this.videoFailCallBack = faile
  582. this._type = type
  583. if(cc.sys.platform == cc.sys.WECHAT_GAME){ //微信
  584. let sysData = this.getPlatformClass().getSystemInfoSync()
  585. if(sysData.brand == 'devtools') return console.log('开发工具不操作')
  586. this._type = type
  587. if(self.videoAd == null || self.videoAd == undefined){
  588. return this.videoCallBack()
  589. }
  590. self.videoAd.onError((res)=>{
  591. console.log('广告出错监听', res)
  592. })
  593. // /**展示 */
  594. self.videoAd
  595. .show()
  596. .then(() => {
  597. })
  598. .catch(err => {
  599. self.showToast('激励视频播放失败')
  600. console.log('打印激励视频错误', err)
  601. // 可以手动加载一次
  602. self.videoAd.load().then(() => {
  603. // 加载成功后需要再显示广告
  604. return self.videoAd.show();
  605. });
  606. });
  607. }
  608. else if(cc.sys.platform == cc.sys.BYTEDANCE_GAME){
  609. this.checkVideo = true
  610. let sysData = this.getPlatformClass().getSystemInfoSync()
  611. if(sysData.appName == 'devtools') return success(),console.log('开发工具不操作')
  612. if(this.videoAd == null || this.videoAd == undefined){
  613. return this.videoCallBack()
  614. }
  615. this.videoAd.onError((res)=>{
  616. //console.log('广告出错监听', res)
  617. })
  618. // /**展示 */
  619. this.videoAd
  620. .show()
  621. .then(() => {
  622. })
  623. .catch(err => {
  624. self.showToast('激励视频播放失败')
  625. //console.log('打印激励视频错误', err)
  626. // 可以手动加载一次
  627. self.videoAd.load().then(() => {
  628. // 加载成功后需要再显示广告
  629. return self.videoAd.show();
  630. });
  631. });
  632. }else if(this.platform == 'ks'){
  633. }
  634. else{
  635. self.videoCallBack()
  636. }
  637. }
  638. /**
  639. * 录制视频开始
  640. */
  641. recording_Video_Start(){
  642. if(this.isdebug) return
  643. var self = this
  644. if (cc.sys.platform == cc.sys.BYTEDANCE_GAME) {
  645. let sysData = this.getPlatformClass().getSystemInfoSync()
  646. if(sysData.appName == 'devtools') return console.log('开发工具不操作')
  647. self.videoRecordingState = 1;
  648. self.videoIsExist = 0;
  649. self.video_recorder = this.getPlatformClass().getGameRecorderManager();
  650. self.videoStartTime = Date.parse(new Date().toString()) //Date.parse(new Date());
  651. //开始回调
  652. self.video_recorder.onStart(res => {
  653. //console.log('录屏开始');
  654. //console.log(res);
  655. });
  656. //开始
  657. self.video_recorder.start({
  658. duration: self.recorderTime, //录制时长
  659. });
  660. //录制结束回调
  661. self.video_recorder.onStop(res => {
  662. self.videoRecordingState = 2;
  663. self.videoPath = res.videoPath;
  664. //console.log('录屏结束', self.videoPath)
  665. // do somethine;
  666. });
  667. //录制错误回调
  668. self.video_recorder.onError(res => {
  669. //console.log("录屏error", res)
  670. });
  671. }
  672. }
  673. /**
  674. * 录制视频结束
  675. */
  676. recording_Video_End(){
  677. if(this.isdebug) return
  678. if (cc.sys.platform == cc.sys.BYTEDANCE_GAME) {
  679. let sysData = this.getPlatformClass().getSystemInfoSync()
  680. if(sysData.appName == 'devtools') return console.log('开发工具不操作')
  681. var self = this
  682. //console.log('录屏结束');
  683. //gameConfig.shareVideoTips = 0;
  684. if (self.video_recorder) {
  685. var endTime = Date.parse(new Date().toString()) //Date.parse(new Date());
  686. if ((endTime - self.videoStartTime) / 1000 <= 5) {
  687. self.videoIsExist = 1;
  688. }else{
  689. self.videoIsExist = 0;
  690. }
  691. self.video_recorder.stop();
  692. }
  693. }
  694. }
  695. /**
  696. * 录制视频分享
  697. * 参数1 成功回调方法
  698. * 参数2 失败回调方法
  699. */
  700. recording_Video_Share(callBack, failCallBack){
  701. if(this.isdebug) return callBack()
  702. if (cc.sys.platform == cc.sys.BYTEDANCE_GAME) {
  703. let sysData = this.getPlatformClass().getSystemInfoSync()
  704. if(sysData.appName == 'devtools') return console.log('开发工具不操作')
  705. var self = this
  706. if(self.videoIsExist == 1){
  707. self.showToast('录制时间过短')
  708. return;
  709. }
  710. var result = 200;
  711. self.videoRecordingState = 3;
  712. this.getPlatformClass().shareAppMessage({
  713. channel: 'video',
  714. title: '',
  715. imageUrl: '',
  716. query: '',
  717. extra: {
  718. videoPath: self.videoPath, // 可用录屏得到的视频地址
  719. videoTopics: self.tt_info.videoTopics,
  720. createChallenge: true
  721. },
  722. success() {
  723. self.showToast('分享视频成功')
  724. callBack()
  725. //self.recording_Video_Start()
  726. },
  727. fail(e) {
  728. result = 201;
  729. self.showToast('分享视频失败')
  730. //console.log('分享失败', e)
  731. failCallBack()
  732. //self.recording_Video_Start()
  733. }
  734. })
  735. }else{
  736. callBack()
  737. }
  738. }
  739. //录屏是否存在
  740. getVideoIsExist(){
  741. return this.videoIsExist
  742. }
  743. /**
  744. * 添加桌面
  745. * @param scb
  746. * @param fcb
  747. */
  748. addShortcut(scb:Function, fcb:Function){
  749. this.getPlatformClass().addShortcut({
  750. success() {
  751. this.showToast("添加桌面成功");
  752. scb()
  753. },
  754. fail(err) {
  755. this.showToast("添加桌面失败");
  756. fcb()
  757. },
  758. });
  759. }
  760. /**
  761. * 检测是否添加到桌面
  762. * @returns true 不支持 或者 已添加
  763. */
  764. checkShortcut(){
  765. if(this.isdebug) return
  766. if((cc.sys.platform == cc.sys.WECHAT_GAME || cc.sys.platform == cc.sys.BYTEDANCE_GAME) && cc.sys.os == cc.sys.OS_ANDROID){
  767. const version = this.getPlatformClass().getSystemInfoSync().SDKVersion
  768. if (utils.compareVersion(version, '2.46.0') >= 0) {
  769. this.getPlatformClass().checkShortcut({
  770. success(res) {
  771. let exist = res.status.exist
  772. //console.log("检查快捷方式", res.status);
  773. return exist
  774. },
  775. fail(res) {
  776. return true
  777. //console.log("检查快捷方式失败", res.errMsg);
  778. },
  779. });
  780. }else{
  781. return true
  782. }
  783. }else{
  784. return true
  785. }
  786. }
  787. /**
  788. * 提示框
  789. */
  790. showToast(str){
  791. if (cc.sys.platform === cc.sys.WECHAT_GAME || cc.sys.platform == cc.sys.BYTEDANCE_GAME) {
  792. this.getPlatformClass().showToast({
  793. title: str,
  794. duration: 1000,
  795. success(res) {
  796. //console.log(`${res}`);
  797. },
  798. fail(res) {
  799. //console.log(`showToast调用失败`);
  800. }
  801. });
  802. }else{
  803. console.log(str)
  804. }
  805. }
  806. /**
  807. * 获取不重复的随机数
  808. * @param minValue 最小值
  809. * @param maxValue 最大值
  810. * @param valueNum 随机个数
  811. */
  812. getRandomValueDif(minValue, maxValue, valueNum) {
  813. // 全部随机数值
  814. let allNums = new Array;
  815. // 判断获取随机数个数
  816. let size = valueNum ? (valueNum > maxValue - minValue + 1 ? maxValue - minValue + 1 : valueNum) : 1;
  817. // 生成随机数值区间数组
  818. for (let i = minValue, k = 0; i <= maxValue; i++ , k++) {
  819. allNums[k] = i;
  820. }
  821. let arr = []
  822. // 随机从数组里面取值
  823. allNums.sort(function () { return 0.5 - Math.random(); });
  824. for (let j = 0; j < size; j++) {
  825. let index = Math.floor(Math.random() * allNums.length);
  826. arr.push(allNums[index]);
  827. let tmp = allNums[index];
  828. allNums[index] = allNums[allNums.length - 1];
  829. allNums[allNums.length - 1] = tmp;
  830. allNums.pop();
  831. }
  832. return arr;
  833. }
  834. /**
  835. * 获取范围内的随机数
  836. * @param minValue 最小值
  837. * @param maxValue 最大值
  838. */
  839. getRangeRandom(minValue, maxValue) {
  840. // 获取数组从第一个开始到指定个数的下标区间
  841. return this.getRandomValueDif(minValue, maxValue, 1)[0];
  842. }
  843. vibrateShort(){
  844. if(cc.sys.platform == cc.sys.WECHAT_GAME || cc.sys.platform == cc.sys.BYTEDANCE_GAME){
  845. this.getPlatformClass().vibrateShort()
  846. }
  847. }
  848. }