LoginUI.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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.TowerUI, () => {
  160. Framework.layer.close(ViewID.LoginUI);
  161. Framework.layer.close(ViewID.SelectServer);
  162. });
  163. } else {
  164. Framework.layer.open(ViewID.TowerUI, () => {
  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. //适龄提示
  190. } else if (target.name == 'agreement_node') {
  191. if (this.agreementState) {
  192. this.agreementState = 0;
  193. UserData.save_locally('AgreementState', 0)
  194. } else {
  195. this.agreementState = 1;
  196. UserData.save_locally('AgreementState', 1)
  197. }
  198. this.agreementIco.active = this.agreementState == 1 ? true : false;
  199. } else if (target.name == 'agreement_tx') {
  200. //用户协议
  201. } else if (target.name == 'privacy_tx') {
  202. //隐私政策
  203. } else if (target.name == 'bulletin_btn') {
  204. Framework.layer.open(ViewID.Bulletin);
  205. } else if (target.name == 'refresh_tx') {
  206. //刷新重启游戏大概
  207. }
  208. }
  209. private _enterGame(state: number) {
  210. // if (UserData.player.uuid == "") {
  211. // UIHelper.buttonEnable(this.Button.btn_login, true);
  212. // this._touchLogin = false;
  213. // Framework.tips.setTips("网络错误,请查看网络是否正常!");
  214. // this.Button.btn_loginex.node.active = true;
  215. // return;
  216. // }
  217. // if (UserData.player.area == GameConf.AreaType.error) {
  218. // Framework.layer.open(ViewID.HometownUI, () => {
  219. // Framework.layer.close(this);
  220. // });
  221. // } else {
  222. //载入界面
  223. // }
  224. }
  225. private _loginEx() {
  226. //开始登陆
  227. // if (sys.platform == sys.Platform.WECHAT_GAME) {
  228. // //微信
  229. // // @ts-ignore
  230. // wx.login({
  231. // success: (result) => {
  232. // //console.log(result);
  233. // LoginMgr.check(result.code, (token: string | null) => {
  234. // if (token) {
  235. // // LoginMgr.login(1, token, this._enterGame.bind(this));
  236. // } else {
  237. // //登录失败
  238. // this.Button.btn_loginex.node.active = true;
  239. // Framework.tips.setTips("登录失败");
  240. // }
  241. // });
  242. // },
  243. // fail: (result) => {
  244. // console.log(result);
  245. // this.Button.btn_loginex.node.active = true;
  246. // }
  247. // });
  248. // } else if (sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  249. // //字节
  250. // // @ts-ignore
  251. // tt.login({
  252. // force: false,
  253. // success: (result) => {
  254. // console.log(`login 调用成功${result.code} ------ ${result.anonymousCode}`);
  255. // LoginMgr.check(result.code, (token: string | null) => {
  256. // if (token) {
  257. // // LoginMgr.login(2, token, this._enterGame.bind(this));
  258. // } else {
  259. // //登录失败
  260. // this.Button.btn_loginex.node.active = true;
  261. // Framework.tips.setTips("登录失败");
  262. // }
  263. // });
  264. // },
  265. // fail: (result) => {
  266. // console.log(`login 调用失败`);
  267. // this.Button.btn_loginex.node.active = true;
  268. // Framework.tips.setTips("登录失败");
  269. // },
  270. // });
  271. // } else {
  272. // this.Button.btn_loginex.node.active = true;
  273. // }
  274. }
  275. private _initPool() {
  276. //初始化池
  277. // let queue = new AsyncQueue();
  278. // queue.pushMulti("InitPool", async (next: NextFunction, params: any, args: any) => {
  279. // //加载道具池
  280. // MaterialPool.init("package", "prefab/pool/MaterialItem", 15, 120, () => {
  281. // next && next();
  282. // console.log("道具池加载成功!");
  283. // });
  284. // }, async (next: NextFunction, params: any, args: any) => {
  285. // Framework.tips.setTipsNode("package", "prefab/other/TipsNode", "Label", () => {
  286. // next && next();
  287. // });
  288. // }, async (next: NextFunction, params: any, args: any) => {
  289. // RewardPool.init("package", "prefab/other/RewardEffect", 3, 120, () => {
  290. // next && next();
  291. // });
  292. // }, async (next: NextFunction, params: any, args: any) => {
  293. // BulletlPool.init("package", "prefab/other/Bullet", 6, 120, () => {
  294. // next && next();
  295. // });
  296. // }, async (next: NextFunction, params: any, args: any) => {
  297. // FloatTextPool.init("package", "prefab/other/FloatText", 10, 120, () => {
  298. // next && next();
  299. // });
  300. // });
  301. // queue.complete = () => {
  302. // };
  303. // queue.play();
  304. this._loginEx();
  305. }
  306. //获取服务器列表
  307. private getServerList() {
  308. LoginMgr.getServerList((data) => {
  309. if (data) {
  310. UserData.parseLoginData(data);
  311. this.accountData = UserData.getAccountData()
  312. this.updateCurServer()
  313. }
  314. }, this.loginData)
  315. }
  316. updateCurServer(isRefresh?: boolean) {
  317. let sid = -1;
  318. if (isRefresh) {
  319. sid = UserData.getSid();
  320. }
  321. if (sid == -1) {
  322. const roles = UserData.getRoleServers();
  323. if (roles.length === 0) {
  324. // 无角色,寻找推荐服务器
  325. const recmds = UserData.getRecmdServers();
  326. for (const v of recmds) {
  327. if (v && v.sid) {
  328. sid = v.sid;
  329. break;
  330. }
  331. }
  332. } else {
  333. // 找最近登录的服务器,roles 已排序,第一个是最近的
  334. for (const v of roles) {
  335. if (v && v.sid) {
  336. sid = v.sid;
  337. break;
  338. }
  339. }
  340. }
  341. }
  342. let serverData = UserData.getServerBySid(sid)
  343. if (!serverData) {
  344. return;
  345. }
  346. this.serverData = serverData;
  347. UserData.setSid(serverData['sid'], false);
  348. this.serverIndex.string = `${serverData.sid}${StringUtil.getLanguageData('服')}`;
  349. this.serverName.string = serverData.name;
  350. if (serverData.status == 0) {
  351. this.load('package', 'texture/login/gray_1/spriteFrame', SpriteFrame, (res: SpriteFrame) => {
  352. this.serverState.getComponent(Sprite).spriteFrame = res;
  353. })
  354. } else if (serverData.status == 1) {
  355. this.load('package', 'texture/login/red_1/spriteFrame', SpriteFrame, (res: SpriteFrame) => {
  356. this.serverState.getComponent(Sprite).spriteFrame = res;
  357. })
  358. } else if (serverData.status == 2) {
  359. this.load('package', 'texture/login/green_1/spriteFrame', SpriteFrame, (res: SpriteFrame) => {
  360. this.serverState.getComponent(Sprite).spriteFrame = res;
  361. })
  362. }
  363. this.loginRoot.active = false
  364. this.selectServer.active = true
  365. }
  366. }