FamilyPageHas.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. import { _decorator, Component, Node, Animation } from "cc";
  2. import BaseUI from "../../scripts/base/BaseUI";
  3. import { MemberManager } from "./MemberManager";
  4. import { InputNameLayer } from "./InputNameLayer";
  5. import EV, { EV_TYPE } from "../../scripts/mgr/EV";
  6. import AB from "../../scripts/base/AB";
  7. import { instantiate } from "cc";
  8. import { RankPlayerDto } from "../../scripts/api/RankM";
  9. import { RankPlayerItem } from "../item/RankPlayerItem";
  10. import { FamilyMemberItem } from "./FamilyMemberItem";
  11. import PayM from "../../scripts/mgr/PayM";
  12. import { Label } from "cc";
  13. import ShareM from "../../scripts/mgr/ShareM";
  14. import { Tips } from "../../scripts/mgr/Tips";
  15. import FamilyM, { FamilyInfo, FamilyMember } from "../../scripts/api/FamilyM";
  16. import UserM from "../../scripts/api/UserM";
  17. import Utils from "../../scripts/utils/Utils";
  18. import { TipsData, TipsLayer } from "../layer/TipsLayer";
  19. import { GoodsId } from "../../scripts/api/GoodsId";
  20. import { HelpImage, HelpType } from "../layer/HelpImage";
  21. const { ccclass, property } = _decorator;
  22. @ccclass("FamilyPageHas")
  23. export class FamilyPageHas extends BaseUI {
  24. private _data: FamilyInfo;
  25. async editFamilyNotice() {
  26. let notice = await InputNameLayer.show(100);
  27. if (notice == null) {
  28. return;
  29. }
  30. let familyId = UserM.ins.getFamilyId();
  31. if (familyId == null || familyId.length == 0) {
  32. return;
  33. }
  34. let result = await FamilyM.ins.editFamilyNotice(familyId, notice);
  35. if (!result) {
  36. return;
  37. }
  38. Tips.show("Success");
  39. this.setText("lbl_my_family_notice", notice);
  40. }
  41. onShow() {
  42. this.staticUI();
  43. this.familyInfo();
  44. setTimeout(() => {
  45. this.showMembers();
  46. }, 1000);
  47. }
  48. staticUI() {
  49. this.setText(
  50. "lbl_family_tips_member",
  51. Utils.setI18nLabel("Family.FamilyMemberBenefits")
  52. );
  53. this.setText(
  54. "lbl_family_tips_owner",
  55. Utils.setI18nLabel("Family.FamilyOwnerBenefits")
  56. );
  57. }
  58. private icon_family_pog_owner: Node;
  59. private icon_family_pog_owner_anim: Node;
  60. private icon_family_box: Node;
  61. private icon_family_box_anim: Node;
  62. protected onLoad(): void {
  63. super.onLoad();
  64. this.FindNode("owner_manage_node").active = false;
  65. this.FindNode("lbl_new_approve_node").active = false;
  66. this.icon_family_pog_owner = this.FindNode("icon_family_pog_owner");
  67. this.icon_family_pog_owner_anim = this.FindNode(
  68. "icon_family_pog_owner_anim"
  69. );
  70. this.icon_family_box = this.FindNode("icon_family_box");
  71. this.icon_family_box_anim = this.FindNode("icon_family_box_anim");
  72. EV.ins.on(EV_TYPE.FAMILY_DATA_UPDATE, this.familyInfo.bind(this), this);
  73. EV.ins.on(EV_TYPE.FAMILY_MEMBER_UPDATE, this.showMembers.bind(this), this);
  74. EV.ins.on(EV_TYPE.FAMILY_MEMBER_UPDATE, this.ownerUI.bind(this), this);
  75. }
  76. protected onDestroy(): void {
  77. EV.ins.off(EV_TYPE.FAMILY_DATA_UPDATE, this.familyInfo.bind(this), this);
  78. EV.ins.off(EV_TYPE.FAMILY_MEMBER_UPDATE, this.showMembers.bind(this), this);
  79. EV.ins.off(EV_TYPE.FAMILY_MEMBER_UPDATE, this.ownerUI.bind(this), this);
  80. }
  81. async familyUI() {
  82. let result = this._data;
  83. this.setText(
  84. "lbl_family_pog",
  85. Utils.formatNumber(Number(result.dailyPog), 1)
  86. );
  87. this.setText(
  88. "lbl_family_exp",
  89. Utils.formatNumber(Number(result.xp), 0) + "/100"
  90. );
  91. this.setText(
  92. "lbl_family_game_pass_count",
  93. Utils.formatNumber(Number(result.seasonPassGame), 0)
  94. );
  95. this.setText(
  96. "lbl_people_count",
  97. `${result.memberSize}/${result.totalSize}`
  98. );
  99. this.setText("lbl_my_family_name", result.familyName);
  100. this.setText("lbl_my_family_owner_name", result.ownerDto?.ownerName);
  101. this.setText("lbl_my_family_notice", result.notice);
  102. this.setText(
  103. "lbl_family_reward_box_count",
  104. "x" + Utils.formatNumber(Number(result.boxCount), 0)
  105. );
  106. let showBoxAnim = result.boxCount > 0;
  107. // showBoxAnim = true;
  108. this.icon_family_box.active = !showBoxAnim;
  109. this.icon_family_box_anim.active = showBoxAnim;
  110. if (result.ownerDto?.owner) {
  111. this.FindNode("owner_manage_node").active = true;
  112. this.ownerUI();
  113. } else {
  114. this.FindNode("owner_manage_node").active = false;
  115. }
  116. }
  117. ownerUI() {
  118. if (this._data.ownerDto == null) {
  119. return;
  120. }
  121. this.setText(
  122. "lbl_owner_reward_pog",
  123. "x" + Utils.formatNumber(Number(this._data.ownerDto?.rewardPog), 1)
  124. );
  125. let showPogAnim = this._data.ownerDto.rewardPog > 0;
  126. // showPogAnim = true;
  127. this.icon_family_pog_owner.active = !showPogAnim;
  128. this.icon_family_pog_owner_anim.active = showPogAnim;
  129. this.showNewApproveCount();
  130. }
  131. async familyInfo() {
  132. let self = this;
  133. let familyId = UserM.ins.getFamilyId();
  134. if (familyId == null || familyId.length == 0) {
  135. return;
  136. }
  137. let result: FamilyInfo = await FamilyM.ins.getFamilyInfo(familyId);
  138. if (!result) {
  139. return;
  140. }
  141. this._data = result;
  142. this.familyUI();
  143. }
  144. async showNewApproveCount() {
  145. let result = await FamilyM.ins.familyRequestList(this._data.familyId);
  146. let newApproveCount = 0;
  147. if (result != null) {
  148. newApproveCount = result.length;
  149. }
  150. this.FindNode("lbl_new_approve_node").active = newApproveCount > 0;
  151. if (newApproveCount > 0) {
  152. this.setText("lbl_new_approve_count", `${newApproveCount}`);
  153. }
  154. }
  155. async showMembers() {
  156. let familyId = UserM.ins.getFamilyId();
  157. if (familyId == null || familyId.length == 0) {
  158. return;
  159. }
  160. let members_layout = this.FindNode("members_layout");
  161. let p = await AB.ins.loadPrefab("prefab/family/FamilyMemberItem");
  162. members_layout.removeAllChildren();
  163. let list: FamilyMember[] = await FamilyM.ins.familyMemberList(familyId);
  164. if (list == null || list.length == 0) {
  165. return;
  166. }
  167. for (let index = 0; index < list.length; index++) {
  168. let item = instantiate(p);
  169. item.active = true;
  170. item.parent = members_layout;
  171. let playerData = list[index];
  172. item.getComponent(FamilyMemberItem).init(playerData);
  173. }
  174. let familySize = list.length;
  175. this.setText("lbl_people_count", `${familySize}/${this._data.totalSize}`);
  176. }
  177. protected simpleOnBtnClick(name: string): void {
  178. let self = this;
  179. switch (name) {
  180. case "btn_leave_family":
  181. this.leaveFamily();
  182. break;
  183. case "btn_edit_family_notice":
  184. this.editFamilyNotice();
  185. break;
  186. case "btn_approve":
  187. MemberManager.show(this._data);
  188. break;
  189. case "btn_share_family":
  190. this.shareFamily();
  191. break;
  192. case "btn_family_reward_box":
  193. this.openFamilyRewardBox();
  194. break;
  195. case "btn_family_reward_owner_pog":
  196. this.openFamilyRewardOwnerPog();
  197. break;
  198. case "btn_upgrade_family":
  199. this.upgradeFamily();
  200. break;
  201. case "btn_box_family_box":
  202. HelpImage.show(HelpType.FamilyBox);
  203. break;
  204. }
  205. }
  206. async leaveFamily() {
  207. if (this._data.ownerDto != null) {
  208. TipsLayer.showConfirm(
  209. "",
  210. Utils.setI18nLabel("Family.LeaveFamilyOwnerConfirm")
  211. );
  212. return;
  213. }
  214. let ok = await TipsLayer.showConfirm("", Utils.setI18nLabel("Family.LeaveFamilyConfirm"));
  215. if (!ok) {
  216. return;
  217. }
  218. let result = await FamilyM.ins.leaveFamily();
  219. if (result) {
  220. Tips.show(Utils.setI18nLabel("Family.LeaveFamilySuccess"));
  221. await UserM.ins.refreshInfo();
  222. }
  223. }
  224. async upgradeFamily() {
  225. let costDiamond = 10;
  226. if (UserM.ins.getGoodsCount(GoodsId.GEM) < costDiamond) {
  227. Tips.show("Not enough diamond");
  228. return;
  229. }
  230. let upgradeCost = `${costDiamond} Diamond`;
  231. let dialogResult = await TipsLayer.showConfirm(
  232. "",
  233. Utils.setI18nLabel("Family.UpgradeFamilyConfirm", "" + upgradeCost)
  234. );
  235. if (!dialogResult) {
  236. return;
  237. }
  238. let result = await FamilyM.ins.upgradeFamily(this._data.familyId);
  239. if (result) {
  240. TipsLayer.showConfirm(
  241. "",
  242. Utils.setI18nLabel("Family.UpgradeFamilySuccess")
  243. );
  244. EV.ins.emit(EV_TYPE.FAMILY_DATA_UPDATE);
  245. await UserM.ins.refreshInfo();
  246. }
  247. }
  248. async openFamilyRewardOwnerPog() {
  249. if (this._data.ownerDto == null) {
  250. return;
  251. }
  252. if (this._data.ownerDto.owner == false) {
  253. return;
  254. }
  255. if (this._data.ownerDto.rewardPog <= 0) {
  256. Tips.show("No reward");
  257. return;
  258. }
  259. let result = await FamilyM.ins.claimFamilyRewardOwnerPog(
  260. this._data.familyId
  261. );
  262. if (result) {
  263. this._data.ownerDto.rewardStatus = 0;
  264. this._data.ownerDto.rewardPog = 0;
  265. this.familyUI();
  266. }
  267. }
  268. async openFamilyRewardBox() {
  269. let num = this._data.boxCount;
  270. if (num <= 0) {
  271. Tips.show("No reward");
  272. return;
  273. }
  274. if (num > 10) {
  275. num = 10;
  276. }
  277. let result = await FamilyM.ins.claimFamilyRewardMemberBox(
  278. this._data.familyId,
  279. num
  280. );
  281. if (result) {
  282. console.warn("result", result);
  283. this._data.boxCount = this._data.boxCount - num;
  284. this.familyUI();
  285. }
  286. }
  287. shareFamily() {
  288. Tips.show("Sharing...");
  289. let familyName = this.FindAs("lbl_my_family_name", Label).string;
  290. let familyOwner = this.FindAs("lbl_my_family_owner_name", Label).string;
  291. ShareM.ins.shareFamily(familyName, familyOwner);
  292. }
  293. }