LoginUI.ts 12 KB

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