SdkMgr.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. import { sys } from 'cc';
  2. // import { Alert } from '../game/gameUi/Alert';
  3. import { KS } from './Platform/kuaishouSDK/kuaishou';
  4. import { Meizu } from './Platform/meizuSDK/meizu';
  5. import { OPPO } from './Platform/oppoSDK/oppoAdMange';
  6. import { Toutiao } from './Platform/toutiao/toutiao';
  7. import { Vivo } from './Platform/vivoSDK/Vivo';
  8. import { Wechat } from './Platform/wechat/wechat';
  9. import { EType, IPos, WXCustomAd } from './Platform/wechat/WXCustomAd';
  10. import { XM } from './Platform/xiaomiSDK/xiaomi';
  11. import { tyqSDK } from './tyq-sdk';
  12. // import { JXDef } from '../conventions/JXCommon';
  13. let adStr = "广告";
  14. /**渠道 */
  15. export enum Channel {
  16. /**默认 浏览器*/
  17. DEFAULT = 0,
  18. /**微信 */
  19. WECHAT = 1,
  20. /**头条 抖音 */
  21. TT = 2,
  22. /**快手 */
  23. KS = 3,
  24. /**oppo渠道 */
  25. OPPO = 4,
  26. /**vivo渠道 */
  27. VIVO = 5,
  28. /**魅族渠道 */
  29. MEIZU = 6,
  30. /**小米渠道 */
  31. XIAOMI = 7,
  32. ANDROID,
  33. IOS,
  34. }
  35. export interface IUserInfo {
  36. nickName: string;
  37. avatar: string;
  38. sex?: number,
  39. uid?: string,
  40. }
  41. export default class SdkMgr {
  42. protected static _ins: SdkMgr;
  43. /**录屏对象 */
  44. private _mediaRecorder: any = null;
  45. public static get ins(): SdkMgr {
  46. if (!this._ins) {
  47. this._ins = new SdkMgr;
  48. }
  49. return this._ins;
  50. }
  51. constructor() {
  52. // if (sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  53. // this.channel = Channel.TT;
  54. // } else if (window.kwaigame) {
  55. // this.channel = Channel.KS;
  56. // } else if (sys.platform == sys.Platform.WECHAT_GAME && window.wx) {
  57. // this.channel = Channel.WECHAT;
  58. // } else if (sys.isMobile && sys.isBrowser) {
  59. // this.channel = Channel.DEFAULT;
  60. // } else if (false) {
  61. // this.channel = Channel.DEFAULT;
  62. // }
  63. if (sys.platform == sys.WECHAT_GAME) {
  64. this.channel = Channel.WECHAT;
  65. }
  66. }
  67. private channel: Channel = Channel.DEFAULT;
  68. private _videoRes: string = "";
  69. public getChannel() {
  70. return this.channel;
  71. }
  72. /**观看广告 */
  73. public showRewardAd(des: any, callback: Function, cancel?: Function, fail?: Function) {
  74. let cb = () => {
  75. // GameMgr.lUserData.doneTask(ETaskType.watchAd);
  76. // loader.loadRes('Alert', Prefab, (err, prefab: Prefab) => {
  77. // // if (err) {
  78. // // console.error(err);
  79. // // return;
  80. // // }
  81. // // let node = instantiate(prefab);
  82. // // find('Canvas').addChild(node);
  83. // // node.getComponent(Alert).init("广告观看成功");
  84. // })
  85. callback();
  86. }
  87. console.log("观看广告", des);
  88. switch (this.channel) {
  89. case Channel.DEFAULT: {
  90. cb();
  91. break;
  92. }
  93. case Channel.TT: {
  94. Toutiao.showRewardVideoAd("guanggao").then((is) => {
  95. if (is) {
  96. console.log("success")
  97. this.umaSetPoint(adStr, { way: des });
  98. cb();
  99. } else if (cancel) {
  100. cancel();
  101. }
  102. }, (res) => {
  103. // GameMgr.uiMgr.showToast("广告加载失败");
  104. if (fail) {
  105. fail();
  106. }
  107. })
  108. break;
  109. }
  110. case Channel.WECHAT: {
  111. Wechat.showRewardVideoAd().then((isSuc) => {
  112. if (isSuc) {
  113. console.log("success")
  114. this.umaSetPoint(adStr, { way: des });
  115. cb();
  116. } else if (cancel) {
  117. cancel();
  118. // loader.loadRes('Alert', Prefab, (err, prefab: Prefab) => {
  119. // if (err) {
  120. // console.error(err);
  121. // return;
  122. // }
  123. // let node = instantiate(prefab);
  124. // find('Canvas').addChild(node);
  125. // node.getComponent(Alert).init("取消观看广告");
  126. // })
  127. }
  128. }, (res?) => {
  129. if (fail) {
  130. fail();
  131. }
  132. // loader.loadRes('Alert', Prefab, (err, prefab: Prefab) => {
  133. // // if (err) {
  134. // // console.error(err);
  135. // // return;
  136. // // }
  137. // // let node = instantiate(prefab);
  138. // // find('Canvas').addChild(node);
  139. // // node.getComponent(Alert).init("广告加载失败");
  140. // })
  141. })
  142. break;
  143. }
  144. case Channel.MEIZU: {
  145. Meizu.showRewardedVideoAd().then(() => {
  146. console.log("success")
  147. this.umaSetPoint(adStr, { way: des });
  148. cb();
  149. }, () => {
  150. console.log("fail")
  151. })
  152. break;
  153. }
  154. case Channel.OPPO: {
  155. OPPO.showRewardVideoAd(() => {
  156. console.log("success")
  157. this.umaSetPoint(adStr, { way: des });
  158. cb();
  159. }, () => {
  160. console.log("fail")
  161. })
  162. break;
  163. }
  164. case Channel.VIVO: {
  165. Vivo.showRewardVideoAd((isSuc) => {
  166. if (isSuc) {
  167. this.umaSetPoint(adStr, { way: des });
  168. console.log("success")
  169. cb();
  170. } else {
  171. console.log("fail")
  172. }
  173. })
  174. break;
  175. }
  176. case Channel.XIAOMI: {
  177. XM.showRewardedVideoAd(() => {
  178. this.umaSetPoint(adStr, { way: des });
  179. console.log("success");
  180. cb();
  181. }, () => {
  182. console.log("fail")
  183. })
  184. break;
  185. }
  186. case Channel.KS: {
  187. KS.showRewardedVideoAd(() => {
  188. this.umaSetPoint(adStr, { way: des });
  189. cb();
  190. })
  191. break;
  192. }
  193. }
  194. }
  195. public umaSetPoint(event: string, obj?: object) {
  196. return;
  197. switch (this.channel) {
  198. case Channel.DEFAULT: {
  199. break;
  200. }
  201. case Channel.TT: {
  202. tt.uma && tt.uma.trackEvent(event, obj ? obj : null);
  203. break;
  204. }
  205. case Channel.KS: {
  206. break;
  207. }
  208. case Channel.WECHAT: {
  209. wx.uma && wx.uma.trackEvent(event, obj ? obj : null);
  210. break;
  211. }
  212. }
  213. }
  214. public showMoreGamesModal() {
  215. switch (this.channel) {
  216. case Channel.TT: {
  217. Toutiao.showMoreGamesModal();
  218. break;
  219. }
  220. }
  221. }
  222. public ttFocusOn() {
  223. Toutiao.openAwemeUserProfile();
  224. }
  225. /**录屏开始 */
  226. protected startRecoding() {
  227. }
  228. /**事件添加开始录屏 */
  229. public recordStart() {
  230. console.log("录屏开始*--*");
  231. if (this.channel == Channel.TT) {
  232. Toutiao.startRecordScreen(300, this.startRecoding);
  233. console.log("录屏开始11111", 11111);
  234. } else if (this.channel == Channel.KS) {
  235. this._mediaRecorder = kwaigame.createMediaRecorder();
  236. this._mediaRecorder.start();
  237. } else if (this.channel == Channel.WECHAT) {
  238. }
  239. }
  240. public puaseRecord() {
  241. console.log("暂停录屏");
  242. if (this.channel == Channel.TT) {
  243. Toutiao.pauseRecordScreen();
  244. } else if (this.channel == Channel.KS) {
  245. this._mediaRecorder.pause();
  246. }
  247. }
  248. public resumeRecord() {
  249. console.log("继续录屏")
  250. if (this.channel === Channel.TT) {
  251. Toutiao.resumeRecordScreen()
  252. } else if (this.channel == Channel.KS) {
  253. this._mediaRecorder.resume();
  254. }
  255. }
  256. /**事件添加结束录屏 */
  257. public recordEnd() {
  258. console.log("录屏结束*--*");
  259. if (this.channel == Channel.TT) {
  260. Toutiao.stopRecordScreen((path) => {
  261. Toutiao.clipVideo(60, path, (res) => {
  262. this._videoRes = res.videoPath;
  263. console.log("录屏结束*--*", 11111);
  264. })
  265. });
  266. } else if (this.channel == Channel.KS) {
  267. this._mediaRecorder.stop();
  268. }
  269. }
  270. /**事件转发 */
  271. public recordCut(cb: Function) {
  272. if (this.channel == Channel.TT) {
  273. console.log("录屏" + this._videoRes);
  274. Toutiao.shareAppMessage("最强小英雄", this._videoRes, ["最强小英雄", "抖音小游戏", "卡牌回合", "卡牌格斗"]).then(() => {
  275. console.log('分享视频成功');
  276. cb();
  277. this.umaSetPoint("分享视频")
  278. })
  279. } else if (this.channel == Channel.KS) {
  280. this._mediaRecorder.publishVideo({
  281. });
  282. } else {
  283. console.log("假装分享视频成功")
  284. cb();
  285. }
  286. }
  287. //授权
  288. public authorize(cb: (userInfo: IUserInfo | null) => void) {
  289. if (this.channel == Channel.TT) {
  290. Toutiao.TTLogin(() => {
  291. Toutiao.toutiaoLogin().then((res: any) => {
  292. if (res) {
  293. let userInfo: IUserInfo = {
  294. nickName: res.name,
  295. avatar: res.photo,
  296. }
  297. cb(userInfo);
  298. //获取抖音的oppenid
  299. } else {
  300. cb(null);
  301. }
  302. }).catch((res) => {
  303. console.log(`toutiaoLogin 调用失败`);
  304. console.log(res);
  305. cb(null);
  306. })
  307. }, (res) => {
  308. console.log(res);
  309. cb(null);
  310. }, false)
  311. } else if (this.channel == Channel.KS) {
  312. KS.KSlogin(() => {
  313. KS.KSgetUserInfo((res: any) => {
  314. let userInfo: IUserInfo = {
  315. nickName: res.userName,
  316. avatar: res.userHead,
  317. }
  318. cb(userInfo);
  319. })
  320. })
  321. } else if (this.channel == Channel.DEFAULT) {
  322. cb(null);
  323. } else if (this.channel == Channel.WECHAT) {
  324. // Wechat.login(() => {
  325. // Wechat.getUserInfo((res: any) => {
  326. // let userInfo: IUserInfo = {
  327. // nickName: res.userInfo.nickName,
  328. // avatar: res.userInfo.avatarUrl,
  329. // }
  330. // cb(userInfo);
  331. // }, (res) => {
  332. // console.log(`getUserInfo 调用失败`);
  333. // console.log(res);
  334. // cb(null);
  335. // })
  336. // }, (res) => {
  337. // log(res);
  338. // cb(null);
  339. // });
  340. let button = wx.createUserInfoButton({
  341. type: 'text',
  342. text: '获取用户信息',
  343. style: {
  344. left: 10,
  345. top: 76,
  346. width: 200,
  347. height: 40,
  348. lineHeight: 40,
  349. backgroundColor: '#ff0000',
  350. color: '#ffffff',
  351. textAlign: 'center',
  352. fontSize: 16,
  353. borderRadius: 4
  354. }
  355. })
  356. button.onTap((res) => {
  357. console.log(res.userInfo.avatarUrl);
  358. console.log(res.userInfo.nickName);
  359. })
  360. } else {
  361. cb(null);
  362. }
  363. }
  364. public initGame() {
  365. }
  366. public loginOut() {
  367. }
  368. /**是否显示分享录屏按钮 */
  369. public isShowRecordBtn() {
  370. if (this.channel == Channel.TT || this.channel == Channel.KS) {
  371. return true;
  372. }
  373. return false;
  374. }
  375. public isWechat() {
  376. return this.channel == Channel.WECHAT;
  377. }
  378. public preLoadAd() {
  379. if (this.channel == Channel.WECHAT) {
  380. Wechat.preLoadAd();
  381. }
  382. }
  383. //显示插屏广告
  384. public showIntersAd(closeCb?: Function) {
  385. if (this.channel == Channel.VIVO) {
  386. Vivo.showInterstitialAd();
  387. } else if (this.channel == Channel.TT) {
  388. Toutiao.showInterstitialAd();
  389. } else if (this.channel == Channel.WECHAT) {
  390. Wechat.showInterstitialAd(closeCb);
  391. } else if (this.channel == Channel.OPPO) {
  392. OPPO.showInterstitialAd();
  393. }
  394. }
  395. public showBanner(node) {
  396. if (this.channel == Channel.TT) {
  397. Toutiao.showBannerAd();
  398. } else if (this.channel == Channel.WECHAT) {
  399. Wechat.showBannerAd(4000);
  400. } else if (this.channel == Channel.VIVO) {
  401. Vivo.showBannerAd();
  402. } else if (this.channel == Channel.OPPO) {
  403. OPPO.showBannerAd();
  404. }
  405. }
  406. public showBannerTurns(time) {
  407. if (this.channel == Channel.WECHAT) {
  408. Wechat.showBannerTurns(time);
  409. }
  410. }
  411. public hideBanner() {
  412. if (this.channel == Channel.TT) {
  413. Toutiao.hideBannerAd();
  414. } else if (this.channel == Channel.VIVO) {
  415. Vivo.hideBannerAd();
  416. } else if (this.channel == Channel.OPPO) {
  417. OPPO.hideBannerAd();
  418. } else if (this.channel == Channel.WECHAT) {
  419. Wechat.hideBannerAd();
  420. }
  421. }
  422. public showWxCustomAd(flag, type: EType, pos: IPos, extraId?, cb?: Function) {
  423. if (this.channel == Channel.WECHAT) {
  424. WXCustomAd.createCustomAd(flag, type, pos, extraId, cb);
  425. }
  426. }
  427. public hideWxCustomAd(flag) {
  428. if (this.channel == Channel.WECHAT) {
  429. WXCustomAd.hideCustomAd(flag);
  430. }
  431. }
  432. public hideAllWxCustom() {
  433. if (this.channel == Channel.WECHAT) {
  434. WXCustomAd.hideAllAd();
  435. }
  436. }
  437. /**跳转小游戏 */
  438. jumpGame(appid: string, event, id) {
  439. // if (cc.sys.platform != cc.sys.WECHAT_GAME) return
  440. Wechat.navigateToMiniProgram(appid, "", () => {
  441. console.log("成功跳转");
  442. tyqSDK.collectClickEventGame(event, id);
  443. }, () => {
  444. console.log("跳转失败");
  445. //好友在玩界面
  446. })
  447. }
  448. }