StoreView.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. Seed: {
  5. default: null,
  6. type: cc.Node,
  7. },
  8. Exchange: {
  9. default: null,
  10. type: cc.Node,
  11. },
  12. SeedBtn: {
  13. default: null,
  14. type: cc.Node,
  15. },
  16. ExchangeBtn: {
  17. default: null,
  18. type: cc.Node,
  19. },
  20. EquipmentBtn: {
  21. default: null,
  22. type: cc.Node,
  23. },
  24. SelectedFrame: cc.Node,
  25. //Building40: cc.Node,
  26. ManageUI: cc.Node,
  27. //四个按钮对应的sprite
  28. Normal_Seed_Sprite: cc.SpriteFrame,
  29. Selecteded_Seed_Sprite: cc.SpriteFrame,
  30. Normal_Exchange_Sprite: cc.SpriteFrame,
  31. Selecteded_Exchange_Sprite: cc.SpriteFrame,
  32. //需要切换节点顺序的父节点
  33. container: cc.Node,
  34. //商城的列表
  35. mySeedList: [],
  36. seedPrefab: cc.Prefab,
  37. seedSpriteFrame: {
  38. default: [],
  39. type: [cc.SpriteFrame],
  40. },
  41. //商城的图标列表
  42. mySeedIconList: {
  43. default: [],
  44. type: [cc.SpriteFrame],
  45. },
  46. seedNodeArry: {
  47. default: [],
  48. type: [cc.Node],
  49. serializable: false,
  50. visible: false,
  51. },
  52. snbAmount: 0,
  53. cntAmount: 0,
  54. ExchangePopView: {
  55. default: null,
  56. type: cc.Node,
  57. },
  58. PayCNTPopView: {
  59. default: null,
  60. type: cc.Node,
  61. },
  62. PaySNBPopView: {
  63. default: null,
  64. type: cc.Node,
  65. },
  66. buySeedIndex: -1,
  67. snbAmountLabel: {
  68. default: [],
  69. type: [cc.Label],
  70. },
  71. //拿到仓库列表的节点
  72. buildingView: {
  73. default: null,
  74. type: cc.Node,
  75. },
  76. myWareHouseSeedList: null,
  77. //轮询种子列表状态
  78. AutoGetSeedsData: null,
  79. bottomLabel: {
  80. default: null,
  81. type: cc.Node,
  82. },
  83. seedsView: {
  84. default: null,
  85. type: cc.Node,
  86. },
  87. // ExchangeView: {
  88. // default: null,
  89. // type: cc.Node,
  90. // },
  91. seedBottomLabel: {
  92. default: null,
  93. type: cc.Node,
  94. },
  95. scrollViewNode: {
  96. default: null,
  97. type: cc.Node,
  98. },
  99. //购买种类数值
  100. toggleInputValue: {
  101. default: '1',
  102. visible: false,
  103. },
  104. PayCNTPopViewNameLabel: {
  105. default: null,
  106. type: cc.Label,
  107. },
  108. PayCNTPopViewPriceLabel: {
  109. default: null,
  110. type: cc.Label,
  111. },
  112. PayCNTPopViewMutureDurLabel: {
  113. default: null,
  114. type: cc.Label,
  115. },
  116. isPlaying: false,
  117. },
  118. HiddenAll() {
  119. if (this.Seed.active) this.Seed.active = false
  120. if (this.Exchange.active) this.Exchange.active = false
  121. },
  122. start() {
  123. //获取到仓库列表的脚本
  124. this.buildingViewScript = this.buildingView.getComponent('BuildingView')
  125. //默认设置第一个节点为 最上面
  126. this.SeedBtn.setSiblingIndex(10)
  127. // console.log('SNB=======', GlobalD.GameData.SNB)
  128. this.snbAmountLabel.string = GlobalD.GameData.SNB
  129. //这里也初始化获取一个数据
  130. GlobalD.GameData.getWarehouseSeedAndFruit((vaule) => {
  131. this.myWareHouseSeedList = vaule.data.seed
  132. })
  133. //获取种子和果实
  134. GlobalD.GameData.getMallSeed((vaule) => {
  135. this.mySeedList = vaule.data
  136. // console.log('this.mySeedList[i]=', this.mySeedList);
  137. for (let i = 0; i < this.mySeedList.length; i++) {
  138. //没有种子数量不显示 todo 看看后续需不需要处理删除
  139. //if (this.mySeedList[i].amount < 1) continue;
  140. let _seed = cc.instantiate(this.seedPrefab)
  141. //if (this.mySeedList[i].amount < 1)
  142. if (this.mySeedList[i].amount < 1) {
  143. //_seed.getChildByName("SellOut").active = true;
  144. } else {
  145. _seed.getChildByName('SellOut').active = false
  146. }
  147. // _seed.getChildByName("buy_button").active = true;
  148. // _seed.getChildByName("Name").active = false;
  149. // _seed.getChildByName("Name").getChildByName("Num").getComponent(cc.Label).strng = '';//'x'+this.mySeedList[i].amount;
  150. this.seedNodeArry.push(_seed)
  151. _seed.parent = this.Seed
  152. let _seedScript = _seed.getComponent('Content_Button')
  153. _seedScript.Name = this.mySeedList[i].name
  154. _seedScript.Price = ''
  155. _seedScript.CNT = this.mySeedList[i].priceCnt
  156. _seedScript.SNB = this.mySeedList[i].priceSnb
  157. _seedScript.Mature = this.mySeedList[i].maturity + '天'
  158. _seedScript.Synopsis = this.mySeedList[i].seedDescribe
  159. //设置一个生成点
  160. let _spawnScript = _seed.getComponent('Content_seed')
  161. _spawnScript.myIndex = i
  162. _spawnScript.SpawnPoint = cc.find('Canvas/SpawnParent/SpawnPoint')
  163. _seedScript.NumLabel.string = this.mySeedList[i].amount
  164. _seedScript.NameLabel.string = this.mySeedList[i].name
  165. switch (this.mySeedList[i].picture) {
  166. case 'Cabbage':
  167. _seedScript.bgSprite.spriteFrame = this.mySeedIconList[0] //this.seedSpriteFrame[0];
  168. break
  169. case 'Potato':
  170. _seedScript.bgSprite.spriteFrame = this.mySeedIconList[1] //this.seedSpriteFrame[1];
  171. break
  172. case 'Carrot':
  173. _seedScript.bgSprite.spriteFrame = this.mySeedIconList[2] //this.seedSpriteFrame[2];
  174. break
  175. case 'Broccoli':
  176. _seedScript.bgSprite.spriteFrame = this.mySeedIconList[3] //this.seedSpriteFrame[3];
  177. break
  178. case 'Tomato':
  179. _seedScript.bgSprite.spriteFrame = this.mySeedIconList[4] //this.seedSpriteFrame[4];
  180. break
  181. case 'Squash':
  182. _seedScript.bgSprite.spriteFrame = this.mySeedIconList[5] //this.seedSpriteFrame[5];
  183. break
  184. case 'Eggplant':
  185. _seedScript.bgSprite.spriteFrame = this.mySeedIconList[6] //this.seedSpriteFrame[6];
  186. break
  187. case 'Pepper':
  188. _seedScript.bgSprite.spriteFrame = this.mySeedIconList[7] //this.seedSpriteFrame[7];
  189. break
  190. case 'Lentil':
  191. _seedScript.bgSprite.spriteFrame = this.mySeedIconList[8] //this.seedSpriteFrame[8];
  192. break
  193. default:
  194. break
  195. }
  196. }
  197. })
  198. },
  199. //切换建筑的面板
  200. onSwitchBuildingContent(event, index) {
  201. // this.SeedBtn.setScale(1)
  202. // this.ExchangeBtn.setScale(1)
  203. this.SeedBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Seed_Sprite
  204. this.ExchangeBtn.getComponent(cc.Sprite).spriteFrame =
  205. this.Normal_Exchange_Sprite
  206. // this.SeedBtn.setContentSize(100, 54)
  207. // this.ExchangeBtn.setContentSize(100, 54)
  208. // this.SeedBtn.y = -5
  209. // this.ExchangeBtn.y = -5
  210. this.HiddenAll()
  211. //面板设置index
  212. //种子
  213. if ('0' == index) {
  214. // this.SeedBtn.y = -20
  215. this.SeedBtn.getComponent(cc.Sprite).spriteFrame =
  216. this.Selecteded_Seed_Sprite
  217. this.Seed.active = true
  218. if (this.seedNodeArry.length != 0) {
  219. this.SelectedFrame.parent = this.seedNodeArry[0]
  220. }
  221. // this.SeedBtn.zIndex = 10
  222. // this.SeedBtn.setContentSize(110, 100)
  223. // this.ExchangeBtn.zIndex = 1
  224. // this.ExchangeBtn.setContentSize(86, 88);
  225. this.bottomLabel.active = false
  226. this.scrollViewNode.active = true
  227. this.seedBottomLabel.active = true
  228. this.seedsView.active = true
  229. this.Exchange.active = false
  230. }
  231. //兑换
  232. else if ('1' == index) {
  233. // this.ExchangeBtn.y = -20
  234. this.ExchangeBtn.getComponent(cc.Sprite).spriteFrame =
  235. this.Selecteded_Exchange_Sprite
  236. // this.ExchangeBtn.zIndex = 10
  237. // this.ExchangeBtn.setContentSize(110, 100)
  238. // this.SeedBtn.zIndex = 1
  239. // this.SeedBtn.setContentSize(86, 88);
  240. this.bottomLabel.active = true
  241. this.scrollViewNode.active = false
  242. this.seedBottomLabel.active = false
  243. this.seedsView.active = false
  244. this.Exchange.active = true
  245. }
  246. },
  247. switch: function () {
  248. var children = this.container.children
  249. var length = children.length
  250. if (length > 1) {
  251. var src = Math.floor(Math.random() * length)
  252. var node = children[src]
  253. var dst = src === length - 1 ? 0 : src + 1
  254. node.setSiblingIndex(dst)
  255. }
  256. },
  257. OpentSNBExchangeToCNT() {
  258. //限制相关人员交易
  259. if (1 === GlobalD.UserInfo.limitTran) {
  260. GlobalD.GameData.showToast(
  261. cc.find('Canvas/UICamera'),
  262. '转换CNT通道关闭,请联系相关管理人员!',
  263. 1
  264. )
  265. return
  266. }
  267. //更新数据
  268. this.snbAmountLabel.string = GlobalD.GameData.SNB
  269. this.ExchangePopView.active = true
  270. },
  271. SNBExchangeToCNT() {
  272. //限制相关人员交易
  273. if (1 === GlobalD.UserInfo.limitTran) {
  274. GlobalD.GameData.showToast(
  275. cc.find('Canvas/UICamera'),
  276. '转换CNT通道关闭,请联系相关管理人员!',
  277. 1
  278. )
  279. return
  280. }
  281. if (0 === Number(this.snbAmount) || 0 != Number(this.snbAmount) % 5) {
  282. GlobalD.GameData.showToast(
  283. cc.find('Canvas/UICamera'),
  284. '请输入5的倍数兑换!',
  285. 1
  286. )
  287. return
  288. }
  289. console.log('发起兑换:' + Number(this.snbAmount))
  290. GlobalD.GameData.onSnbToCnt(Number(this.snbAmount), (data) => {
  291. //更新数据
  292. this.snbAmountLabel.string = GlobalD.GameData.SNB
  293. this.ExchangePopView.active = false
  294. console.log('data=', data)
  295. // console.log('res=', res)
  296. })
  297. },
  298. onPayCNTPopView(myIndex) {
  299. this.buySeedIndex = myIndex
  300. this.PayCNTPopView.active = true
  301. // console.log('this.mySeedList[this.buySeedIndex] =',this.mySeedList[this.buySeedIndex])
  302. // this.PayCNTPopView.getChildByName("BG").getChildByName("BG1_sprite_splash").getChildByName("NameLabel").getChildByName("label").getComponent(cc.Label).string = this.mySeedList[this.buySeedIndex].name;
  303. // this.PayCNTPopView.getChildByName("BG").getChildByName("BG1_sprite_splash").getChildByName("PriceLabel").getChildByName("label").getComponent(cc.Label).string = this.mySeedList[this.buySeedIndex].priceCnt + ' CNT';
  304. // this.PayCNTPopView.getChildByName("BG").getChildByName("BG1_sprite_splash").getChildByName("MutureDurLabel").getChildByName("label").getComponent(cc.Label).string = this.mySeedList[this.buySeedIndex].maturity;
  305. this._updatePrice()
  306. // this.PayCNTPopViewNameLabel.string = this.mySeedList[this.buySeedIndex].name;
  307. // this.PayCNTPopViewPriceLabel.string = this.mySeedList[this.buySeedIndex].priceCnt + ' CNT';
  308. // this.PayCNTPopViewMutureDurLabel.string = this.mySeedList[this.buySeedIndex].maturity;
  309. },
  310. onPaySNBPopView(myIndex) {
  311. this.buySeedIndex = myIndex
  312. this.PaySNBPopView.active = true
  313. // console.log('this.mySeedList[this.buySeedIndex] =',this.mySeedList[this.buySeedIndex])
  314. this.PaySNBPopView.getChildByName('BG')
  315. .getChildByName('BG1_sprite_splash')
  316. .getChildByName('NameLabel')
  317. .getChildByName('label')
  318. .getComponent(cc.Label).string = this.mySeedList[this.buySeedIndex].name
  319. this.PaySNBPopView.getChildByName('BG')
  320. .getChildByName('BG1_sprite_splash')
  321. .getChildByName('PriceLabel')
  322. .getChildByName('label')
  323. .getComponent(cc.Label).string =
  324. this.mySeedList[this.buySeedIndex].priceCnt + ' SNB'
  325. this.PaySNBPopView.getChildByName('BG')
  326. .getChildByName('BG1_sprite_splash')
  327. .getChildByName('MutureDurLabel')
  328. .getChildByName('label')
  329. .getComponent(cc.Label).string =
  330. this.mySeedList[this.buySeedIndex].maturity
  331. },
  332. PayCNT() {
  333. console.log('this.cntAmount=', this.cntAmount)
  334. console.log(
  335. 'this.cntAmount price=',
  336. this.cntAmount * parseInt(this.mySeedList[this.buySeedIndex].priceCnt)
  337. )
  338. //购买种子也要使用轮询
  339. let currentAmount = 0
  340. for (let i = 0; i < this.myWareHouseSeedList.length; i++) {
  341. if (
  342. this.myWareHouseSeedList[i].id == this.mySeedList[this.buySeedIndex].id
  343. ) {
  344. currentAmount = this.myWareHouseSeedList[i].amount
  345. }
  346. }
  347. //当前背包的数量
  348. console.log('currentAmount:', {
  349. seedId: this.mySeedList[this.buySeedIndex].id,
  350. currentAmount: currentAmount, //把当前背包的数据传进去
  351. })
  352. let playAmount =
  353. this.cntAmount * parseInt(this.mySeedList[this.buySeedIndex].priceCnt)
  354. if (0 === playAmount) {
  355. GlobalD.GameData.showToast(
  356. cc.find('Canvas/UICamera'),
  357. '请输入购买数量',
  358. 1
  359. )
  360. return
  361. }
  362. // if (this.isPlaying) {
  363. // GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "购买过快!", 1);
  364. // return;
  365. // }
  366. // this.isPlaying = true;
  367. // GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "区块确认中,请耐心等待!", 120);
  368. GlobalD.GameData.payCNT(
  369. playAmount,
  370. 4,
  371. this.mySeedList[this.buySeedIndex].id + '',
  372. (data) => {
  373. const [err, tx] = data
  374. // this.isPlaying = false;
  375. if (err === null) {
  376. // TODO 成功, 支付为链上操作,需要提供回调接口给这边服务端确认交易成功后修改购买订单结果
  377. console.log(tx) // 交易hash,唯一标识符
  378. //todo 种子
  379. if (this.AutoGetSeedsData) {
  380. this.unschedule(this.AutoGetSeedsData)
  381. this.AutoGetSeedsData = null
  382. }
  383. this.AutoGetSeedsData = () => {
  384. // getSeedCount ++;
  385. GlobalD.GameData.onGetSeedState(
  386. {
  387. seedId: this.mySeedList[this.buySeedIndex].id,
  388. currentAmount: currentAmount, //把当前背包的数据传进去
  389. },
  390. (value) => {
  391. console.log('轮询种子:' + JSON.stringify(value))
  392. if (0 === value.code) {
  393. this.unschedule(this.AutoGetSeedsData)
  394. console.log('轮询种子购买成功')
  395. GlobalD.GameData.showToast(
  396. cc.find('Canvas/UICamera'),
  397. '种子购买成功!',
  398. 1
  399. )
  400. // this.PayCNTPopView.active = false;
  401. //直接重新初始化一下列表,
  402. this.buildingViewScript.onUpdateList()
  403. }
  404. }
  405. )
  406. }
  407. this.schedule(this.AutoGetSeedsData, 5)
  408. } else {
  409. console.log(err)
  410. GlobalD.GameData.showToast(cc.find('Canvas/UICamera'), err, 2)
  411. }
  412. }
  413. )
  414. },
  415. PaySNB() {
  416. console.log('this.snbAmount=', this.cntAmount)
  417. console.log('priceSnb price=', this.mySeedList[this.buySeedIndex].priceSnb)
  418. console.log(
  419. 'this.snbAmount price=',
  420. this.cntAmount * parseInt(this.mySeedList[this.buySeedIndex].priceSnb)
  421. )
  422. console.log(
  423. '支付价格:' +
  424. this.cntAmount * parseInt(this.mySeedList[this.buySeedIndex].priceSnb)
  425. )
  426. let playAmount =
  427. this.cntAmount * parseInt(this.mySeedList[this.buySeedIndex].priceSnb)
  428. if (0 === playAmount) {
  429. GlobalD.GameData.showToast(
  430. cc.find('Canvas/UICamera'),
  431. '请输入购买数量',
  432. 1
  433. )
  434. return
  435. }
  436. if (this.isPlaying) {
  437. GlobalD.GameData.showToast(cc.find('Canvas/UICamera'), '购买过快!', 1)
  438. return
  439. }
  440. GlobalD.GameData.showToast(cc.find('Canvas/UICamera'), '支付处理中...', 10)
  441. this.isPlaying = true
  442. GlobalD.GameData.onBuySeedsWithSNB(
  443. playAmount,
  444. this.mySeedList[this.buySeedIndex].id,
  445. (flag, value) => {
  446. // console.log("SNB购买后", value);
  447. this.isPlaying = false
  448. if (flag && 0 === value.code) {
  449. //todo 种子
  450. GlobalD.GameData.showToast(
  451. cc.find('Canvas/UICamera'),
  452. '种子购买成功!',
  453. 1
  454. )
  455. // this.PayCNTPopView.active = false;
  456. //重新获取一下snb,会自动更新到面板
  457. GlobalD.GameData.onGetUserSnb()
  458. //直接重新初始化一下列表,
  459. this.buildingViewScript.onUpdateList()
  460. //更新日志数据
  461. cc.find('GameNode/ManageDapp')
  462. .getComponent('ManageDapp')
  463. .onUpdateSnbList()
  464. } else {
  465. GlobalD.GameData.showToast(cc.find('Canvas/UICamera'), value.msg, 2)
  466. }
  467. }
  468. )
  469. },
  470. onTextChanged1: function (text, editbox, customEventData) {
  471. // 这里的 text 表示 修改完后的 EditBox 的文本内容
  472. // 这里 editbox 是一个 cc.EditBox 对象
  473. // 这里的 customEventData 参数就等于你之前设置的 "foobar"
  474. this.cntAmount = parseInt(text)
  475. },
  476. // 假设这个回调是给 textChanged 事件的
  477. onTextChanged2: function (text, editbox, customEventData) {
  478. // 这里的 text 表示 修改完后的 EditBox 的文本内容
  479. // 这里 editbox 是一个 cc.EditBox 对象
  480. // 这里的 customEventData 参数就等于你之前设置的 "foobar"
  481. // this.snbAmount = parseInt(text);
  482. var numberTemp = new RegExp('^[A-Za-z0-9]+$')
  483. if (numberTemp.test(text)) {
  484. if (Number(text) >= 1) {
  485. this.snbAmount = Number(text)
  486. } else {
  487. this.snbAmount = 0
  488. editbox.string = this.snbAmount
  489. }
  490. } else {
  491. this.snbAmount = 0
  492. editbox.string = this.snbAmount
  493. console.log('请输入5的倍数', this.snbAmount)
  494. }
  495. },
  496. _updatePrice() {
  497. switch (this.toggleInputValue) {
  498. case '1':
  499. // this.totalPriceValue = 700 * this.inputInviteValue;
  500. // this.totalPriceLabel.string = this.totalPriceValue + "CNT";
  501. this.PayCNTPopViewNameLabel.string =
  502. this.mySeedList[this.buySeedIndex].name
  503. this.PayCNTPopViewPriceLabel.string =
  504. this.mySeedList[this.buySeedIndex].priceCnt + ' CNT'
  505. this.PayCNTPopViewMutureDurLabel.string =
  506. this.mySeedList[this.buySeedIndex].maturity
  507. break
  508. case '2':
  509. // this.totalPriceValue = 2500 * this.inputInviteValue;
  510. // this.totalPriceLabel.string = this.totalPriceValue + "CNT";
  511. this.PayCNTPopViewNameLabel.string =
  512. this.mySeedList[this.buySeedIndex].name
  513. this.PayCNTPopViewPriceLabel.string =
  514. this.mySeedList[this.buySeedIndex].priceSnb + ' SNB'
  515. this.PayCNTPopViewMutureDurLabel.string =
  516. this.mySeedList[this.buySeedIndex].maturity
  517. break
  518. default:
  519. console.error(
  520. 'this.toggleInputValue 不是1 2 3!',
  521. this.toggleInputValue
  522. )
  523. break
  524. }
  525. },
  526. //根据状态吊起不同支付
  527. onSwitchPlayType() {
  528. switch (this.toggleInputValue) {
  529. case '1': //cnt 支付
  530. this.PayCNT()
  531. break
  532. case '2': //snb 支付
  533. this.PaySNB()
  534. break
  535. default:
  536. console.error('状态吊起错误', this.toggleInputValue)
  537. GlobalD.GameData.showToast(
  538. cc.find('Canvas/UICamera'),
  539. '请先选择支付方式',
  540. 1
  541. )
  542. break
  543. }
  544. },
  545. onToggleInput(value, evnentData) {
  546. this.toggleInputValue = evnentData
  547. this._updatePrice()
  548. },
  549. onDestory() {
  550. if (this.AutoGetSeedsData) {
  551. this.unschedule(this.AutoGetSeedsData)
  552. this.AutoGetSeedsData = null
  553. }
  554. },
  555. })