Pay.ts 3.8 KB

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