6f2a5c0b-3e43-4567-b861-9c48fc7d8e01.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. "use strict";
  2. cc._RF.push(module, '6f2a5wLPkNFZ7hhnEj8fY4B', 'WechatTool');
  3. // common-plugin/Scripts/WechatTool.ts
  4. "use strict";
  5. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  6. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  7. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  8. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  9. return c > 3 && r && Object.defineProperty(target, key, r), r;
  10. };
  11. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  12. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  13. return new (P || (P = Promise))(function (resolve, reject) {
  14. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  15. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  16. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  17. step((generator = generator.apply(thisArg, _arguments || [])).next());
  18. });
  19. };
  20. var __generator = (this && this.__generator) || function (thisArg, body) {
  21. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  22. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  23. function verb(n) { return function (v) { return step([n, v]); }; }
  24. function step(op) {
  25. if (f) throw new TypeError("Generator is already executing.");
  26. while (_) try {
  27. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  28. if (y = 0, t) op = [op[0] & 2, t.value];
  29. switch (op[0]) {
  30. case 0: case 1: t = op; break;
  31. case 4: _.label++; return { value: op[1], done: false };
  32. case 5: _.label++; y = op[1]; op = [0]; continue;
  33. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  34. default:
  35. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  36. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  37. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  38. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  39. if (t[2]) _.ops.pop();
  40. _.trys.pop(); continue;
  41. }
  42. op = body.call(thisArg, _);
  43. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  44. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  45. }
  46. };
  47. Object.defineProperty(exports, "__esModule", { value: true });
  48. var PlatUtils_1 = require("./PlatUtils");
  49. var Utils_1 = require("./Utils");
  50. var YZ_Constant_1 = require("./YZ_Constant");
  51. var YZ_LocalStorage_1 = require("./YZ_LocalStorage");
  52. var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
  53. var ST_ServerUrl = "https://apps.youlesp.com/gss?";
  54. /**
  55. * 游戏盒子的请求链接
  56. */
  57. var GB_ServerUrl = "https://apps.youlesp.com/gbs?";
  58. // 默认配置
  59. var ST_DefaultServerConfig = "";
  60. // 默认配置
  61. var GB_DefaultServerConfig = "";
  62. var ST_LoadConfigInterval = 5000; // 毫秒
  63. var WechatTool = /** @class */ (function () {
  64. function WechatTool() {
  65. /**
  66. * 服务器配置信息
  67. */
  68. this._serverConfig = null;
  69. /**
  70. * 游戏盒子配置信息
  71. */
  72. this._gameBoxServerConfig = null;
  73. //设备UID
  74. this._uid = "0";
  75. //服务器返回UID
  76. this._service_uid = "0";
  77. this._sysInfo = null;
  78. this._shareCallback = null;
  79. this._isShare = false;
  80. this._lastUpdateTime = 0;
  81. this._appIdList = [];
  82. this._jumpIds = "";
  83. //启动来源的场景:
  84. this._luanchType = "";
  85. this.clue_token = ""; //字节token
  86. this.wx_code = ""; //登录后返回的code
  87. //来源APPID
  88. this._source_app_id = "";
  89. //启动参数
  90. this._luanchData = "";
  91. this._loginTime = 0;
  92. this._loginInterval = 30;
  93. this._reportLoginTime = 0;
  94. this._reportLoginInterval = 30;
  95. this.isReport = false;
  96. this.isReportActive = false;
  97. this.passCount = 0; //通关数据
  98. this.playCount = 0; //开始关卡数据
  99. }
  100. Object.defineProperty(WechatTool.prototype, "ServerConfig", {
  101. get: function () {
  102. return this._serverConfig;
  103. },
  104. enumerable: false,
  105. configurable: true
  106. });
  107. Object.defineProperty(WechatTool.prototype, "gameBoxServerConfig", {
  108. get: function () {
  109. return this._gameBoxServerConfig;
  110. },
  111. enumerable: false,
  112. configurable: true
  113. });
  114. Object.defineProperty(WechatTool.prototype, "uid", {
  115. get: function () {
  116. if (this._uid != "0")
  117. return this._uid;
  118. this._login();
  119. return "0";
  120. },
  121. enumerable: false,
  122. configurable: true
  123. });
  124. Object.defineProperty(WechatTool.prototype, "serviceId", {
  125. /**
  126. * 服务器返回UID
  127. */
  128. get: function () {
  129. if (this._service_uid != "0")
  130. return this._service_uid;
  131. this._login();
  132. return "0";
  133. },
  134. enumerable: false,
  135. configurable: true
  136. });
  137. Object.defineProperty(WechatTool.prototype, "SysInfo", {
  138. get: function () {
  139. return this._sysInfo;
  140. },
  141. enumerable: false,
  142. configurable: true
  143. });
  144. /**
  145. *
  146. * @param data 配置数据
  147. */
  148. WechatTool.prototype.init = function (data) {
  149. var _this = this;
  150. if (PlatUtils_1.default.IsWechat) {
  151. //@ts-ignore
  152. try {
  153. //@ts-ignore
  154. this._sysInfo = wx.getSystemInfoSync();
  155. Utils_1.utils.showLog("微信小游戏平台信息: " + JSON.stringify(this.SysInfo));
  156. }
  157. catch (e) {
  158. // Do something when catch error
  159. }
  160. try {
  161. this._luanchData = YZ_LocalStorage_1.default.getItem(YZ_Constant_1.default.ST_LUANCH_DATA);
  162. this._luanchType = YZ_LocalStorage_1.default.getItem(YZ_Constant_1.default.ST_LUANCH_TYPE);
  163. //@ts-ignore
  164. var options = wx.getLaunchOptionsSync();
  165. options && Utils_1.utils.showLog(">>获取到小程序启动参数:" + JSON.stringify(options));
  166. if (this._luanchData && this._luanchType) {
  167. Utils_1.utils.showLog("获取到本地缓存数据启动类型:" + this._luanchType);
  168. Utils_1.utils.showLog("获取到本地缓存数据启动参数:" + this._luanchData);
  169. }
  170. else {
  171. if (options) {
  172. if (options.scene) {
  173. Utils_1.utils.showLog("获取到启动场景值:" + options.scene);
  174. this._luanchType = options.scene;
  175. YZ_LocalStorage_1.default.setItem(YZ_Constant_1.default.ST_LUANCH_TYPE, this._luanchType);
  176. }
  177. if (options.query) {
  178. var queryData = JSON.stringify(options.query);
  179. this._luanchData = queryData;
  180. YZ_LocalStorage_1.default.setItem(YZ_Constant_1.default.ST_LUANCH_DATA, this._luanchData);
  181. Utils_1.utils.showLog("获取到小程序启动参数:" + queryData);
  182. }
  183. if (options.referrerInfo && JSON.stringify(options.referrerInfo) != "{}") {
  184. Utils_1.utils.showLog("获取到小程序来源信息:" + JSON.stringify(options.referrerInfo));
  185. if (options.referrerInfo.appId) {
  186. Utils_1.utils.showLog("获取到小程序启动来源的appId:" + options.referrerInfo.appId);
  187. this._source_app_id = options.referrerInfo.appId;
  188. }
  189. }
  190. }
  191. }
  192. }
  193. catch (erro) {
  194. Utils_1.utils.showLog("获取到小程序启动参数异常");
  195. }
  196. if (data) {
  197. var configObj = JSON.parse(data);
  198. if (configObj && configObj.wechat) {
  199. ST_DefaultServerConfig = JSON.stringify(configObj.wechat);
  200. }
  201. if (configObj && configObj.gamebox) {
  202. GB_DefaultServerConfig = JSON.stringify(configObj.gamebox);
  203. }
  204. }
  205. // 拉取服务器配置
  206. // this.loadJumpIds().then(() => {
  207. // console.log("本地跳转ID加载成功!");
  208. // this._loadConfig();
  209. // }).catch((erro) => {
  210. // console.error("本地跳转ID加载失败:", erro);
  211. this._loadConfig();
  212. // })
  213. //加载配置的跳转ID
  214. // this.loadAppIdList();
  215. // 开启右上角转发
  216. //@ts-ignore
  217. wx.showShareMenu();
  218. //@ts-ignore
  219. wx.onShareAppMessage(this._getShareInfo.bind(this));
  220. //@ts-ignore
  221. wx.onShow(function (res) {
  222. cc.log("OnShow");
  223. if (_this._isShare) {
  224. _this._isShare = false;
  225. if (_this._shareCallback) {
  226. _this._shareCallback(true, "分享成功!");
  227. }
  228. }
  229. _this._checkForUpdate();
  230. Utils_1.utils.emitCommonEvent(YZ_Constant_1.default.EC_OnShow);
  231. });
  232. //@ts-ignore
  233. wx.onHide(function () {
  234. cc.log("onHide");
  235. Utils_1.utils.emitCommonEvent(YZ_Constant_1.default.EC_OnHide);
  236. });
  237. this._checkForUpdate();
  238. }
  239. };
  240. WechatTool.prototype._checkForUpdate = function () {
  241. if (PlatUtils_1.default.IsWechat) {
  242. if ((new Date()).getTime() - this._lastUpdateTime < ST_LoadConfigInterval) {
  243. cc.log("检查更新太频繁!");
  244. return;
  245. }
  246. this._lastUpdateTime = (new Date()).getTime();
  247. if (this.isOverMinVersion("1.9.90")) {
  248. cc.log("检查更新......");
  249. //@ts-ignore
  250. var updateManager_1 = wx.getUpdateManager();
  251. updateManager_1.onCheckForUpdate(function (res) {
  252. // 请求完新版本信息的回调
  253. if (res.hasUpdate) {
  254. cc.log("有新版本需要更新!");
  255. }
  256. else {
  257. cc.log("没有新版本!");
  258. }
  259. });
  260. updateManager_1.onUpdateReady(function () {
  261. //@ts-ignore
  262. wx.showModal({
  263. title: '更新提示',
  264. content: '新版本已经准备好,是否重启应用?',
  265. success: function (res) {
  266. if (res.confirm) {
  267. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  268. updateManager_1.applyUpdate();
  269. }
  270. }
  271. });
  272. });
  273. updateManager_1.onUpdateFailed(function () {
  274. // 新版本下载失败
  275. cc.log("新版本下载失败!");
  276. });
  277. }
  278. }
  279. };
  280. WechatTool.prototype._buildServerUrl = function () {
  281. var url = "";
  282. if (Utils_1.utils.config.wechatconfig && Utils_1.utils.config.wechatconfig.appID) {
  283. var appid = Utils_1.utils.config.wechatconfig.appID;
  284. var channel = "wechat";
  285. var jumpId = this._jumpIds ? this._jumpIds : Utils_1.utils.config.wechatconfig.jumpId;
  286. url = ST_ServerUrl + ("kyx=true&app_id=" + appid + "&channel=" + channel + "&jump_id=" + jumpId);
  287. }
  288. else {
  289. cc.log("微信 APPID配置出错!");
  290. }
  291. return url;
  292. };
  293. WechatTool.prototype._buildGameBoxServerUrl = function () {
  294. var url = GB_ServerUrl + "m=ghome&index=0&type=1";
  295. return url;
  296. };
  297. WechatTool.prototype._loadConfig = function () {
  298. var _this = this;
  299. if (PlatUtils_1.default.IsWechat) {
  300. cc.log("加载服务器配置.......");
  301. var method = "m=g";
  302. var url_1 = ST_ServerUrl + method;
  303. // let jumpId: string = this._jumpIds ? this._jumpIds : utils.config.wechatconfig.jumpId;
  304. // url += `&jump_id=${jumpId}`
  305. Utils_1.utils.commomHttpRequest(url_1, function (ret, data) {
  306. if (ret) {
  307. cc.log("微信服务器配置数据获取成功: data = ", data);
  308. if (data) {
  309. var result = JSON.parse(data);
  310. if (result) {
  311. if (!Utils_1.utils.DebugLoacalConfig) {
  312. _this._serverConfig = result;
  313. }
  314. else {
  315. cc.warn("开启了本地数据测试,使用本地配置!");
  316. }
  317. }
  318. else {
  319. cc.log("微信服务器配置数据不是合法的JSON数据, 使用本地配置!");
  320. }
  321. }
  322. }
  323. else {
  324. cc.log("微信服务器配置数据获取失败, 使用本地配置!");
  325. }
  326. if (!_this._serverConfig) {
  327. _this._serverConfig = JSON.parse(ST_DefaultServerConfig);
  328. }
  329. else {
  330. if (_this._serverConfig.shares && _this._serverConfig.shares.sy_title) {
  331. Utils_1.utils.config.otherconfig.shareTitle = _this._serverConfig.shares.sy_title;
  332. }
  333. if (_this._serverConfig.shares && _this._serverConfig.shares.sy_img) {
  334. Utils_1.utils.config.otherconfig.shareImgUrl = _this._serverConfig.shares.sy_img;
  335. }
  336. if (_this._serverConfig.is_local_pos_id == "false") {
  337. // 使用服务器下发的广告id
  338. cc.log("使用服务器下发的广告ID");
  339. Utils_1.utils.config.wechatconfig.insertId = _this.ServerConfig.intersititia_pos_id || Utils_1.utils.config.wechatconfig.insertId;
  340. Utils_1.utils.config.wechatconfig.videoId = _this.ServerConfig.video_pos_id || Utils_1.utils.config.wechatconfig.videoId;
  341. Utils_1.utils.config.wechatconfig.boxId = _this._serverConfig.box_pos_id || Utils_1.utils.config.wechatconfig.boxId;
  342. Utils_1.utils.config.wechatconfig.bannerBoxId = _this._serverConfig.banner_box_pos_id || Utils_1.utils.config.wechatconfig.bannerBoxId;
  343. Utils_1.utils.config.wechatconfig.nativeBannerId = _this._serverConfig.native_banner_pos_id || Utils_1.utils.config.wechatconfig.nativeBannerId;
  344. Utils_1.utils.config.wechatconfig.nativeInsertIds = _this._serverConfig.native_intersititial_pos_id || Utils_1.utils.config.wechatconfig.nativeInsertIds;
  345. var bannerLocation = ["home", "game", "level", "skin", "pause", "over"];
  346. for (var i = 0; i < bannerLocation.length; i++) {
  347. if (_this.ServerConfig.banner_pos_id[bannerLocation[i]]) {
  348. Utils_1.utils.config.wechatconfig.setBannerId(YZ_Constant_1.BannerLocationToEnum(bannerLocation[i]), _this.ServerConfig.banner_pos_id[bannerLocation[i]]);
  349. }
  350. }
  351. if (_this.ServerConfig.native_customad_configs) {
  352. for (var i = 0; i < _this.ServerConfig.native_customad_configs.length; i++) {
  353. Utils_1.utils.showLog("获取到原生模版广告配置:" + _this.ServerConfig.native_customad_configs[i].location, ">>>>", JSON.stringify(_this.ServerConfig.native_customad_configs[i]));
  354. Utils_1.utils.config.wechatconfig.setCustomAdInfo(_this.ServerConfig.native_customad_configs[i].location, _this.ServerConfig.native_customad_configs[i]);
  355. }
  356. }
  357. }
  358. else {
  359. cc.log("使用本地配置的广告ID");
  360. }
  361. }
  362. Utils_1.utils.emitServerInitEvent();
  363. if (_this._serverConfig && _this._serverConfig.openBox) {
  364. if (_this._serverConfig.openBox != "true") {
  365. cc.log("服务器游戏盒子配置为关闭状态!");
  366. return;
  367. }
  368. url_1 = _this._buildGameBoxServerUrl();
  369. Utils_1.utils.commomHttpRequest(url_1, function (ret, data) {
  370. if (ret) {
  371. cc.log("微信游戏盒子服务器配置数据获取成功: data = ", data);
  372. if (data) {
  373. var result = JSON.parse(data);
  374. if (result) {
  375. if (!Utils_1.utils.DebugLoacalConfig) {
  376. _this._gameBoxServerConfig = result;
  377. }
  378. else {
  379. cc.warn("开启了本地数据测试,使用本地配置!");
  380. }
  381. }
  382. else {
  383. cc.log("微信游戏盒子微信服务器配置数据不是合法的JSON数据, 使用本地配置!");
  384. }
  385. }
  386. }
  387. else {
  388. cc.log("微信游戏盒子服务器配置数据获取失败, 使用本地配置!");
  389. }
  390. if (!_this._gameBoxServerConfig) {
  391. _this._gameBoxServerConfig = JSON.parse(GB_DefaultServerConfig);
  392. }
  393. });
  394. }
  395. });
  396. }
  397. };
  398. /**
  399. *
  400. * @param id 跳转ID
  401. * @param callback 跳转回调
  402. */
  403. WechatTool.prototype.navigateToMiniProgram = function (id, callback, path) {
  404. if (PlatUtils_1.default.IsWechat) {
  405. var completeCallback_1 = callback;
  406. //@ts-ignore
  407. wx.navigateToMiniProgram({
  408. appId: id.indexOf(":") > -1 ? id.split(":")[0] : id,
  409. path: path ? path : "",
  410. success: function (res) {
  411. if (completeCallback_1) {
  412. completeCallback_1(true);
  413. }
  414. },
  415. fail: function (res) {
  416. // cc.log("跳转失败!", id);
  417. cc.log("\u8DF3\u8F6C\u5931\u8D25! id=" + id + "; res=" + JSON.stringify(res));
  418. if (completeCallback_1) {
  419. completeCallback_1(false);
  420. }
  421. }
  422. });
  423. }
  424. };
  425. WechatTool.prototype.getSystemInfo = function () {
  426. if (PlatUtils_1.default.IsWechat) {
  427. //@ts-ignore
  428. return wx.getSystemInfoSync();
  429. }
  430. };
  431. WechatTool.prototype.previewImage = function (url) {
  432. if (PlatUtils_1.default.IsWechat) {
  433. //@ts-ignore
  434. wx.previewImage({
  435. urls: [url] // 需要预览的图片http链接列表
  436. });
  437. }
  438. };
  439. WechatTool.prototype.share = function (callback) {
  440. if (callback === void 0) { callback = null; }
  441. if (PlatUtils_1.default.IsWechat) {
  442. this._shareCallback = callback;
  443. this._isShare = true;
  444. //@ts-ignore
  445. wx.shareAppMessage(this._getShareInfo());
  446. }
  447. };
  448. /**
  449. * 上报数据
  450. */
  451. WechatTool.prototype.postData = function (otherGameAppId) {
  452. if (PlatUtils_1.default.IsWechat) {
  453. var method = "m=rjump";
  454. var url = ST_ServerUrl + method + ("&jump_app_id=" + otherGameAppId);
  455. cc.log("上报数据, url=", url);
  456. Utils_1.utils.commomHttpRequest(url, function (ret, data) {
  457. if (ret) {
  458. cc.log("数据上报成功!");
  459. }
  460. else {
  461. cc.log("数据上报失败!");
  462. }
  463. });
  464. }
  465. };
  466. /**
  467. * 上报互推组件数据
  468. * @param otherGameAppId 跳转的ID
  469. * @param location 当前位置
  470. * @param status 0:点击,1:跳转成功
  471. */
  472. WechatTool.prototype.postDataByLocation = function (otherGameAppId, location, status) {
  473. if (status === void 0) { status = 0; }
  474. if (PlatUtils_1.default.IsWechat) {
  475. var method = "m=rjump";
  476. var url = ST_ServerUrl + method + ("&jump_app_id=" + otherGameAppId + "&location=" + location + "&status=" + status);
  477. Utils_1.utils.commomHttpRequest(url, function (ret, data) {
  478. if (ret) {
  479. cc.log("数据上报成功!");
  480. }
  481. else {
  482. cc.log("数据上报失败!");
  483. }
  484. });
  485. }
  486. };
  487. /**
  488. * 上报互推组件显示位置
  489. * @param otherGameAppId 跳转的ID
  490. * @param location 当前位置
  491. * @param status 0:点击,1:跳转成功
  492. */
  493. WechatTool.prototype.postRecommentShowData = function (location) {
  494. if (PlatUtils_1.default.IsWechat) {
  495. var method = "m=rjumpshow";
  496. var url = ST_ServerUrl + method + ("&location=" + location);
  497. Utils_1.utils.commomHttpRequest(url, function (ret, data) {
  498. if (ret) {
  499. cc.log("数据上报成功!");
  500. }
  501. else {
  502. cc.log("数据上报失败!");
  503. }
  504. }.bind(this));
  505. }
  506. };
  507. WechatTool.prototype._getShareInfo = function () {
  508. if (PlatUtils_1.default.IsWechat) {
  509. return {
  510. title: Utils_1.utils.config.otherconfig.shareTitle,
  511. imageUrl: Utils_1.utils.config.otherconfig.shareImgUrl
  512. };
  513. }
  514. return {};
  515. };
  516. WechatTool.prototype.isOverMinVersion = function (minVersion) {
  517. //@ts-ignore
  518. var curVersion = wx.getSystemInfoSync().SDKVersion;
  519. return this._compareVersion(curVersion, minVersion) >= 0;
  520. };
  521. WechatTool.prototype._compareVersion = function (v1, v2) {
  522. v1 = v1.split('.');
  523. v2 = v2.split('.');
  524. var len = Math.max(v1.length, v2.length);
  525. while (v1.length < len) {
  526. v1.push('0');
  527. }
  528. while (v2.length < len) {
  529. v2.push('0');
  530. }
  531. for (var i = 0; i < len; i++) {
  532. var num1 = parseInt(v1[i]);
  533. var num2 = parseInt(v2[i]);
  534. if (num1 > num2) {
  535. return 1;
  536. }
  537. else if (num1 < num2) {
  538. return -1;
  539. }
  540. }
  541. return 0;
  542. };
  543. /**
  544. * 获取交叉推广数据
  545. */
  546. WechatTool.prototype.getRecommondGameList = function () {
  547. if (PlatUtils_1.default.IsWechat && Utils_1.utils.wechatTool && Utils_1.utils.wechatTool.ServerConfig) {
  548. return Utils_1.utils.wechatTool.ServerConfig.jump_list;
  549. }
  550. return null;
  551. };
  552. // /**
  553. // * 加载游戏配置的跳转ID
  554. // */
  555. // loadAppIdList() {
  556. // CompatibleTool.LoadResRes("./src/game.json", (err, res) => {
  557. // if (err) {
  558. // cc.error('加载game.json文件出错', err);
  559. // return;
  560. // }
  561. // if (res && res.navigateToMiniProgramAppIdList && res.navigateToMiniProgramAppIdList.length) {
  562. // let taskQty = res.subpackages.length;
  563. // for (let i = 0; i < taskQty; i++) {
  564. // this._appIdList.push(res.navigateToMiniProgramAppIdList[i].name);
  565. // }
  566. // cc.log("配置的跳转ID:", this._appIdList);
  567. // }
  568. // });
  569. // }
  570. /**
  571. * 验证ID 是否配置
  572. * @param appId 应用ID
  573. */
  574. WechatTool.prototype.checkAppId = function (appId) {
  575. if (Utils_1.utils.config.wechatconfig.jumpId.indexOf(appId) > -1) {
  576. return true;
  577. }
  578. cc.log("appId :", appId, "不再配置列表中!");
  579. return false;
  580. };
  581. WechatTool.prototype._login = function () {
  582. var curTime = new Date().getTime();
  583. var interval = (curTime - this._loginTime) / 1000;
  584. if (interval > 0 && interval < this._loginInterval) {
  585. Utils_1.utils.showLog("\u767B\u5F55\u8BF7\u6C42\u95F4\u9694\u5C0F\u4E8E\uFF1A" + this._loginInterval + "\u79D2");
  586. return;
  587. }
  588. this._loginTime = curTime;
  589. var self = this;
  590. this._uid = YZ_LocalStorage_1.default.getItem(YZ_Constant_1.default.ST_UID);
  591. this._uid = this._uid ? this._uid : "0";
  592. this._service_uid = YZ_LocalStorage_1.default.getItem(YZ_Constant_1.default.ST_SERVICE_UID);
  593. this._service_uid = this._service_uid ? this._service_uid : "0";
  594. Utils_1.utils.showLog("获取本地保存的uid=" + this._uid + ",服务器UID=" + this._service_uid);
  595. if (this._uid == "0") {
  596. //@ts-ignore
  597. wx.login({
  598. success: function (res) {
  599. if (res.code) {
  600. self.wx_code = res.code;
  601. self.reportLogin(res.code);
  602. }
  603. else {
  604. Utils_1.utils.showLog('登录失败!' + res.errMsg);
  605. }
  606. },
  607. fail: function () {
  608. },
  609. complete: function () {
  610. self.reportLogin();
  611. }
  612. });
  613. }
  614. };
  615. /**
  616. * 上报登录接口获取UID
  617. */
  618. WechatTool.prototype.reportLogin = function (code) {
  619. if (code === void 0) { code = ""; }
  620. return __awaiter(this, void 0, void 0, function () {
  621. var self, curTime, interval, method, url;
  622. var _this = this;
  623. return __generator(this, function (_a) {
  624. if (this.isReport)
  625. return [2 /*return*/];
  626. this.isReport = true;
  627. self = this;
  628. curTime = new Date().getTime();
  629. interval = (curTime - this._reportLoginTime) / 1000;
  630. if (interval > 0 && interval < this._reportLoginInterval) {
  631. Utils_1.utils.showLog("\u4E0A\u62A5\u767B\u5F55\u83B7\u53D6UID\u5C0F\u4E8E\uFF1A" + this._reportLoginInterval + "\u79D2");
  632. return [2 /*return*/];
  633. }
  634. this._reportLoginTime = curTime;
  635. method = "m=login";
  636. url = ST_ServerUrl + method + ("&device_data=" + encodeURI(JSON.stringify(this._sysInfo)) + "&code=" + code);
  637. Utils_1.utils.commomHttpRequest(url, function (ret, data) {
  638. if (ret) {
  639. if (data) {
  640. Utils_1.utils.showLog("#data=" + data);
  641. var result = JSON.parse(data);
  642. if (result.uid) {
  643. self._service_uid = "" + result.uid;
  644. if (!self.isReportActive) {
  645. self.isReportActive = true;
  646. _this.reportAttributedEvent(YZ_Constant_1.AttributedType.Active, YZ_Constant_1.AttributedKey.Active, YZ_Constant_1.AttributedValue.Active);
  647. }
  648. YZ_LocalStorage_1.default.setItem(YZ_Constant_1.default.ST_SERVICE_UID, self._service_uid);
  649. }
  650. if (result.device_uid) {
  651. self._uid = "" + result.device_uid;
  652. YZ_LocalStorage_1.default.setItem(YZ_Constant_1.default.ST_UID, self._uid);
  653. }
  654. Utils_1.utils.showLog("服务器请求登录成功! _service_uid=" + self._service_uid + " #device_id=" + self._uid);
  655. }
  656. }
  657. _this.isReport = false;
  658. });
  659. return [2 /*return*/];
  660. });
  661. });
  662. };
  663. /**
  664. * 上报关卡数据
  665. * @param level 当前关卡ID
  666. * @param levelName 关卡名称
  667. * @param status 状态
  668. */
  669. WechatTool.prototype.postLevel = function (level, status, levelName) {
  670. if (PlatUtils_1.default.IsWechat) {
  671. if (status == YZ_Constant_1.LevelStatus.GameWin) {
  672. this.passCount++;
  673. if (this.passCount === Utils_1.utils.getConfigByKey("pass_level_count_active")) {
  674. this.reportAttributedEvent(YZ_Constant_1.AttributedType.GameAddiction, YZ_Constant_1.AttributedKey.GameAddiction, YZ_Constant_1.AttributedValue.PassLevelCountAction);
  675. }
  676. }
  677. else if (status === YZ_Constant_1.LevelStatus.GameStart) {
  678. this.playCount++;
  679. if (this.playCount == Utils_1.utils.getConfigByKey("play_level_count_active")) {
  680. this.reportAttributedEvent(YZ_Constant_1.AttributedType.GameAddiction, YZ_Constant_1.AttributedKey.GameAddiction, YZ_Constant_1.AttributedValue.PlayLevelCountAction);
  681. }
  682. }
  683. var method = "m=rlevel";
  684. var url = ST_ServerUrl + method + ("&level_id=" + level + "&level_name=" + encodeURI(levelName) + "&status=" + status);
  685. Utils_1.utils.commomHttpRequest(url, function (ret, data) {
  686. if (ret) {
  687. Utils_1.utils.showLog("关卡数据上报成功!");
  688. }
  689. else {
  690. Utils_1.utils.showLog("关卡数据上报失败!");
  691. }
  692. }.bind(this));
  693. }
  694. };
  695. /**
  696. * 上报自定义事件
  697. * @param level 当前关卡ID
  698. * @param levelName 关卡名称
  699. * @param status 状态
  700. */
  701. WechatTool.prototype.sendEvent = function (eventName) {
  702. if (PlatUtils_1.default.IsWechat) {
  703. var method = "m=revent";
  704. var url = ST_ServerUrl + method + ("&event=" + encodeURI(eventName));
  705. Utils_1.utils.commomHttpRequest(url, function (ret, data) {
  706. if (ret) {
  707. Utils_1.utils.showLog("上报自定义事件成功!");
  708. }
  709. else {
  710. Utils_1.utils.showLog("上报自定义事件失败!");
  711. }
  712. }.bind(this));
  713. }
  714. };
  715. /**
  716. * 弹出提示框
  717. * @param msg 消息
  718. */
  719. WechatTool.prototype.showToast = function (msg) {
  720. if (PlatUtils_1.default.IsWechat) {
  721. //@ts-ignore
  722. wx.showToast({
  723. title: msg,
  724. icon: "none",
  725. duration: 2000,
  726. success: function (res) {
  727. console.log("" + res);
  728. },
  729. fail: function (res) {
  730. console.log("showToast\u8C03\u7528\u5931\u8D25");
  731. }
  732. });
  733. }
  734. };
  735. /**
  736. * 上报归因事件
  737. * @param eventType 事件类型 0 激活, 25:关键行为
  738. * @param eventValue 事件描述
  739. */
  740. WechatTool.prototype.reportAttributedEvent = function (eventType, eventKey, eventValue) {
  741. if (this._luanchType != "1069" && this._luanchType != "1036" && this._luanchType != "1065") {
  742. Utils_1.utils.showLog("luanchType=" + this._luanchType + ",不进行归因上报");
  743. return;
  744. }
  745. Utils_1.utils.showLog("上报归因事件: #eventType=" + eventType + "#eventKey=" + eventKey + "#eventValue=" + eventValue);
  746. if (eventType == YZ_Constant_1.AttributedType.Active && YZ_LocalStorage_1.default.getItem(YZ_Constant_1.default.ST_IS_REPORT_USER_ACTIVE)) {
  747. Utils_1.utils.showLog("用户已经上报过激活,不再进行激活上报!");
  748. return;
  749. }
  750. else if (eventType == YZ_Constant_1.AttributedType.GameAddiction && YZ_LocalStorage_1.default.getItem(YZ_Constant_1.default.ST_IS_REPORT_GAME_ADDICTION)) {
  751. Utils_1.utils.showLog("用户已经上报过关键行为,不再进行激活上报!");
  752. return;
  753. }
  754. var xhr = new XMLHttpRequest();
  755. xhr.timeout = 15000; // 单位毫秒
  756. var data = {};
  757. data.app_id = Utils_1.utils.config.wechatconfig.appID;
  758. data.channel = "wechat_kyx";
  759. data.uid = this._service_uid;
  760. data.event_type = eventType;
  761. data.event_key = eventKey;
  762. data.event_value = eventValue;
  763. data.app_version = Utils_1.utils.config.wechatconfig.version;
  764. data.code = this.wx_code;
  765. // data.clue_token = this.clue_token;
  766. data.luanchDate = encodeURI(this._luanchData);
  767. Utils_1.utils.showLog("归因请求参数:" + JSON.stringify(data));
  768. var requestData = JSON.stringify(data);
  769. Utils_1.utils.showLog("归因 > json_data=" + Utils_1.utils.aesEncrypt(requestData));
  770. var requestUrl = "https://track.youletd.com/wechatcounterpart/dockingreturn?json=" + Utils_1.utils.aesEncrypt(requestData) + "&time_stamp=" + (new Date()).getTime();
  771. Utils_1.utils.showLog("归因服务器地址:" + requestUrl);
  772. xhr.open('GET', requestUrl);
  773. xhr.send();
  774. xhr.onreadystatechange = function () {
  775. Utils_1.utils.showLog("请求状态改变, reaedyState=", xhr.readyState, "; status=", xhr.status);
  776. if (xhr.readyState == 4) {
  777. if (xhr.status == 200) {
  778. if (eventType == YZ_Constant_1.AttributedType.Active)
  779. YZ_LocalStorage_1.default.setItem(YZ_Constant_1.default.ST_IS_REPORT_USER_ACTIVE, "true");
  780. if (eventType == YZ_Constant_1.AttributedType.GameAddiction)
  781. YZ_LocalStorage_1.default.setItem(YZ_Constant_1.default.ST_IS_REPORT_GAME_ADDICTION, "true");
  782. Utils_1.utils.showLog("归因上报成功:", xhr.responseText);
  783. }
  784. else {
  785. Utils_1.utils.showLog("归因上报失败!");
  786. }
  787. }
  788. };
  789. xhr.ontimeout = function () {
  790. Utils_1.utils.showLog("归因请求超时!");
  791. };
  792. xhr.onerror = function (err) {
  793. Utils_1.utils.showLog("归因请求失败!");
  794. };
  795. };
  796. /**
  797. * 友盟游戏开始上报
  798. * @param levelID
  799. */
  800. WechatTool.prototype.umaOnStart = function (levelID) {
  801. if (!this.checkUmeng())
  802. return;
  803. //@ts-ignore
  804. wx.uma.stage.onStart({
  805. stageId: levelID,
  806. stageName: "\u7B2C" + levelID + "\u5173" // 关卡id
  807. });
  808. };
  809. /**
  810. *
  811. * @returns 是否开启友盟
  812. */
  813. WechatTool.prototype.checkUmeng = function () {
  814. //@ts-ignore
  815. if (!wx.uma) {
  816. Utils_1.utils.showLog("未对接友盟SDK");
  817. return false;
  818. }
  819. return true;
  820. };
  821. /**
  822. * 友盟结算上报
  823. * @param levelID 关卡id
  824. * @param stageId
  825. * @param event
  826. */
  827. WechatTool.prototype.umaReportedLevel = function (levelID, event) {
  828. if (!this.checkUmeng())
  829. return;
  830. //@ts-ignore
  831. wx.uma.stage.onEnd({
  832. stageId: levelID,
  833. stageName: "\u7B2C" + levelID + "\u5173",
  834. event: event
  835. });
  836. };
  837. /**
  838. * 友盟自定义事件
  839. * @param eventId 事件ID,注意:事件ID必须要在后台配置
  840. * @param params 事件内容
  841. */
  842. WechatTool.prototype.umaTrackEvent = function (eventId, params) {
  843. if (!this.checkUmeng())
  844. return;
  845. //@ts-ignore
  846. wx.uma.trackEvent(eventId, params);
  847. };
  848. WechatTool = __decorate([
  849. ccclass
  850. ], WechatTool);
  851. return WechatTool;
  852. }());
  853. exports.default = WechatTool;
  854. cc._RF.pop();