OrderView.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { AppKernel } from '@scripts/app/AppKernel';
  2. import { UIViewID } from '@scripts/define/UIDefine';
  3. import { UIPopup } from '@scripts/framework/ui/UIPopup';
  4. import { IGoodsConfig } from '@scripts/store/payStore/data';
  5. import { formatGCNum, formatGoodsNum, formatSCNum } from '@scripts/utils/Helper';
  6. import { addBtnClick } from '@scripts/utils/UIHelper';
  7. import { formatCP, formatVipLv } from '@scripts/utils/VipHelper';
  8. import { _decorator, Component, find, instantiate, Label, Node, Sprite, SpriteFrame, tween, Tween } from 'cc';
  9. const { ccclass, property } = _decorator;
  10. const ITEM_POSY = [[19.5], [95, -55], [134, 19.5, -95.5]]
  11. export interface IOrderViewArg {
  12. icon?: SpriteFrame;
  13. goods: IGoodsConfig;
  14. }
  15. @ccclass('OrderView')
  16. export class OrderView extends UIPopup<IOrderViewArg> {
  17. readonly viewId: UIViewID = UIViewID.OrderView;
  18. @property(Node)
  19. btnClose: Node;
  20. @property(Sprite)
  21. sptIcon: Sprite;
  22. @property(Node)
  23. sptLight: Node;
  24. @property(Label)
  25. lblMoney: Label;
  26. @property(Label)
  27. lblButton: Label;
  28. @property(Node)
  29. btnPay: Node;
  30. @property(Node)
  31. scNode: Node;
  32. @property(Node)
  33. gcNode: Node;
  34. @property(Node)
  35. vipNode: Node;
  36. private goods: IGoodsConfig;
  37. private scValue: ItemValue;
  38. private gcValue: ItemValue;
  39. private vipValue: ItemValue;
  40. protected onCreate(): void | Promise<void> {
  41. addBtnClick(this.btnClose, this.closeSelf, this);
  42. addBtnClick(this.btnPay, this.onBtnPayClick, this);
  43. this.scValue = new ItemValue(this.scNode);
  44. this.gcValue = new ItemValue(this.gcNode);
  45. this.vipValue = new ItemValue(this.vipNode);
  46. }
  47. protected onOpen({ goods, icon }: IOrderViewArg): void | Promise<void> {
  48. this.goods = goods;
  49. this.updateView(goods, icon);
  50. }
  51. private onBtnPayClick() {
  52. }
  53. private updateView(goods: IGoodsConfig, icon?: SpriteFrame) {
  54. this.testGears(goods);
  55. Tween.stopAllByTarget(this.sptLight);
  56. tween(this.sptLight).by(1, { angle: -100 }).repeatForever().start();
  57. if (icon) {
  58. this.sptIcon.spriteFrame = icon;
  59. }
  60. const money = this.goods.money;
  61. this.lblMoney.string = `$${money}`;
  62. this.lblButton.string = `Buy $${money}`;
  63. this.vipValue.updateView(goods.cp, goods.cp_reward, goods.cp_reward_display_percent);
  64. this.scValue.updateView(goods.sc, goods.sc_reward, goods.sc_reward_display_percent);
  65. this.gcValue.updateView(goods.gc,
  66. goods.gc_reward + goods.gc_vip_reward,
  67. goods.gc_reward_display_percent + goods.gc_vip_reward_display_percent);
  68. this.scNode.active = goods.sc > 0;
  69. this.gcNode.active = goods.gc > 0;
  70. this.vipNode.active = goods.cp > 0;
  71. const nodes: Node[] = [];
  72. if (goods.sc) nodes.push(this.scNode);
  73. if (goods.gc) nodes.push(this.gcNode);
  74. if (goods.cp) nodes.push(this.vipNode);
  75. const count = nodes.length;
  76. if (count === 0) return;
  77. const posList = ITEM_POSY[count - 1];
  78. for (let i = 0; i < count; ++i) {
  79. const y = posList[i];
  80. const node = nodes[i];
  81. node.y = y;
  82. }
  83. }
  84. private testGears(goods: IGoodsConfig) {
  85. const payServices = AppKernel.services.paymentServices;
  86. const layout = find("container/testGears", this.node);
  87. const prefab = find("Button", layout);
  88. for (let i = 0; i < goods.gears.length; ++i) {
  89. const item = instantiate(prefab);
  90. item.setParent(layout);
  91. item.active = true;
  92. const gear = goods.gears[i];
  93. const label = find("Label", item).getComponent(Label);
  94. label.string = gear.name;
  95. const gearIdx = i;
  96. addBtnClick(item, () => {
  97. payServices.startPayment(goods, gearIdx)
  98. })
  99. }
  100. }
  101. }
  102. class ItemValue {
  103. lblValue: Label;
  104. lblDesc: Label;
  105. constructor(readonly node: Node) {
  106. this.lblValue = find("lblValue", node).getComponent(Label);
  107. const descNode = find("lblDesc", node);
  108. if (descNode) this.lblDesc = descNode.getComponent(Label);
  109. }
  110. updateView(value: number, bonus: number, percent: number) {
  111. const hasBonus = this.lblDesc && percent > 0 && bonus > 0;
  112. this.lblDesc.node.active = hasBonus;
  113. this.lblValue.node.y = hasBonus ? 11.5 : -2;
  114. this.lblValue.string = formatGoodsNum(value + bonus);
  115. if (hasBonus) {
  116. const base = formatGoodsNum(value);
  117. this.lblDesc.string = `BASE ${base} + ${percent}% BONUS ${formatGoodsNum(bonus)}`;
  118. }
  119. }
  120. }