HttpMgr.ts 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  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 CashOutUI from "../view/CashOutUI";
  6. import DataMgr from "./DataMgr";
  7. import EffectMgr from "./EffectMgr";
  8. import GameMgr, { UI_NAME } from "./GameMgr";
  9. import StorageMgr from "./StorageMgr";
  10. const { ccclass, property } = cc._decorator;
  11. @ccclass
  12. export default class HttpMgr {
  13. //单例模式---------------------------
  14. private static instance: HttpMgr = null;
  15. public static get Inst(): HttpMgr {
  16. if (!HttpMgr.instance) {
  17. HttpMgr.instance = new HttpMgr();
  18. }
  19. return HttpMgr.instance;
  20. }
  21. /**
  22. * 获取request
  23. * @param url url
  24. * @param bodyData 数据
  25. * @param httpMethod method 默认GET,可以选择POST
  26. */
  27. httpRquest(url: string, bodyData: any = null, httpMethod: HTTP_METHOD): Promise<any> {
  28. if (!GameConst.ifConnectService) {
  29. mk.console.log("[HttpMgr] 不连接服务器开关打开");
  30. return new Promise((resolve, reject) => { reject() });
  31. return;
  32. }
  33. // LogUtil.log("[HttpMgr] 连接服务器" + url);
  34. // LogUtil.log("[getRequest] url", url);
  35. // LogUtil.log("[getRequest] bodyData", bodyData);
  36. return new Promise((resolve, reject) => {
  37. var request = cc.loader.getXMLHttpRequest();
  38. request.open(httpMethod, url, true)
  39. request.onerror = (err) => {
  40. mk.console.error(`${HTTP_METHOD[httpMethod]} 错误 onerror`);
  41. EffectMgr.Inst.addTip("您的网络发生异常,请检查下哈");
  42. reject(err);
  43. }
  44. request.ontimeout = (err) => {
  45. mk.console.error(`${HTTP_METHOD[httpMethod]} 超时 ontimeout`);
  46. reject(err);
  47. }
  48. request.onloadend = () => {
  49. let respone = request.response;
  50. if (!respone) {
  51. mk.console.error("[getRequest] 获取用户信息错误 ", request);
  52. reject();
  53. }
  54. let info;
  55. if (typeof (respone) === "string") {
  56. info = JSON.parse(respone);
  57. }
  58. else {
  59. info = respone;
  60. }
  61. // LogUtil.log("info", info);
  62. // LogUtil.log("url.split",url.split("/"))
  63. // LogUtil.log(`[HttpMgr] ${HTTP_METHOD[httpMethod]}:${url.split("/")[url.split("/").length - 1]}`, info.errmsg)
  64. if (info.errcode === 0) {
  65. resolve(info)
  66. }
  67. else {
  68. if (info.errcode === -20003) {
  69. GameMgr.Inst.sendEvent("Error", `服务器发生-20003错误`);
  70. }
  71. reject(info);
  72. }
  73. }
  74. let headers = ["Content-Type", "application/json;charset=UTF-8"];
  75. for (var i = 0; i < headers.length; i += 2) {
  76. request.setRequestHeader(headers[i], headers[i + 1]);
  77. }
  78. //POST方式:跟后端了解,可以没有BodyData参数;GET一定没有BodyData参数
  79. if (bodyData) {
  80. request.send(bodyData)
  81. }
  82. else {
  83. request.send()
  84. }
  85. })
  86. }
  87. //----------------------------接口操作-------------------------------------
  88. /**上线
  89. * 如果有key 或者 uin 直接进入AccountInfo
  90. * 如果没有 连接
  91. */
  92. online() {
  93. //这个需要打印
  94. console.log("[HttpMgr] GameConst.url_service", GameConst.url_service);
  95. if (GameConst.session_key != '' && GameConst.uin != '') {
  96. mk.console.log("[HttpMgr] online 已经有缓存信息");
  97. HttpMgr.instance.getUserInfo();
  98. }
  99. else {
  100. mk.console.log("[HttpMgr] online 新用户");
  101. HttpMgr.instance.connect();
  102. }
  103. }
  104. /**连接
  105. * 获取零食的tmp_uin
  106. */
  107. connect() {
  108. // GameMgr.Inst.sendEvent(UI_NAME.Loading, "开始加载");
  109. let url = `${GameConst.url_service}/connect`;
  110. let bodyData = {
  111. "psk": GameConst.randomKey,
  112. "appid": GameConst.appid
  113. }
  114. mk.console.log("[connect]bodyData", bodyData)
  115. let encryptData = mk.encrypt.rsaEncrypt(JSON.stringify(bodyData));
  116. this.httpRquest(url, encryptData, HTTP_METHOD.POST).then((responseData) => {
  117. let decryptData = mk.encrypt.decrypt(responseData.encrypt, GameConst.randomKey, GameConst.appid);
  118. mk.console.log("[HttpMgr] connect", decryptData);
  119. if (decryptData) {
  120. GameConst.tmp_uin = JSON.parse(decryptData).tmp_uin;
  121. StorageMgr.Inst.setStorage(DATA_STORAGE_KEY.uin, GameConst.tmp_uin);
  122. HttpMgr.instance.wxlogin();
  123. //星云关键埋点
  124. GameMgr.Inst.sendEventCp(`${UI_NAME.Loading}1`, "开始加载");
  125. }
  126. }).catch((err) => {
  127. mk.console.log("[HttpMgr] connect err", err);
  128. });
  129. }
  130. /**微信登陆
  131. * @param ifWxAuthCallBack 是否授权回调
  132. */
  133. wxlogin(ifWxAuthCallBack: boolean = false) {
  134. mk.console.log("[Httpmgr] wxlogin 微信登陆");
  135. let url = `${GameConst.url_service}/wxlogin`;
  136. let data = {
  137. "code": GameConst.wxCode,
  138. "timestamp": Math.floor(new Date().getTime() * 0.001)
  139. }
  140. let bodydata = null;
  141. let encryptInfo = null
  142. let uin = GameConst.wxCode == '' ? GameConst.tmp_uin : GameConst.uin;
  143. let encryptKey = GameConst.wxCode == "" ? GameConst.randomKey : GameConst.session_key;
  144. encryptInfo = mk.encrypt.encrypt(JSON.stringify(data), encryptKey, GameConst.appid);
  145. bodydata = {
  146. "uin": uin,
  147. "encrypt": encryptInfo
  148. }
  149. // LogUtil.log("[Httpmgr] wxlogin uin",uin);
  150. // LogUtil.log("[Httpmgr] wxlogin encryptKey",encryptKey);
  151. // LogUtil.log("[Httpmgr] wxlogin encryptInfo",encryptInfo);
  152. // return;
  153. this.httpRquest(url, JSON.stringify(bodydata), HTTP_METHOD.POST).then((responseData) => {
  154. /**解密的数据 */
  155. let decryptData = null;
  156. if (GameConst.wxCode != '') {
  157. decryptData = mk.encrypt.decrypt(responseData.encrypt, GameConst.session_key, GameConst.appid)
  158. }
  159. else {
  160. decryptData = mk.encrypt.decrypt(responseData.encrypt, GameConst.randomKey, GameConst.appid)
  161. }
  162. if (decryptData) {
  163. let data_parse = JSON.parse(decryptData);
  164. //【测试状态】不发送事件,就不打印日志
  165. mk.console.log("[HttpMgr] wxlogin", data_parse);
  166. GameConst.login_ticket = data_parse.login_ticket
  167. /**加密的数据 */
  168. let encrypt_login_ticket =mk.encrypt.encrypt(GameConst.login_ticket, GameConst.localEncryptKey, GameConst.appid)
  169. StorageMgr.Inst.setStorage(DATA_STORAGE_KEY.login_ticket, encrypt_login_ticket);
  170. if (GameConst.uin != data_parse.uin) {
  171. GameConst.uin = data_parse.uin
  172. // GameConst.isNew = true; //根本无须判定,getDetailInfo那边会鉴定
  173. }
  174. else {
  175. // GameConst.isNew = false;//根本无须判定,getDetailInfo那边会鉴定
  176. }
  177. mk.console.log("GameConst.isNew", GameConst.isNew);
  178. mk.console.log("GameConst.uin", GameConst.uin);
  179. let encrypt_uin = mk.encrypt.encrypt(GameConst.uin, GameConst.localEncryptKey, GameConst.appid)
  180. StorageMgr.Inst.setStorage(DATA_STORAGE_KEY.uin, encrypt_uin)
  181. HttpMgr.instance.checklogin(ifWxAuthCallBack);
  182. }
  183. }).catch((err) => {
  184. mk.console.log("[HttpMgr] wxlogin err", err);
  185. });
  186. }
  187. checklogin(ifWxAuthCallBack: boolean = false) {
  188. let url = `${GameConst.url_service}/checklogin`;
  189. let tmp_key
  190. if (GameConst.wxCode != '') {
  191. tmp_key = GameConst.session_key
  192. }
  193. else {
  194. tmp_key = mk.encrypt.randomString()
  195. GameConst.randomKey_new = tmp_key
  196. }
  197. let bodyData =
  198. {
  199. "tmp_key": tmp_key,
  200. "uin": GameConst.uin,
  201. "login_ticket": GameConst.login_ticket,
  202. "appid": GameConst.appid
  203. }
  204. let rasEncryptData = mk.encrypt.rsaEncrypt(JSON.stringify(bodyData));
  205. this.httpRquest(url, rasEncryptData, HTTP_METHOD.POST).then((responseData) => {
  206. let decryptData = null;
  207. if (GameConst.wxCode != '') {
  208. decryptData = mk.encrypt.decrypt(responseData.encrypt, GameConst.session_key, GameConst.appid)
  209. }
  210. else {
  211. decryptData = mk.encrypt.decrypt(responseData.encrypt, GameConst.randomKey_new, GameConst.appid)
  212. }
  213. mk.console.log("[HttpMgr] checklogin", decryptData);
  214. if (decryptData) {
  215. let data_parse = JSON.parse(decryptData)
  216. GameConst.session_key = data_parse.session_key
  217. let encrypt_session_key = mk.encrypt.encrypt(GameConst.session_key, GameConst.localEncryptKey, GameConst.appid)
  218. StorageMgr.Inst.setStorage(DATA_STORAGE_KEY.session_key, encrypt_session_key)
  219. HttpMgr.Inst.getUserInfo(ifWxAuthCallBack);
  220. }
  221. }).catch((err) => {
  222. mk.console.log("[HttpMgr] checklogin err", err);
  223. });;
  224. }
  225. /**获取账户信息 */
  226. getUserInfo(ifWxAuthCallBack: boolean = false) {
  227. let url = `${GameConst.url_service}/user`;
  228. let data =
  229. {
  230. "uin": GameConst.uin,
  231. "login_ticket": GameConst.login_ticket,
  232. "timestamp": Math.floor(new Date().getTime() * 0.001)
  233. }
  234. mk.console.log("getUserInfo", data);
  235. let encryptData = mk.encrypt.encrypt(JSON.stringify(data), GameConst.session_key, GameConst.appid)
  236. let bodyData = {
  237. "uin": GameConst.uin,
  238. "encrypt": encryptData
  239. }
  240. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  241. let data = this.parseResponseData(responseData)
  242. mk.console.log("[HttpMgr] user", data);
  243. // Object.keys(data).forEach((e) => {
  244. // LogUtil.log("[HttpMgr]1 " + e);
  245. // LogUtil.log("[HttpMgr]2 " + data[e]);
  246. // });
  247. if (data) {
  248. GameConst.isNew = data["is_new_user"];
  249. mk.console.log("[HttpMgr] user GameConst.isNew", GameConst.isNew);
  250. //新手 初始化引导
  251. if (GameConst.isNew) {
  252. }
  253. else {
  254. }
  255. // LogUtil.log("data.is_new_user", data.is_new_user);
  256. GameConst.isAuth = data["is_auth"]
  257. mk.console.log("GameConst.isAuth", GameConst.isAuth);
  258. //是否首次登陆
  259. PlayerConst.ifFirstLoginToday = data["is_first_login_today"];
  260. if (data.is_auth) {
  261. let encrypt_auth = mk.encrypt.encrypt('true', GameConst.localEncryptKey, GameConst.appid)
  262. StorageMgr.Inst.setStorage(DATA_STORAGE_KEY.isAuth, encrypt_auth)
  263. PlayerConst.headImgUrl = data["HeadImgUrl"];
  264. PlayerConst.nickName = data["NickName"];
  265. let nickname = StorageMgr.Inst.getStorage(DATA_STORAGE_KEY.nickname);
  266. if (!!nickname) {
  267. //改名了
  268. if (nickname != data["nickname"]) {
  269. StorageMgr.Inst.setStorage(DATA_STORAGE_KEY.nickname, PlayerConst.nickName);
  270. }
  271. }
  272. //原本放在初始化 首页、提现、用户信息的头像取消,采用分发事件代替
  273. HttpMgr.Inst.getDetailInfo(ifWxAuthCallBack)
  274. }
  275. else {
  276. HttpMgr.Inst.getDetailInfo(ifWxAuthCallBack)
  277. }
  278. }
  279. }).catch((err) => {
  280. mk.console.log("[HttpMgr] user err", err);
  281. });
  282. }
  283. //-----------------------------------游戏相关接口---------------------------------
  284. /**获取细节信息 */
  285. getDetailInfo(ifWxAuthCallBack: boolean = false) {
  286. // LogUtil.log("[HttpMgr] getDetailInfo");
  287. let url = `${GameConst.url_service}/game/details`;
  288. let data = {}
  289. let bodyData = this.encryptBodyData(data);
  290. // LogUtil.log("[HttpMgr] getDetailInfo", bodyData);
  291. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  292. let data = this.parseResponseData(responseData);
  293. mk.console.log("[HttpMgr] details", data);
  294. //解析获取的信息
  295. PlayerConst.getDetailInfo(data);
  296. //如果微信登陆回调返回
  297. if (ifWxAuthCallBack) {
  298. //检测是否刷新一下数据
  299. if (PlayerConst.ifFirstLoginToday) {
  300. if (PlayerConst.cheeckIfNewAuthUser(data.register_date)) {
  301. mk.console.log("[WxAuth] 首日新用户")
  302. }
  303. else {
  304. mk.console.log("[WxAuth] 首日登陆老用户")
  305. DataMgr.Inst.resteData();
  306. }
  307. }
  308. else {
  309. mk.console.log("[WxAuth] 非首日登陆")
  310. }
  311. //获取打卡信息
  312. HttpMgr.Inst.getClockInInfo();
  313. console.log("Detail 分发 微信授权事件");
  314. //分发微信授权返回事件
  315. mk.event.emit(EVENT_TYPE.BACK_WxAuth);
  316. //初始化游戏 Game
  317. //初始化结束按钮 gameOverUI
  318. //初始化每日提现 node_dailyCashOutUI
  319. }
  320. else {
  321. if (PlayerConst.ifFirstLoginToday) {
  322. PlayerConst.ifFirstLoginToday = false;
  323. DataMgr.Inst.resteData();
  324. }
  325. }
  326. /**是否登陆结束 */
  327. GameConst.ifLoginFinished = true;
  328. }).catch((err) => {
  329. mk.console.log("[HttpMgr] details", err);
  330. });
  331. }
  332. /**获取体力值 */
  333. getStrength(): Promise<any> {
  334. let url = `${GameConst.url_service}/game/strength`;
  335. let data = {}
  336. let bodyData = this.encryptBodyData(data);
  337. return new Promise((resolve, reject) => {
  338. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  339. let data = this.parseResponseData(responseData);
  340. mk.console.log("[HttpMgr] strength", data);
  341. resolve(data);
  342. }).catch((err) => {
  343. reject(err);
  344. });
  345. })
  346. }
  347. /**增减体力
  348. * @param:handle_type 1 是扣除 2 看视频获取 3 恢复体力
  349. * @param:num 数量
  350. * @param:curLevel 当前的关卡数目
  351. */
  352. strengthFigure(handle_type: number, num: number, curLevel: number): Promise<any> {
  353. let url = `${GameConst.url_service}/game/strength/figure`;
  354. let data = {
  355. "handel_type": handle_type,
  356. "num": num,
  357. "pass_no": curLevel
  358. }
  359. let bodyData = this.encryptBodyData(data);
  360. return new Promise((resolve, reject) => {
  361. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  362. let data = this.parseResponseData(responseData);
  363. mk.console.log("[HttpMgr] strength/figure", data, handle_type, PlayerConst.energyNum);
  364. resolve(data);
  365. }).catch((err) => {
  366. reject(err);
  367. });
  368. });
  369. }
  370. /**道具信息 */
  371. propsInfo() {
  372. let url = `${GameConst.url_service}/game/propsInfo`;
  373. let data = {}
  374. let bodyData = this.encryptBodyData(data);
  375. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  376. let data = this.parseResponseData(responseData);
  377. mk.console.log("[HttpMgr] propsInfo", data);
  378. });
  379. }
  380. /**道具
  381. * @param handel_type 处理类型 1 增 2 减
  382. * @param prop_code 道具编号
  383. * @param prop_num 道具数量
  384. */
  385. prop(handel_type: number, prop_code: string, prop_num: number): Promise<any> {
  386. mk.console.log("prop_code", prop_code);
  387. let url = `${GameConst.url_service}/game/prop`;
  388. let data = {
  389. "handel_type": handel_type,
  390. "prop_code": prop_code,
  391. "count": prop_num
  392. }
  393. let bodyData = this.encryptBodyData(data);
  394. return new Promise((resolve, reject) => {
  395. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  396. let data = this.parseResponseData(responseData);
  397. mk.console.log("[HttpMgr] prop", data);
  398. }).catch((err) => {
  399. reject(err);
  400. });
  401. })
  402. }
  403. /**通关
  404. * @param levelNum 关卡数目
  405. */
  406. pass(levelNum: number, score: number = 0): Promise<any> {
  407. let url = `${GameConst.url_service}/game/pass`;
  408. let data = {}
  409. if (score) {
  410. data = {
  411. "pass_no": levelNum,
  412. "score": score
  413. }
  414. }
  415. else {
  416. data = {
  417. "pass_no": levelNum,
  418. }
  419. }
  420. let bodyData = this.encryptBodyData(data);
  421. return new Promise((resolve, reject) => {
  422. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  423. let data = this.parseResponseData(responseData);
  424. mk.console.log("[HttpMgr] pass", data);
  425. this.getDetailInfo();
  426. resolve(data);
  427. }).catch((err) => {
  428. mk.console.log("[HttpMgr] pass err", err);
  429. reject(err);
  430. });
  431. })
  432. }
  433. /**获取打卡信息 */
  434. getClockInInfo(): Promise<any> {
  435. let url = `${GameConst.url_service}/cash/info`;
  436. let data = {
  437. }
  438. let bodyData = this.encryptBodyData(data);
  439. return new Promise((resolve, reject) => {
  440. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  441. let data = this.parseResponseData(responseData);
  442. mk.console.log("[HttpMgr] getClickInInfo", data);
  443. PlayerConst.clockInCashInfo = data;
  444. let node_cashOutUI = mk.ui.getCurOnPanel("CashOutUI");
  445. if (node_cashOutUI) {
  446. let cashOutUI = node_cashOutUI.getComponent(CashOutUI)
  447. cashOutUI.initCashOutItem();
  448. cashOutUI.setCurSelectItemBtn(cashOutUI.curSelectCashOutItem);
  449. }
  450. resolve(data);
  451. }).catch((err) => {
  452. mk.console.log("刷新提现!!!!!!!!!!!!!!!!!!!!!!!!err", err.errmsg);
  453. reject(err);
  454. });
  455. })
  456. }
  457. //-----------------------------------红包关接口-----------------------------------
  458. /**新手红包
  459. * @param:get_type 获取类型 0 普通 1 双倍
  460. * @param:pass_no 关卡数
  461. * @param:red_mondey 红包数目
  462. */
  463. newerRedPacket(get_type: number, pass_no: number, red_mondey: number): Promise<any> {
  464. let url = `${GameConst.url_service}/red-package/newer`;
  465. let data = {
  466. "get_type": get_type,
  467. "pass_no": pass_no,
  468. "red_money": red_mondey
  469. }
  470. // LogUtil.log("data",data);
  471. let bodyData = this.encryptBodyData(data);
  472. return new Promise((resolve, reject) => {
  473. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  474. let data = this.parseResponseData(responseData);
  475. mk.console.log("[HttpMgr] newerRedPacket", data);
  476. }).catch((err) => {
  477. reject(err);
  478. });
  479. })
  480. }
  481. /**登陆红包
  482. * @param:get_type 获取类型 0 普通 1 双倍
  483. * @param:pass_no 关卡数
  484. * @param:red_mondey 红包数目
  485. */
  486. loginRedPacket(get_type: number, pass_no: number, red_mondey: number): Promise<any> {
  487. let url = `${GameConst.url_service}/red-package/login`;
  488. let data = {
  489. "get_type": get_type,
  490. "pass_no": pass_no,
  491. "red_money": red_mondey
  492. }
  493. // LogUtil.log("data",data);
  494. let bodyData = this.encryptBodyData(data);
  495. return new Promise((resolve, reject) => {
  496. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  497. let data = this.parseResponseData(responseData);
  498. mk.console.log("[HttpMgr] loginRedPacket", data);
  499. }).catch((err) => {
  500. reject(err);
  501. });
  502. })
  503. }
  504. /**通关红包
  505. * @param:get_type 获取类型 0 普通 1 双倍
  506. * @param:pass_no 关卡数
  507. * @param:red_mondey 红包数目
  508. */
  509. passRedPacket(get_type: number, pass_no: number, red_mondey: number): Promise<any> {
  510. let url = `${GameConst.url_service}/red-package/pass`;
  511. let data = {
  512. "get_type": get_type,
  513. "pass_no": pass_no,
  514. "red_money": red_mondey
  515. }
  516. // LogUtil.log("data",data);
  517. let bodyData = this.encryptBodyData(data);
  518. return new Promise((resolve, reject) => {
  519. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  520. let data = this.parseResponseData(responseData);
  521. mk.console.log("[HttpMgr] passRedPacket", data);
  522. if (data.clock_date) {
  523. PlayerConst.clockInDate = data.clock_date;
  524. }
  525. mk.console.log("data.clock_count", data.clock_count);
  526. if (data.clock_count) {
  527. PlayerConst.clockInDay = data.clock_count;
  528. mk.console.log("PlayerConst.clockInDay", PlayerConst.clockInDay);
  529. }
  530. }).catch((err) => {
  531. reject(err);
  532. });
  533. })
  534. }
  535. /**随机红包
  536. * @param:get_type 获取类型 0 普通 1 双倍
  537. * @param:pass_no 关卡数
  538. * @param:red_mondey 红包数目
  539. */
  540. randomRedPacket(get_type: number, pass_no: number, red_mondey: number): Promise<any> {
  541. let url = `${GameConst.url_service}/red-package/random`;
  542. let data = {
  543. "get_type": get_type,
  544. "pass_no": pass_no,
  545. "red_money": red_mondey
  546. }
  547. // LogUtil.log("data",data);
  548. let bodyData = this.encryptBodyData(data);
  549. return new Promise((resolve, reject) => {
  550. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  551. let data = this.parseResponseData(responseData);
  552. mk.console.log("[HttpMgr] randomRedPacket", data);
  553. }).catch((err) => {
  554. reject(err);
  555. });
  556. })
  557. }
  558. //-----------------------------------提现接口-----------------------------------
  559. /**提现
  560. * @param amount 提现数额
  561. * @param cash_type 提现类型
  562. */
  563. cash(amount: number, cash_type: number): Promise<any> {
  564. let url = `${GameConst.url_service}/cash/getCash`;
  565. let data = {
  566. "amount": amount,
  567. "cash_type": cash_type
  568. }
  569. let bodyData = this.encryptBodyData(data);
  570. mk.console.log("cash_type", cash_type);
  571. return new Promise((resolve, reject) => {
  572. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  573. let data = this.parseResponseData(responseData);
  574. mk.console.log("[HttpMgr] cash", data);
  575. resolve(data);
  576. }).catch((err) => {
  577. reject(err);
  578. });
  579. })
  580. }
  581. caseRecords(): Promise<any> {
  582. let url = `${GameConst.url_service}/cash/records`;
  583. let data = {
  584. }
  585. let bodyData = this.encryptBodyData(data);
  586. return new Promise((resolve, reject) => {
  587. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  588. let data = this.parseResponseData(responseData);
  589. mk.console.log("[HttpMgr] cash/records", data);
  590. }).catch((err) => {
  591. reject(err);
  592. });
  593. })
  594. }
  595. //--------------------------------积分 商城接口接口---------------------------------
  596. /**得分 暂且不用 合并到 pass接口*/
  597. public passScore(levelNum: number, score: number): Promise<any> {
  598. mk.console.log("[HttpMgr] game/passScore data", levelNum, score);
  599. let url = `${GameConst.url_service}/game/passScore`;
  600. let data = {
  601. "pass_no": levelNum,
  602. "score": score
  603. }
  604. let bodyData = this.encryptBodyData(data);
  605. return new Promise((resolve, reject) => {
  606. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  607. let data = this.parseResponseData(responseData);
  608. mk.console.log("[HttpMgr] game/passScore", data);
  609. resolve(data);
  610. }).catch((err) => {
  611. mk.console.log("[HttpMgr] game/passScore err", err);
  612. EffectMgr.Inst.addTip("你忘记啦,这关领取过哦");
  613. reject(err);
  614. });
  615. })
  616. }
  617. /**显示商城标题 */
  618. public shopGetSlogan() {
  619. let url = `${GameConst.url_service}/shopping-mall/getSlogan`;
  620. let data = {
  621. }
  622. let bodyData = this.encryptBodyData(data);
  623. return new Promise((resolve, reject) => {
  624. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  625. let data = this.parseResponseData(responseData);
  626. mk.console.log("[HttpMgr] shopping-mall/getSlogan", data);
  627. }).catch((err) => {
  628. mk.console.log("[HttpMgr] shopping-mall/getSlogan", err);
  629. reject(err);
  630. });
  631. })
  632. }
  633. /**商城商品列表 */
  634. public shopGoodsList() {
  635. let url = `${GameConst.url_service}/shopping-mall/goodsList`;
  636. let data = {
  637. }
  638. let bodyData = this.encryptBodyData(data);
  639. return new Promise((resolve, reject) => {
  640. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  641. let data = this.parseResponseData(responseData);
  642. mk.console.log("[HttpMgr] shopping-mall/goodsList", data);
  643. GameConst.getShopGoodsListConfig(data);
  644. }).catch((err) => {
  645. mk.console.log("[HttpMgr] shopping-mall/goodsList", err);
  646. reject(err);
  647. });
  648. })
  649. }
  650. /**商城转盘奖励列表 */
  651. public shopPrizeList() {
  652. let url = `${GameConst.url_service}/shopping-mall/prizeList`;
  653. let data = {
  654. }
  655. let bodyData = this.encryptBodyData(data);
  656. return new Promise((resolve, reject) => {
  657. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  658. let data = this.parseResponseData(responseData);
  659. mk.console.log("[HttpMgr] shopping-mall/prizeList", data);
  660. GameConst.getShopPrizeListConfig(data);
  661. }).catch((err) => {
  662. mk.console.log("[HttpMgr] shopping-mall/prizeList", err);
  663. reject(err);
  664. });
  665. })
  666. }
  667. /**抽奖 */
  668. public shopLuckDraw(): Promise<any> {
  669. let url = `${GameConst.url_service}/shopping-mall/luckdraw`;
  670. let data = {
  671. }
  672. let bodyData = this.encryptBodyData(data);
  673. return new Promise((resolve, reject) => {
  674. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  675. let data = this.parseResponseData(responseData);
  676. mk.console.log("[HttpMgr] shopping-mall/luckdraw", data);
  677. resolve(data);
  678. }).catch((err) => {
  679. mk.console.log("[HttpMgr] shopping-mall/luckdraw", err);
  680. reject(err);
  681. });
  682. })
  683. }
  684. /**显示记录
  685. * @param type 1兑换记录 2 抽奖记录
  686. * @param
  687. */
  688. public shopExchangeRecord(type: number): Promise<any> {
  689. let url = `${GameConst.url_service}/shopping-mall/exchangeRecord`;
  690. let data = {
  691. "type": type
  692. }
  693. let bodyData = this.encryptBodyData(data);
  694. return new Promise((resolve, reject) => {
  695. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  696. let data = this.parseResponseData(responseData);
  697. if (type === 1) {
  698. GameConst.data_exchangeRecord = data;
  699. }
  700. else if (type === 2) {
  701. GameConst.data_luckDrawRecord = data;
  702. }
  703. mk.console.log("[HttpMgr] shopping-mall/exchangeRecord", data);
  704. resolve(data);
  705. }).catch((err) => {
  706. mk.console.log("[HttpMgr] shopping-mall/exchangeRecord", err);
  707. reject(err);
  708. });
  709. })
  710. }
  711. /**商城兑换
  712. * @param goods_code 商品code
  713. * @param count 个数 默认是1
  714. */
  715. public shopExchange(goods_code: string, count: number = 1): Promise<any> {
  716. let url = `${GameConst.url_service}/shopping-mall/doExChange`;
  717. let data = {
  718. "goods_code": goods_code,
  719. "count": count
  720. }
  721. let bodyData = this.encryptBodyData(data);
  722. return new Promise((resolve, reject) => {
  723. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  724. let data = this.parseResponseData(responseData);
  725. resolve(data);
  726. mk.console.log("[HttpMgr] shopping-mall/doExChange", data);
  727. }).catch((err) => {
  728. mk.console.log("[HttpMgr] shopping-mall/doExChange", err);
  729. reject(err);
  730. });
  731. })
  732. }
  733. //互推位相关接口--------------------------------------------------------------------
  734. /**抽奖 */
  735. public recommendList(): Promise<any> {
  736. let url = `${GameConst.url_service}/fission/recommendList`;
  737. let data = {
  738. }
  739. let bodyData = this.encryptBodyData(data);
  740. return new Promise((resolve, reject) => {
  741. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  742. let data = this.parseResponseData(responseData);
  743. mk.console.log("[HttpMgr] fission/recommendList", data);
  744. resolve(data);
  745. }).catch((err) => {
  746. mk.console.log("[HttpMgr] fission/recommendList", err);
  747. reject(err);
  748. });
  749. })
  750. }
  751. /**抽奖 */
  752. public appDownloadLog(downloadAppId: string): Promise<any> {
  753. let url = `${GameConst.url_service}/fission/appDownloadLog`;
  754. let data = {
  755. "downloadAppId": downloadAppId,
  756. }
  757. let bodyData = this.encryptBodyData(data);
  758. return new Promise((resolve, reject) => {
  759. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  760. let data = this.parseResponseData(responseData);
  761. mk.console.log("[HttpMgr] fission/appDownloadLog", data);
  762. resolve(data);
  763. }).catch((err) => {
  764. mk.console.log("[HttpMgr] fission/appDownloadLog", err);
  765. reject(err);
  766. });
  767. })
  768. }
  769. //API回传--------------------------------------------------------------------------
  770. /**更新设备信息 */
  771. public updateDeviceInfo(deviceInfo: any) {
  772. let url = `${GameConst.url_service}/user/machine`;
  773. let data = deviceInfo;
  774. let bodyData = this.encryptBodyData(data);
  775. return new Promise((resolve, reject) => {
  776. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  777. let data = this.parseResponseData(responseData);
  778. mk.console.log("[HttpMgr] user/machine", data);
  779. }).catch((err) => {
  780. reject(err);
  781. mk.console.log("[HttpMgr] user/machine err", err);
  782. });
  783. })
  784. }
  785. /**巨量API回传
  786. * @param type 0 激活 6 次留
  787. */
  788. public juliangAPIActive(type: number) {
  789. if (cc.sys.os != cc.sys.OS_ANDROID) {
  790. return;
  791. }
  792. mk.console.log("巨量回传 type", type);
  793. let url = `${GameConst.url_service}/ad/oe/callback/activate`;
  794. let data = {
  795. "event_type": type,
  796. "source": GameConst.appVersion + ';'
  797. }
  798. let bodyData = this.encryptBodyData(data);
  799. return new Promise((resolve, reject) => {
  800. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  801. let data = this.parseResponseData(responseData);
  802. mk.console.log("[HttpMgr] juliangcallback/activate", data);
  803. }).catch((err) => {
  804. mk.console.log("[HttpMgr] juliangcallback/activate err", err);
  805. reject(err);
  806. });
  807. })
  808. }
  809. //星云统计--------------------------------------------------------------------
  810. /** 向星云发送事件埋点 */
  811. public sendEventCp(eventKey: string, eventDes: string) {
  812. mk.console.log('seventKey ', eventKey);
  813. mk.console.log('eventDes ', eventDes)
  814. //使用
  815. let uin = !!GameConst.uin ? GameConst.uin : GameConst.tmp_uin;
  816. if (!uin) {
  817. mk.console.log("没有Uin,不执行");
  818. return;
  819. }
  820. let url = `${GameConst.url_service}/cp`;
  821. let isFirstDay = PlayerConst.loginDay == 1 ? 1 : 0;
  822. let data = {
  823. "appid": GameConst.appid,
  824. "uid": uin,
  825. "isFirstDay": isFirstDay, // 0 1 是第一天
  826. "umengChannel": GameConst.umengChannel,
  827. "deviceType": GameConst.deviceType,
  828. "androidVerison": GameConst.androidVersion,
  829. "version": GameConst.appVersion,
  830. "point": eventKey,
  831. "description": eventDes
  832. }
  833. // LogUtil.log("[HttpMgr] sendEventCp data", data);
  834. let bodyData = this.encryptBodyData(data);
  835. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  836. let data = this.parseResponseData(responseData);
  837. mk.console.log("[HttpMgr] sendEventCp", data);
  838. }).catch((err) => {
  839. mk.console.log("[HttpMgr] sendEventCp err", err);
  840. });
  841. }
  842. updateGameVersion(version: string) {
  843. let url = `${GameConst.url_service}/game/updateVersion`;
  844. let data = {
  845. "version": version,
  846. }
  847. let bodyData = this.encryptBodyData(data);
  848. return new Promise((resolve, reject) => {
  849. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  850. let data = this.parseResponseData(responseData);
  851. mk.console.log("[HttpMgr] game/updateVersion", data);
  852. }).catch((err) => {
  853. mk.console.log("[HttpMgr] game/updateVersion err", err);
  854. reject(err);
  855. });
  856. })
  857. }
  858. /** 更新观看广告次数
  859. */
  860. updateVideoTimes(placementId, adunit_format, network_type, network_placement_id,
  861. network_firm_id, adsource_id, adsource_index, adsource_price,
  862. adsource_isheaderbidding, publisher_revenue, precision, ecpm_level) {
  863. if (!GameConst.ifShowAd) {
  864. return;
  865. }
  866. let url = `${GameConst.url_service}/video/updateVideo`;
  867. let data = {
  868. "imei": GameConst.imei,
  869. "idfa": GameConst.idfa,
  870. "oaid": GameConst.oaid,
  871. "uin": GameConst.uin,
  872. "version": GameConst.appVersion,
  873. "tf_channel": GameConst.tf_channel,
  874. "destoon_ad_place": placementId,
  875. "ad_type": 1,
  876. "adunit_format": adunit_format,
  877. "network_type": network_type,
  878. "network_placement_id": network_placement_id,
  879. "network_firm_id": network_firm_id,
  880. "adsource_id": adsource_id,
  881. "adsource_index": adsource_index,
  882. "adsource_price": adsource_price,
  883. "adsource_isheaderbidding": adsource_isheaderbidding,
  884. "publisher_revenue": publisher_revenue,
  885. "precision": precision,
  886. "ecpm_level": ecpm_level
  887. }
  888. mk.console.log("[HttpMgr] updateVideoTimes data", data);
  889. Object.keys(data).forEach((key) => {
  890. mk.console.log(`updateVideo ${key}`, data[key]);
  891. })
  892. let bodyData = this.encryptBodyData(data);
  893. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  894. let data = this.parseResponseData(responseData);
  895. mk.console.log("[HttpMgr] updateVideoTimes ok", data);
  896. }).catch((err) => {
  897. mk.console.log("[HttpMgr] updateVideoTimes err", err);
  898. });
  899. }
  900. /** 更新其他广告次数
  901. * @param type 1:激励视频成功、 2:插屏、3:信息流 4 观看视频
  902. */
  903. public updateADLog(type, placementId, adunit_format, network_type, network_placement_id,
  904. network_firm_id, adsource_id, adsource_index, adsource_price,
  905. adsource_isheaderbidding, publisher_revenue, precision, ecpm_level) {
  906. if (!GameConst.ifShowAd) {
  907. return;
  908. }
  909. let url = `${GameConst.url_service}/video/updateADLog`;
  910. let data = {
  911. "imei": GameConst.imei,
  912. "idfa": GameConst.idfa,
  913. "oaid": GameConst.oaid,
  914. "uin": GameConst.uin,
  915. "version": GameConst.appVersion,
  916. "tf_channel": GameConst.tf_channel,
  917. "destoon_ad_place": placementId,
  918. "ad_type": type,
  919. "adunit_format": adunit_format,
  920. "network_type": network_type,
  921. "network_placement_id": network_placement_id,
  922. "network_firm_id": network_firm_id,
  923. "adsource_id": adsource_id,
  924. "adsource_index": adsource_index,
  925. "adsource_price": adsource_price,
  926. "adsource_isheaderbidding": adsource_isheaderbidding,
  927. "publisher_revenue": publisher_revenue,
  928. "precision": precision,
  929. "ecpm_level": ecpm_level
  930. }
  931. mk.console.log("[HttpMgr] updateADLog data", data);
  932. let bodyData = this.encryptBodyData(data);
  933. this.httpRquest(url, JSON.stringify(bodyData), HTTP_METHOD.POST).then((responseData) => {
  934. let data = this.parseResponseData(responseData);
  935. mk.console.log("[HttpMgr] updateADLog ok", data);
  936. }).catch((err) => {
  937. mk.console.log("[HttpMgr] updateADLog err", err);
  938. });
  939. }
  940. //-----------------------------------加解密-----------------------------------
  941. /**加密参数 */
  942. private encryptBodyData(data: any): any {
  943. let encryptBodyData: any = null;
  944. let encryptData: any = null;
  945. //LogUtil.log("GameConst.tmp_uin",GameConst.tmp_uin);
  946. data.timestamp = Math.floor(new Date().getTime() * 0.001);
  947. if (GameConst.isAuth) {
  948. encryptData = mk.encrypt.encrypt(JSON.stringify(data), GameConst.session_key, GameConst.appid);
  949. encryptBodyData = {
  950. "uin": GameConst.uin,
  951. "encrypt": encryptData
  952. }
  953. }
  954. else {
  955. encryptData = mk.encrypt.encrypt(JSON.stringify(data), GameConst.session_key, GameConst.appid);
  956. encryptBodyData = {
  957. "uin": GameConst.tmp_uin,
  958. "encrypt": encryptData
  959. }
  960. }
  961. return encryptBodyData;
  962. }
  963. /**
  964. * 解析返回的数据
  965. * @param response 返回数据
  966. * @param ifDataClear 是否数据是明文发送 默认暗文
  967. */
  968. private parseResponseData(response: any, ifDataClear: boolean = false): any {
  969. let data = null
  970. if (ifDataClear) {
  971. data = response.data
  972. }
  973. else {
  974. if (response.encrypt) {
  975. data = mk.encrypt.decrypt(response.encrypt, GameConst.session_key, GameConst.appid)
  976. data = JSON.parse(data)
  977. }
  978. else if (response.data) {
  979. data = response.data
  980. }
  981. }
  982. return data
  983. }
  984. }
  985. export enum HTTP_METHOD {
  986. GET = "GET",
  987. POST = "POST"
  988. }
  989. export enum HTTP_TYPE {
  990. connect = "connect",
  991. recommendList = "recommendList"
  992. }
  993. //通关接口ERR CODE
  994. export enum PASS_ERR_CODE {
  995. /** 关卡号不正*/
  996. PASSNO_INCORRECT = 9001,
  997. /**关卡号不能为空或者小于1 */
  998. PASSNO_NULL = 9002,//
  999. /***获取积分异常(积分传参非法)*/
  1000. SCORE_PARAM_ILLEGAL = 9003,
  1001. /**已达单关积分获取次数*/
  1002. SCORE_OVER_TIMES = 9004,
  1003. /**已达今日积分上限*/
  1004. SCORE_REACH_LIMIT = 9005
  1005. }