StoreView.js 19 KB

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