LeaseInfo.js 9.1 KB

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