UserInfoView.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 { setAvatars, 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 onClose(_reason: UICloseReason): void | Promise<void> {
  86. this._googleWebBtn?.setActive(false);
  87. }
  88. protected onDestroy(): void {
  89. AppKernel.events.off('user:update', this.updateUserInfo, this);
  90. AppKernel.events.off('open:ui-view', this.handleSubViewOpen, this);
  91. AppKernel.events.off('close:ui-view', this.handleSubViewClose, this);
  92. this._googleWebBtn?.clear();
  93. super.onDestroy();
  94. }
  95. private updateUserInfo() {
  96. this._userInfo = AppKernel.stores.mainStore.userInfo;
  97. const {
  98. FaceID,
  99. NickName,
  100. GameID,
  101. Level,
  102. vipLevel,
  103. Score,
  104. InsureScore,
  105. Email,
  106. Phone,
  107. third_party
  108. } = this._userInfo; // TODO: 获取用户信息
  109. console.log("updateUserInfo", this._userInfo);
  110. if (FaceID >= 1) setAvatars(this.avatar, FaceID);
  111. this.nickname.string = NickName;
  112. this.id.string = `ID:${GameID}`;
  113. this.level.string = `LV.${Level}`;
  114. this.vip_level.string = `VIP.${vipLevel}`;
  115. this.score.string = Score;
  116. this.gold_score.string = InsureScore;
  117. this.email.string = Email ? Email : "Not bound";
  118. const btn_email_label = this.btn_email.getChildByName("label");
  119. btn_email_label.getComponent(Label).string = Email ? "CHANGE" : "BIND";
  120. this.phone.string = Phone ? Phone : "Not bound";
  121. const btn_phone_label = this.btn_phone.getChildByName("label");
  122. btn_phone_label.getComponent(Label).string = Phone ? "CHANGE" : "BIND";
  123. if (third_party) {
  124. const third_party_unlink = this.third_party.getChildByName("unlink");
  125. const third_party_linked = this.third_party.getChildByName("linked");
  126. const { bound, provider } = third_party;
  127. third_party_linked.active = bound;
  128. third_party_unlink.active = !bound;
  129. if (bound) {
  130. const google = third_party_linked.getChildByName("google");
  131. const facebook = third_party_linked.getChildByName("facebook");
  132. google.active = provider === "google";
  133. facebook.active = provider === "facebook";
  134. }
  135. }
  136. }
  137. private onBtnCloseClick() {
  138. AppKernel.ui.close(UIViewID.UserInfoView);
  139. }
  140. private onBtnEmailClick() {
  141. const { Email } = this._userInfo;
  142. AppKernel.ui.open(Email ? UIViewID.ChangeEmail : UIViewID.BindEmail);
  143. }
  144. private onBtnPhoneClick() {
  145. const { Phone } = this._userInfo;
  146. AppKernel.ui.open(Phone ? UIViewID.ChangePhone : UIViewID.BindPhone);
  147. }
  148. private onBtnAvatarClick() {
  149. AppKernel.ui.open(UIViewID.ChangeAvatar);
  150. }
  151. private onBtnNameClick() {
  152. AppKernel.ui.open(UIViewID.ChangeName);
  153. }
  154. private onBtnCopyIDClick() {
  155. const { GameID } = this._userInfo;
  156. }
  157. private async onBtnBindGoogleClick() {
  158. AppKernel.ui.loading(true);
  159. const res = await AppKernel.stores.mainStore.reqBindGoogle();
  160. AppKernel.ui.loading(false);
  161. if (res) AppKernel.ui.toast("Bind success");
  162. }
  163. private async onGoogleWebLogin(res) {
  164. AppKernel.ui.loading(true);
  165. const result = await AppKernel.stores.mainStore.reqBindGoogle(res);
  166. AppKernel.ui.loading(false);
  167. if (result) AppKernel.ui.toast("Bind success");
  168. }
  169. private async onBtnBindFacebookClick() {
  170. AppKernel.ui.loading(true);
  171. const res = await AppKernel.stores.mainStore.reqBindFacebook();
  172. AppKernel.ui.loading(false);
  173. if (res) AppKernel.ui.toast("Bind success");
  174. }
  175. private onBtnUnlinkThirdPartyClick() {
  176. const { Phone, Email } = this._userInfo;
  177. console.log("onBtnUnlinkThirdPartyClick", Phone, Email);
  178. if (!Phone && !Email) {
  179. AppKernel.ui.dialog({
  180. message: "Bind an email or phone number before unlinking this account.",
  181. callBack: (res) => {
  182. // AppKernel.appLobby.openLogin();
  183. // console.log("close dialog");
  184. return true;
  185. }
  186. })
  187. } else {
  188. AppKernel.ui.open(UIViewID.UnbindAccount);
  189. }
  190. }
  191. private handleSubViewOpen(payload: UIViewID) {
  192. if (this.listenViews.includes(payload)) {
  193. this._googleWebBtn?.setActive(false);
  194. console.log("UserInfoView handleSubUIOpen", payload);
  195. }
  196. }
  197. private handleSubViewClose(payload: UIViewID) {
  198. if (this.listenViews.includes(payload)) {
  199. this._googleWebBtn?.setActive(true);
  200. console.log("UserInfoView handleSubUIClose", payload);
  201. }
  202. }
  203. }