StoreView.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. import date from "../Unit/date.js"
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. StoreScrollView: {
  6. default: null,
  7. type: cc.ScrollView
  8. },
  9. Seed: {
  10. default: null,
  11. type: cc.Node,
  12. },
  13. Exchange: {
  14. default: null,
  15. type: cc.Node,
  16. },
  17. SeedBtn: {
  18. default: null,
  19. type: cc.Node,
  20. },
  21. ExchangeBtn: {
  22. default: null,
  23. type: cc.Node,
  24. },
  25. EquipmentCotainer: {
  26. default: null,
  27. type: cc.Node,
  28. },
  29. EquipmentBtn: {
  30. default: null,
  31. type: cc.Node,
  32. },
  33. SelectedFrame: cc.Node,
  34. ManageUI: cc.Node,
  35. //四个按钮对应的sprite
  36. Normal_Seed_Sprite: cc.SpriteFrame,
  37. Selecteded_Seed_Sprite: cc.SpriteFrame,
  38. Normal_Exchange_Sprite: cc.SpriteFrame,
  39. Selecteded_Exchange_Sprite: cc.SpriteFrame,
  40. Normal_EquipmentBtn_Sprite: cc.SpriteFrame,
  41. Selecteded_EquipmentBtn_Sprite: cc.SpriteFrame,
  42. //需要切换节点顺序的父节点
  43. container: cc.Node,
  44. //商城的列表
  45. mySeedList: [],
  46. seedPrefab: cc.Prefab,
  47. seedSpriteFrame: {
  48. default: [],
  49. type: [cc.SpriteFrame],
  50. },
  51. //商城的图标列表
  52. mySeedIconList: {
  53. default: [],
  54. type: [cc.SpriteFrame],
  55. },
  56. seedNodeArry: {
  57. default: [],
  58. type: [cc.Node],
  59. serializable: false,
  60. visible: false,
  61. },
  62. //装备start---
  63. equipmentList: [],
  64. equipPrefab: cc.Prefab,
  65. equipNodeArry: {
  66. default: [],
  67. type: [cc.Node],
  68. serializable: false,
  69. visible: false,
  70. },
  71. equipSpriteFrame: {
  72. default: [],
  73. type: [cc.SpriteFrame],
  74. },
  75. equipIconList: {
  76. default: [],
  77. type: [cc.SpriteFrame],
  78. },
  79. buyEquipIndex: -1,
  80. PayEquipPopView: {
  81. default: null,
  82. type: cc.Node,
  83. },
  84. //装备end---
  85. snbAmount: 0,
  86. luckyAmount: 0,
  87. playAmount: 0,
  88. ExchangePopView: {
  89. default: null,
  90. type: cc.Node,
  91. },
  92. ExchangeLuckyPopView: {
  93. default: null,
  94. type: cc.Node,
  95. },
  96. PayCNTPopView: {
  97. default: null,
  98. type: cc.Node,
  99. },
  100. PaySNBPopView: {
  101. default: null,
  102. type: cc.Node,
  103. },
  104. buySeedIndex: -1,
  105. snbAmountLabel: {
  106. default: null,
  107. type: cc.Label,
  108. },
  109. luckyAmountLabel: {
  110. default: null,
  111. type: cc.Label,
  112. },
  113. //拿到仓库列表的节点
  114. buildingView: {
  115. default: null,
  116. type: cc.Node,
  117. },
  118. myWareHouseSeedList: null,
  119. //轮询种子列表状态
  120. AutoGetSeedsData: null,
  121. //轮询防护包装备等
  122. AutoGetEquipsData: null,
  123. seedsView: {
  124. default: null,
  125. type: cc.Node,
  126. },
  127. // ExchangeView: {
  128. // default: null,
  129. // type: cc.Node,
  130. // },
  131. seedBottomLabel: {
  132. default: null,
  133. type: cc.Node,
  134. },
  135. scrollViewNode: {
  136. default: null,
  137. type: cc.Node,
  138. },
  139. //购买种类数值
  140. toggleInputValue: {
  141. default: '1',
  142. visible: false,
  143. },
  144. PayCNTPopViewNameLabel: {
  145. default: null,
  146. type: cc.Label,
  147. },
  148. PayCNTPopViewCNTLabel: {
  149. default: null,
  150. type: cc.Label,
  151. },
  152. PayCNTPopViewSNBLabel: {
  153. default: null,
  154. type: cc.Label,
  155. },
  156. PayCNTPopViewMutureDurLabel: {
  157. default: null,
  158. type: cc.Label,
  159. },
  160. PopViewSeed: {
  161. default: null,
  162. type: cc.Sprite,
  163. },
  164. isPlaying: false,
  165. //拿到申请列表的节点
  166. applySnbView: {
  167. default: null,
  168. type: cc.Node,
  169. },
  170. },
  171. HiddenAll() {
  172. if (this.Seed.active) this.Seed.active = false
  173. if (this.Exchange.active) this.Exchange.active = false
  174. if (this.EquipmentCotainer.active) this.EquipmentCotainer.active = false;
  175. if (this.StoreScrollView) {
  176. this.StoreScrollView.stopAutoScroll();
  177. this.StoreScrollView.scrollToTop();
  178. }
  179. },
  180. start() {
  181. //获取到仓库列表的脚本
  182. this.buildingViewScript = this.buildingView.getComponent('BuildingView')
  183. //默认设置第一个节点为 最上面
  184. this.SeedBtn.setSiblingIndex(10)
  185. // console.log('SNB=======', GlobalD.GameData.SNB)
  186. this.snbAmountLabel.string = GlobalD.GameData.SNB
  187. this.luckyAmountLabel.string = GlobalD.GameData.GetLucky();
  188. //这里也初始化获取一个数据
  189. GlobalD.GameData.getWarehouseSeedAndFruit((vaule) => {
  190. this.myWareHouseSeedList = vaule.data.seed
  191. })
  192. //获取种子和果实
  193. GlobalD.GameData.getMallSeed((vaule) => {
  194. this.mySeedList = vaule.data
  195. // console.log('this.mySeedList[i]=', this.mySeedList);
  196. for (let i = 0; i < this.mySeedList.length; i++) {
  197. //没有种子数量不显示 todo 看看后续需不需要处理删除
  198. //if (this.mySeedList[i].amount < 1) continue;
  199. let _seed = cc.instantiate(this.seedPrefab)
  200. //if (this.mySeedList[i].amount < 1)
  201. if (this.mySeedList[i].amount < 1) {
  202. _seed.getChildByName("Sell").active = false;
  203. _seed.getChildByName("SellOut").active = true;
  204. } else {
  205. _seed.getChildByName("Sell").active = true;
  206. _seed.getChildByName('SellOut').active = false
  207. }
  208. // _seed.getChildByName("buy_button").active = true;
  209. // _seed.getChildByName("Name").active = false;
  210. // _seed.getChildByName("Name").getChildByName("Num").getComponent(cc.Label).strng = '';//'x'+this.mySeedList[i].amount;
  211. this.seedNodeArry.push(_seed)
  212. _seed.parent = this.Seed
  213. let _seedScript = _seed.getComponent('Content_Button')
  214. _seedScript.Name = this.mySeedList[i].name
  215. _seedScript.Price = ''
  216. _seedScript.CNT = this.mySeedList[i].priceCnt
  217. _seedScript.SNB = this.mySeedList[i].priceSnb
  218. _seedScript.Mature = "成熟期:" + this.mySeedList[i].maturity + '天'
  219. _seedScript.Synopsis = this.mySeedList[i].seedDescribe
  220. //设置一个生成点
  221. let _spawnScript = _seed.getComponent('Content_seed')
  222. _spawnScript.myIndex = i
  223. _spawnScript.SpawnPoint = cc.find('Canvas/SpawnParent/SpawnPoint')
  224. _seedScript.NumLabel.string = this.mySeedList[i].amount
  225. _seedScript.NameLabel.string = this.mySeedList[i].name
  226. switch (this.mySeedList[i].picture) {
  227. case 'Cabbage':
  228. _seedScript.bgSprite.spriteFrame = this.mySeedIconList[0] //this.seedSpriteFrame[0];
  229. break
  230. case 'Potato':
  231. _seedScript.bgSprite.spriteFrame = this.mySeedIconList[1] //this.seedSpriteFrame[1];
  232. break
  233. case 'Carrot':
  234. _seedScript.bgSprite.spriteFrame = this.mySeedIconList[2] //this.seedSpriteFrame[2];
  235. break
  236. case 'Broccoli':
  237. _seedScript.bgSprite.spriteFrame = this.mySeedIconList[3] //this.seedSpriteFrame[3];
  238. break
  239. case 'Tomato':
  240. _seedScript.bgSprite.spriteFrame = this.mySeedIconList[4] //this.seedSpriteFrame[4];
  241. break
  242. case 'Squash':
  243. _seedScript.bgSprite.spriteFrame = this.mySeedIconList[5] //this.seedSpriteFrame[5];
  244. break
  245. case 'Eggplant':
  246. _seedScript.bgSprite.spriteFrame = this.mySeedIconList[6] //this.seedSpriteFrame[6];
  247. break
  248. case 'Pepper':
  249. _seedScript.bgSprite.spriteFrame = this.mySeedIconList[7] //this.seedSpriteFrame[7];
  250. break
  251. case 'Lentil':
  252. _seedScript.bgSprite.spriteFrame = this.mySeedIconList[8] //this.seedSpriteFrame[8];
  253. break
  254. default:
  255. break
  256. }
  257. this.mySeedList[i].bgSpriteFrame = _seedScript.bgSprite.spriteFrame;
  258. }
  259. })
  260. //获取装备列表
  261. GlobalD.GameData.onGetMallEquipmentList((vaule) => {
  262. this.equipmentList = vaule.data
  263. console.log('this.equipmentList=', this.equipmentList);
  264. for (let i = 0; i < this.equipmentList.length; i++) {
  265. let _equip = cc.instantiate(this.equipPrefab)
  266. if (this.equipmentList[i].amount < 1) {
  267. _equip.getChildByName("Sell").active = false;
  268. _equip.getChildByName("SellOut").active = true;
  269. } else {
  270. _equip.getChildByName("Sell").active = true;
  271. _equip.getChildByName('SellOut').active = false
  272. }
  273. this.equipNodeArry.push(_equip)
  274. _equip.parent = this.EquipmentCotainer;
  275. let _equipScript = _equip.getComponent('Content_Button')
  276. _equipScript.Name = this.equipmentList[i].name
  277. // _equipScript.Price = ''
  278. _equipScript.CNT = this.equipmentList[i].priceCnt;
  279. _equipScript.SNB = this.equipmentList[i].priceSnb
  280. _equipScript.Mature = "有效期:" + this.equipmentList[i].effectiveDay + '天'
  281. _equipScript.Synopsis = this.equipmentList[i].itemDescribe;
  282. //设置一个生成点
  283. let _spawnScript = _equip.getComponent('Content_equip')
  284. _spawnScript.onSetStoreView(this.node);
  285. _spawnScript.myIndex = i
  286. _equipScript.NumLabel.string = this.equipmentList[i].amount
  287. _equipScript.NameLabel.string = this.equipmentList[i].name
  288. switch (this.equipmentList[i].picture) {
  289. case 'dog':
  290. _equipScript.bgSprite.spriteFrame = this.equipIconList[0];
  291. _equipScript.showConsumeType = 2;
  292. break
  293. case 'beatDogStick':
  294. _equipScript.bgSprite.spriteFrame = this.equipIconList[1];
  295. _equipScript.showConsumeType = 2;
  296. break
  297. case 'natural':
  298. _equipScript.bgSprite.spriteFrame = this.equipIconList[2];
  299. _equipScript.showConsumeType = 1;
  300. break
  301. case 'beast':
  302. _equipScript.bgSprite.spriteFrame = this.equipIconList[3];
  303. _equipScript.showConsumeType = 1;
  304. break
  305. default:
  306. break
  307. }
  308. this.equipmentList[i].bgSpriteFrame = _equipScript.bgSprite.spriteFrame;
  309. }
  310. })
  311. },
  312. //切换建筑的面板
  313. onSwitchBuildingContent(event, index) {
  314. this.SeedBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Seed_Sprite
  315. this.ExchangeBtn.getComponent(cc.Sprite).spriteFrame =
  316. this.Normal_Exchange_Sprite
  317. this.EquipmentBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_EquipmentBtn_Sprite
  318. this.HiddenAll()
  319. //面板设置index
  320. //种子
  321. if ('0' == index) {
  322. this.SeedBtn.getComponent(cc.Sprite).spriteFrame =
  323. this.Selecteded_Seed_Sprite
  324. this.Seed.active = true
  325. if (this.seedNodeArry.length != 0) {
  326. this.SelectedFrame.parent = this.seedNodeArry[0]
  327. }
  328. this.scrollViewNode.active = true
  329. this.seedBottomLabel.active = true
  330. this.seedsView.active = true
  331. this.Exchange.active = false
  332. }
  333. //兑换
  334. else if ('1' == index) {
  335. this.ExchangeBtn.getComponent(cc.Sprite).spriteFrame =
  336. this.Selecteded_Exchange_Sprite
  337. this.scrollViewNode.active = false
  338. this.seedBottomLabel.active = false
  339. this.seedsView.active = false
  340. this.Exchange.active = true
  341. }
  342. //装备
  343. else if ('2' == index) {
  344. this.EquipmentBtn.getComponent(cc.Sprite).spriteFrame =
  345. this.Selecteded_EquipmentBtn_Sprite
  346. this.EquipmentCotainer.active = true;
  347. this.scrollViewNode.active = true
  348. this.seedBottomLabel.active = true
  349. this.seedsView.active = true
  350. this.Exchange.active = false
  351. }
  352. },
  353. switch: function () {
  354. var children = this.container.children
  355. var length = children.length
  356. if (length > 1) {
  357. var src = Math.floor(Math.random() * length)
  358. var node = children[src]
  359. var dst = src === length - 1 ? 0 : src + 1
  360. node.setSiblingIndex(dst)
  361. }
  362. },
  363. OpentSNBExchangeToCNT() {
  364. // GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "转换CNT通道关闭!", 1);
  365. // return;
  366. //限制相关人员交易
  367. if (1 === GlobalD.UserInfo.limitTran) {
  368. GlobalD.GameData.showToast(
  369. cc.find('Canvas/UICamera'),
  370. '转换CNT通道关闭,请联系相关管理人员!',
  371. 1
  372. )
  373. return
  374. }
  375. //更新数据
  376. this.snbAmountLabel.string = GlobalD.GameData.SNB
  377. this.ExchangePopView.active = true
  378. },
  379. SNBExchangeToCNT() {
  380. //限制相关人员交易
  381. if (1 === GlobalD.UserInfo.limitTran) {
  382. GlobalD.GameData.showToast(
  383. cc.find('Canvas/UICamera'),
  384. '转换CNT通道关闭,请联系相关管理人员!',
  385. 1
  386. )
  387. return
  388. }
  389. if (0 === Number(this.snbAmount) || 0 != Number(this.snbAmount) % 5) {
  390. GlobalD.GameData.showToast(
  391. cc.find('Canvas/UICamera'),
  392. '请输入5的倍数兑换!',
  393. 1
  394. )
  395. return
  396. }
  397. console.log('发起兑换:' + Number(this.snbAmount))
  398. GlobalD.GameData.onSnbToCnt(Number(this.snbAmount), (data) => {
  399. //更新数据
  400. this.snbAmountLabel.string = GlobalD.GameData.SNB
  401. this.ExchangePopView.active = false
  402. console.log('data=', data)
  403. // console.log('res=', res)
  404. })
  405. },
  406. /**
  407. * 提交申请
  408. */
  409. onApplyingTran() {
  410. //限制相关人员交易
  411. if (1 === GlobalD.UserInfo.limitTran) {
  412. GlobalD.GameData.showToast(
  413. cc.find('Canvas/UICamera'),
  414. '转换CNT通道关闭,请联系相关管理人员!',
  415. 1
  416. )
  417. return
  418. }
  419. if (0 === Number(this.snbAmount) || 0 != Number(this.snbAmount) % 5) {
  420. GlobalD.GameData.showToast(
  421. cc.find('Canvas/UICamera'),
  422. '请输入5的倍数兑换!',
  423. 1
  424. )
  425. return
  426. }
  427. console.log('申请兑换:' + Number(this.snbAmount))
  428. GlobalD.GameData.onPostApplySnbToCnt(Number(this.snbAmount), (value) => {
  429. console.log("兑换", value);
  430. if (0 === value.code) {
  431. this.ExchangePopView.active = false
  432. GlobalD.GameData.showToast(
  433. cc.find('Canvas/UICamera'),
  434. '已提交申请,详情见申请列表兑换CNT。',
  435. 2
  436. )
  437. } else {
  438. GlobalD.GameData.showToast(
  439. cc.find('Canvas/UICamera'),
  440. value.msg,
  441. 2
  442. )
  443. }
  444. });
  445. },
  446. /**
  447. * 幸运值转换snb
  448. * @returns
  449. */
  450. OpenLuckyExchangeToSNB() {
  451. //限制相关人员交易
  452. if (1 === GlobalD.UserInfo.limitTran) {
  453. GlobalD.GameData.showToast(
  454. cc.find('Canvas/UICamera'),
  455. '转换SNB通道关闭,请联系相关管理人员!',
  456. 1
  457. )
  458. return
  459. }
  460. //更新数据
  461. this.luckyAmountLabel.string = GlobalD.GameData.GetLucky();
  462. this.ExchangeLuckyPopView.active = true
  463. },
  464. LuckyExchangeToSNB() {
  465. //限制相关人员交易
  466. if (1 === GlobalD.UserInfo.limitTran) {
  467. GlobalD.GameData.showToast(
  468. cc.find('Canvas/UICamera'),
  469. '转换SNB通道关闭,请联系相关管理人员!',
  470. 1
  471. )
  472. return
  473. }
  474. if (0 >= Number(this.luckyAmount) || 0 != Number(this.luckyAmount) % 50) {
  475. GlobalD.GameData.showToast(
  476. cc.find('Canvas/UICamera'),
  477. '请输入50的倍数兑换!',
  478. 1
  479. )
  480. return
  481. }
  482. console.log('发起兑换:' + Number(this.luckyAmount))
  483. GlobalD.GameData.onPostLuckToSnb({ luckNum: Number(this.luckyAmount) }, (value) => {
  484. //更新数据
  485. this.luckyAmountLabel.string = GlobalD.GameData.GetLucky();
  486. console.log(GlobalD.GameData.GetLucky());
  487. this.ExchangeLuckyPopView.active = false;
  488. })
  489. },
  490. onTextChangedLucky: function (text, editbox, customEventData) {
  491. var numberTemp = new RegExp('^[A-Za-z0-9]+$')
  492. if (numberTemp.test(text)) {
  493. if (Number(text) >= 1) {
  494. this.luckyAmount = Number(text)
  495. } else {
  496. this.luckyAmount = 0
  497. editbox.string = this.luckyAmount
  498. }
  499. } else {
  500. this.luckyAmount = 0
  501. editbox.string = this.luckyAmount
  502. console.log('请输入50的倍数', this.luckyAmount)
  503. }
  504. },
  505. onPayCNTPopView(myIndex) {
  506. this.buySeedIndex = myIndex
  507. this.PayCNTPopView.active = true
  508. this._updatePrice()
  509. },
  510. onPaySNBPopView(myIndex) {
  511. this.buySeedIndex = myIndex
  512. this.PaySNBPopView.active = true
  513. // console.log('this.mySeedList[this.buySeedIndex] =',this.mySeedList[this.buySeedIndex])
  514. this.PaySNBPopView.getChildByName('BG')
  515. .getChildByName('BG1_sprite_splash')
  516. .getChildByName('NameLabel')
  517. .getChildByName('label')
  518. .getComponent(cc.Label).string = this.mySeedList[this.buySeedIndex].name
  519. this.PaySNBPopView.getChildByName('BG')
  520. .getChildByName('BG1_sprite_splash')
  521. .getChildByName('PriceLabel')
  522. .getChildByName('label')
  523. .getComponent(cc.Label).string =
  524. this.mySeedList[this.buySeedIndex].priceCnt + ' SNB'
  525. this.PaySNBPopView.getChildByName('BG')
  526. .getChildByName('BG1_sprite_splash')
  527. .getChildByName('MutureDurLabel')
  528. .getChildByName('label')
  529. .getComponent(cc.Label).string =
  530. this.mySeedList[this.buySeedIndex].maturity
  531. },
  532. PayCNT() {
  533. console.log('this.playAmount=', this.playAmount)
  534. console.log(
  535. 'this.playAmount price=',
  536. this.playAmount * parseInt(this.mySeedList[this.buySeedIndex].priceCnt)
  537. )
  538. //购买种子也要使用轮询
  539. let currentAmount = 0
  540. for (let i = 0; i < this.myWareHouseSeedList.length; i++) {
  541. if (
  542. this.myWareHouseSeedList[i].id == this.mySeedList[this.buySeedIndex].id
  543. ) {
  544. currentAmount = this.myWareHouseSeedList[i].amount
  545. }
  546. }
  547. //当前背包的数量
  548. console.log('currentAmount:', {
  549. seedId: this.mySeedList[this.buySeedIndex].id,
  550. currentAmount: currentAmount, //把当前背包的数据传进去
  551. })
  552. let playAmount =
  553. this.playAmount * parseInt(this.mySeedList[this.buySeedIndex].priceCnt)
  554. if (0 === playAmount) {
  555. GlobalD.GameData.showToast(
  556. cc.find('Canvas/UICamera'),
  557. '请输入购买数量',
  558. 1
  559. )
  560. return
  561. }
  562. // if (this.isPlaying) {
  563. // GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "购买过快!", 1);
  564. // return;
  565. // }
  566. // this.isPlaying = true;
  567. // GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "区块确认中,请耐心等待!", 120);
  568. GlobalD.GameData.payCNT(
  569. playAmount,
  570. 4,
  571. this.mySeedList[this.buySeedIndex].id + '',
  572. (data) => {
  573. const [err, tx] = data
  574. // this.isPlaying = false;
  575. if (err === null) {
  576. // TODO 成功, 支付为链上操作,需要提供回调接口给这边服务端确认交易成功后修改购买订单结果
  577. console.log(tx) // 交易hash,唯一标识符
  578. //todo 种子
  579. if (this.AutoGetSeedsData) {
  580. this.unschedule(this.AutoGetSeedsData)
  581. this.AutoGetSeedsData = null
  582. }
  583. this.AutoGetSeedsData = () => {
  584. // getSeedCount ++;
  585. GlobalD.GameData.onGetSeedState(
  586. {
  587. seedId: this.mySeedList[this.buySeedIndex].id,
  588. currentAmount: currentAmount, //把当前背包的数据传进去
  589. },
  590. (value) => {
  591. console.log('轮询种子:' + JSON.stringify(value))
  592. if (0 === value.code) {
  593. this.unschedule(this.AutoGetSeedsData)
  594. console.log('轮询种子购买成功')
  595. GlobalD.GameData.showToast(
  596. cc.find('Canvas/UICamera'),
  597. '种子购买成功!',
  598. 1
  599. )
  600. // this.PayCNTPopView.active = false;
  601. //直接重新初始化一下列表,
  602. this.buildingViewScript.onUpdateList()
  603. }
  604. }
  605. )
  606. }
  607. this.schedule(this.AutoGetSeedsData, 5)
  608. } else {
  609. console.log(err)
  610. GlobalD.GameData.showToast(cc.find('Canvas/UICamera'), err, 2)
  611. }
  612. }
  613. )
  614. },
  615. PaySNB() {
  616. console.log('this.playAmount=', this.playAmount)
  617. console.log('priceSnb price=', this.mySeedList[this.buySeedIndex].priceSnb)
  618. console.log(
  619. 'this.playAmount price=',
  620. this.playAmount * parseInt(this.mySeedList[this.buySeedIndex].priceSnb)
  621. )
  622. console.log(
  623. '支付价格:' +
  624. this.playAmount * parseInt(this.mySeedList[this.buySeedIndex].priceSnb)
  625. )
  626. let playAmount =
  627. this.playAmount * parseInt(this.mySeedList[this.buySeedIndex].priceSnb)
  628. if (0 === playAmount) {
  629. GlobalD.GameData.showToast(
  630. cc.find('Canvas/UICamera'),
  631. '请输入购买数量',
  632. 1
  633. )
  634. return
  635. }
  636. if (this.isPlaying) {
  637. GlobalD.GameData.showToast(cc.find('Canvas/UICamera'), '购买过快!', 1)
  638. return
  639. }
  640. GlobalD.GameData.showToast(cc.find('Canvas/UICamera'), '支付处理中...', 10)
  641. this.isPlaying = true
  642. GlobalD.GameData.onBuySeedsWithSNB(
  643. playAmount,
  644. this.mySeedList[this.buySeedIndex].id,
  645. (flag, value) => {
  646. // console.log("SNB购买后", value);
  647. this.isPlaying = false
  648. if (flag && 0 === value.code) {
  649. //todo 种子
  650. GlobalD.GameData.showToast(
  651. cc.find('Canvas/UICamera'),
  652. '种子购买成功!',
  653. 1
  654. )
  655. // this.PayCNTPopView.active = false;
  656. //重新获取一下snb,会自动更新到面板
  657. GlobalD.GameData.onGetUserSnb()
  658. //直接重新初始化一下列表,
  659. this.buildingViewScript.onUpdateList()
  660. //更新日志数据
  661. cc.find('GameNode/ManageDapp')
  662. .getComponent('ManageDapp')
  663. .onUpdateSnbList()
  664. } else {
  665. GlobalD.GameData.showToast(cc.find('Canvas/UICamera'), value.msg, 2)
  666. }
  667. }
  668. )
  669. },
  670. onTextChanged1: function (text, editbox, customEventData) {
  671. // 这里的 text 表示 修改完后的 EditBox 的文本内容
  672. // 这里 editbox 是一个 cc.EditBox 对象
  673. // 这里的 customEventData 参数就等于你之前设置的 "foobar"
  674. // this.playAmount = parseInt(text)
  675. var numberTemp = new RegExp('^[A-Za-z0-9]+$')
  676. if (numberTemp.test(text)) {
  677. if (Number(text) >= 1) {
  678. this.playAmount = Number(text)
  679. } else {
  680. this.playAmount = 0
  681. editbox.string = this.playAmount
  682. }
  683. } else {
  684. this.playAmount = 0
  685. editbox.string = this.playAmount
  686. // console.log('playAmount', this.snbAmount)
  687. }
  688. },
  689. // 假设这个回调是给 textChanged 事件的
  690. onTextChanged2: function (text, editbox, customEventData) {
  691. // 这里的 text 表示 修改完后的 EditBox 的文本内容
  692. // 这里 editbox 是一个 cc.EditBox 对象
  693. // 这里的 customEventData 参数就等于你之前设置的 "foobar"
  694. // this.snbAmount = parseInt(text);
  695. var numberTemp = new RegExp('^[A-Za-z0-9]+$')
  696. if (numberTemp.test(text)) {
  697. if (Number(text) >= 1) {
  698. this.snbAmount = Number(text)
  699. } else {
  700. this.snbAmount = 0
  701. editbox.string = this.snbAmount
  702. }
  703. } else {
  704. this.snbAmount = 0
  705. editbox.string = this.snbAmount
  706. console.log('请输入5的倍数', this.snbAmount)
  707. }
  708. },
  709. _updatePrice() {
  710. this.PayCNTPopViewNameLabel.string = this.mySeedList[this.buySeedIndex].name;
  711. this.PayCNTPopViewCNTLabel.string = this.mySeedList[this.buySeedIndex].priceCnt;
  712. this.PayCNTPopViewSNBLabel.string = this.mySeedList[this.buySeedIndex].priceSnb;
  713. this.PayCNTPopViewMutureDurLabel.string = this.mySeedList[this.buySeedIndex].maturity;
  714. this.PopViewSeed.spriteFrame = this.mySeedList[this.buySeedIndex].bgSpriteFrame;
  715. // switch (this.toggleInputValue) {
  716. // case '1':
  717. // this.PayCNTPopViewNameLabel.string =
  718. // this.mySeedList[this.buySeedIndex].name
  719. // this.PayCNTPopViewCNTLabel.string =
  720. // this.mySeedList[this.buySeedIndex].priceCnt + ' CNT'
  721. // this.PayCNTPopViewMutureDurLabel.string =
  722. // this.mySeedList[this.buySeedIndex].maturity
  723. // break
  724. // case '2':
  725. // this.PayCNTPopViewNameLabel.string =
  726. // this.mySeedList[this.buySeedIndex].name
  727. // this.PayCNTPopViewCNTLabel.string =
  728. // this.mySeedList[this.buySeedIndex].priceSnb + ' SNB'
  729. // this.PayCNTPopViewMutureDurLabel.string =
  730. // this.mySeedList[this.buySeedIndex].maturity
  731. // break
  732. // default:
  733. // console.error(
  734. // 'this.toggleInputValue 不是1 2 3!',
  735. // this.toggleInputValue
  736. // )
  737. // break
  738. // }
  739. },
  740. //根据状态吊起不同支付
  741. onSwitchPlayType() {
  742. switch (this.toggleInputValue) {
  743. case '1': //cnt 支付
  744. this.PayCNT()
  745. break
  746. case '2': //snb 支付
  747. this.PaySNB()
  748. break
  749. default:
  750. console.error('状态吊起错误', this.toggleInputValue)
  751. GlobalD.GameData.showToast(
  752. cc.find('Canvas/UICamera'),
  753. '请先选择支付方式',
  754. 1
  755. )
  756. break
  757. }
  758. },
  759. onToggleInput(value, evnentData) {
  760. this.toggleInputValue = evnentData
  761. this._updatePrice()
  762. },
  763. //购买装备
  764. onPayEquipPopView(myIndex) {
  765. this.buyEquipIndex = myIndex
  766. this.PayEquipPopView.active = true
  767. this.PayEquipPopView.getComponent("PayEquipPopView").setInfo(this.equipmentList[this.buyEquipIndex]);
  768. },
  769. PayEquipSNB() {
  770. console.log(
  771. '支付价格Snb:' + this.equipmentList[this.buyEquipIndex].priceSnb
  772. )
  773. if (this.isPlaying) {
  774. GlobalD.GameData.showToast(cc.find('Canvas/UICamera'), '购买过快!', 1)
  775. return
  776. }
  777. GlobalD.GameData.showToast(cc.find('Canvas/UICamera'), '支付处理中...', 10)
  778. this.isPlaying = true
  779. GlobalD.GameData.onGetAddEquipment(
  780. { mallOtherId: this.equipmentList[this.buyEquipIndex].id },
  781. (flag, value) => {
  782. console.log("SNB购买后", value);
  783. this.isPlaying = false
  784. if (flag && 0 === value.code) {
  785. let _equip = value.data.equip;
  786. //todo 装备
  787. GlobalD.GameData.showToast(
  788. cc.find('Canvas/UICamera'),
  789. '购买装备成功',
  790. 1
  791. )
  792. //重新获取一下snb,会自动更新到面板
  793. GlobalD.GameData.onGetUserSnb()
  794. if (0 === _equip.otherType) {
  795. GlobalD.Dog = _equip;
  796. this.ManageUI.getComponent('ManageUI').onSettingDogAlpha(255);
  797. //刷新狗状态
  798. let _dogContainerScript = cc.find("Canvas/DogContainer").getComponent("DogContainer")
  799. _dogContainerScript.onSetSelfDogState(_equip);
  800. } else if (1 === _equip.otherType) {
  801. GlobalD.Stick = _equip;
  802. this.ManageUI.getComponent('ManageUI').onSettingStickAlpha(255);
  803. }
  804. //更新日志数据
  805. cc.find('GameNode/ManageDapp')
  806. .getComponent('ManageDapp')
  807. .onUpdateSnbList()
  808. } else {
  809. GlobalD.GameData.showToast(cc.find('Canvas/UICamera'), value.msg, 2)
  810. }
  811. }
  812. )
  813. },
  814. //用cnt购买
  815. PayEquipCNT() {
  816. console.log(
  817. '支付价格Cnt:' + this.equipmentList[this.buyEquipIndex].priceCnt + ",购买类型:" + this.equipmentList[this.buyEquipIndex].otherType
  818. )
  819. if (!(this.equipmentList[this.buyEquipIndex].otherType == 2 || this.equipmentList[this.buyEquipIndex].otherType == 3)) return;
  820. if (this.isPlaying) {
  821. GlobalD.GameData.showToast(cc.find('Canvas/UICamera'), '购买过快!', 1)
  822. return
  823. }
  824. GlobalD.GameData.showToast(cc.find('Canvas/UICamera'), '支付处理中...', 10)
  825. this.isPlaying = true
  826. let playAmount = this.equipmentList[this.buyEquipIndex].priceCnt
  827. if (0 === playAmount) {
  828. GlobalD.GameData.showToast(
  829. cc.find('Canvas/UICamera'),
  830. 'CNT不能为0!',
  831. 1
  832. )
  833. return
  834. }
  835. GlobalD.GameData.payCNT(
  836. playAmount,
  837. this.equipmentList[this.buyEquipIndex].otherType,// payType
  838. this.equipmentList[this.buyEquipIndex].id + '',
  839. (data) => {
  840. const [err, tx] = data
  841. this.isPlaying = false;
  842. if (err === null) {
  843. // TODO 成功, 支付为链上操作,需要提供回调接口给这边服务端确认交易成功后修改购买订单结果
  844. console.log(tx) // 交易hash,唯一标识符
  845. //todo 轮询装备
  846. if (this.AutoGetEquipsData) {
  847. this.unschedule(this.AutoGetEquipsData)
  848. this.AutoGetEquipsData = null
  849. }
  850. this.AutoGetEquipsData = () => {
  851. GlobalD.GameData.onGetDisasterProtected((value) => {
  852. if (0 === value.code) {
  853. // console.log('轮询购买防护包:' + JSON.stringify(value))
  854. let data = value.data;
  855. if (this.equipmentList[this.buyEquipIndex].otherType == 2) {
  856. if (GlobalD.NaturalPack == null) {
  857. if (data.naturalProtected != null) {
  858. this.unschedule(this.AutoGetEquipsData)
  859. GlobalD.GameData.showToast(
  860. cc.find('Canvas/UICamera'),
  861. '购买自然防护包成功!',
  862. 1
  863. )
  864. this.ManageUI.getComponent('ManageUI').onRunProtected(value);
  865. }
  866. } else if (!date.datesIsEqual(data.naturalProtected.protectTime, GlobalD.NaturalPack.protectTime)) {
  867. this.unschedule(this.AutoGetEquipsData)
  868. GlobalD.GameData.showToast(
  869. cc.find('Canvas/UICamera'),
  870. '增加自然防护包成功!',
  871. 1
  872. )
  873. this.ManageUI.getComponent('ManageUI').onRunProtected(value);
  874. }
  875. } else if (this.equipmentList[this.buyEquipIndex].otherType == 3) {
  876. if (GlobalD.BeastPack == null) {
  877. if (data.beastProtected != null) {
  878. this.unschedule(this.AutoGetEquipsData)
  879. GlobalD.GameData.showToast(
  880. cc.find('Canvas/UICamera'),
  881. '购买野兽防护包成功!',
  882. 1
  883. )
  884. this.ManageUI.getComponent('ManageUI').onRunProtected(value);
  885. }
  886. } else if (!date.datesIsEqual(data.beastProtected.protectTime, GlobalD.BeastPack.protectTime)) {
  887. this.unschedule(this.AutoGetEquipsData)
  888. //如果时间不相等,叠加时间成功
  889. GlobalD.GameData.showToast(
  890. cc.find('Canvas/UICamera'),
  891. '增加野兽防护包成功!',
  892. 1
  893. )
  894. this.ManageUI.getComponent('ManageUI').onRunProtected(value);
  895. }
  896. } else {
  897. this.unschedule(this.AutoGetEquipsData)
  898. }
  899. }
  900. })
  901. }
  902. this.schedule(this.AutoGetEquipsData, 5)
  903. } else {
  904. console.log(err)
  905. GlobalD.GameData.showToast(cc.find('Canvas/UICamera'), err, 2)
  906. }
  907. }
  908. )
  909. },
  910. onDestory() {
  911. if (this.AutoGetSeedsData) {
  912. this.unschedule(this.AutoGetSeedsData)
  913. this.AutoGetSeedsData = null
  914. }
  915. },
  916. //申请列表
  917. OpenApplyList() {
  918. this.applySnbView.active = true;
  919. }
  920. })