UIUserInfo.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { EditBox, Toggle } from "cc";
  2. import { GameUILayers } from "../../scripts/GameUILayers";
  3. import { UserMgr } from "../scripts/UserMgr";
  4. import { UserInfo } from "../shared/types/UserInfo";
  5. import { Layout_UIUserInfo } from "./Layout_UIUserInfo";
  6. import { ModuleDef } from "../../scripts/ModuleDef";
  7. import { ConfigMgr } from "../scripts/ConfigMgr";
  8. const genderStrArr = ['保密', '男', '女'];
  9. @tgx_class(ModuleDef.BASIC)
  10. export class UIUserInfo extends tgx.UIController {
  11. constructor() {
  12. super("ui_user_info/ui_user_info", GameUILayers.POPUP, Layout_UIUserInfo);
  13. }
  14. private _userInfo: UserInfo;
  15. private _gender: number = undefined;
  16. private _introduction: string = undefined;
  17. protected async onCreated(userInfo: UserInfo): Promise<void> {
  18. this._userInfo = userInfo;
  19. let config = await ConfigMgr.inst.getBasicConfig();
  20. if (!config) {
  21. tgx.UIAlert.show('获取匹配失败,请稍后再试');
  22. this.close();
  23. return;
  24. }
  25. let layout = this._layout as Layout_UIUserInfo;
  26. this.onButtonEvent(layout.btnClose, async () => {
  27. if (this._gender != undefined || this._introduction != undefined) {
  28. tgx.UIAlert.show(`是否花费${config.userInfoModifyCost}战币修改信息?`, true).onClick(async b => {
  29. this.close();
  30. if (b) {
  31. if (UserMgr.inst.coin < config.userInfoModifyCost) {
  32. tgx.UIAlert.show('金币不足,修改失败');
  33. return;
  34. }
  35. let ret = await UserMgr.inst.rpc_ModifyUserInfo(this._gender, this._introduction);
  36. if (ret.isSucc) {
  37. tgx.UIAlert.show('用户信息修改成功!');
  38. }
  39. else {
  40. tgx.UIAlert.show('用户信息修改失败!');
  41. }
  42. }
  43. });
  44. }
  45. else {
  46. this.close();
  47. }
  48. });
  49. this.onButtonEvent(layout.btnIntroductionEdit, () => {
  50. layout.editIntroduction.node.active = true;
  51. layout.editIntroduction.string = this._introduction || this._userInfo.introduction || '';
  52. });
  53. layout.editIntroduction.node.on(EditBox.EventType.EDITING_DID_ENDED, this.onEditDidEnd, this);
  54. this.onButtonEvent(layout.btnMaskBg, this.onEditDidEnd, this);
  55. this.onToggleEvent(layout.toggleGenderOptions, (v: Toggle) => {
  56. let index = layout.toggleGenderOptions.toggleItems.indexOf(v);
  57. if (index != this._userInfo.gender) {
  58. this._gender = index;
  59. }
  60. });
  61. this.initData();
  62. }
  63. onEditDidEnd() {
  64. let layout = this._layout as Layout_UIUserInfo;
  65. if (!layout.editIntroduction.node.active) {
  66. return;
  67. }
  68. if (this._userInfo.introduction != layout.editIntroduction.string) {
  69. layout.lblIntroduction.string = layout.editIntroduction.string;
  70. this._introduction = layout.lblIntroduction.string;
  71. }
  72. layout.editIntroduction.node.active = false;
  73. }
  74. initData() {
  75. if (!this.node) {
  76. return;
  77. }
  78. let layout = this._layout as Layout_UIUserInfo;
  79. let bShow = !!this._userInfo;
  80. layout.lblName.node.active = bShow;
  81. layout.lblId.node.active = bShow;
  82. layout.lblGender.node.active = bShow;
  83. layout.toggleGenderOptions.node.active = bShow;
  84. layout.lblIntroduction.node.active = bShow;
  85. if (!this._userInfo) {
  86. return;
  87. }
  88. UserMgr.inst.setUserIconAndName(this._userInfo.uid, layout.sprHeadImg);
  89. layout.lblName.string = '昵称:' + this._userInfo.name;
  90. layout.lblId.string = 'ID:' + this._userInfo.uid;
  91. layout.lblIntroduction.string = this._userInfo.introduction || '这家伙很懒,什么也没留下~';
  92. layout.lblGender.string = '性别:';
  93. let gender = this._userInfo.gender || 0;
  94. if (this._userInfo.uid == UserMgr.inst.uid) {
  95. layout.toggleGenderOptions.toggleItems[gender].isChecked = true;
  96. }
  97. else {
  98. layout.toggleGenderOptions.node.active = false;
  99. layout.btnIntroductionEdit.enabled = false;
  100. layout.lblGender.string = '性别:' + genderStrArr[gender];
  101. }
  102. }
  103. }