Pay.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { _decorator, Component, EditBox, game, instantiate, Label, Node, Prefab } 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. const { ccclass, property } = _decorator;
  7. type RateInfo = {
  8. Min: number
  9. Max: number
  10. Ratio: number
  11. }
  12. //money * rate * (1 + Ratio) = gold;
  13. export type ShopData = {
  14. MinMoney: number //最小金额
  15. Rate: number //金币/钱
  16. DefaultPrice: number[] //默认商品
  17. RatioConfig: RateInfo[]
  18. }
  19. @ccclass('Pay')
  20. export class Pay extends Component {
  21. public static clacGold(money: number){
  22. let config = Pay.data_shop;
  23. if(!config || money < config.MinMoney)return 0;
  24. let ratio_info = config.RatioConfig.find(function(info){return money >= info.Min && money <= info.Max});
  25. if(!ratio_info){
  26. let ratios = config.RatioConfig;
  27. ratio_info = ratios[ratios.length - 1];
  28. }
  29. return Math.floor(money * (ratio_info.Ratio + 1) * config.Rate);
  30. }
  31. public static buy(price:number){
  32. let user_id = Pay.user_id;
  33. if(user_id.length == 0){
  34. nav_info.scene.showTip("يرجى إدخال ال ID حقك");
  35. return;
  36. }
  37. let url = nav_info.is_test ? Config.url_pay_test : Config.url_pay;
  38. let final_url = `${url}?UserID=${user_id}&Price=${price}&ProductID=0&PartnerID=110&Currency=SAR&Country=SA&T=${new Date().getTime()}`;
  39. window.open(final_url);
  40. }
  41. public static user_id = "";
  42. public static data_shop:ShopData = null;
  43. private node_descid: Node = null;
  44. private node_buy: Node = null;
  45. private gene_items: Generate = null;
  46. private node_light: Node = null;
  47. private lb_money: Label = null;
  48. private edit_id: EditBox = null;
  49. private select_data = 0;
  50. protected start(){
  51. UtilsPanel.getAllNeedCom(this, this.node, true);
  52. UtilsPanel.addBtnEvent(this.node_buy, this.onBuy, this);
  53. // @ts-ignore
  54. game.on("select_item", this.onSelect, this);
  55. let url = nav_info.is_test ? Config.url_product_test : Config.url_product;
  56. // this.test();return;
  57. new WebRequest().getData(url, "", (succ:boolean,content:string)=>{
  58. if(succ){
  59. let info:ShopData = Pay.data_shop = JSON.parse(content);
  60. // console.log("fwef:",info);
  61. this.gene_items.initData(new Array(info.DefaultPrice.length + 1));
  62. this.node_light.setSiblingIndex(-1);
  63. }
  64. }, false);
  65. }
  66. private onEditChange(){
  67. this.node_descid.active = this.edit_id.string.length == 0;
  68. Pay.user_id = this.edit_id.string;
  69. }
  70. private onSelect(money:number, node:Node){
  71. this.select_data = money;
  72. this.lb_money.string = money + "";
  73. this.node_light.active = true;
  74. this.node_light.setPosition(node.position);
  75. }
  76. private onBuy(){
  77. if(this.select_data == 0){
  78. nav_info.scene.showTip("يرجى اختيار الباقة التي ترغب بشرائها");
  79. return;
  80. }
  81. Pay.buy(this.select_data);
  82. }
  83. // private test(){
  84. // let info = Pay.data_shop = {"MinMoney": 450, "Rate": 10000, "DefaultPrice": [1000,3000,6000,8000,10000],
  85. // "RatioConfig": [
  86. // {"Min":400,"Max": 1000, "Ratio": 0.7},
  87. // {"Min":1001,"Max": 3000, "Ratio": 0.8},
  88. // {"Min":3001,"Max": 6000, "Ratio": 1},
  89. // {"Min":6001,"Max": 8000, "Ratio": 1.2},
  90. // {"Min":8001,"Max": 10000, "Ratio": 1.4},
  91. // {"Min":10001,"Max": 10000000, "Ratio": 2}
  92. // ]}
  93. // this.gene_items.initData(new Array(info.DefaultPrice.length + 1));
  94. // }
  95. }