LeaseInfo.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. let _configLandInfo = this.leaseFarmlandInfoNode.getComponent("LeaseFarmlandInfo").configLandInfo
  106. switch (this.toggleInputValue) {
  107. case "1": //700
  108. this.totalPriceValue = _configLandInfo.rental1 * this.inputInviteValue;
  109. this.totalPriceLabel.string = this.totalPriceValue + "CNT";
  110. break;
  111. case "2": //2500
  112. this.totalPriceValue = _configLandInfo.rental2 * this.inputInviteValue;
  113. this.totalPriceLabel.string = this.totalPriceValue + "CNT";
  114. break;
  115. case "3": //5000
  116. this.totalPriceValue = _configLandInfo.rental3 * this.inputInviteValue;
  117. this.totalPriceLabel.string = this.totalPriceValue + "CNT";
  118. break;
  119. default:
  120. console.error("this.toggleInputValue 不是1 2 3!", this.toggleInputValue);
  121. break;
  122. }
  123. },
  124. /**
  125. * 显示面板时候,设置一下初始化数据
  126. */
  127. onInitLeaseInfo(multiple) {
  128. //这里处理倍数
  129. this.inputInviteValue = multiple;
  130. this.inputContainer.getComponent(cc.EditBox).string = this.inputInviteValue;
  131. this._updatePrice();
  132. },
  133. //租赁支付
  134. onLeaseInfo() {
  135. console.log('支付总额:' + this.totalPriceValue);
  136. if (!GlobalD.dapp) {
  137. console.error("onBindInviteInfo warn,dapp未初始化!");
  138. return;
  139. }
  140. let _self = this;
  141. //cntAmount,payType,itemType,callback
  142. let _configLandInfo = this.leaseFarmlandInfoNode.getComponent("LeaseFarmlandInfo").configLandInfo
  143. //租赁的itemType 要处理一下
  144. let _itemType = this.toggleInputValue + "or" + _configLandInfo.id;
  145. GlobalD.GameData.payCNT(this.totalPriceValue, 1, _itemType, (data) => {
  146. console.log("土地支付:", this.totalPriceValue, "== playType=", 1, ",item_type==", _itemType);
  147. console.log("支付data:", data);
  148. const [err, tx] = data;
  149. if (err === null) {
  150. // TODO 成功, 支付为链上操作,需要提供回调接口给这边服务端确认交易成功后修改购买订单结果
  151. console.log(tx) // 交易hash,唯一标识符
  152. let _leaseFarmlandInfo = this.leaseFarmlandInfoNode.getComponent("LeaseFarmlandInfo");
  153. //todo 如果是土地租赁,轮询土地状态
  154. if (this.AutoGetLandData) {
  155. this.unschedule(this.AutoGetLandData);
  156. this.AutoGetLandData = null;
  157. }
  158. this.AutoGetLandData = () => {
  159. GlobalD.GameData.getLandState({
  160. landId: _configLandInfo.id,
  161. callback: (res, value) => {
  162. console.log("轮询土地数据:" + JSON.stringify(value.msg));
  163. if (0 === value.code) {
  164. let _landData = value.data;
  165. let _leaseLandInfo = _leaseFarmlandInfo.leaseLandInfo
  166. //如果是空的,说明没有创建过土地
  167. if (_leaseLandInfo == null) {
  168. this.unschedule(this.AutoGetLandData);
  169. // console.log("轮询到土地购买成功!");
  170. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "土地购买成功!", 2);
  171. //解锁土地操作
  172. _leaseFarmlandInfo.setLeaseLandInfo(_landData,true);
  173. this.onClose();
  174. } else {
  175. //根据update时间判断
  176. if (!date.datesIsEqual(_leaseLandInfo.updateTime, _landData.updateTime)) {
  177. //如果不是空,判断为续租
  178. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "续租土地成功!", 2);
  179. //续租土地时候, 只更新 租赁相关参照
  180. let _oldLeaseLandInfo = Object.assign({}, _leaseLandInfo, {
  181. isLease: _landData.isLease,
  182. isPlant: _landData.isPlant,
  183. leaseDays: _landData.leaseDays,
  184. leaseDaysMill: _landData.leaseDaysMill,
  185. leaseDaysRemaining: _landData.leaseDaysRemaining,
  186. leaseTime: _landData.leaseTime,
  187. rentalExpenses: _landData.rentalExpenses,
  188. updateTime: _landData.updateTime
  189. });
  190. // console.log("续租之后的信息:", _landData, _leaseLandInfo, _oldLeaseLandInfo);
  191. _leaseFarmlandInfo.setLeaseLandInfo(_oldLeaseLandInfo,true);
  192. this.onClose();
  193. }
  194. }
  195. }
  196. }
  197. });
  198. };
  199. this.schedule(this.AutoGetLandData, 5);
  200. } else {
  201. console.log(err)
  202. }
  203. });
  204. // GlobalD.dapp.bindParent(_self.inputInviteValue).then((data) => {
  205. // if (data.err === null) {
  206. // // 返回成功,见下面消息体
  207. // console.log(data.res)
  208. // //绑定成功时候,隐藏输入框,设置父节点
  209. // GlobalD.Dapp.UserInfo.parent = data.res;
  210. // _self.parentInviteValue.string = GlobalD.Dapp.UserInfo.parent.invite_code;
  211. // //如果存在父节点,隐藏输入框
  212. // _self.inputContainer.active = false;
  213. // } else {
  214. // //TODO 服务错误
  215. // console.log(data.err)
  216. // }
  217. // })
  218. },
  219. onClose() {
  220. // if (this.AutoGetLandData) {
  221. // this.unschedule(this.AutoGetLandData);
  222. // this.AutoGetLandData = null;
  223. // }
  224. this.node.destroy();
  225. },
  226. onDestroy() {
  227. if (this.AutoGetLandData) {
  228. this.unschedule(this.AutoGetLandData);
  229. this.AutoGetLandData = null;
  230. }
  231. }
  232. });