StoreView.js 20 KB

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