StoreView.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. switch: function () {
  233. var children = this.container.children;
  234. var length = children.length;
  235. if (length > 1) {
  236. var src = Math.floor(Math.random() * length);
  237. var node = children[src];
  238. var dst = src === length - 1 ? 0 : src + 1;
  239. node.setSiblingIndex(dst);
  240. }
  241. },
  242. OpentSNBExchangeToCNT() {
  243. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "转换CNT通道关闭!", 1);
  244. return;
  245. //限制相关人员交易
  246. if (1 === GlobalD.UserInfo.limitTran) {
  247. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "转换CNT通道关闭,请联系相关管理人员!", 1);
  248. return;
  249. }
  250. //更新数据
  251. this.snbAmountLabel.string = GlobalD.GameData.SNB;
  252. this.ExchangePopView.active = true;
  253. },
  254. SNBExchangeToCNT() {
  255. //限制相关人员交易
  256. if (1 === GlobalD.UserInfo.limitTran) {
  257. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "转换CNT通道关闭,请联系相关管理人员!", 1);
  258. return;
  259. }
  260. if (0 === Number(this.snbAmount) || 0 != Number(this.snbAmount) % 5) {
  261. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "请输入5的倍数兑换!", 1);
  262. return;
  263. }
  264. console.log("发起兑换:" + Number(this.snbAmount));
  265. GlobalD.GameData.onSnbToCnt(Number(this.snbAmount), (data) => {
  266. //更新数据
  267. this.snbAmountLabel.string = GlobalD.GameData.SNB;
  268. this.ExchangePopView.active = false;
  269. console.log('data=', data)
  270. // console.log('res=', res)
  271. });
  272. },
  273. onPayCNTPopView(myIndex) {
  274. this.buySeedIndex = myIndex;
  275. this.PayCNTPopView.active = true;
  276. // console.log('this.mySeedList[this.buySeedIndex] =',this.mySeedList[this.buySeedIndex])
  277. // this.PayCNTPopView.getChildByName("BG").getChildByName("BG1_sprite_splash").getChildByName("NameLabel").getChildByName("label").getComponent(cc.Label).string = this.mySeedList[this.buySeedIndex].name;
  278. // this.PayCNTPopView.getChildByName("BG").getChildByName("BG1_sprite_splash").getChildByName("PriceLabel").getChildByName("label").getComponent(cc.Label).string = this.mySeedList[this.buySeedIndex].priceCnt + ' CNT';
  279. // this.PayCNTPopView.getChildByName("BG").getChildByName("BG1_sprite_splash").getChildByName("MutureDurLabel").getChildByName("label").getComponent(cc.Label).string = this.mySeedList[this.buySeedIndex].maturity;
  280. this._updatePrice();
  281. // this.PayCNTPopViewNameLabel.string = this.mySeedList[this.buySeedIndex].name;
  282. // this.PayCNTPopViewPriceLabel.string = this.mySeedList[this.buySeedIndex].priceCnt + ' CNT';
  283. // this.PayCNTPopViewMutureDurLabel.string = this.mySeedList[this.buySeedIndex].maturity;
  284. },
  285. onPaySNBPopView(myIndex) {
  286. this.buySeedIndex = myIndex;
  287. this.PaySNBPopView.active = true;
  288. // console.log('this.mySeedList[this.buySeedIndex] =',this.mySeedList[this.buySeedIndex])
  289. this.PaySNBPopView.getChildByName("BG").getChildByName("BG1_sprite_splash").getChildByName("NameLabel").getChildByName("label").getComponent(cc.Label).string = this.mySeedList[this.buySeedIndex].name;
  290. this.PaySNBPopView.getChildByName("BG").getChildByName("BG1_sprite_splash").getChildByName("PriceLabel").getChildByName("label").getComponent(cc.Label).string = this.mySeedList[this.buySeedIndex].priceCnt + ' SNB';
  291. this.PaySNBPopView.getChildByName("BG").getChildByName("BG1_sprite_splash").getChildByName("MutureDurLabel").getChildByName("label").getComponent(cc.Label).string = this.mySeedList[this.buySeedIndex].maturity;
  292. },
  293. PayCNT() {
  294. console.log('this.cntAmount=', this.cntAmount)
  295. console.log('this.cntAmount price=', this.cntAmount * parseInt(this.mySeedList[this.buySeedIndex].priceCnt))
  296. //购买种子也要使用轮询
  297. let currentAmount = 0;
  298. for (let i = 0; i < this.myWareHouseSeedList.length; i++) {
  299. if (this.myWareHouseSeedList[i].id == this.mySeedList[this.buySeedIndex].id) {
  300. currentAmount = this.myWareHouseSeedList[i].amount;
  301. }
  302. }
  303. //当前背包的数量
  304. console.log("currentAmount:", {
  305. seedId: this.mySeedList[this.buySeedIndex].id,
  306. currentAmount: currentAmount //把当前背包的数据传进去
  307. });
  308. let playAmount = this.cntAmount * parseInt(this.mySeedList[this.buySeedIndex].priceCnt);
  309. if (0 === playAmount) {
  310. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "请输入购买数量", 1);
  311. return;
  312. }
  313. // if (this.isPlaying) {
  314. // GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "购买过快!", 1);
  315. // return;
  316. // }
  317. // this.isPlaying = true;
  318. // GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "区块确认中,请耐心等待!", 120);
  319. GlobalD.GameData.payCNT(playAmount, 4, this.mySeedList[this.buySeedIndex].id + "", (data) => {
  320. const [err, tx] = data;
  321. // this.isPlaying = false;
  322. if (err === null) {
  323. // TODO 成功, 支付为链上操作,需要提供回调接口给这边服务端确认交易成功后修改购买订单结果
  324. console.log(tx) // 交易hash,唯一标识符
  325. //todo 种子
  326. if (this.AutoGetSeedsData) {
  327. this.unschedule(this.AutoGetSeedsData);
  328. this.AutoGetSeedsData = null;
  329. }
  330. this.AutoGetSeedsData = () => {
  331. // getSeedCount ++;
  332. GlobalD.GameData.onGetSeedState(
  333. {
  334. seedId: this.mySeedList[this.buySeedIndex].id,
  335. currentAmount: currentAmount //把当前背包的数据传进去
  336. },
  337. (value) => {
  338. console.log("轮询种子:" + JSON.stringify(value));
  339. if (0 === value.code) {
  340. this.unschedule(this.AutoGetSeedsData);
  341. console.log("轮询种子购买成功");
  342. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "种子购买成功!", 1);
  343. // this.PayCNTPopView.active = false;
  344. //直接重新初始化一下列表,
  345. this.buildingViewScript.onUpdateList();
  346. }
  347. });
  348. };
  349. this.schedule(this.AutoGetSeedsData, 5);
  350. } else {
  351. console.log(err)
  352. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), err, 2);
  353. }
  354. });
  355. },
  356. PaySNB() {
  357. console.log('this.snbAmount=', this.cntAmount)
  358. console.log('priceSnb price=', this.mySeedList[this.buySeedIndex].priceSnb)
  359. console.log('this.snbAmount price=', this.cntAmount * parseInt(this.mySeedList[this.buySeedIndex].priceSnb))
  360. console.log("支付价格:" + this.cntAmount * parseInt(this.mySeedList[this.buySeedIndex].priceSnb));
  361. let playAmount = this.cntAmount * parseInt(this.mySeedList[this.buySeedIndex].priceSnb);
  362. if (0 === playAmount) {
  363. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "请输入购买数量", 1);
  364. return;
  365. }
  366. if (this.isPlaying) {
  367. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "购买过快!", 1);
  368. return;
  369. }
  370. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "支付处理中...", 10);
  371. this.isPlaying = true;
  372. GlobalD.GameData.onBuySeedsWithSNB(playAmount, this.mySeedList[this.buySeedIndex].id, (flag, value) => {
  373. // console.log("SNB购买后", value);
  374. this.isPlaying = false;
  375. if (flag && 0 === value.code) {
  376. //todo 种子
  377. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "种子购买成功!", 1);
  378. // this.PayCNTPopView.active = false;
  379. //重新获取一下snb,会自动更新到面板
  380. GlobalD.GameData.onGetUserSnb();
  381. //直接重新初始化一下列表,
  382. this.buildingViewScript.onUpdateList();
  383. //更新日志数据
  384. cc.find("GameNode/ManageDapp").getComponent("ManageDapp").onUpdateSnbList();
  385. } else {
  386. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), value.msg, 2);
  387. }
  388. });
  389. },
  390. onTextChanged1: function (text, editbox, customEventData) {
  391. // 这里的 text 表示 修改完后的 EditBox 的文本内容
  392. // 这里 editbox 是一个 cc.EditBox 对象
  393. // 这里的 customEventData 参数就等于你之前设置的 "foobar"
  394. this.cntAmount = parseInt(text);
  395. },
  396. // 假设这个回调是给 textChanged 事件的
  397. onTextChanged2: function (text, editbox, customEventData) {
  398. // 这里的 text 表示 修改完后的 EditBox 的文本内容
  399. // 这里 editbox 是一个 cc.EditBox 对象
  400. // 这里的 customEventData 参数就等于你之前设置的 "foobar"
  401. // this.snbAmount = parseInt(text);
  402. var numberTemp = new RegExp("^[A-Za-z0-9]+$");
  403. if (numberTemp.test(text)) {
  404. if (Number(text) >= 1) {
  405. this.snbAmount = Number(text);
  406. } else {
  407. this.snbAmount = 0;
  408. editbox.string = this.snbAmount;
  409. }
  410. } else {
  411. this.snbAmount = 0;
  412. editbox.string = this.snbAmount;
  413. console.log("请输入5的倍数", this.snbAmount);
  414. }
  415. },
  416. onToggleInput(value, evnentData) {
  417. this.toggleInputValue = evnentData;
  418. this._updatePrice();
  419. },
  420. _updatePrice() {
  421. switch (this.toggleInputValue) {
  422. case "1":
  423. // this.totalPriceValue = 700 * 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].priceCnt + ' CNT';
  427. this.PayCNTPopViewMutureDurLabel.string = this.mySeedList[this.buySeedIndex].maturity;
  428. break;
  429. case "2":
  430. // this.totalPriceValue = 2500 * this.inputInviteValue;
  431. // this.totalPriceLabel.string = this.totalPriceValue + "CNT";
  432. this.PayCNTPopViewNameLabel.string = this.mySeedList[this.buySeedIndex].name;
  433. this.PayCNTPopViewPriceLabel.string = this.mySeedList[this.buySeedIndex].priceSnb + ' SNB';
  434. this.PayCNTPopViewMutureDurLabel.string = this.mySeedList[this.buySeedIndex].maturity;
  435. break;
  436. default:
  437. console.error("this.toggleInputValue 不是1 2 3!", this.toggleInputValue);
  438. break;
  439. }
  440. },
  441. //根据状态吊起不同支付
  442. onSwitchPlayType() {
  443. switch (this.toggleInputValue) {
  444. case "1"://cnt 支付
  445. this.PayCNT();
  446. break;
  447. case "2": //snb 支付
  448. this.PaySNB();
  449. break;
  450. default:
  451. console.error("状态吊起错误", this.toggleInputValue);
  452. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "请先选择支付方式", 1);
  453. break;
  454. }
  455. },
  456. onDestory() {
  457. if (this.AutoGetSeedsData) {
  458. this.unschedule(this.AutoGetSeedsData);
  459. this.AutoGetSeedsData = null;
  460. }
  461. }
  462. });