ShopView.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. import Main from "../../../main/Main";
  2. import { AudioMgr, GameViewType } from "../../../main/ViewManage";
  3. import ViewObject from "../../../main/ViewObject";
  4. import FqPay, { UM_EVENT_ID } from "../../../pay/FqPay";
  5. import { HttpStateType, ReveData } from "../../../util/CHttp";
  6. import { __BuyCoinData, __BuyPandoraData, __BuyPowerData, __PayData } from "../../data/sdata/SManage";
  7. import PayShopItem from "../../pay/PayShopItem";
  8. import TopMenu from "../TopMenu";
  9. import ExchangeAmethystView from "./ExchangeAmethystView";
  10. import ShopCoinItem from "./ShopCoinItem";
  11. import ShopPandoraItem from "./ShopPandoraItem";
  12. import ShopPowerItem from "./ShopPowerItem";
  13. interface ShopData {
  14. marketMoneyData: Array<__BuyCoinData>,
  15. marketPowerData: Array<__BuyPowerData>,
  16. playerMarketMoney: Map<string, number>,
  17. playerMarketPower: Map<string, number>,
  18. }
  19. interface PandoraData {
  20. playerPandora: {
  21. count: number
  22. data: Map<string, number>
  23. }
  24. pandoraData: Array<__BuyPandoraData>
  25. }
  26. enum BuyType {
  27. marketMoney = "marketMoney", // 金币
  28. marketPower = "marketPower", // 体力
  29. pandora = "pandora", // 潘多拉
  30. }
  31. const { ccclass, property } = cc._decorator;
  32. @ccclass
  33. export default class ShopView extends ViewObject {
  34. @property(cc.Node)
  35. insetContent: cc.Node = null;
  36. @property(cc.Node)
  37. content: cc.Node = null;
  38. @property(cc.Prefab)
  39. payItem: cc.Prefab = null;
  40. @property(cc.Prefab)
  41. powerItem: cc.Prefab = null;
  42. @property(cc.Prefab)
  43. coinItem: cc.Prefab = null;
  44. @property(cc.Prefab)
  45. pandoraItem: cc.Prefab = null;
  46. @property(cc.Node)
  47. amethystNode: cc.Node = null;
  48. @property(cc.Node)
  49. diamondNode: cc.Node = null;
  50. @property(cc.Node)
  51. powerTips: cc.Node = null;
  52. private curInsetNode: cc.Node = null;
  53. private curInsetNum: number = 104; // 默认插页104
  54. shopData: ShopData = null;
  55. pandoraData: PandoraData = null;
  56. shopPandoraItem: ShopPandoraItem = null;
  57. pandorId: number = 0;
  58. pandoraDrawCount: number = 1; // 抽奖次数
  59. needAmethyst: number = 0; // 需要紫水晶数量
  60. _isClick: boolean = false; // 防止连续点击
  61. itemList: Map<string | number, Array<cc.Node>> = new Map;
  62. public httpEvent(reveData: ReveData) {
  63. let data = reveData.data;
  64. if (data["_pack"] != undefined) {
  65. this.refreshAmethyst();
  66. }
  67. }
  68. init(index: number = 104) {
  69. this.main.topNode.getComponent(TopMenu).forbidBtn(false);
  70. this.curInsetNum = index;
  71. this.curInsetNode = this.insetContent.getChildByName(index + "");
  72. this.powerTips.active = Number(index) == 102;
  73. this.changeStatus(this.curInsetNode, true);
  74. this.changeLayout(this.curInsetNum);
  75. this.refreshAmethyst();
  76. this.refreshDiamond();
  77. this.getPandoraData(() => {
  78. if (index == 104) {
  79. this.pandoraView();
  80. }
  81. });
  82. this.getShopData(() => {
  83. if (index == 102) {
  84. this.powerView();
  85. } else if (index == 103) {
  86. this.coinView();
  87. }
  88. });
  89. if (index == 101) {
  90. this.payView();
  91. }
  92. }
  93. getShopData(callBack: Function) {
  94. let msg = {};
  95. this.main.gameHttp.sendJson('marketMoney/v1/data', msg, (state, reve: ReveData) => {
  96. this.main.stopLoad();
  97. if (state == HttpStateType.SUCCESS) {
  98. if (reve.retCode == 0) {
  99. console.log("==reve=getShopData===", reve)
  100. this.shopData = reve.data;
  101. callBack && callBack();
  102. } else {
  103. this.main.showTips(reve.message);
  104. }
  105. } else {
  106. this.main.showTips('网络异常');
  107. }
  108. });
  109. }
  110. getPandoraData(callBack: Function) {
  111. let msg = {};
  112. this.main.gameHttp.sendJson('pandora/v1/data', msg, (state, reve: ReveData) => {
  113. this.main.stopLoad();
  114. if (state == HttpStateType.SUCCESS) {
  115. if (reve.retCode == 0) {
  116. console.log("==reve=getPandoraData===", reve)
  117. this.pandoraData = reve.data;
  118. callBack && callBack();
  119. } else {
  120. this.main.showTips(reve.message);
  121. }
  122. } else {
  123. this.main.showTips('网络异常');
  124. }
  125. });
  126. }
  127. /**
  128. *
  129. * @param type
  130. * @param shopId
  131. */
  132. buy(type: string, shopId: number, callBack: Function) {
  133. let msg = {
  134. id: shopId
  135. };
  136. this.main.gameHttp.sendJson(`${type}/v1/buy`, msg, (state, reve: ReveData) => {
  137. this.main.stopLoad();
  138. if (state == HttpStateType.SUCCESS) {
  139. if (reve.retCode == 0) {
  140. console.log("==reve==buy==", reve)
  141. this.main.playerEffectByPath(AudioMgr.gift);
  142. this.main.showReward(reve);
  143. callBack && callBack();
  144. } else {
  145. if (type == BuyType.pandora && reve.retCode == 600501) {
  146. this.showExchangeAmethystView();
  147. } else {
  148. this.main.showTips(reve.message);
  149. }
  150. }
  151. } else {
  152. this.main.showTips('网络异常');
  153. }
  154. this._isClick = false;
  155. });
  156. }
  157. playVideo(callback: (result: number) => void, id: number) {
  158. let eventId1 = "";
  159. let eventId2 = "";
  160. if (this.curInsetNum == 102) {
  161. eventId1 = UM_EVENT_ID.ad_power_0;
  162. eventId2 = UM_EVENT_ID.ad_power_1;
  163. } else if (this.curInsetNum == 103) {
  164. eventId1 = UM_EVENT_ID.ad_money_0;
  165. eventId2 = UM_EVENT_ID.ad_money_1;
  166. } else if (this.curInsetNum == 104) {
  167. if (id == 1001) {
  168. eventId1 = UM_EVENT_ID.ad_pdl_t_0;
  169. eventId2 = UM_EVENT_ID.ad_pdl_t_1;
  170. } else if (id == 1002) {
  171. eventId1 = UM_EVENT_ID.ad_pdl_m_0;
  172. eventId2 = UM_EVENT_ID.ad_pdl_m_1;
  173. }
  174. }
  175. let fqPay: FqPay = new FqPay(this.main);
  176. fqPay.adVideo(callback, eventId1, eventId2);
  177. }
  178. // 切换插页的时候做隐藏处理
  179. showList(lastIndex: number, curIndex: number) {
  180. if (this.itemList[lastIndex] && this.itemList[lastIndex].length) {
  181. this.itemList[lastIndex].forEach((element: cc.Node) => {
  182. element.active = false;
  183. });
  184. }
  185. if (this.itemList[curIndex] && this.itemList[curIndex].length) {
  186. this.itemList[curIndex].forEach((element: cc.Node) => {
  187. element.active = true;
  188. });
  189. }
  190. }
  191. isCreatorItem(curIndex: number) {
  192. let b = true;
  193. if (this.itemList[curIndex]) {
  194. b = false;
  195. }
  196. return b
  197. }
  198. payView() {
  199. if (!this.isCreatorItem(101)) return
  200. let _payDatas = this.main.sManage.payDatas
  201. for (let i = 0; i < _payDatas.length; i++) {
  202. const element = _payDatas[i];
  203. if (element.type == 1) {
  204. let node = cc.instantiate(this.payItem)
  205. node.parent = this.content;
  206. if (!this.itemList[101]) {
  207. this.itemList[101] = [];
  208. }
  209. this.itemList[101].push(node);
  210. let payItem = node.getComponent(PayShopItem)
  211. payItem.init(element, this.main)
  212. payItem.setCallback((item: PayShopItem) => {
  213. let fqPay: FqPay = new FqPay(this.main)
  214. fqPay.pay(item._payData, () => {
  215. this.refreshDiamond();
  216. this.itemList[101].forEach((element: cc.Node) => {
  217. element.getComponent(PayShopItem).showFirstGave();
  218. });
  219. })
  220. })
  221. }
  222. }
  223. }
  224. powerView() {
  225. if (!this.isCreatorItem(102)) return
  226. this.shopData.marketPowerData.forEach((data, index) => {
  227. let node = cc.instantiate(this.powerItem)
  228. node.parent = this.content;
  229. if (!this.itemList[102]) {
  230. this.itemList[102] = [];
  231. }
  232. this.itemList[102].push(node);
  233. let powerItem = node.getComponent(ShopPowerItem);
  234. powerItem.lbtime.active = index == 0;
  235. powerItem.init(data, this.shopData.playerMarketPower);
  236. powerItem.setCallback((item: ShopPowerItem) => {
  237. console.log("===buy power===")
  238. if (item.shopId == item.videoId) {
  239. if (item.videoMap[item.shopId]) {
  240. this.playVideo(result => {
  241. if (result == 1) {
  242. this.buy(BuyType.marketPower, item.shopId, () => {
  243. item.refresh(item.shopId);
  244. });
  245. } else {
  246. this.main.showTips("广告还未准备好");
  247. }
  248. }, item.shopId)
  249. } else {
  250. this.main.showTips("今日广告次数已用完");
  251. }
  252. } else {
  253. if (this._isClick) {
  254. this.main.showTips("当前操作过于频繁!");
  255. return
  256. }
  257. this._isClick = true;
  258. this.buy(BuyType.marketPower, item.shopId, () => {
  259. this.refreshDiamond();
  260. });
  261. }
  262. })
  263. })
  264. }
  265. coinView() {
  266. if (!this.isCreatorItem(103)) return
  267. this.shopData.marketMoneyData.forEach((data, index) => {
  268. let node = cc.instantiate(this.coinItem)
  269. node.parent = this.content;
  270. if (!this.itemList[103]) {
  271. this.itemList[103] = [];
  272. }
  273. this.itemList[103].push(node);
  274. let coinItem = node.getComponent(ShopCoinItem);
  275. coinItem.lbtime.active = index == 0;
  276. coinItem.init(data, this.shopData.playerMarketMoney);
  277. coinItem.setCallback((item: ShopCoinItem) => {
  278. console.log("====buy coin======", item.shopId)
  279. if (item.shopId == item.videoId) {
  280. if (item.videoMap[item.shopId]) {
  281. this.playVideo(result => {
  282. if (result == 1) {
  283. this.buy(BuyType.marketMoney, item.shopId, () => {
  284. item.refresh(item.shopId);
  285. });
  286. } else {
  287. this.main.showTips("广告还未准备好");
  288. }
  289. }, item.shopId)
  290. } else {
  291. this.main.showTips("今日广告次数已用完");
  292. }
  293. } else {
  294. if (this._isClick) {
  295. this.main.showTips("当前操作过于频繁!");
  296. return
  297. }
  298. this._isClick = true;
  299. this.buy(BuyType.marketMoney, item.shopId, () => {
  300. this.refreshDiamond();
  301. });
  302. }
  303. })
  304. });
  305. }
  306. // 潘多拉魔盒
  307. pandoraView() {
  308. if (!this.isCreatorItem(104)) return
  309. this.pandoraData.pandoraData.forEach(data => {
  310. let node = cc.instantiate(this.pandoraItem)
  311. node.parent = this.content;
  312. if (!this.itemList[104]) {
  313. this.itemList[104] = [];
  314. }
  315. this.itemList[104].push(node);
  316. let pandoraItem = node.getComponent(ShopPandoraItem);
  317. pandoraItem.init(data, this.pandoraData.playerPandora);
  318. pandoraItem.setCallback((item: ShopPandoraItem) => {
  319. console.log("====buy pandora======")
  320. this.pandoraDrawCount = item.pandoraDrawCount;
  321. this.needAmethyst = item.needAmethyst;
  322. this.pandorId = item.shopId;
  323. this.shopPandoraItem = item;
  324. if (!item.videoMap[item.shopId]) {
  325. if (this._isClick) {
  326. this.main.showTips("当前操作过于频繁!");
  327. return
  328. }
  329. this._isClick = true;
  330. this.buy(BuyType.pandora, item.shopId, () => {
  331. item.refreshDraw(item.shopId);
  332. this.refreshAmethyst();
  333. });
  334. } else {
  335. this.playVideo(result => {
  336. if (result == 1) {
  337. this.buy(BuyType.pandora, item.shopId, () => {
  338. item.refreshDraw(item.shopId);
  339. item.refresh(item.shopId);
  340. });
  341. } else {
  342. this.main.showTips("广告还未准备好");
  343. }
  344. }, item.shopId)
  345. }
  346. })
  347. })
  348. }
  349. // // 紫水晶
  350. // amethystView() {
  351. // this.content.removeAllChildren();
  352. // for (let i = 0; i < 2; i++) {
  353. // let node = cc.instantiate(this.amethystItem)
  354. // node.parent = this.content
  355. // let amethystItem = node.getComponent(ShopAmethystItem);
  356. // amethystItem.setCallback((item: ShopAmethystItem) => {
  357. // console.log("====buy amethyst======")
  358. // })
  359. // }
  360. // }
  361. changeStatus(target: cc.Node, click: boolean) {
  362. if (click) {
  363. target.getChildByName("liang").active = true;
  364. target.getChildByName("an").active = false;
  365. } else {
  366. target.getChildByName("liang").active = false;
  367. target.getChildByName("an").active = true;
  368. }
  369. }
  370. changeLayout(index: number) {
  371. let layout = this.content.getComponent(cc.Layout);
  372. if (index == 101) {
  373. layout.paddingLeft = 10;
  374. layout.paddingTop = 0;
  375. } else if (index == 104 || index == 102 || index == 103) {
  376. layout.paddingLeft = 30;
  377. layout.paddingTop = 80;
  378. }
  379. }
  380. refreshAmethyst() {
  381. let num = this.main.player.getGoodCount(1009);
  382. this.amethystNode.getChildByName("lbamethyst").getComponent(cc.Label).string = ` ${num}`;
  383. }
  384. refreshDiamond() {
  385. let num = this.main.player.role.gold;
  386. this.diamondNode.getChildByName("lbdiamond").getComponent(cc.Label).string = ` ${num}`;
  387. }
  388. showExchangeAmethystView() {
  389. this.main.viewManage.loadFunc(GameViewType.exchangeAmethyst, (viewObject: ViewObject) => {
  390. let view = viewObject as ExchangeAmethystView;
  391. view.show();
  392. view.init(this.pandoraDrawCount, this.needAmethyst, () => {
  393. this.buy(BuyType.pandora, this.pandorId, () => {
  394. this.shopPandoraItem.refreshDraw(this.pandorId);
  395. this.refreshAmethyst();
  396. this.refreshDiamond();
  397. });
  398. });
  399. });
  400. }
  401. onClickInset(eventTouch: cc.Event.EventTouch, data: string) {
  402. if (this.curInsetNum == Number(data)) return
  403. this.changeStatus(this.curInsetNode, false);
  404. this.changeStatus(eventTouch.target, true);
  405. this.changeLayout(Number(data));
  406. this.curInsetNode = eventTouch.target;
  407. let lastIndex = this.curInsetNum;
  408. this.showList(lastIndex, Number(data));
  409. this.curInsetNum = Number(data);
  410. this.powerTips.active = Number(data) == 102;
  411. // this.amethystNode.active = Number(data) == 104;
  412. if (Number(data) == 101) {
  413. this.payView();
  414. } else if (Number(data) == 102) {
  415. this.powerView();
  416. } else if (Number(data) == 103) {
  417. this.coinView();
  418. } else if (Number(data) == 104) {
  419. this.pandoraView();
  420. }
  421. // console.log("onClickInset", arguments);
  422. }
  423. onClose() {
  424. this.main.topNode.getComponent(TopMenu).forbidBtn(true);
  425. this.exitDistroy();
  426. }
  427. // update (dt) {}
  428. }