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