StoreView.js 18 KB

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