Pay.ts 3.8 KB

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