BuyDogInfo.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. * 购买小狗
  3. */
  4. cc.Class({
  5. extends: cc.Component,
  6. properties: {
  7. inputInviteValue: {
  8. default: 1,
  9. visible: false
  10. },
  11. //1 小包,2 大包
  12. toggleInputValue: {
  13. default: '1',
  14. visible: false
  15. },
  16. totalPriceLabel: {
  17. default: null,
  18. type: cc.Label,
  19. serializable: true
  20. },
  21. totalPriceValue: {
  22. default: 0,
  23. visible: false
  24. },
  25. currentFoodUnitLabel: {
  26. default: null,
  27. type: cc.Label,
  28. serializable: true
  29. },
  30. ToastParent: {
  31. default: null,
  32. type: cc.Node,
  33. serializable: true
  34. },
  35. //输入节点
  36. inputContainer: {
  37. default: null,
  38. type: cc.Node,
  39. serializable: true
  40. },
  41. foodTypeToggle1: {
  42. default: null,
  43. type: cc.Toggle,
  44. serializable: true
  45. },
  46. foodTypeToggle2: {
  47. default: null,
  48. type: cc.Toggle,
  49. serializable: true
  50. },
  51. foodList: {
  52. default: [],
  53. visible: false
  54. },
  55. foodSmall: {
  56. default: null,
  57. visible: false
  58. },
  59. foodBig: {
  60. default: null,
  61. visible: false
  62. },
  63. currentFood: {
  64. default: null,
  65. visible: false
  66. },
  67. parentNode: {
  68. default: null,
  69. visible: false
  70. }
  71. },
  72. onLoadFoodList() {
  73. GlobalD.GameData.onGetMallFoodList((value) => {
  74. if (0 === value.code) {
  75. this.foodList = value.data;
  76. for (let i = 0; i < this.foodList.length; i++) {
  77. if (1 === this.foodList[i].id) {
  78. this.foodSmall = this.foodList[i];
  79. this.currentFood = this.foodSmall;
  80. this.currentFoodUnitLabel.string = "X" + this.currentFood.priceSnb;
  81. } else if (2 === this.foodList[i].id) {
  82. this.foodBig = this.foodList[i];
  83. }
  84. }
  85. this.toggleInputValue = '1';
  86. this._updatePrice();
  87. } else {
  88. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), value.msg, 1);
  89. }
  90. });
  91. this.inputContainer.getComponent(cc.EditBox).string = this.inputInviteValue;
  92. },
  93. //初始化时候需要初始化一次默认值
  94. onToggleInput(value, evnentData) {
  95. // console.log(value.isChecked, evnentData);
  96. this.toggleInputValue = evnentData;
  97. this._updatePrice();
  98. },
  99. inputValue(value, e) {
  100. var numberTemp = new RegExp("^[A-Za-z0-9]+$");
  101. let limitValue = 100;
  102. if (numberTemp.test(value)) {
  103. if (Number(value) >= 1 && Number(value) <= limitValue) {
  104. this.inputInviteValue = Number(value);
  105. } else if (Number(value) > limitValue) {
  106. //限制只能输入100
  107. this.inputInviteValue = limitValue;
  108. this.inputContainer.getComponent(cc.EditBox).string = this.inputInviteValue;
  109. } else {
  110. this.inputInviteValue = 1;
  111. this.inputContainer.getComponent(cc.EditBox).string = this.inputInviteValue;
  112. }
  113. } else {
  114. this.inputInviteValue = 1;
  115. this.inputContainer.getComponent(cc.EditBox).string = this.inputInviteValue;
  116. // console.log("请输入整数的倍数", this.inputInviteValue);
  117. }
  118. this._updatePrice();
  119. },
  120. _updatePrice() {
  121. switch (this.toggleInputValue) {
  122. case "1":
  123. this.totalPriceValue = this.foodSmall.priceSnb * this.inputInviteValue;
  124. this.totalPriceLabel.string = "X" + this.totalPriceValue;
  125. this.currentFood = this.foodSmall;
  126. break;
  127. case "2":
  128. this.totalPriceValue = this.foodBig.priceSnb * this.inputInviteValue;
  129. this.totalPriceLabel.string = "X" + this.totalPriceValue;
  130. this.currentFood = this.foodBig;
  131. break;
  132. default:
  133. console.error("this.toggleInputValue 不是1 2", this.toggleInputValue);
  134. break;
  135. }
  136. this.currentFoodUnitLabel.string = "X" + this.currentFood.priceSnb;
  137. },
  138. //租赁支付
  139. onPlaySnbInfo() {
  140. if (this.currentFood == null) {
  141. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "购买狗粮NULL", 1);
  142. return;
  143. }
  144. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "添加狗粮..", 5);
  145. let data = { foodId: this.currentFood.id, buyAmount: this.inputInviteValue };
  146. GlobalD.GameData.onGetAddDogFood(data, (value) => {
  147. console.log(value);
  148. if (0 === value.code) {
  149. GlobalD.GameData.SetSNB(value.data.snb + value.data.snbPart);
  150. GlobalD.Dog = value.data.dog;
  151. GlobalD.GameData.hideToast();
  152. //更新日志数据
  153. cc.find("GameNode/ManageDapp").getComponent("ManageDapp").onUpdateSnbList();
  154. //需要赋值
  155. this.parentNode.getComponent('DogContainer')._updateDogState(GlobalD.Dog);
  156. } else {
  157. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), value.msg, 1);
  158. }
  159. });
  160. },
  161. onClose() {
  162. this.node.destroy();
  163. }
  164. });