BuyDogInfo.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /**
  2. * 购买小狗
  3. */
  4. import date from "../Unit/date";
  5. cc.Class({
  6. extends: cc.Component,
  7. properties: {
  8. inputInviteValue: {
  9. default: 1,
  10. visible: false
  11. },
  12. //1 对应3个月,2对应1年,3对应5年
  13. toggleInputValue: {
  14. default: '2',
  15. visible: false
  16. },
  17. totalPriceLabel: {
  18. default: null,
  19. type: cc.Label,
  20. serializable: true
  21. },
  22. totalPriceValue: {
  23. default: 0,
  24. visible: false
  25. },
  26. ToastParent: {
  27. default: null,
  28. type: cc.Node,
  29. serializable: true
  30. },
  31. //输入节点
  32. inputContainer: {
  33. default: null,
  34. type: cc.Node,
  35. serializable: true
  36. },
  37. foodTypeToggle2: {
  38. default: null,
  39. type: cc.Toggle,
  40. serializable: true
  41. },
  42. foodTypeToggle3: {
  43. default: null,
  44. type: cc.Toggle,
  45. serializable: true
  46. },
  47. BuyTypeToggle2: {
  48. default: null,
  49. type: cc.Toggle,
  50. serializable: true
  51. },
  52. BuyTypeToggle3: {
  53. default: null,
  54. type: cc.Toggle,
  55. serializable: true
  56. },
  57. //这个显示的时候,拦截输入
  58. blockInput: {
  59. default: null,
  60. type: cc.Node,
  61. serializable: true
  62. },
  63. leaseFarmlandInfoNode: {
  64. default: null,
  65. type: cc.Node,
  66. serializable: true,
  67. visible: false,
  68. toolTip: "操作的土地对象,初始化时候传入"
  69. },
  70. //轮询土地状态
  71. AutoGetLandData: null,
  72. },
  73. // LIFE-CYCLE CALLBACKS:
  74. // onLoad () {},
  75. // start() {
  76. // },
  77. onOpenInfo() {
  78. this.node.active = true;
  79. },
  80. //初始化时候需要初始化一次默认值
  81. onToggleInput(value, evnentData) {
  82. // console.log(value.isChecked, evnentData);
  83. this.toggleInputValue = evnentData;
  84. this._updatePrice();
  85. },
  86. inputLeaseValue(value, e) {
  87. var numberTemp = new RegExp("^[A-Za-z0-9]+$");
  88. let limitValue = 100;
  89. if (GlobalD.GameSetting && GlobalD.GameSetting.maxLeaseMulti) {
  90. limitValue = GlobalD.GameSetting.maxLeaseMulti;
  91. console.log("配置的数据limitValue:",limitValue);
  92. }
  93. if (numberTemp.test(value)) {
  94. if (Number(value) >= 1 && Number(value) <= limitValue) {
  95. this.inputInviteValue = Number(value);
  96. } else if (Number(value) > limitValue) {
  97. //限制只能输入100
  98. this.inputInviteValue = limitValue;
  99. this.inputContainer.getComponent(cc.EditBox).string = this.inputInviteValue;
  100. } else {
  101. this.inputInviteValue = 1;
  102. this.inputContainer.getComponent(cc.EditBox).string = this.inputInviteValue;
  103. }
  104. } else {
  105. this.inputInviteValue = 1;
  106. this.inputContainer.getComponent(cc.EditBox).string = this.inputInviteValue;
  107. console.log("请输入整数的倍数", this.inputInviteValue);
  108. }
  109. this._updatePrice();
  110. },
  111. _updatePrice() {
  112. //this.inputInviteValue 默认是1倍
  113. // console.log( this.inputInviteValue);
  114. let _configLandInfo = this.leaseFarmlandInfoNode.getComponent("LeaseFarmlandInfo").configLandInfo
  115. switch (this.toggleInputValue) {
  116. case "1": //700
  117. this.totalPriceValue = _configLandInfo.rental1 * this.inputInviteValue;
  118. this.totalPriceLabel.string = this.totalPriceValue + "CNT";
  119. break;
  120. case "2": //2500
  121. this.totalPriceValue = _configLandInfo.rental2 * this.inputInviteValue;
  122. this.totalPriceLabel.string = this.totalPriceValue + "CNT";
  123. break;
  124. case "3": //5000
  125. this.totalPriceValue = _configLandInfo.rental3 * this.inputInviteValue;
  126. this.totalPriceLabel.string = this.totalPriceValue + "CNT";
  127. break;
  128. default:
  129. console.error("this.toggleInputValue 不是1 2 3!", this.toggleInputValue);
  130. break;
  131. }
  132. },
  133. /**
  134. * 显示面板时候,设置一下初始化数据
  135. */
  136. onInitLeaseInfo(multiple) {
  137. //这里处理倍数
  138. this.inputInviteValue = multiple;
  139. this.inputContainer.getComponent(cc.EditBox).string = this.inputInviteValue;
  140. this._updatePrice();
  141. },
  142. //租赁支付
  143. onLeaseInfo() {
  144. console.log('支付总额:' + this.totalPriceValue);
  145. if (!GlobalD.dapp) {
  146. console.error("onBindInviteInfo warn,dapp未初始化!");
  147. return;
  148. }
  149. let _self = this;
  150. //cntAmount,payType,itemType,callback
  151. let _configLandInfo = this.leaseFarmlandInfoNode.getComponent("LeaseFarmlandInfo").configLandInfo
  152. //租赁的itemType 要处理一下
  153. let _itemType = this.toggleInputValue + "or" + _configLandInfo.id;
  154. GlobalD.GameData.payCNT(this.totalPriceValue, 1, _itemType, (data) => {
  155. console.log("土地支付:", this.totalPriceValue, "== playType=", 1, ",item_type==", _itemType);
  156. console.log("支付data:", data);
  157. const [err, tx] = data;
  158. if (err === null) {
  159. // TODO 成功, 支付为链上操作,需要提供回调接口给这边服务端确认交易成功后修改购买订单结果
  160. console.log(tx) // 交易hash,唯一标识符
  161. let _leaseFarmlandInfo = this.leaseFarmlandInfoNode.getComponent("LeaseFarmlandInfo");
  162. //todo 如果是土地租赁,轮询土地状态
  163. if (this.AutoGetLandData) {
  164. this.unschedule(this.AutoGetLandData);
  165. this.AutoGetLandData = null;
  166. }
  167. this.AutoGetLandData = () => {
  168. GlobalD.GameData.getLandState({
  169. landId: _configLandInfo.id,
  170. callback: (value) => {
  171. console.log("轮询土地数据:" + JSON.stringify(value.msg));
  172. if (0 === value.code) {
  173. let _landData = value.data;
  174. let _leaseLandInfo = _leaseFarmlandInfo.leaseLandInfo
  175. //如果是空的,说明没有创建过土地
  176. if (_leaseLandInfo == null) {
  177. this.unschedule(this.AutoGetLandData);
  178. // console.log("轮询到土地购买成功!");
  179. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "土地购买成功!", 2);
  180. //解锁土地操作
  181. _leaseFarmlandInfo.setLeaseLandInfo(_landData, true);
  182. this.onClose();
  183. } else {
  184. //根据update时间判断
  185. if (!date.datesIsEqual(_leaseLandInfo.updateTime, _landData.updateTime)) {
  186. //如果不是空,判断为续租
  187. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "续租土地成功!", 2);
  188. //续租土地时候, 只更新 租赁相关参照
  189. let _oldLeaseLandInfo = Object.assign({}, _leaseLandInfo, {
  190. isLease: _landData.isLease,
  191. isPlant: _landData.isPlant,
  192. leaseDays: _landData.leaseDays,
  193. leaseDaysMill: _landData.leaseDaysMill,
  194. leaseDaysRemaining: _landData.leaseDaysRemaining,
  195. leaseTime: _landData.leaseTime,
  196. rentalExpenses: _landData.rentalExpenses,
  197. updateTime: _landData.updateTime
  198. });
  199. // console.log("续租之后的信息:", _landData, _leaseLandInfo, _oldLeaseLandInfo);
  200. _leaseFarmlandInfo.setLeaseLandInfo(_oldLeaseLandInfo, true);
  201. this.onClose();
  202. }
  203. }
  204. }
  205. }
  206. });
  207. };
  208. this.schedule(this.AutoGetLandData, 5);
  209. } else {
  210. console.log(err)
  211. }
  212. });
  213. // GlobalD.dapp.bindParent(_self.inputInviteValue).then((data) => {
  214. // if (data.err === null) {
  215. // // 返回成功,见下面消息体
  216. // console.log(data.res)
  217. // //绑定成功时候,隐藏输入框,设置父节点
  218. // GlobalD.Dapp.UserInfo.parent = data.res;
  219. // _self.parentInviteValue.string = GlobalD.Dapp.UserInfo.parent.invite_code;
  220. // //如果存在父节点,隐藏输入框
  221. // _self.inputContainer.active = false;
  222. // } else {
  223. // //TODO 服务错误
  224. // console.log(data.err)
  225. // }
  226. // })
  227. },
  228. onClose() {
  229. // if (this.AutoGetLandData) {
  230. // this.unschedule(this.AutoGetLandData);
  231. // this.AutoGetLandData = null;
  232. // }
  233. this.node.destroy();
  234. },
  235. onDestroy() {
  236. if (this.AutoGetLandData) {
  237. this.unschedule(this.AutoGetLandData);
  238. this.AutoGetLandData = null;
  239. }
  240. }
  241. });