HttpMgr.ts 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. import { EVENT_TYPE } from "../../game/data/GameData";
  2. import MKSwitch from "../../mk/MKSwitch";
  3. import GameConst from "../data/GameConst";
  4. import PlayerConst, { DATA_STORAGE_KEY } from "../data/PlayerConst";
  5. import DataMgr from "./DataMgr";
  6. import EffectMgr from "./EffectMgr";
  7. import GameMgr, { UI_NAME } from "./GameMgr";
  8. import StorageMgr from "./StorageMgr";
  9. const { ccclass, property } = cc._decorator;
  10. @ccclass
  11. export default class HttpMgr {
  12. //单例模式---------------------------
  13. private static instance: HttpMgr = null;
  14. public static get Inst(): HttpMgr {
  15. if (!HttpMgr.instance) {
  16. HttpMgr.instance = new HttpMgr();
  17. }
  18. return HttpMgr.instance;
  19. }
  20. /**
  21. * 获取request
  22. * @param url url
  23. * @param bodyData 数据
  24. * @param httpMethod method 默认GET,可以选择POST
  25. */
  26. httpRquest(url: string, bodyData: any = null, httpMethod: HTTP_METHOD): Promise<any> {
  27. if (!GameConst.ifConnectService) {
  28. mk.console.log("[HttpMgr] 不连接服务器开关打开");
  29. return new Promise((resolve, reject) => { reject() });
  30. return;
  31. }
  32. // LogUtil.log("[HttpMgr] 连接服务器" + url);
  33. // LogUtil.log("[getRequest] url", url);
  34. // LogUtil.log("[getRequest] bodyData", bodyData);
  35. return new Promise((resolve, reject) => {
  36. var request = cc.loader.getXMLHttpRequest();
  37. request.open(httpMethod, url, true)
  38. request.onerror = (err) => {
  39. mk.console.error(`${HTTP_METHOD[httpMethod]} 错误 onerror`);
  40. EffectMgr.Inst.addTip("您的网络发生异常,请检查下哈");
  41. reject(err);
  42. }
  43. request.ontimeout = (err) => {
  44. mk.console.error(`${HTTP_METHOD[httpMethod]} 超时 ontimeout`);
  45. reject(err);
  46. }
  47. request.onloadend = () => {
  48. let respone = request.response;
  49. if (!respone) {
  50. mk.console.error("[getRequest] 获取用户信息错误 ", request);
  51. reject();
  52. }
  53. let info;
  54. if (typeof (respone) === "string") {
  55. info = JSON.parse(respone);
  56. }
  57. else {
  58. info = respone;
  59. }
  60. // LogUtil.log("info", info);
  61. // LogUtil.log("url.split",url.split("/"))
  62. // LogUtil.log(`[HttpMgr] ${HTTP_METHOD[httpMethod]}:${url.split("/")[url.split("/").length - 1]}`, info.errmsg)
  63. if (info.errcode === 0) {
  64. resolve(info)
  65. }
  66. else {
  67. if (info.errcode === -20003) {
  68. GameMgr.Inst.sendEvent("Error", `服务器发生-20003错误`);
  69. }
  70. reject(info);
  71. }
  72. }
  73. let headers = ["Content-Type", "application/json;charset=UTF-8"];
  74. for (var i = 0; i < headers.length; i += 2) {
  75. request.setRequestHeader(headers[i], headers[i + 1]);
  76. }
  77. //POST方式:跟后端了解,可以没有BodyData参数;GET一定没有BodyData参数
  78. if (bodyData) {
  79. request.send(bodyData)
  80. }
  81. else {
  82. request.send()
  83. }
  84. })
  85. }
  86. //----------------------------接口操作-------------------------------------
  87. /**上线
  88. * 如果有key 或者 uin 直接进入AccountInfo
  89. * 如果没有 连接
  90. */
  91. online() {
  92. //这个需要打印
  93. console.log("[HttpMgr] GameConst.url_service", GameConst.url_service);
  94. if (GameConst.session_key != '' && GameConst.uin != '') {
  95. mk.console.log("[HttpMgr] online 已经有缓存信息");
  96. HttpMgr.instance.getUserInfo();
  97. }
  98. else {
  99. mk.console.log("[HttpMgr] online 新用户");
  100. HttpMgr.instance.connect();
  101. }
  102. }
  103. /**连接
  104. * 获取零食的tmp_uin
  105. */
  106. connect() {
  107. // GameMgr.Inst.sendEvent(UI_NAME.Loading, "开始加载");
  108. let url = `${GameConst.url_service}/connect`;
  109. let bodyData = {
  110. "psk": GameConst.randomKey,
  111. "appid": GameConst.appid
  112. }
  113. mk.console.log("[connect]bodyData", bodyData)
  114. let encryptData = mk.encrypt.rsaEncrypt(JSON.stringify(bodyData));
  115. this.httpRquest(url, encryptData, HTTP_METHOD.POST).then((responseData) => {
  116. let decryptData = mk.encrypt.decrypt(responseData.encrypt, GameConst.randomKey, GameConst.appid);
  117. mk.console.log("[HttpMgr] connect", decryptData);
  118. if (decryptData) {
  119. GameConst.tmp_uin = JSON.parse(decryptData).tmp_uin;
  120. StorageMgr.Inst.setStorage(DATA_STORAGE_KEY.uin, GameConst.tmp_uin);
  121. HttpMgr.instance.wxlogin();
  122. //星云关键埋点
  123. GameMgr.Inst.sendEventCp(`${UI_NAME.Loading}1`, "开始加载");
  124. }
  125. }).catch((err) => {
  126. mk.console.log("[HttpMgr] connect err", err);
  127. });
  128. }
  129. /**微信登陆
  130. * @param ifWxAuthCallBack 是否授权回调
  131. */
  132. wxlogin(ifWxAuthCallBack: boolean = false) {
  133. mk.console.log("[Httpmgr] wxlogin 微信登陆");
  134. let url = `${GameConst.url_service}/wxlogin`;
  135. let data = {
  136. "code": GameConst.wxCode,
  137. "timestamp": Math.floor(new Date().getTime() * 0.001)
  138. }
  139. let bodydata = null;
  140. let encryptInfo = null
  141. let uin = GameConst.wxCode == '' ? GameConst.tmp_uin : GameConst.uin;
  142. let encryptKey = GameConst.wxCode == "" ? GameConst.randomKey : GameConst.session_key;
  143. encryptInfo = mk.encrypt.encrypt(JSON.stringify(data), encryptKey, GameConst.appid);
  144. bodydata = {
  145. "uin": uin,
  146. "encrypt": encryptInfo
  147. }
  148. // LogUtil.log("[Httpmgr] wxlogin uin",uin);
  149. // LogUtil.log("[Httpmgr] wxlogin encryptKey",encryptKey);
  150. // LogUtil.log("[Httpmgr] wxlogin encryptInfo",encryptInfo);
  151. // return;
  152. this.httpRquest(url, JSON.stringify(bodydata), HTTP_METHOD.POST).then((responseData) => {
  153. /**解密的数据 */
  154. let decryptData = null;
  155. if (GameConst.wxCode != '') {
  156. decryptData = mk.encrypt.decrypt(responseData.encrypt, GameConst.session_key, GameConst.appid)
  157. }
  158. else {
  159. decryptData = mk.encrypt.decrypt(responseData.encrypt, GameConst.randomKey, GameConst.appid)
  160. }
  161. if (decryptData) {
  162. let data_parse = JSON.parse(decryptData);
  163. //【测试状态】不发送事件,就不打印日志
  164. mk.console.log("[HttpMgr] wxlogin", data_parse);
  165. GameConst.login_ticket = data_parse.login_ticket
  166. /**加密的数据 */
  167. let encrypt_login_ticket =mk.encrypt.encrypt(GameConst.login_ticket, GameConst.localEncryptKey, GameConst.appid)
  168. StorageMgr.Inst.setStorage(DATA_STORAGE_KEY.login_ticket, encrypt_login_ticket);
  169. if (GameConst.uin != data_parse.uin) {
  170. GameConst.uin = data_parse.uin
  171. // GameConst.isNew = true; //根本无须判定,getDetailInfo那边会鉴定
  172. }
  173. else {
  174. // GameConst.isNew = false;//根本无须判定,getDetailInfo那边会鉴定
  175. }
  176. mk.console.log("GameConst.isNew", GameConst.isNew);
  177. mk.console.log("GameConst.uin", GameConst.uin);
  178. let encrypt_uin = mk.encrypt.encrypt(GameConst.uin, GameConst.localEncryptKey, GameConst.appid)
  179. StorageMgr.Inst.setStorage(DATA_STORAGE_KEY.uin, encrypt_uin)
  180. HttpMgr.instance.checklogin(ifWxAuthCallBack);
  181. }
  182. }).catch((err) => {
  183. mk.console.log("[HttpMgr] wxlogin err", err);
  184. });
  185. }
  186. checklogin(ifWxAuthCallBack: boolean = false) {
  187. let url = `${GameConst.url_service}/checklogin`;
  188. let tmp_key
  189. if (GameConst.wxCode != '') {
  190. tmp_key = GameConst.session_key
  191. }
  192. else {
  193. tmp_key = mk.encrypt.randomString()
  194. GameConst.randomKey_new = tmp_key
  195. }
  196. let bodyData =
  197. {
  198. "tmp_key": tmp_key,
  199. "uin": GameConst.uin,
  200. "login_ticket": GameConst.login_ticket,
  201. "appid": GameConst.appid
  202. }
  203. let rasEncryptData = mk.encrypt.rsaEncrypt(JSON.stringify(bodyData));
  204. this.httpRquest(url, rasEncryptData, HTTP_METHOD.POST).then((responseData) => {
  205. let decryptData = null;
  206. if (GameConst.wxCode != '') {
  207. decryptData = mk.encrypt.decrypt(responseData.encrypt, GameConst.session_key, GameConst.appid)
  208. }
  209. else {
  210. decryptData = mk.encrypt.decrypt(responseData.encrypt, GameConst.randomKey_new, GameConst.appid)
  211. }
  212. mk.console.log("[HttpMgr] checklogin", decryptData);
  213. if (decryptData) {
  214. let data_parse = JSON.parse(decryptData)
  215. GameConst.session_key = data_parse.session_key
  216. let encrypt_session_key = mk.encrypt.encrypt(GameConst.session_key, GameConst.localEncryptKey, GameConst.appid)
  217. StorageMgr.Inst.setStorage(DATA_STORAGE_KEY.session_key, encrypt_session_key)
  218. HttpMgr.Inst.getUserInfo(ifWxAuthCallBack);
  219. }
  220. }).catch((err) => {
  221. mk.console.log("[HttpMgr] checklogin err", err);
  222. });;
  223. }
  224. /**获取账户信息 */
  225. getUserInfo(ifWxAuthCallBack: boolean = false) {
  226. let url = `${GameConst.url_service}/user`;
  227. let data =
  228. {
  229. "uin": GameConst.uin,
  230. "login_ticket": GameConst.login_ticket,
  231. "timestamp": Math.floor(new Date().getTime() * 0.001)
  232. }
  233. mk.console.log("getUserInfo", data);
  234. let encryptData = mk.encrypt.encrypt(JSON.stringify(data), GameConst.session_key, GameConst.appid)
  235. let bodyData = {
  236. "uin": GameConst.uin,
  237. "encrypt": encryptData
  238. }
  239. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  240. let data = this.parseResponseData(responseData)
  241. mk.console.log("[HttpMgr] user", data);
  242. // Object.keys(data).forEach((e) => {
  243. // LogUtil.log("[HttpMgr]1 " + e);
  244. // LogUtil.log("[HttpMgr]2 " + data[e]);
  245. // });
  246. if (data) {
  247. GameConst.isNew = data["is_new_user"];
  248. mk.console.log("[HttpMgr] user GameConst.isNew", GameConst.isNew);
  249. //新手 初始化引导
  250. if (GameConst.isNew) {
  251. }
  252. else {
  253. }
  254. // LogUtil.log("data.is_new_user", data.is_new_user);
  255. GameConst.isAuth = data["is_auth"]
  256. mk.console.log("GameConst.isAuth", GameConst.isAuth);
  257. //是否首次登陆
  258. PlayerConst.ifFirstLoginToday = data["is_first_login_today"];
  259. if (data.is_auth) {
  260. let encrypt_auth = mk.encrypt.encrypt('true', GameConst.localEncryptKey, GameConst.appid)
  261. StorageMgr.Inst.setStorage(DATA_STORAGE_KEY.isAuth, encrypt_auth)
  262. PlayerConst.headImgUrl = data["HeadImgUrl"];
  263. PlayerConst.nickName = data["NickName"];
  264. let nickname = StorageMgr.Inst.getStorage(DATA_STORAGE_KEY.nickname);
  265. if (!!nickname) {
  266. //改名了
  267. if (nickname != data["nickname"]) {
  268. StorageMgr.Inst.setStorage(DATA_STORAGE_KEY.nickname, PlayerConst.nickName);
  269. }
  270. }
  271. //原本放在初始化 首页、提现、用户信息的头像取消,采用分发事件代替
  272. HttpMgr.Inst.getDetailInfo(ifWxAuthCallBack)
  273. }
  274. else {
  275. HttpMgr.Inst.getDetailInfo(ifWxAuthCallBack)
  276. }
  277. }
  278. }).catch((err) => {
  279. mk.console.log("[HttpMgr] user err", err);
  280. });
  281. }
  282. //-----------------------------------游戏相关接口---------------------------------
  283. /**获取细节信息 */
  284. getDetailInfo(ifWxAuthCallBack: boolean = false) {
  285. // LogUtil.log("[HttpMgr] getDetailInfo");
  286. let url = `${GameConst.url_service}/game/details`;
  287. let data = {}
  288. let bodyData = this.encryptBodyData(data);
  289. // LogUtil.log("[HttpMgr] getDetailInfo", bodyData);
  290. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  291. let data = this.parseResponseData(responseData);
  292. mk.console.log("[HttpMgr] details", data);
  293. //解析获取的信息
  294. PlayerConst.getDetailInfo(data);
  295. //如果微信登陆回调返回
  296. if (ifWxAuthCallBack) {
  297. //检测是否刷新一下数据
  298. if (PlayerConst.ifFirstLoginToday) {
  299. if (PlayerConst.cheeckIfNewAuthUser(data.register_date)) {
  300. mk.console.log("[WxAuth] 首日新用户")
  301. }
  302. else {
  303. mk.console.log("[WxAuth] 首日登陆老用户")
  304. DataMgr.Inst.resteData();
  305. }
  306. }
  307. else {
  308. mk.console.log("[WxAuth] 非首日登陆")
  309. }
  310. console.log("Detail 分发 微信授权事件");
  311. //分发微信授权返回事件
  312. mk.event.emit(EVENT_TYPE.BACK_WxAuth);
  313. //初始化游戏 Game
  314. //初始化结束按钮 gameOverUI
  315. //初始化每日提现 node_dailyCashOutUI
  316. }
  317. else {
  318. if (PlayerConst.ifFirstLoginToday) {
  319. PlayerConst.ifFirstLoginToday = false;
  320. DataMgr.Inst.resteData();
  321. }
  322. }
  323. /**是否登陆结束 */
  324. GameConst.ifLoginFinished = true;
  325. }).catch((err) => {
  326. mk.console.log("[HttpMgr] details", err);
  327. });
  328. }
  329. /**道具信息 */
  330. propsInfo() {
  331. let url = `${GameConst.url_service}/game/propsInfo`;
  332. let data = {}
  333. let bodyData = this.encryptBodyData(data);
  334. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  335. let data = this.parseResponseData(responseData);
  336. mk.console.log("[HttpMgr] propsInfo", data);
  337. });
  338. }
  339. /**道具
  340. * @param handel_type 处理类型 1 增 2 减
  341. * @param prop_code 道具编号
  342. * @param prop_num 道具数量
  343. */
  344. prop(handel_type: number, prop_code: string, prop_num: number): Promise<any> {
  345. mk.console.log("prop_code", prop_code);
  346. let url = `${GameConst.url_service}/game/prop`;
  347. let data = {
  348. "handel_type": handel_type,
  349. "prop_code": prop_code,
  350. "count": prop_num
  351. }
  352. let bodyData = this.encryptBodyData(data);
  353. return new Promise((resolve, reject) => {
  354. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  355. let data = this.parseResponseData(responseData);
  356. mk.console.log("[HttpMgr] prop", data);
  357. }).catch((err) => {
  358. reject(err);
  359. });
  360. })
  361. }
  362. //-----------------------------------红包关接口-----------------------------------
  363. /**新手红包
  364. * @param:get_type 获取类型 0 普通 1 双倍
  365. * @param:pass_no 关卡数
  366. * @param:red_mondey 红包数目
  367. */
  368. newerRedPacket(get_type: number, pass_no: number, red_mondey: number): Promise<any> {
  369. let url = `${GameConst.url_service}/red-package/newer`;
  370. let data = {
  371. "get_type": get_type,
  372. "pass_no": pass_no,
  373. "red_money": red_mondey
  374. }
  375. // LogUtil.log("data",data);
  376. let bodyData = this.encryptBodyData(data);
  377. return new Promise((resolve, reject) => {
  378. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  379. let data = this.parseResponseData(responseData);
  380. mk.console.log("[HttpMgr] newerRedPacket", data);
  381. }).catch((err) => {
  382. reject(err);
  383. });
  384. })
  385. }
  386. /**登陆红包
  387. * @param:get_type 获取类型 0 普通 1 双倍
  388. * @param:pass_no 关卡数
  389. * @param:red_mondey 红包数目
  390. */
  391. loginRedPacket(get_type: number, pass_no: number, red_mondey: number): Promise<any> {
  392. let url = `${GameConst.url_service}/red-package/login`;
  393. let data = {
  394. "get_type": get_type,
  395. "pass_no": pass_no,
  396. "red_money": red_mondey
  397. }
  398. // LogUtil.log("data",data);
  399. let bodyData = this.encryptBodyData(data);
  400. return new Promise((resolve, reject) => {
  401. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  402. let data = this.parseResponseData(responseData);
  403. mk.console.log("[HttpMgr] loginRedPacket", data);
  404. }).catch((err) => {
  405. reject(err);
  406. });
  407. })
  408. }
  409. /**通关红包
  410. * @param:get_type 获取类型 0 普通 1 双倍
  411. * @param:pass_no 关卡数
  412. * @param:red_mondey 红包数目
  413. */
  414. passRedPacket(get_type: number, pass_no: number, red_mondey: number): Promise<any> {
  415. let url = `${GameConst.url_service}/red-package/pass`;
  416. let data = {
  417. "get_type": get_type,
  418. "pass_no": pass_no,
  419. "red_money": red_mondey
  420. }
  421. // LogUtil.log("data",data);
  422. let bodyData = this.encryptBodyData(data);
  423. return new Promise((resolve, reject) => {
  424. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  425. let data = this.parseResponseData(responseData);
  426. mk.console.log("[HttpMgr] passRedPacket", data);
  427. }).catch((err) => {
  428. reject(err);
  429. });
  430. })
  431. }
  432. /**随机红包
  433. * @param:get_type 获取类型 0 普通 1 双倍
  434. * @param:pass_no 关卡数
  435. * @param:red_mondey 红包数目
  436. */
  437. randomRedPacket(get_type: number, pass_no: number, red_mondey: number): Promise<any> {
  438. let url = `${GameConst.url_service}/red-package/random`;
  439. let data = {
  440. "get_type": get_type,
  441. "pass_no": pass_no,
  442. "red_money": red_mondey
  443. }
  444. // LogUtil.log("data",data);
  445. let bodyData = this.encryptBodyData(data);
  446. return new Promise((resolve, reject) => {
  447. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  448. let data = this.parseResponseData(responseData);
  449. mk.console.log("[HttpMgr] randomRedPacket", data);
  450. }).catch((err) => {
  451. reject(err);
  452. });
  453. })
  454. }
  455. //-----------------------------------提现接口-----------------------------------
  456. /**提现
  457. * @param amount 提现数额
  458. * @param cash_type 提现类型
  459. */
  460. cash(amount: number, cash_type: number): Promise<any> {
  461. let url = `${GameConst.url_service}/cash/getCash`;
  462. let data = {
  463. "amount": amount,
  464. "cash_type": cash_type
  465. }
  466. let bodyData = this.encryptBodyData(data);
  467. mk.console.log("cash_type", cash_type);
  468. return new Promise((resolve, reject) => {
  469. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  470. let data = this.parseResponseData(responseData);
  471. mk.console.log("[HttpMgr] cash", data);
  472. resolve(data);
  473. }).catch((err) => {
  474. reject(err);
  475. });
  476. })
  477. }
  478. caseRecords(): Promise<any> {
  479. let url = `${GameConst.url_service}/cash/records`;
  480. let data = {
  481. }
  482. let bodyData = this.encryptBodyData(data);
  483. return new Promise((resolve, reject) => {
  484. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  485. let data = this.parseResponseData(responseData);
  486. mk.console.log("[HttpMgr] cash/records", data);
  487. }).catch((err) => {
  488. reject(err);
  489. });
  490. })
  491. }
  492. //互推位相关接口--------------------------------------------------------------------
  493. /**抽奖 */
  494. public recommendList(): Promise<any> {
  495. let url = `${GameConst.url_service}/fission/recommendList`;
  496. let data = {
  497. }
  498. let bodyData = this.encryptBodyData(data);
  499. return new Promise((resolve, reject) => {
  500. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  501. let data = this.parseResponseData(responseData);
  502. mk.console.log("[HttpMgr] fission/recommendList", data);
  503. resolve(data);
  504. }).catch((err) => {
  505. mk.console.log("[HttpMgr] fission/recommendList", err);
  506. reject(err);
  507. });
  508. })
  509. }
  510. /**抽奖 */
  511. public appDownloadLog(downloadAppId: string): Promise<any> {
  512. let url = `${GameConst.url_service}/fission/appDownloadLog`;
  513. let data = {
  514. "downloadAppId": downloadAppId,
  515. }
  516. let bodyData = this.encryptBodyData(data);
  517. return new Promise((resolve, reject) => {
  518. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  519. let data = this.parseResponseData(responseData);
  520. mk.console.log("[HttpMgr] fission/appDownloadLog", data);
  521. resolve(data);
  522. }).catch((err) => {
  523. mk.console.log("[HttpMgr] fission/appDownloadLog", err);
  524. reject(err);
  525. });
  526. })
  527. }
  528. //API回传--------------------------------------------------------------------------
  529. /**更新设备信息 */
  530. public updateDeviceInfo(deviceInfo: any) {
  531. let url = `${GameConst.url_service}/user/machine`;
  532. let data = deviceInfo;
  533. let bodyData = this.encryptBodyData(data);
  534. return new Promise((resolve, reject) => {
  535. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  536. let data = this.parseResponseData(responseData);
  537. mk.console.log("[HttpMgr] user/machine", data);
  538. }).catch((err) => {
  539. reject(err);
  540. mk.console.log("[HttpMgr] user/machine err", err);
  541. });
  542. })
  543. }
  544. /**巨量API回传
  545. * @param type 0 激活 6 次留
  546. */
  547. public juliangAPIActive(type: number) {
  548. if (cc.sys.os != cc.sys.OS_ANDROID) {
  549. return;
  550. }
  551. mk.console.log("巨量回传 type", type);
  552. let url = `${GameConst.url_service}/ad/oe/callback/activate`;
  553. let data = {
  554. "event_type": type,
  555. "source": GameConst.appVersion + ';'
  556. }
  557. let bodyData = this.encryptBodyData(data);
  558. return new Promise((resolve, reject) => {
  559. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  560. let data = this.parseResponseData(responseData);
  561. mk.console.log("[HttpMgr] juliangcallback/activate", data);
  562. }).catch((err) => {
  563. mk.console.log("[HttpMgr] juliangcallback/activate err", err);
  564. reject(err);
  565. });
  566. })
  567. }
  568. //星云统计--------------------------------------------------------------------
  569. /** 向星云发送事件埋点 */
  570. public sendEventCp(eventKey: string, eventDes: string) {
  571. mk.console.log('seventKey ', eventKey);
  572. mk.console.log('eventDes ', eventDes)
  573. //使用
  574. let uin = !!GameConst.uin ? GameConst.uin : GameConst.tmp_uin;
  575. if (!uin) {
  576. mk.console.log("没有Uin,不执行");
  577. return;
  578. }
  579. let url = `${GameConst.url_service}/cp`;
  580. let isFirstDay = PlayerConst.loginDay == 1 ? 1 : 0;
  581. let data = {
  582. "appid": GameConst.appid,
  583. "uid": uin,
  584. "isFirstDay": isFirstDay, // 0 1 是第一天
  585. "umengChannel": GameConst.umengChannel,
  586. "deviceType": GameConst.deviceType,
  587. "androidVerison": GameConst.androidVersion,
  588. "version": GameConst.appVersion,
  589. "point": eventKey,
  590. "description": eventDes
  591. }
  592. // LogUtil.log("[HttpMgr] sendEventCp data", data);
  593. let bodyData = this.encryptBodyData(data);
  594. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  595. let data = this.parseResponseData(responseData);
  596. mk.console.log("[HttpMgr] sendEventCp", data);
  597. }).catch((err) => {
  598. mk.console.log("[HttpMgr] sendEventCp err", err);
  599. });
  600. }
  601. updateGameVersion(version: string) {
  602. let url = `${GameConst.url_service}/game/updateVersion`;
  603. let data = {
  604. "version": version,
  605. }
  606. let bodyData = this.encryptBodyData(data);
  607. return new Promise((resolve, reject) => {
  608. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  609. let data = this.parseResponseData(responseData);
  610. mk.console.log("[HttpMgr] game/updateVersion", data);
  611. }).catch((err) => {
  612. mk.console.log("[HttpMgr] game/updateVersion err", err);
  613. reject(err);
  614. });
  615. })
  616. }
  617. /** 更新观看广告次数
  618. */
  619. updateVideoTimes(placementId, adunit_format, network_type, network_placement_id,
  620. network_firm_id, adsource_id, adsource_index, adsource_price,
  621. adsource_isheaderbidding, publisher_revenue, precision, ecpm_level) {
  622. if (!GameConst.ifShowAd) {
  623. return;
  624. }
  625. let url = `${GameConst.url_service}/video/updateVideo`;
  626. let data = {
  627. "imei": GameConst.imei,
  628. "idfa": GameConst.idfa,
  629. "oaid": GameConst.oaid,
  630. "uin": GameConst.uin,
  631. "version": GameConst.appVersion,
  632. "tf_channel": GameConst.tf_channel,
  633. "destoon_ad_place": placementId,
  634. "ad_type": 1,
  635. "adunit_format": adunit_format,
  636. "network_type": network_type,
  637. "network_placement_id": network_placement_id,
  638. "network_firm_id": network_firm_id,
  639. "adsource_id": adsource_id,
  640. "adsource_index": adsource_index,
  641. "adsource_price": adsource_price,
  642. "adsource_isheaderbidding": adsource_isheaderbidding,
  643. "publisher_revenue": publisher_revenue,
  644. "precision": precision,
  645. "ecpm_level": ecpm_level
  646. }
  647. mk.console.log("[HttpMgr] updateVideoTimes data", data);
  648. Object.keys(data).forEach((key) => {
  649. mk.console.log(`updateVideo ${key}`, data[key]);
  650. })
  651. let bodyData = this.encryptBodyData(data);
  652. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  653. let data = this.parseResponseData(responseData);
  654. mk.console.log("[HttpMgr] updateVideoTimes ok", data);
  655. }).catch((err) => {
  656. mk.console.log("[HttpMgr] updateVideoTimes err", err);
  657. });
  658. }
  659. /** 更新其他广告次数
  660. * @param type 1:激励视频成功、 2:插屏、3:信息流 4 观看视频
  661. */
  662. public updateADLog(type, placementId, adunit_format, network_type, network_placement_id,
  663. network_firm_id, adsource_id, adsource_index, adsource_price,
  664. adsource_isheaderbidding, publisher_revenue, precision, ecpm_level) {
  665. if (!GameConst.ifShowAd) {
  666. return;
  667. }
  668. let url = `${GameConst.url_service}/video/updateADLog`;
  669. let data = {
  670. "imei": GameConst.imei,
  671. "idfa": GameConst.idfa,
  672. "oaid": GameConst.oaid,
  673. "uin": GameConst.uin,
  674. "version": GameConst.appVersion,
  675. "tf_channel": GameConst.tf_channel,
  676. "destoon_ad_place": placementId,
  677. "ad_type": type,
  678. "adunit_format": adunit_format,
  679. "network_type": network_type,
  680. "network_placement_id": network_placement_id,
  681. "network_firm_id": network_firm_id,
  682. "adsource_id": adsource_id,
  683. "adsource_index": adsource_index,
  684. "adsource_price": adsource_price,
  685. "adsource_isheaderbidding": adsource_isheaderbidding,
  686. "publisher_revenue": publisher_revenue,
  687. "precision": precision,
  688. "ecpm_level": ecpm_level
  689. }
  690. mk.console.log("[HttpMgr] updateADLog data", data);
  691. let bodyData = this.encryptBodyData(data);
  692. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  693. let data = this.parseResponseData(responseData);
  694. mk.console.log("[HttpMgr] updateADLog ok", data);
  695. }).catch((err) => {
  696. mk.console.log("[HttpMgr] updateADLog err", err);
  697. });
  698. }
  699. //-----------------------------------加解密-----------------------------------
  700. /**加密参数 */
  701. private encryptBodyData(data: any): any {
  702. let encryptBodyData: any = null;
  703. let encryptData: any = null;
  704. //LogUtil.log("GameConst.tmp_uin",GameConst.tmp_uin);
  705. data.timestamp = Math.floor(new Date().getTime() * 0.001);
  706. if (GameConst.isAuth) {
  707. encryptData = mk.encrypt.encrypt(JSON.stringify(data), GameConst.session_key, GameConst.appid);
  708. encryptBodyData = {
  709. "uin": GameConst.uin,
  710. "encrypt": encryptData
  711. }
  712. }
  713. else {
  714. encryptData = mk.encrypt.encrypt(JSON.stringify(data), GameConst.session_key, GameConst.appid);
  715. encryptBodyData = {
  716. "uin": GameConst.tmp_uin,
  717. "encrypt": encryptData
  718. }
  719. }
  720. return encryptBodyData;
  721. }
  722. /**
  723. * 解析返回的数据
  724. * @param response 返回数据
  725. * @param ifDataClear 是否数据是明文发送 默认暗文
  726. */
  727. private parseResponseData(response: any, ifDataClear: boolean = false): any {
  728. let data = null
  729. if (ifDataClear) {
  730. data = response.data
  731. }
  732. else {
  733. if (response.encrypt) {
  734. data = mk.encrypt.decrypt(response.encrypt, GameConst.session_key, GameConst.appid)
  735. data = JSON.parse(data)
  736. }
  737. else if (response.data) {
  738. data = response.data
  739. }
  740. }
  741. return data
  742. }
  743. }
  744. export enum HTTP_METHOD {
  745. GET = "GET",
  746. POST = "POST"
  747. }
  748. export enum HTTP_TYPE {
  749. connect = "connect",
  750. recommendList = "recommendList"
  751. }
  752. //通关接口ERR CODE
  753. export enum PASS_ERR_CODE {
  754. /** 关卡号不正*/
  755. PASSNO_INCORRECT = 9001,
  756. /**关卡号不能为空或者小于1 */
  757. PASSNO_NULL = 9002,//
  758. /***获取积分异常(积分传参非法)*/
  759. SCORE_PARAM_ILLEGAL = 9003,
  760. /**已达单关积分获取次数*/
  761. SCORE_OVER_TIMES = 9004,
  762. /**已达今日积分上限*/
  763. SCORE_REACH_LIMIT = 9005
  764. }