UserInfoView.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import { _decorator, Button, Color, EditBox, instantiate, Label, Node, RichText, Sprite, SpriteFrame, UITransform } from 'cc';
  2. import { UIView } from '@scripts/framework/ui/UIView';
  3. import { AppKernel } from '@scripts/app/AppKernel';
  4. import { UIViewID } from '@scripts/define/UIDefine';
  5. import { UICloseReason } from '@scripts/framework/ui/UILayer';
  6. import { formatCurrency } from '@scripts/utils/Helper';
  7. import { addBtnClick } from '@scripts/utils/UIHelper';
  8. import { setSpriteFrame } from '@scripts/utils/ResHelper';
  9. const { ccclass, property } = _decorator;
  10. @ccclass('UserInfoView')
  11. export class UserInfoView extends UIView {
  12. readonly viewId = UIViewID.UserInfoView;
  13. @property(Node)
  14. btn_close: Node;
  15. @property(Node)
  16. btn_email: Node;
  17. @property(Node)
  18. btn_phone: Node;
  19. @property(Node)
  20. btn_avatar: Node;
  21. @property(Node)
  22. btn_name: Node;
  23. @property(Node)
  24. btn_copy_id: Node;
  25. @property(Node)
  26. btn_bind_google: Node;
  27. @property(Node)
  28. btn_bind_facebook: Node;
  29. @property(Node)
  30. btn_unlink_third_party: Node;
  31. @property(Sprite)
  32. avatar: Sprite;
  33. @property(Label)
  34. nickname: Label;
  35. @property(Label)
  36. id: Label;
  37. @property(Label)
  38. level: Label;
  39. @property(Label)
  40. vip_level: Label;
  41. @property(Label)
  42. score: Label;
  43. @property(Label)
  44. gold_score: Label;
  45. @property(Label)
  46. email: Label;
  47. @property(Label)
  48. phone: Label;
  49. @property(Node)
  50. third_party: Node;
  51. private _userInfo: any;
  52. private _googleWebBtn;
  53. private listenViews: UIViewID[] = [
  54. UIViewID.BindEmail,
  55. UIViewID.ChangeEmail,
  56. UIViewID.BindPhone,
  57. UIViewID.ChangePhone,
  58. UIViewID.ChangeAvatar,
  59. UIViewID.ChangeName,
  60. UIViewID.UnbindAccount,
  61. ];
  62. protected onCreate(): void | Promise<void> {
  63. AppKernel.events.on('user:update', this.updateUserInfo, this);
  64. AppKernel.events.on('open:ui-view', this.handleSubViewOpen, this);
  65. AppKernel.events.on('close:ui-view', this.handleSubViewClose, this);
  66. addBtnClick(this.btn_close, this.onBtnCloseClick, this);
  67. addBtnClick(this.btn_email, this.onBtnEmailClick, this);
  68. addBtnClick(this.btn_phone, this.onBtnPhoneClick, this);
  69. addBtnClick(this.btn_avatar, this.onBtnAvatarClick, this);
  70. addBtnClick(this.btn_name, this.onBtnNameClick, this);
  71. addBtnClick(this.btn_copy_id, this.onBtnCopyIDClick, this);
  72. addBtnClick(this.btn_bind_google, this.onBtnBindGoogleClick, this);
  73. addBtnClick(this.btn_bind_facebook, this.onBtnBindFacebookClick, this);
  74. addBtnClick(this.btn_unlink_third_party, this.onBtnUnlinkThirdPartyClick, this);
  75. this._googleWebBtn = AppKernel.google.createLoginBtn(
  76. this.btn_bind_google,
  77. res => this.onGoogleWebLogin(res),
  78. );
  79. }
  80. protected onOpen(_params: unknown): void | Promise<void> {
  81. /// 播放完打开动画后 再显示h5 google login btn dom
  82. this._googleWebBtn?.setActive(true);
  83. this.updateUserInfo();
  84. }
  85. protected onBeforeClose(_reason: UICloseReason): boolean | Promise<boolean> {
  86. AppKernel.events.off('user:update', this.updateUserInfo, this);
  87. AppKernel.events.off('open:ui-view', this.handleSubViewOpen, this);
  88. AppKernel.events.off('close:ui-view', this.handleSubViewClose, this);
  89. return true;
  90. }
  91. protected onClose(_reason: UICloseReason): void | Promise<void> {
  92. this._googleWebBtn?.setActive(false);
  93. }
  94. protected onDestroy(): void {
  95. this._googleWebBtn?.clear();
  96. super.onDestroy();
  97. }
  98. private updateUserInfo() {
  99. this._userInfo = AppKernel.stores.mainStore.userInfo;
  100. const {
  101. FaceID,
  102. NickName,
  103. GameID,
  104. Level,
  105. vipLevel,
  106. Score,
  107. InsureScore,
  108. Email,
  109. Phone,
  110. third_party
  111. } = this._userInfo; // TODO: 获取用户信息
  112. console.log("updateUserInfo", this._userInfo);
  113. if (FaceID >= 1) setSpriteFrame(this.avatar, "common/avatars/" + FaceID, "lobbyRes");
  114. this.nickname.string = NickName;
  115. this.id.string = `ID:${GameID}`;
  116. this.level.string = `LV.${Level}`;
  117. this.vip_level.string = `VIP.${vipLevel}`;
  118. this.score.string = Score;
  119. this.gold_score.string = InsureScore;
  120. this.email.string = Email ? Email : "Not bound";
  121. const btn_email_label = this.btn_email.getChildByName("label");
  122. btn_email_label.getComponent(Label).string = Email ? "CHANGE" : "BIND";
  123. this.phone.string = Phone ? Phone : "Not bound";
  124. const btn_phone_label = this.btn_phone.getChildByName("label");
  125. btn_phone_label.getComponent(Label).string = Phone ? "CHANGE" : "BIND";
  126. if (third_party) {
  127. const third_party_unlink = this.third_party.getChildByName("unlink");
  128. const third_party_linked = this.third_party.getChildByName("linked");
  129. const { bound, provider } = third_party;
  130. third_party_linked.active = bound;
  131. third_party_unlink.active = !bound;
  132. if (bound) {
  133. const google = third_party_linked.getChildByName("google");
  134. const facebook = third_party_linked.getChildByName("facebook");
  135. google.active = provider === "google";
  136. facebook.active = provider === "facebook";
  137. }
  138. }
  139. }
  140. private onBtnCloseClick() {
  141. AppKernel.ui.close(UIViewID.UserInfoView);
  142. }
  143. private onBtnEmailClick() {
  144. const { Email } = this._userInfo;
  145. AppKernel.ui.open(Email ? UIViewID.ChangeEmail : UIViewID.BindEmail);
  146. }
  147. private onBtnPhoneClick() {
  148. const { Phone } = this._userInfo;
  149. AppKernel.ui.open(Phone ? UIViewID.ChangePhone : UIViewID.BindPhone);
  150. }
  151. private onBtnAvatarClick() {
  152. AppKernel.ui.open(UIViewID.ChangeAvatar);
  153. }
  154. private onBtnNameClick() {
  155. AppKernel.ui.open(UIViewID.ChangeName);
  156. }
  157. private onBtnCopyIDClick() {
  158. const { GameID } = this._userInfo;
  159. }
  160. private async onBtnBindGoogleClick() {
  161. AppKernel.ui.loading(true);
  162. const res = await AppKernel.stores.mainStore.reqBindGoogle();
  163. AppKernel.ui.loading(false);
  164. if (res) AppKernel.ui.toast("Bind success");
  165. }
  166. private async onGoogleWebLogin(res) {
  167. AppKernel.ui.loading(true);
  168. const result = await AppKernel.stores.mainStore.reqBindGoogle(res);
  169. AppKernel.ui.loading(false);
  170. if (result) AppKernel.ui.toast("Bind success");
  171. }
  172. private async onBtnBindFacebookClick() {
  173. AppKernel.ui.loading(true);
  174. const res = await AppKernel.stores.mainStore.reqBindFacebook();
  175. AppKernel.ui.loading(false);
  176. if (res) AppKernel.ui.toast("Bind success");
  177. }
  178. private onBtnUnlinkThirdPartyClick() {
  179. const { Phone, Email } = this._userInfo;
  180. console.log("onBtnUnlinkThirdPartyClick", Phone, Email);
  181. if (!Phone && !Email) {
  182. AppKernel.ui.dialog({
  183. message: "Bind an email or phone number before unlinking this account.",
  184. callBack: (res) => {
  185. // AppKernel.appLobby.openLogin();
  186. // console.log("close dialog");
  187. return true;
  188. }
  189. })
  190. } else {
  191. AppKernel.ui.open(UIViewID.UnbindAccount);
  192. }
  193. }
  194. private handleSubViewOpen(payload: UIViewID) {
  195. if (this.listenViews.includes(payload)) {
  196. this._googleWebBtn?.setActive(false);
  197. console.log("UserInfoView handleSubUIOpen", payload);
  198. }
  199. }
  200. private handleSubViewClose(payload: UIViewID) {
  201. if (this.listenViews.includes(payload)) {
  202. this._googleWebBtn?.setActive(true);
  203. console.log("UserInfoView handleSubUIClose", payload);
  204. }
  205. }
  206. }