StoreView.js 20 KB

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