LoginUI.ts 12 KB

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