StoreView.js 29 KB

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