LeaseInfo.js 9.9 KB

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