YZ_Tool_Kwai.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. import PlatUtils from "./PlatUtils";
  2. import { utils } from "./Utils";
  3. import YZ_Constant, { LevelStatus } from "./YZ_Constant";
  4. import YZ_LocalStorage from "./YZ_LocalStorage";
  5. const { ccclass, property } = cc._decorator;
  6. const ST_ServerUrl: string = "http://apps.youlesp.com/gss?";
  7. const POST_ServerUrl: string = "http://report.youlesp.com/gss?";
  8. // 默认配置
  9. let ST_DefaultServerConfig: string = "";
  10. @ccclass
  11. export default class YZ_Tool_Kwai {
  12. _recorder: any = null;
  13. _videoPath: string = null;
  14. _shareCallback: Function = null;
  15. recorderStarTime: number = 0; //开始录屏时间
  16. isAutoShare: boolean = true; //是否自动分享
  17. isClickEnd: boolean = false;
  18. _serverConfig: any = null;
  19. public get ServerConfig() {
  20. return this._serverConfig;
  21. }
  22. _sysInfo: any = {};
  23. public get SysInfo() {
  24. return this._sysInfo;
  25. }
  26. /**
  27. * 当前版本号
  28. */
  29. public gameVersion(): string {
  30. return utils.config.kwaiConfig.version;
  31. }
  32. //设备UID
  33. _uid: string = "0";
  34. public get uid() {
  35. // if (this._service_uid != "0") return this._uid;
  36. // this._login();
  37. return "0";
  38. }
  39. //服务器返回UID
  40. _service_uid: string = "0";
  41. /**
  42. * 服务器返回UID
  43. */
  44. public get serviceId() {
  45. if (this._service_uid != "0") return this._service_uid;
  46. this.reportLogin();
  47. return "0";
  48. }
  49. _loginTime: number = 0;
  50. _loginInterval: number = 30;
  51. async _login() {
  52. let curTime: number = new Date().getTime();
  53. let interval: number = (curTime - this._loginTime) / 1000;
  54. if (interval > 0 && interval < 30) {
  55. utils.showLog(`登录请求间隔小于:${this._loginInterval}秒`);
  56. return;
  57. }
  58. this._loginTime = curTime;
  59. let self = this;
  60. utils.showLog("baidu暂时不获取uid,uid全部为0");
  61. this._uid = "0";
  62. // this.reportLogin();
  63. }
  64. _reportLoginTime: number = 0;
  65. _reportLoginInterval: number = 30;
  66. isReport: boolean = false;
  67. //@ts-ignore
  68. kwaigame: any = kwaigame;
  69. /**
  70. * 上报登录接口获取UID
  71. */
  72. reportLogin() {
  73. if (this.isReport) return;
  74. this.isReport = true;
  75. let self = this;
  76. let curTime: number = new Date().getTime();
  77. let interval: number = (curTime - self._reportLoginTime) / 1000;
  78. if (interval > 0 && interval < 30) {
  79. utils.showLog(`上报登录获取UID小于:${self._reportLoginInterval}秒`);
  80. return;
  81. }
  82. self._reportLoginTime = curTime;
  83. let method = "m=login";
  84. let url: string = ST_ServerUrl + method + `&device_data=0`;
  85. utils.commomHttpRequest(url, (ret, data) => {
  86. if (ret) {
  87. if (data) {
  88. let result = JSON.parse(data);
  89. utils.showLog("data=" + data);
  90. utils.showLog("result=" + result);
  91. utils.showLog("result.uid=" + result.uid);
  92. if (result.uid) {
  93. self._service_uid = "" + result.uid;
  94. utils.showLog("服务器请求登录成功! _service_uid=" + self._service_uid);
  95. YZ_LocalStorage.setItem(YZ_Constant.ST_SERVICE_UID, self._service_uid);
  96. }
  97. }
  98. } else {
  99. utils.showLog("获取数据失败1");
  100. }
  101. this.isReport = false;
  102. })
  103. }
  104. /**
  105. *
  106. * @param data 配置数据
  107. */
  108. public init(data: string) {
  109. if (PlatUtils.IsKwai) {
  110. if (data) {
  111. let configObj: any = JSON.parse(data);
  112. if (configObj && configObj.kwai) {
  113. utils.showLog("本地配置数据:" + JSON.stringify(configObj.kwai));
  114. ST_DefaultServerConfig = JSON.stringify(configObj.kwai);
  115. }
  116. }
  117. this._service_uid = YZ_LocalStorage.getItem(YZ_Constant.ST_SERVICE_UID);
  118. this._service_uid = this._service_uid ? this._service_uid : "0";
  119. // // 获取系统信息
  120. // this._sysInfo = this.swan.getSystemInfoSync();
  121. // if (this._sysInfo) {
  122. // utils.showLog("百度小游戏平台信息: ", JSON.stringify(this._sysInfo));
  123. // } else {
  124. // utils.showLog("系统信息获取失败!");
  125. // }
  126. this._loadConfig();
  127. this.initRecord();
  128. // utils.emitServerInitEvent();
  129. }
  130. }
  131. readyGo() {
  132. this.kwaigame.readyGo();
  133. }
  134. _loadConfig() {
  135. if (PlatUtils.IsKwai) {
  136. let method: string = "m=g";
  137. utils.commomHttpRequest(ST_ServerUrl + method, (ret, data) => {
  138. if (ret) {
  139. utils.showLog("快手服务器配置数据获取成功: data = ", data);
  140. if (data) {
  141. let result = JSON.parse(data);
  142. if (result) {
  143. if (!utils.DebugLoacalConfig) {
  144. this._serverConfig = result;
  145. } else {
  146. cc.warn("开启了本地数据测试,使用本地配置!");
  147. }
  148. } else {
  149. utils.showLog("快手服务器配置数据不是合法的JSON数据, 使用本地配置!");
  150. }
  151. }
  152. } else {
  153. utils.showLog("快手服务器配置数据获取失败, 使用本地配置!");
  154. }
  155. if (!this._serverConfig) {
  156. this._serverConfig = JSON.parse(ST_DefaultServerConfig);
  157. } else {
  158. if (this._serverConfig.shares && this._serverConfig.shares.sy_title) {
  159. utils.config.otherconfig.shareTitle = this._serverConfig.shares.sy_title;
  160. }
  161. if (this._serverConfig.shares && this._serverConfig.shares.sy_img) {
  162. utils.config.otherconfig.shareImgUrl = this._serverConfig.shares.sy_img;
  163. }
  164. if (this._serverConfig.is_local_pos_id
  165. && this._serverConfig.is_local_pos_id == "false") {
  166. // 使用服务器下发的广告id
  167. utils.showLog("使用服务器下发的广告id");
  168. utils.config.kwaiConfig.videoId = this._serverConfig.video_pos_id;
  169. utils.config.kwaiConfig.insertId = this._serverConfig.intersititia_pos_id || utils.config.kwaiConfig.insertId;
  170. } else {
  171. utils.showLog("使用本地配置的广告ID");
  172. }
  173. }
  174. utils.emitServerInitEvent();
  175. });
  176. }
  177. }
  178. initRecord() {
  179. if (!this.checkCanShowRecored()) {
  180. utils.showLog("当前平台不支持录屏组件!");
  181. return;
  182. }
  183. this._recorder = this.kwaigame.createMediaRecorder();
  184. if (this._recorder) {
  185. this._recorder.init({
  186. callback: (error) => {
  187. if (error) {
  188. utils.showLog("录屏初始化失败: " + JSON.stringify(error));
  189. this._recorder = null;
  190. return;
  191. }
  192. utils.showLog("录屏初始化成功");
  193. }
  194. });
  195. this._recorder.onError({
  196. listener: (error) => {
  197. utils.isRecording = false;
  198. cc.game.emit("YZ_CommonMessage", { type: "YZ_RecordEnd" });
  199. utils.showLog("录屏终止: " + JSON.stringify(error));
  200. }
  201. });
  202. }
  203. // if (this._recorder) {
  204. // this._recorder.onStart((res) => {
  205. // self._videoPath = "";
  206. // this.isClickEnd = false;
  207. // this.is_auto_share_video = false;
  208. // utils.isRecording = true;
  209. // console.log("录屏开始>>>");
  210. // utils.showLog('录屏开始');
  211. // this.recorderStarTime = new Date().getTime();
  212. // cc.game.emit("YZ_CommonMessage", { type: "YZ_RecordStart" });
  213. // });
  214. // this._recorder.onStop((res) => {
  215. // utils.showLog("录屏结束 :" + res.videoPath);
  216. // utils.isRecording = false;
  217. // cc.game.emit("YZ_CommonMessage", { type: "YZ_RecordEnd" });
  218. // let curTime: number = new Date().getTime();
  219. // let interval: number = (curTime - this.recorderStarTime) / 1000;
  220. // if (interval > 3) {
  221. // self._videoPath = res.videoPath;
  222. // utils.showLog("录屏时间大于3秒", interval);
  223. // if (this.isAutoShare) {
  224. // if (this.isClickEnd) {
  225. // this.shareVideo();
  226. // this._shareCallback = null;
  227. // this.is_auto_share_video = true;
  228. // } else if (utils.checkResultShow(1)) {
  229. // utils.showLog("满足自动分享条件,弹出分享窗口!");
  230. // if (utils.rewardCloseFunc == null) {
  231. // utils.rewardValue = 0;
  232. // utils.showShareRecordPanel();
  233. // }
  234. // } else if (interval > 100) {
  235. // if (utils.rewardCloseFunc == null) {
  236. // utils.rewardValue = 0;
  237. // utils.showShareRecordPanel();
  238. // }
  239. // }
  240. // } else {
  241. // this.isAutoShare = true;
  242. // }
  243. // } else {
  244. // //@ts-ignore
  245. // if (this.isClickEnd) {
  246. // this.tt.showToast({
  247. // title: "录屏时间小于3秒!",
  248. // icon: "fail",
  249. // duration: 2000,
  250. // success(res) {
  251. // console.log(`${res}`);
  252. // },
  253. // fail(res) {
  254. // console.log(`showToast调用失败`);
  255. // }
  256. // });
  257. // }
  258. // self._videoPath = "";
  259. // utils.showLog("录屏时间小于3秒,录录屏地址置空!");
  260. // }
  261. // });
  262. // }
  263. }
  264. /**
  265. * 分享,有录屏就分享录屏,没有录屏就分享图片
  266. */
  267. public share(callback: Function) {
  268. if (PlatUtils.IsKwai) {
  269. this._shareCallback = callback;
  270. if (this.recorderStarTime > 0) {
  271. this.shareVideo();
  272. } else {
  273. this.shareImage();
  274. }
  275. }
  276. }
  277. /**
  278. * 验证是否支持录屏
  279. */
  280. checkCanShowRecored() {
  281. return this.kwaigame.isSupport(this.kwaigame.Support.features.Recorder);
  282. }
  283. /**
  284. * 分享录屏
  285. */
  286. shareVideo() {
  287. if (this._recorder) {
  288. if (!this._canShowRecord) {
  289. this._shareCallback && this._shareCallback(false, "录屏时间不足!")
  290. return;
  291. }
  292. this._recorder.publishVideo({
  293. callback: (error) => {
  294. if (error) {
  295. utils.showLog("分享录屏失败: " + JSON.stringify(error));
  296. this._shareCallback && this._shareCallback(false, "分享录屏失败!")
  297. return;
  298. }
  299. this._canShowRecord = false;
  300. utils.showLog("分享录屏成功");
  301. this._shareCallback && this._shareCallback(true, "分享录屏成功!")
  302. }
  303. });
  304. } else {
  305. if (this.checkCanShowRecored()) {
  306. utils.showLog("当前平台不支持录屏组件!");
  307. } else {
  308. utils.showLog("录屏初始化失败!");
  309. }
  310. this._shareCallback && this._shareCallback(false, "分享录屏失败!")
  311. }
  312. }
  313. /**
  314. * 开始录屏
  315. */
  316. public recordStart() {
  317. if (this._recorder) {
  318. this._recorder.start({
  319. callback: (error) => {
  320. if (error) {
  321. utils.isRecording = false;
  322. utils.showLog("开始录屏失败: " + JSON.stringify(error));
  323. return;
  324. }
  325. this._canShowRecord = false;
  326. utils.isRecording = true;
  327. this.recorderStarTime = new Date().getTime();
  328. cc.game.emit("YZ_CommonMessage", { type: "YZ_RecordStart" });
  329. utils.showLog("开始录屏成功");
  330. }
  331. });
  332. }
  333. }
  334. _canShowRecord: boolean = false;
  335. /**
  336. * 结束录屏
  337. */
  338. public recordEnd() {
  339. if (this._recorder) {
  340. this._recorder.stop({
  341. callback: (error) => {
  342. utils.isRecording = false;
  343. cc.game.emit("YZ_CommonMessage", { type: "YZ_RecordEnd" });
  344. this._canShowRecord = false;
  345. if (error) {
  346. utils.showLog("停止录屏失败: " + JSON.stringify(error));
  347. return;
  348. }
  349. utils.showLog("停止录屏成功");
  350. let curTime: number = new Date().getTime();
  351. let interval: number = (curTime - this.recorderStarTime) / 1000;
  352. if (interval > 3) {
  353. this._canShowRecord = true;
  354. utils.showLog("录屏时间大于3秒", interval, " isClickEnd=", this.isClickEnd);
  355. if (this.isAutoShare) {
  356. if (this.isClickEnd) {
  357. this.shareVideo();
  358. this._shareCallback = null;
  359. } else if (utils.checkResultShow(1)) {
  360. utils.showLog("满足自动分享条件,弹出分享窗口!");
  361. if (utils.rewardCloseFunc == null) {
  362. utils.rewardValue = 0;
  363. utils.showShareRecordPanel();
  364. }
  365. } else if (interval > 100) {
  366. if (utils.rewardCloseFunc == null) {
  367. utils.rewardValue = 0;
  368. utils.showShareRecordPanel();
  369. }
  370. }
  371. } else {
  372. this.isAutoShare = true;
  373. }
  374. } else {
  375. //@ts-ignore
  376. if (this.isClickEnd) {
  377. // this.tt.showToast({
  378. // title: "录屏时间小于3秒!",
  379. // icon: "fail",
  380. // duration: 2000,
  381. // success(res) {
  382. // console.log(`${res}`);
  383. // },
  384. // fail(res) {
  385. // console.log(`showToast调用失败`);
  386. // }
  387. // });
  388. }
  389. // self._videoPath = "";
  390. utils.showLog("录屏时间小于3秒,录录屏地址置空!");
  391. }
  392. }
  393. });
  394. }
  395. }
  396. shareImage() {
  397. this.kwaigame.shareToMsg(this._getShareInfo());
  398. }
  399. _getShareInfo() {
  400. let params: any = {};
  401. params.title = utils.config.otherconfig.shareTitle;
  402. params.desc = utils.config.otherconfig.shareDesc ? utils.config.otherconfig.shareDesc : utils.config.otherconfig.shareTitle;
  403. params.iconUrl = utils.config.otherconfig.shareIcon ? utils.config.otherconfig.shareIcon : utils.config.otherconfig.shareImgUrl;
  404. params.imageUrl = utils.config.otherconfig.shareImgUrl;
  405. params.extension = {
  406. isShare: "share"
  407. };
  408. params.response = (result) => {
  409. utils.showLog("分享完成: " + JSON.stringify(result));
  410. this._shareCallback && this._shareCallback(true, "分享成功!");
  411. }
  412. return params;
  413. }
  414. /**
  415. * 上报关卡数据
  416. * @param level 当前关卡ID
  417. * @param levelName 关卡名称
  418. * @param status 状态
  419. */
  420. public postLevel(level: string, status: LevelStatus, levelName?: string) {
  421. if (PlatUtils.IsKwai) {
  422. let method = "m=rlevel";
  423. let url: string = POST_ServerUrl + method + `&level_id=${level}&level_name=${encodeURI(levelName)}&status=${status}`;
  424. utils.commomHttpRequest(url, function (ret, data) {
  425. if (ret) {
  426. utils.showLog("关卡数据上报成功!");
  427. } else {
  428. utils.showLog("关卡数据上报失败!");
  429. }
  430. }.bind(this));
  431. }
  432. }
  433. /**
  434. * 上报自定义事件
  435. * @param level 当前关卡ID
  436. * @param levelName 关卡名称
  437. * @param status 状态
  438. */
  439. public sendEvent(eventName: string) {
  440. if (PlatUtils.IsKwai) {
  441. let method = "m=revent";
  442. let url: string = POST_ServerUrl + method + `&event=${encodeURI(eventName)}`;
  443. utils.commomHttpRequest(url, function (ret, data) {
  444. if (ret) {
  445. utils.showLog("上报自定义事件成功!");
  446. } else {
  447. utils.showLog("上报自定义事件失败!");
  448. }
  449. }.bind(this));
  450. }
  451. }
  452. _compareVersion(first: string, second: string) {
  453. return parseInt(first.split(".").join("")) >= parseInt(second.split(".").join(""));
  454. }
  455. }