OrderView.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 { formatGoodsNum } from '@scripts/utils/Helper';
  6. import { addBtnClick } from '@scripts/utils/UIHelper';
  7. import { formatVip } 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. } else {
  60. }
  61. const money = this.goods.money;
  62. this.lblMoney.string = `$${money}`;
  63. this.lblButton.string = `Buy $${money}`;
  64. this.scValue.updateView(goods.sc, goods.sc_reward, goods.sc_reward_display_percent);
  65. this.gcValue.updateView(goods.gc, goods.gc_reward, goods.gc_reward_display_percent);
  66. this.vipValue.lblValue.string = formatVip(goods.cp);
  67. this.scNode.active = goods.sc > 0;
  68. this.gcNode.active = goods.gc > 0;
  69. this.vipNode.active = goods.cp > 0;
  70. const nodes: Node[] = [];
  71. if (goods.sc) nodes.push(this.scNode);
  72. if (goods.gc) nodes.push(this.gcNode);
  73. if (goods.cp) nodes.push(this.vipNode);
  74. const count = nodes.length;
  75. if (count === 0) return;
  76. const posList = ITEM_POSY[count - 1];
  77. for (let i = 0; i < count; ++i) {
  78. const y = posList[i];
  79. const node = nodes[i];
  80. node.y = y;
  81. }
  82. }
  83. private testGears(goods: IGoodsConfig) {
  84. const payServices = AppKernel.services.paymentServices;
  85. const layout = find("container/testGears", this.node);
  86. const prefab = find("Button", layout);
  87. for (let i = 0; i < goods.gears.length; ++i) {
  88. const item = instantiate(prefab);
  89. item.setParent(layout);
  90. item.active = true;
  91. const gear = goods.gears[i];
  92. const label = find("Label", item).getComponent(Label);
  93. label.string = gear.name;
  94. const gearIdx = i;
  95. addBtnClick(item, () => {
  96. payServices.startPayment(goods, gearIdx)
  97. })
  98. }
  99. }
  100. }
  101. class ItemValue {
  102. lblValue: Label;
  103. lblDesc: Label;
  104. constructor(readonly node: Node) {
  105. this.lblValue = find("lblValue", node).getComponent(Label);
  106. const descNode = find("lblDesc", node);
  107. if (descNode) this.lblDesc = descNode.getComponent(Label);
  108. }
  109. updateView(value: number, bonus: number, percent: number) {
  110. const total = formatGoodsNum(value + bonus);
  111. this.lblValue.string = formatGoodsNum(value);
  112. if (this.lblDesc) {
  113. this.lblDesc.string = `BASE ${total} + ${percent} BONUS ${bonus}`;
  114. }
  115. }
  116. }