StoreView.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. default:
  154. break;
  155. }
  156. }
  157. });
  158. },
  159. //切换建筑的面板
  160. onSwitchBuildingContent(event, index) {
  161. this.SeedBtn.setScale(1);
  162. this.ExchangeBtn.setScale(1);
  163. this.SeedBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Seed_Sprite;
  164. this.ExchangeBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Exchange_Sprite;
  165. this.SeedBtn.setContentSize(100, 54);
  166. this.ExchangeBtn.setContentSize(100, 54);
  167. this.SeedBtn.y = -5;
  168. this.ExchangeBtn.y = -5;
  169. this.HiddenAll();
  170. //面板设置index
  171. //种子
  172. if ('0' == index) {
  173. this.SeedBtn.y = -20;
  174. this.SeedBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Seed_Sprite;
  175. this.Seed.active = true;
  176. if (this.seedNodeArry.length != 0) {
  177. this.SelectedFrame.parent = this.seedNodeArry[0];
  178. }
  179. //if (this.mySeedList.length > 0) {
  180. // let Synopsis = "成熟期:" + this.mySeedList[0].maturity;
  181. // this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.mySeedList[0].name, this.mySeedList[0].priceSnb, Synopsis);
  182. //}
  183. this.SeedBtn.setSiblingIndex(10);
  184. this.SeedBtn.setContentSize(86, 88);
  185. this.bottomLabel.active = false;
  186. }
  187. //兑换
  188. else if ('1' == index) {
  189. this.ExchangeBtn.y = -20;
  190. this.ExchangeBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Exchange_Sprite;
  191. this.Exchange.active = true;
  192. this.ExchangeBtn.setSiblingIndex(10);
  193. this.ExchangeBtn.setContentSize(86, 88);
  194. this.bottomLabel.active = true;
  195. }
  196. },
  197. switch: function () {
  198. var children = this.container.children;
  199. var length = children.length;
  200. if (length > 1) {
  201. var src = Math.floor(Math.random() * length);
  202. var node = children[src];
  203. var dst = src === length - 1 ? 0 : src + 1;
  204. node.setSiblingIndex(dst);
  205. }
  206. },
  207. OpentSNBExchangeToCNT() {
  208. //更新数据
  209. this.snbAmountLabel.string = GlobalD.GameData.SNB;
  210. this.ExchangePopView.active = true;
  211. },
  212. SNBExchangeToCNT() {
  213. GlobalD.GameData.onSnbToCnt(this.snbAmount, (data) => {
  214. //更新数据
  215. this.snbAmountLabel.string = GlobalD.GameData.SNB;
  216. this.ExchangePopView.active = false;
  217. console.log('data=', data)
  218. // console.log('res=', res)
  219. });
  220. },
  221. onPayCNTPopView(myIndex) {
  222. this.buySeedIndex = myIndex;
  223. this.PayCNTPopView.active = true;
  224. // console.log('this.mySeedList[this.buySeedIndex] =',this.mySeedList[this.buySeedIndex])
  225. // this.PayCNTPopView.getChildByName("BG").getChildByName("BG1_sprite_splash").getChildByName("NameLabel").getChildByName("label").getComponent(cc.Label).string = this.mySeedList[this.buySeedIndex].name;
  226. // this.PayCNTPopView.getChildByName("BG").getChildByName("BG1_sprite_splash").getChildByName("PriceLabel").getChildByName("label").getComponent(cc.Label).string = this.mySeedList[this.buySeedIndex].priceCnt + ' CNT';
  227. // this.PayCNTPopView.getChildByName("BG").getChildByName("BG1_sprite_splash").getChildByName("MutureDurLabel").getChildByName("label").getComponent(cc.Label).string = this.mySeedList[this.buySeedIndex].maturity;
  228. this._updatePrice();
  229. // this.PayCNTPopViewNameLabel.string = this.mySeedList[this.buySeedIndex].name;
  230. // this.PayCNTPopViewPriceLabel.string = this.mySeedList[this.buySeedIndex].priceCnt + ' CNT';
  231. // this.PayCNTPopViewMutureDurLabel.string = this.mySeedList[this.buySeedIndex].maturity;
  232. },
  233. onPaySNBPopView(myIndex) {
  234. this.buySeedIndex = myIndex;
  235. this.PaySNBPopView.active = true;
  236. // console.log('this.mySeedList[this.buySeedIndex] =',this.mySeedList[this.buySeedIndex])
  237. this.PaySNBPopView.getChildByName("BG").getChildByName("BG1_sprite_splash").getChildByName("NameLabel").getChildByName("label").getComponent(cc.Label).string = this.mySeedList[this.buySeedIndex].name;
  238. this.PaySNBPopView.getChildByName("BG").getChildByName("BG1_sprite_splash").getChildByName("PriceLabel").getChildByName("label").getComponent(cc.Label).string = this.mySeedList[this.buySeedIndex].priceCnt + ' SNB';
  239. this.PaySNBPopView.getChildByName("BG").getChildByName("BG1_sprite_splash").getChildByName("MutureDurLabel").getChildByName("label").getComponent(cc.Label).string = this.mySeedList[this.buySeedIndex].maturity;
  240. },
  241. PayCNT() {
  242. console.log('this.cntAmount=', this.cntAmount)
  243. console.log('this.cntAmount price=', this.cntAmount * parseInt(this.mySeedList[this.buySeedIndex].priceCnt))
  244. //购买种子也要使用轮询
  245. let currentAmount = 0;
  246. for (let i = 0; i < this.myWareHouseSeedList.length; i++) {
  247. if (this.myWareHouseSeedList[i].id == this.mySeedList[this.buySeedIndex].id) {
  248. currentAmount = this.myWareHouseSeedList[i].amount;
  249. }
  250. }
  251. //当前背包的数量
  252. console.log("currentAmount:", {
  253. seedId: this.mySeedList[this.buySeedIndex].id,
  254. currentAmount: currentAmount //把当前背包的数据传进去
  255. });
  256. let playAmount = this.cntAmount * parseInt(this.mySeedList[this.buySeedIndex].priceCnt);
  257. if (0 === playAmount) {
  258. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "请输入购买数量", 1);
  259. return;
  260. }
  261. // if (this.isPlaying) {
  262. // GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "购买过快!", 1);
  263. // return;
  264. // }
  265. // this.isPlaying = true;
  266. // GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "区块确认中,请耐心等待!", 120);
  267. GlobalD.GameData.payCNT(playAmount, 4, this.mySeedList[this.buySeedIndex].id + "", (data) => {
  268. const [err, tx] = data;
  269. // this.isPlaying = false;
  270. if (err === null) {
  271. // TODO 成功, 支付为链上操作,需要提供回调接口给这边服务端确认交易成功后修改购买订单结果
  272. console.log(tx) // 交易hash,唯一标识符
  273. //todo 种子
  274. if (this.AutoGetSeedsData) {
  275. this.unschedule(this.AutoGetSeedsData);
  276. this.AutoGetSeedsData = null;
  277. }
  278. this.AutoGetSeedsData = () => {
  279. // getSeedCount ++;
  280. GlobalD.GameData.onGetSeedState(
  281. {
  282. seedId: this.mySeedList[this.buySeedIndex].id,
  283. currentAmount: currentAmount //把当前背包的数据传进去
  284. },
  285. (res, value) => {
  286. console.log("轮询种子:" + JSON.stringify(value));
  287. if (0 === value.code) {
  288. this.unschedule(this.AutoGetSeedsData);
  289. console.log("轮询种子购买成功");
  290. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "种子购买成功!", 1);
  291. // this.PayCNTPopView.active = false;
  292. //直接重新初始化一下列表,
  293. this.buildingViewScript.onUpdateList();
  294. }
  295. });
  296. };
  297. this.schedule(this.AutoGetSeedsData, 5);
  298. } else {
  299. console.log(err)
  300. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), err, 2);
  301. }
  302. });
  303. },
  304. PaySNB() {
  305. console.log('this.snbAmount=', this.cntAmount)
  306. console.log('priceSnb price=', this.mySeedList[this.buySeedIndex].priceSnb)
  307. console.log('this.snbAmount price=', this.cntAmount * parseInt(this.mySeedList[this.buySeedIndex].priceSnb))
  308. console.log("支付价格:" + this.cntAmount * parseInt(this.mySeedList[this.buySeedIndex].priceSnb));
  309. let playAmount = this.cntAmount * parseInt(this.mySeedList[this.buySeedIndex].priceSnb);
  310. if (0 === playAmount) {
  311. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "请输入购买数量", 1);
  312. return;
  313. }
  314. if (this.isPlaying) {
  315. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "购买过快!", 1);
  316. return;
  317. }
  318. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "支付处理中...", 10);
  319. this.isPlaying = true;
  320. GlobalD.GameData.onBuySeedsWithSNB(playAmount, this.mySeedList[this.buySeedIndex].id, (res, value) => {
  321. // console.log("SNB购买后", value);
  322. this.isPlaying = false;
  323. if (0 === value.code) {
  324. //todo 种子
  325. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "种子购买成功!", 1);
  326. // this.PayCNTPopView.active = false;
  327. //重新获取一下snb,会自动更新到面板
  328. GlobalD.GameData.onGetUserSnb();
  329. //直接重新初始化一下列表,
  330. this.buildingViewScript.onUpdateList();
  331. //更新日志数据
  332. cc.find("GameNode/ManageDapp").getComponent("ManageDapp").onUpdateSnbList();
  333. } else {
  334. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), value.msg, 2);
  335. }
  336. });
  337. },
  338. onTextChanged1: function (text, editbox, customEventData) {
  339. // 这里的 text 表示 修改完后的 EditBox 的文本内容
  340. // 这里 editbox 是一个 cc.EditBox 对象
  341. // 这里的 customEventData 参数就等于你之前设置的 "foobar"
  342. this.cntAmount = parseInt(text);
  343. },
  344. // 假设这个回调是给 textChanged 事件的
  345. onTextChanged2: function (text, editbox, customEventData) {
  346. // 这里的 text 表示 修改完后的 EditBox 的文本内容
  347. // 这里 editbox 是一个 cc.EditBox 对象
  348. // 这里的 customEventData 参数就等于你之前设置的 "foobar"
  349. this.snbAmount = parseInt(text);
  350. },
  351. onToggleInput(value, evnentData) {
  352. this.toggleInputValue = evnentData;
  353. this._updatePrice();
  354. },
  355. _updatePrice() {
  356. switch (this.toggleInputValue) {
  357. case "1":
  358. // this.totalPriceValue = 700 * this.inputInviteValue;
  359. // this.totalPriceLabel.string = this.totalPriceValue + "CNT";
  360. this.PayCNTPopViewNameLabel.string = this.mySeedList[this.buySeedIndex].name;
  361. this.PayCNTPopViewPriceLabel.string = this.mySeedList[this.buySeedIndex].priceCnt + ' CNT';
  362. this.PayCNTPopViewMutureDurLabel.string = this.mySeedList[this.buySeedIndex].maturity;
  363. break;
  364. case "2":
  365. // this.totalPriceValue = 2500 * this.inputInviteValue;
  366. // this.totalPriceLabel.string = this.totalPriceValue + "CNT";
  367. this.PayCNTPopViewNameLabel.string = this.mySeedList[this.buySeedIndex].name;
  368. this.PayCNTPopViewPriceLabel.string = this.mySeedList[this.buySeedIndex].priceSnb + ' SNB';
  369. this.PayCNTPopViewMutureDurLabel.string = this.mySeedList[this.buySeedIndex].maturity;
  370. break;
  371. default:
  372. console.error("this.toggleInputValue 不是1 2 3!", this.toggleInputValue);
  373. break;
  374. }
  375. },
  376. //根据状态吊起不同支付
  377. onSwitchPlayType() {
  378. switch (this.toggleInputValue) {
  379. case "1"://cnt 支付
  380. this.PayCNT();
  381. break;
  382. case "2": //snb 支付
  383. this.PaySNB();
  384. break;
  385. default:
  386. console.error("状态吊起错误", this.toggleInputValue);
  387. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "请先选择支付方式", 1);
  388. break;
  389. }
  390. }
  391. });