LoginData.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import { StorageKey } from "./StorageData";
  2. /**
  3. * @description 登录数据
  4. * @author 邹勇
  5. */
  6. export class LoginData {
  7. public loadResCompelete: boolean = false;
  8. /** 是否登录 */
  9. public isLogin: boolean = false;
  10. public sessionKey: string = '';
  11. /** 玩家唯一id */
  12. public uin: string = '';
  13. /** 玩家临时id */
  14. public tempUin: string = '';
  15. public loginTicket: string = '';
  16. public isFirstIn: boolean;
  17. public isNew: boolean;
  18. /** 是否授权 */
  19. public isAuth: boolean;
  20. /** 服务协议 */
  21. public ruleStr:string;
  22. /** 隐私协议 */
  23. public privacyStr:string;
  24. /**
  25. * 初始化本地配置
  26. * @returns
  27. */
  28. public async loadLocalRes() {
  29. let result = await mk.loader.load("data/rule_data",cc.JsonAsset);
  30. if(result && result.json){
  31. this.ruleStr = '';
  32. this.privacyStr = '';
  33. let arr = result.json.server;
  34. for(let i=0;i<arr.length;i++){
  35. this.ruleStr += arr[i] + '\n';
  36. }
  37. arr = result.json.privacy;
  38. for(let i=0;i<arr.length;i++){
  39. this.privacyStr += arr[i] + '\n';
  40. }
  41. this.loadResCompelete = true;
  42. }
  43. }
  44. /**
  45. * 登录流程 initApp-->initAd-->connect-->wxLogin-->checkLogin-->getAccountInfo
  46. */
  47. public async login() {
  48. this.isLogin = true;
  49. mk.pool.init(gData.gameData.pools);
  50. gData.appData.init();
  51. gData.adData.init();
  52. //测试参数
  53. if (cc.sys.os == cc.sys.OS_ANDROID) {
  54. mk.ad.ifShowAd = true;
  55. }
  56. else {
  57. mk.ad.ifShowAd = false;
  58. }
  59. //有登录记录直接跳转到登录流程最后一步
  60. if (this.sessionKey != '' && this.uin != '') {
  61. this.getAccountInfo();
  62. return;
  63. }
  64. let ranKey = mk.encrypt.randomString();
  65. let data: any = {
  66. "mac": gData.appData.mac,
  67. "psk": ranKey,
  68. "appId": gData.appData.appId
  69. }
  70. //connect
  71. let response = await mk.http.sendRequest('connectTest', 'POST', JSON.stringify(data));
  72. if (response.errcode != 0) {
  73. return;
  74. }
  75. data = mk.encrypt.decrypt(response.encrypt, ranKey, gData.appData.appId);
  76. this.tempUin = JSON.parse(data).tmp_uin;
  77. //wxLogin
  78. let encode;
  79. if (gData.wechatData.code != null) {//微信登录
  80. encode = mk.encrypt.encrypt(JSON.stringify({ "code": gData.wechatData.code }), this.sessionKey, gData.appData.appId);
  81. data = {
  82. "uin": this.uin,
  83. "encrypt": encode
  84. };
  85. }
  86. else {//游客登录
  87. encode = mk.encrypt.encrypt(JSON.stringify({ "code": gData.wechatData.code }), ranKey, gData.appData.appId);
  88. data = {
  89. "uin": this.tempUin,
  90. "encrypt": encode
  91. };
  92. }
  93. response = await mk.http.sendRequest('wxlogin', 'POST', JSON.stringify(data), false, false);
  94. if (response.errcode != 0) {
  95. if (response.errcode == -10003 || response.errcode == -20003) {
  96. //清除缓存
  97. this.reload();
  98. }
  99. return;
  100. }
  101. if (gData.wechatData.code != null) {
  102. data = mk.encrypt.decrypt(response.encrypt, this.sessionKey, gData.appData.appId);
  103. }
  104. else {
  105. data = mk.encrypt.decrypt(response.encrypt, ranKey, gData.appData.appId);
  106. }
  107. data = JSON.parse(data);
  108. this.loginTicket = data.login_ticket;
  109. mk.storage.setStorage(StorageKey.loginTicket, this.loginTicket);
  110. let isFirstIn = mk.storage.getStorage('isFirstIn');
  111. if (isFirstIn == null) {
  112. this.isFirstIn = true;
  113. mk.storage.setStorage(StorageKey.isFirstIn, 1);
  114. }
  115. if (this.uin != data.uin) {
  116. this.uin = data.uin;
  117. this.isNew = false;
  118. }
  119. else {
  120. this.isNew = true;
  121. }
  122. mk.console.log("uin:", this.uin);
  123. mk.storage.setStorage(StorageKey.uin, this.uin);
  124. console.log('uin ', this.uin)
  125. console.log('tempUin ', this.tempUin)
  126. //checkLogin
  127. let tmp_key = '';
  128. if (gData.wechatData.code != null) {
  129. tmp_key = this.sessionKey;
  130. }
  131. else {
  132. tmp_key = mk.encrypt.randomString();
  133. }
  134. data = {
  135. "tmp_key": tmp_key,
  136. "uin": this.uin,
  137. "login_ticket": this.loginTicket,
  138. "appId": gData.appData.appId
  139. }
  140. response = await mk.http.sendRequest('checklogin', 'POST', JSON.stringify(data));
  141. if (response.errcode != 0) {
  142. return;
  143. }
  144. if (gData.wechatData.code != null) {
  145. data = mk.encrypt.decrypt(response.encrypt, this.sessionKey, gData.appData.appId);
  146. }
  147. else {
  148. data = mk.encrypt.decrypt(response.encrypt, tmp_key, gData.appData.appId);
  149. }
  150. data = JSON.parse(data);
  151. this.sessionKey = data.session_key;
  152. mk.storage.setStorage('sessionKey', this.sessionKey);
  153. if (gData.wechatData.code != null) {
  154. if (this.isNew) {//玩过此游戏并且本地数据是本机的
  155. this.getAccountInfo();
  156. }
  157. else {//玩过此游戏 但 本地数据不是本账户的 清空数据 重新登录游戏
  158. this.isNew = false;
  159. gData.wechatData.code = null;
  160. this.reload();
  161. }
  162. }
  163. else {
  164. this.getAccountInfo();
  165. }
  166. }
  167. private async getAccountInfo() {
  168. let nickname = mk.storage.getStorage(StorageKey.nickname);
  169. if (nickname) {
  170. gData.wechatData.nickName = nickname;
  171. }
  172. else {
  173. let aa = {
  174. "uin": this.uin,
  175. "login_ticket": this.loginTicket
  176. };
  177. let data: any = {
  178. "uin": this.uin,
  179. "encrypt": mk.encrypt.encrypt(JSON.stringify(aa), this.sessionKey, gData.appData.appId)
  180. };
  181. let response = await mk.http.sendRequest('user', 'POST', JSON.stringify(data), false, false);
  182. if (response.errcode != 0) {
  183. if (response.errcode == -10003 || response.errcode == -20003) {
  184. //清除缓存
  185. this.reload();
  186. }
  187. return;
  188. }
  189. response = mk.http.checkData(response, true);
  190. data = response.data;
  191. this.isAuth = data.is_auth;
  192. if (this.isAuth) {
  193. gData.wechatData.nickName = data.nickname;
  194. gData.wechatData.avatar = data.headimgurl;
  195. mk.storage.setStorage(StorageKey.isAuth, 1);
  196. mk.storage.setStorage(StorageKey.nickname, gData.wechatData.nickName, false);
  197. mk.storage.setStorage(StorageKey.avatar, gData.wechatData.avatar, false);
  198. }
  199. }
  200. //获取游戏数据
  201. gData.gameData.init();
  202. }
  203. /**
  204. * 本地数据不是本账户的 清空数据 重新登录游戏
  205. */
  206. public reload() {
  207. let path = cc.sys.localStorage.getItem('HotUpdateSearchPaths');
  208. gData.storageData.clear();
  209. cc.sys.localStorage.setItem('HotUpdateSearchPaths', path);
  210. cc.audioEngine.stopAll();
  211. //调用Android的更好
  212. cc.game.restart();
  213. }
  214. }