Pay.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import { _decorator, Button, Component, EditBox, game, instantiate, Label, Node, Sprite, Tween, tween, Vec3} from 'cc';
  2. import { UtilsPanel } from '../../scene/script/UtilsPanel';
  3. import { Generate } from '../../scene/script/components/Generate';
  4. import { Config, nav_info } from '../../scene/script/NavInfo';
  5. import { WebRequest } from '../../scene/script/WebRequest';
  6. import { funcBg } from '../../scene/script/components/FuncBg';
  7. const { ccclass, property } = _decorator;
  8. export type RateInfo = {
  9. ProductID: string
  10. Gold: number,
  11. PayPrice: number
  12. ShowRate: number
  13. }
  14. //money * rate * (1 + Ratio) = gold;
  15. export type ShopData = {
  16. ProductConfig: RateInfo[]
  17. ExchangeRate: ExchangeRate[]
  18. }
  19. //国家信息
  20. export type ExchangeRate = {
  21. Currency: string // 币种
  22. CountryName: string // 国家名称
  23. CountryCode: string // 国家编码
  24. Rate: number //汇率
  25. Sort: number //显示顺序
  26. }
  27. @ccclass('Pay')
  28. export class Pay extends Component {
  29. // public static clacGold(money: number){
  30. // let config = Pay.data_shop;
  31. // if(!config || money < config.MinMoney)return 0;
  32. // let ratio_info = config.RatioConfig.find(function(info){return money >= info.Min && money <= info.Max});
  33. // if(!ratio_info){
  34. // let ratios = config.RatioConfig;
  35. // ratio_info = ratios[ratios.length - 1];
  36. // }
  37. // return Math.floor(money * (ratio_info.Ratio + 1) * config.Rate);
  38. // }
  39. public static buy(prod_id: string, price: number, currency: string = "SAU") {
  40. let user_id = Pay.user_id;
  41. if (user_id.length == 0) {
  42. nav_info.scene.showTip("يرجى إدخال ال ID حقك");
  43. return;
  44. }
  45. let url = nav_info.is_test ? Config.url_pay_test : Config.url_pay;
  46. let final_url = `${url}?UserID=${user_id}&Price=${price}&ProductID=${prod_id}&PartnerID=110&Currency=${currency}&Country=SA&T=${new Date().getTime()}`;
  47. window.open(final_url);
  48. }
  49. public static user_id = "";
  50. public static data_shop: ShopData = null;
  51. private node_descid: Node = null;
  52. private node_buy: Node = null;
  53. private gene_items: Generate = null;
  54. private node_light: Node = null;
  55. private lb_money: Label = null;
  56. private edit_id: EditBox = null;
  57. private select_data: RateInfo = null;
  58. public currentCountry: ExchangeRate = null;
  59. public typeIndex: number = 0;
  60. private node_typeList: Node = null;
  61. private sp_arrows: Sprite = null;
  62. private node_type: Node = null;
  63. private lb_type: Label = null;
  64. private sp_type: Sprite = null;
  65. private node_arrows: Node = null;
  66. protected start() {
  67. UtilsPanel.getAllNeedCom(this, this.node, true);
  68. UtilsPanel.addBtnEvent(this.node_buy, this.onBuy, this);
  69. UtilsPanel.addBtnEvent(this.node_arrows, this.onArrowsClick, this).transition = Button.Transition.NONE;
  70. // @ts-ignore
  71. game.on("select_item", this.onSelect, this);
  72. let url = nav_info.is_test ? Config.url_product_test : Config.url_product;
  73. // this.test();return;
  74. new WebRequest().getData(url, "", (succ: boolean, content: string) => {
  75. if (succ) {
  76. let info: ShopData = Pay.data_shop = JSON.parse(content);
  77. // console.log("fwef:",info);
  78. this.gene_items.initData(info.ProductConfig);
  79. this.node_light.setSiblingIndex(-1);
  80. this.createTypeList();
  81. }
  82. }, false);
  83. }
  84. private onEditChange() {
  85. this.node_descid.active = this.edit_id.string.length == 0;
  86. Pay.user_id = this.edit_id.string;
  87. }
  88. private onSelect(info: RateInfo, node: Node) {
  89. this.select_data = info;
  90. this.upSelectPrice();
  91. this.node_light.active = true;
  92. this.node_light.setPosition(node.position);
  93. }
  94. private upSelectPrice() {
  95. if (!this.select_data) return;
  96. //取字符串保留2位小数(不足补0)
  97. this.lb_money.string = `${(this.select_data.PayPrice * this.currentCountry.Rate).toFixed(2)} ${this.currentCountry.CountryCode}`;
  98. }
  99. private onBuy() {
  100. if (this.select_data == null) {
  101. nav_info.scene.showTip("يرجى اختيار الباقة التي ترغب بشرائها");
  102. return;
  103. }
  104. //计算汇率 取number类型整数
  105. let price = parseFloat((this.select_data.PayPrice * this.currentCountry.Rate).toFixed(2));
  106. Pay.buy(this.select_data.ProductID, price, this.currentCountry.Currency);
  107. }
  108. // private test(){
  109. // let info = Pay.data_shop = {"MinMoney": 450, "Rate": 10000, "DefaultPrice": [1000,3000,6000,8000,10000],
  110. // "RatioConfig": [
  111. // {"Min":400,"Max": 1000, "Ratio": 0.7},
  112. // {"Min":1001,"Max": 3000, "Ratio": 0.8},
  113. // {"Min":3001,"Max": 6000, "Ratio": 1},
  114. // {"Min":6001,"Max": 8000, "Ratio": 1.2},
  115. // {"Min":8001,"Max": 10000, "Ratio": 1.4},
  116. // {"Min":10001,"Max": 10000000, "Ratio": 2}
  117. // ]}
  118. // this.gene_items.initData(new Array(info.DefaultPrice.length + 1));
  119. // }
  120. //刷新国家列表
  121. private createTypeList() {
  122. if (this.node_typeList.children.length > 0) return;
  123. Pay.data_shop.ExchangeRate.forEach((item, index) => {
  124. let temp = instantiate(this.node_type);
  125. temp.name = index.toString();
  126. temp.getComponent(Sprite).spriteFrame = null;
  127. temp.children[0].active = this.typeIndex === index;
  128. UtilsPanel.setItemIcon(nav_info.bundle, `texture/flag_${item.Currency}/spriteFrame`, temp.children[1].getComponent(Sprite));
  129. temp.children[2].getComponent(Label).string = item.CountryName;
  130. this.node_typeList.addChild(temp);
  131. UtilsPanel.clearBtnEvent(temp, false);
  132. UtilsPanel.addBtnEvent2(temp, 'onSelectType', this, index).transition = Button.Transition.NONE;
  133. });
  134. this.upTypeList();
  135. }
  136. //刷新选中的类型
  137. private upTypeList() {
  138. this.node_typeList.active = false;
  139. this.sp_arrows.node.toScaleY(-1);
  140. this.currentCountry = Pay.data_shop.ExchangeRate[this.typeIndex];
  141. this.lb_type.string = this.currentCountry.CountryName;
  142. UtilsPanel.setItemIcon(nav_info.bundle, `texture/flag_${this.currentCountry.Currency}/spriteFrame`, this.sp_type);
  143. //刷新金币
  144. this.upSelectPrice();
  145. }
  146. //选择 typeList
  147. private onSelectType(event: Event, customEventData: string) {
  148. let index = parseInt(customEventData);
  149. if (this.typeIndex == index) return;
  150. this.typeIndex = index;
  151. this.upTypeList();
  152. }
  153. //显示隐藏 typeList
  154. private onArrowsClick() {
  155. let isOpen = !this.node_typeList.active;
  156. Tween.stopAllByTarget(this.node_typeList);
  157. if (isOpen) {
  158. this.node_typeList.toScaleY(0);
  159. this.node_typeList.active = true;
  160. tween(this.node_typeList)
  161. .to(0.2, { scale: new Vec3(1, 1, 1) }, {
  162. onComplete: () => {
  163. }
  164. })
  165. .start();
  166. } else {
  167. tween(this.node_typeList)
  168. .to(0.2, { scale: new Vec3(1, 0.1, 1) }, {
  169. onComplete: () => {
  170. this.node_typeList.active = false;
  171. }
  172. })
  173. .start();
  174. }
  175. this.sp_arrows.node.toScaleY(isOpen ? 1 : -1);
  176. if (isOpen) {
  177. funcBg.add(this.node_typeList, this.onArrowsClick.bind(this), 0, 0);
  178. this.node_typeList.children.forEach((item, index) => {
  179. item.children[0].active = this.typeIndex == index;
  180. });
  181. }
  182. }
  183. }