StoreView.js 21 KB

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