LoginUI.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. import { sys, tween, Tween, _decorator, Label, EditBox, Node } from 'cc';
  2. import { UIHelper } from '../../../framework/common/UIHelper';
  3. import { AudioID } from '../../../framework/config/AudioConf';
  4. import { FrameworkConf } from '../../../framework/config/FrameworkConf';
  5. import { ViewID } from '../../../framework/config/LayerConf';
  6. import { Framework } from '../../../framework/Framework';
  7. import { BaseView } from '../../../framework/layer/BaseView';
  8. import { AsyncQueue, NextFunction } from '../../../framework/queue/AsyncQueue';
  9. import { BulletlPool, DecoratePool, FloatTextPool, GoodsPool, IconPool, MaterialPool, PopPool, RewardPool, RolePool, TipsPool } from '../../common/Pool';
  10. import { UserData } from '../../data/UserData';
  11. import { LoginMgr } from '../../common/LoginManager';
  12. import { StringUtil } from '../../../framework/util/StringUtil';
  13. import { GameEvent } from '../../data/GameEvent';
  14. const { ccclass, property } = _decorator;
  15. export function getParameterByName(name) {
  16. let url = window.location.href;
  17. name = name.replace(/[\[\]]/g, "\\$&");
  18. const regex = new RegExp(`[?&]${name}(=([^&#]*)|&|#|$)`);
  19. const results = regex.exec(url);
  20. if (!results) return null;
  21. if (!results[2]) return '';
  22. return decodeURIComponent(results[2].replace(/\+/g, ' '));
  23. //return results[2].replace(/\+/g, ' ')
  24. }
  25. @ccclass('LoginUI')
  26. export class LoginUI extends BaseView {
  27. @property(Node)
  28. loginRoot: Node = null;
  29. @property({ type: Label, tooltip: "版本" })
  30. version: Label = null;
  31. @property(Node)
  32. selectServer: Node = null;
  33. @property({ type: Label, tooltip: "ServerName" })
  34. serverName: Label = null;
  35. @property({ type: Label, tooltip: "服务器状态" })
  36. serverState: Label = null;
  37. @property({ type: EditBox, tooltip: "账号" })
  38. userName: EditBox = null;
  39. private scene_back = null;
  40. //登录数据
  41. private loginData: any = null
  42. accountData: any;
  43. serverData: any;
  44. protected onLoad() {
  45. super.onLoad();
  46. this._initPool();
  47. LoginMgr.init();
  48. this.version.string = "版本:1.0.0";
  49. Framework.event.addEvent(FrameworkConf.Event.NET_ERROR, () => {
  50. // if (this.node.active && this._touchLogin) {
  51. // if (UserData.player.uuid != "") {
  52. // this._enterGame(0);
  53. // } else {
  54. // UIHelper.buttonEnable(this.Button.btn_login, true);
  55. // this._touchLogin = false;
  56. // }
  57. // }
  58. }, this);
  59. Framework.event.addEvent(FrameworkConf.Event.NET_CLOSE, () => {
  60. // if (this.node.active && this._touchLogin) {
  61. // if (UserData.player.uuid != "") {
  62. // this._enterGame(0);
  63. // } else {
  64. // UIHelper.buttonEnable(this.Button.btn_login, true);
  65. // this._touchLogin = false;
  66. // }
  67. // }
  68. }, this);
  69. Framework.event.addEvent(GameEvent.SelectServer, () => {
  70. this.updateCurServer(true);
  71. }, this);
  72. Framework.audio.playMusic(AudioID.Game);
  73. let uid = UserData.save_locally('UserID')
  74. let choose_server = UserData.save_locally('choose_server', null, {})
  75. this.loginData = {
  76. url: getParameterByName("url") || "http://118.178.135.110/sdk/debug/login.php",
  77. uid: getParameterByName("uid") || uid || "ttq3",
  78. token: getParameterByName("token"),
  79. }
  80. // this.server_list.push(choose_server)
  81. this.userName.string = this.loginData.uid
  82. // this.serverName.string = "997"
  83. }
  84. protected onDestroy() {
  85. super.onDestroy();
  86. Framework.event.removeEvent(this);
  87. this.scene_back()
  88. }
  89. //UI开打时会调用,如果有初始化代码应该放到此函数
  90. onOpen(SceneBack) {
  91. this.scene_back = SceneBack
  92. // console.log(window["App_Clue"]);
  93. // GuideConf
  94. //this.EditBox.box.string = UserData.save_locally('UserID', null, '');
  95. }
  96. //UI关闭时会调用,该函数在onDestroy前调用
  97. onClose() {
  98. // Tween.stopAllByTarget(this.Node.sign);
  99. }
  100. //框架管理UI层级时会调用,可根据UI情况修改
  101. onShow() {
  102. super.onShow();
  103. }
  104. //框架管理UI层级时会调用,可根据UI情况修改
  105. onHide() {
  106. super.onHide();
  107. }
  108. EditTex(text) {
  109. let reg = new RegExp("^[A-Za-z0-9]+$");
  110. let str = "";
  111. for (let i = 0; i < text.length; i++) {
  112. if (reg.test(text.charAt(i))) {
  113. str += text.charAt(i);
  114. }
  115. }
  116. this.userName.string = str;
  117. // this.EditBox.box.focus();
  118. }
  119. onLogin() {
  120. Framework.audio.playEffect(AudioID.Click);
  121. this.loginData.uid = this.userName.string
  122. UserData.save_locally('UserID', this.loginData.uid)
  123. this.getServerList()
  124. }
  125. onEnter() {
  126. let _gateway_data = {
  127. openid: this.accountData.openId, //上面获取到的openid
  128. openkey: this.accountData.openKey, //上面获取到的openkey
  129. sid: this.serverData.sid, // 选择的服务器列表信息中的id
  130. }
  131. LoginMgr.getGateway(_gateway_data, this.serverData.host, () => {
  132. if (UserData['mark'].first_login && UserData['mark'].first_login == 1) {
  133. Framework.layer.open(ViewID.MainUI, () => {
  134. Framework.layer.close(ViewID.LoginUI);
  135. Framework.layer.close(ViewID.SelectServer);
  136. });
  137. } else {
  138. Framework.layer.open(ViewID.MainUI, () => {
  139. Framework.layer.close(ViewID.LoginUI);
  140. Framework.layer.close(ViewID.SelectServer);
  141. });
  142. }
  143. })
  144. //
  145. //登录游戏
  146. // Framework.layer.close(this);
  147. // if (this.EditBox.box.string.length > 0) {
  148. // const match = SensitiveWordFilter.getInstance().containsSensitiveWord(this.EditBox.box.string);
  149. // if (match) {
  150. // Framework.tips.setTips(`${match}是敏感词`);
  151. // return;
  152. // }
  153. // } else {
  154. // Framework.tips.setTips('账号只能输入英文和数字并且不可为空!');
  155. // }
  156. }
  157. //UI事件处理
  158. private onTouchButton(event: Event) {
  159. let target: any = event.target;
  160. if (target.name == 'select_server') {
  161. Framework.layer.open(ViewID.SelectServer)
  162. }
  163. }
  164. private _enterGame(state: number) {
  165. // if (UserData.player.uuid == "") {
  166. // UIHelper.buttonEnable(this.Button.btn_login, true);
  167. // this._touchLogin = false;
  168. // Framework.tips.setTips("网络错误,请查看网络是否正常!");
  169. // this.Button.btn_loginex.node.active = true;
  170. // return;
  171. // }
  172. // if (UserData.player.area == GameConf.AreaType.error) {
  173. // Framework.layer.open(ViewID.HometownUI, () => {
  174. // Framework.layer.close(this);
  175. // });
  176. // } else {
  177. //载入界面
  178. // }
  179. }
  180. private _loginEx() {
  181. //开始登陆
  182. // if (sys.platform == sys.Platform.WECHAT_GAME) {
  183. // //微信
  184. // // @ts-ignore
  185. // wx.login({
  186. // success: (result) => {
  187. // //console.log(result);
  188. // LoginMgr.check(result.code, (token: string | null) => {
  189. // if (token) {
  190. // // LoginMgr.login(1, token, this._enterGame.bind(this));
  191. // } else {
  192. // //登录失败
  193. // this.Button.btn_loginex.node.active = true;
  194. // Framework.tips.setTips("登录失败");
  195. // }
  196. // });
  197. // },
  198. // fail: (result) => {
  199. // console.log(result);
  200. // this.Button.btn_loginex.node.active = true;
  201. // }
  202. // });
  203. // } else if (sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  204. // //字节
  205. // // @ts-ignore
  206. // tt.login({
  207. // force: false,
  208. // success: (result) => {
  209. // console.log(`login 调用成功${result.code} ------ ${result.anonymousCode}`);
  210. // LoginMgr.check(result.code, (token: string | null) => {
  211. // if (token) {
  212. // // LoginMgr.login(2, token, this._enterGame.bind(this));
  213. // } else {
  214. // //登录失败
  215. // this.Button.btn_loginex.node.active = true;
  216. // Framework.tips.setTips("登录失败");
  217. // }
  218. // });
  219. // },
  220. // fail: (result) => {
  221. // console.log(`login 调用失败`);
  222. // this.Button.btn_loginex.node.active = true;
  223. // Framework.tips.setTips("登录失败");
  224. // },
  225. // });
  226. // } else {
  227. // this.Button.btn_loginex.node.active = true;
  228. // }
  229. }
  230. private _initPool() {
  231. //初始化池
  232. // let queue = new AsyncQueue();
  233. // queue.pushMulti("InitPool", async (next: NextFunction, params: any, args: any) => {
  234. // //加载道具池
  235. // MaterialPool.init("package", "prefab/pool/MaterialItem", 15, 120, () => {
  236. // next && next();
  237. // console.log("道具池加载成功!");
  238. // });
  239. // }, async (next: NextFunction, params: any, args: any) => {
  240. // Framework.tips.setTipsNode("package", "prefab/other/TipsNode", "Label", () => {
  241. // next && next();
  242. // });
  243. // }, async (next: NextFunction, params: any, args: any) => {
  244. // RewardPool.init("package", "prefab/other/RewardEffect", 3, 120, () => {
  245. // next && next();
  246. // });
  247. // }, async (next: NextFunction, params: any, args: any) => {
  248. // BulletlPool.init("package", "prefab/other/Bullet", 6, 120, () => {
  249. // next && next();
  250. // });
  251. // }, async (next: NextFunction, params: any, args: any) => {
  252. // FloatTextPool.init("package", "prefab/other/FloatText", 10, 120, () => {
  253. // next && next();
  254. // });
  255. // });
  256. // queue.complete = () => {
  257. // };
  258. // queue.play();
  259. this._loginEx();
  260. }
  261. //获取服务器列表
  262. private getServerList() {
  263. LoginMgr.getServerList((data) => {
  264. if (data) {
  265. UserData.parseLoginData(data);
  266. this.accountData = UserData.getAccountData()
  267. this.updateCurServer()
  268. }
  269. }, this.loginData)
  270. }
  271. updateCurServer(isRefresh?:boolean) {
  272. let sid = -1;
  273. if (isRefresh){
  274. sid = UserData.getSid();
  275. }
  276. if (sid == -1) {
  277. const roles = UserData.getRoleServers();
  278. if (roles.length === 0) {
  279. // 无角色,寻找推荐服务器
  280. const recmds = UserData.getRecmdServers();
  281. for (const v of recmds) {
  282. if (v && v.sid) {
  283. sid = v.sid;
  284. break;
  285. }
  286. }
  287. } else {
  288. // 找最近登录的服务器,roles 已排序,第一个是最近的
  289. for (const v of roles) {
  290. if (v && v.sid) {
  291. sid = v.sid;
  292. break;
  293. }
  294. }
  295. }
  296. }
  297. let serverData = UserData.getServerBySid(sid)
  298. if (!serverData) {
  299. return;
  300. }
  301. this.serverData = serverData;
  302. UserData.setSid(serverData['sid'], false);
  303. this.serverName.string = serverData.name;
  304. if (serverData.status == 0) {
  305. this.serverState.string = StringUtil.getLanguageData('维护');
  306. }else if(serverData.status == 1){
  307. this.serverState.string = StringUtil.getLanguageData('爆满');
  308. }else if(serverData.status == 2){
  309. this.serverState.string = StringUtil.getLanguageData('推荐');
  310. }
  311. this.loginRoot.active = false
  312. this.selectServer.active = true
  313. }
  314. }