UIREShopView.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. local UIREShopView = require("UIShop/UIREShopView_Generate")
  2. local UIShopBuyTips1 = require("UIShop/UIShopBuyTips1")
  3. local UIShopGoldBuyTips = require("UIShop/UIShopGoldBuyTips")
  4. local exchangeCostKey = 308
  5. local exchangeCosts
  6. function UIREShopView:OnAwake(data)
  7. self.controller = require("UIShop/UIREShopCtr"):new()
  8. self.controller:Init(self)
  9. self.controller:SetData(data)
  10. end
  11. function UIREShopView:AddEventListener()
  12. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name)
  13. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name, UIEventNames.SHOP_DATA_CHANGED, self, self.OnShopDataChanged)
  14. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name, UIEventNames.EID_Open_Activity_Refresh, self, self.OnOpenActivityRefresh)
  15. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name, UIEventNames.EID_Activity_Data_Change, self, self.OnActivityDataChange)
  16. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name, UIEventNames.RED_ENVELOPE_CASH_EXCHANGE_SUCCESS, self, self.RefreshResAndRedEnvelope)
  17. end
  18. function UIREShopView:FillContent(data, uiBase)
  19. self.uiBase = uiBase
  20. local gameObject = self.uiBase:GetRoot()
  21. if gameObject ~= nil then
  22. self.gameObject = gameObject
  23. self.transform = gameObject.transform
  24. end
  25. self:InitGenerate(self.transform, data)
  26. self:Init()
  27. end
  28. function UIREShopView:Init()
  29. local val = GlobalConfig.Instance:GetConfigStrValue(exchangeCostKey)
  30. if val ~= "" and val ~= nil then
  31. exchangeCosts = CommonUtil.DeserializeGlobalStrToTable(val)
  32. end
  33. self.controller:InitData()
  34. self.controller:RefreshCurShopData(true)
  35. self.shopCurrencyItem:SetActive(false)
  36. self:RefreshRedEnvelope()
  37. self:RefreshResourceBoxs()
  38. self:InitGoods()
  39. self:RefreshGoods(true)
  40. self:InitSubPanels()
  41. end
  42. function UIREShopView:InitSubPanels()
  43. if self.shopBuyTips1 == nil then
  44. self.shopBuyTips1 = UIShopBuyTips1:new()
  45. end
  46. self.shopBuyTips1:InitGo(self,self.uiBase:FindChildGo("UIShop/UIShopBuyTips1"))
  47. self.shopBuyTips1:Hide()
  48. if self.shopGoldBuyTips == nil then
  49. self.shopGoldBuyTips = UIShopGoldBuyTips:new()
  50. end
  51. self.shopGoldBuyTips:InitGo(self,self.uiBase:FindChildGo("UIShop/UIShopGoldBuyTips"))
  52. self.shopGoldBuyTips:Hide()
  53. end
  54. function UIREShopView:RefreshResAndRedEnvelope()
  55. self:RefreshResourceBoxs()
  56. self:RefreshRedEnvelope()
  57. end
  58. function UIREShopView:RefreshResourceBoxs()
  59. local cfgData = self.controller:GetCurShopCfgData()
  60. local currencyLs = cfgData and cfgData.Currency or nil
  61. local showIdx = 0
  62. local currencys = self.currencys
  63. if not currencys then
  64. currencys = {}
  65. self.currencys = currencys
  66. end
  67. if currencyLs then
  68. for i = 1, #currencyLs do
  69. showIdx = i
  70. local itemId = currencyLs[i]
  71. local itemLua = currencys[i]
  72. if not itemLua then
  73. itemLua = self:CreateCurrencyItem()
  74. currencys[i] = itemLua
  75. end
  76. itemLua:SetActive(true)
  77. itemLua.icon.image.sprite = nil
  78. itemLua.icon.image.enabled = false
  79. local itemCfgData = ManagerContainer.CfgMgr:GetItemById(itemId)
  80. if itemCfgData then
  81. CommonUtil.LoadIcon(self, itemCfgData.MiniIcon, function(sprite)
  82. itemLua.icon.image.sprite = sprite
  83. itemLua.icon.image.enabled = true
  84. end)
  85. itemLua.number.text.text = CommonUtil.FormatNumber(CommonUtil.GetOwnResCountByItemId(itemId))
  86. else
  87. itemLua.number.text.text = '0'
  88. end
  89. end
  90. end
  91. for i = showIdx + 1, #currencys do
  92. local currency = currencys[i]
  93. if currency then
  94. currency:SetActive(false)
  95. end
  96. end
  97. end
  98. function UIREShopView:RefreshRedEnvelope()
  99. if not exchangeCosts or not exchangeCosts[1] then return end
  100. local resId = tonumber(exchangeCosts[1][1])
  101. local count = tonumber(exchangeCosts[1][2])
  102. local ownerCount = CommonUtil.GetOwnResCountByItemId(resId)
  103. local state = ownerCount >= count
  104. if not state then
  105. self.redEnvelope.desc.text.text = string.formatbykey("CashShopTips01", count)
  106. end
  107. self.redEnvelope.desc:SetActive(not state)
  108. self.redEnvelope.btnSure:SetActive(state)
  109. end
  110. function UIREShopView:InitGoods()
  111. self.goodsScrollView.loopGridView:InitGridView(0, function(gridView, itemIndex, row, column)
  112. return self:GetShopGoodsItemByRowColumn(gridView, itemIndex, row, column)
  113. end, nil)
  114. end
  115. function UIREShopView:CreateCurrencyItem()
  116. local go = CommonUtil.Instantiate(self.shopCurrencyItem.gameObject, self.resourceBox.transform)
  117. return CommonUtil.BindGridViewItem2Lua(self, 'ShopCurrencyItem', go)
  118. end
  119. function UIREShopView:RefreshGoods(resetPos)
  120. local shopData = self.controller:GetCurShopData()
  121. local length = 0
  122. if shopData then
  123. local goods = shopData:GetShowGoodsDatas()
  124. if goods then
  125. length = #goods
  126. end
  127. -- 处理在时间段显示的商品,或不需要通过服务器就能刷新的数据
  128. -- local remain = ManagerContainer.LuaTimerMgr:GetRemainSeconds(shopData:GetNeedRefreshTime())
  129. -- if self.needRefreshTimer then
  130. -- self.needRefreshTimer:Stop()
  131. -- end
  132. -- if remain > 0 then
  133. -- self.needRefreshTimer = Timer.New(function()
  134. -- ManagerContainer.LuaUIMgr:ErrorNoticeDisplay("ShopTips")
  135. -- local curShopData = self.controller:GetCurShopData()
  136. -- if curShopData then
  137. -- curShopData:RefreshShowGoodsDatas()
  138. -- end
  139. -- self:RefreshGoods(true)
  140. -- end, remain + 1, 1)
  141. -- if not self.needRefreshTimer.running then
  142. -- self.needRefreshTimer:Start()
  143. -- end
  144. -- end
  145. end
  146. if resetPos then
  147. self.goodsScrollView.loopGridView:RefreshListByIndex(length, 0)
  148. else
  149. self.goodsScrollView.loopGridView:RefreshListByIndex(length)
  150. end
  151. end
  152. function UIREShopView:GetShopGoodsItemByRowColumn(gridView, itemIndex, row, column)
  153. local shopData = self.controller:GetCurShopData()
  154. if not shopData then return nil end
  155. local goods = shopData:GetShowGoodsDatas()
  156. if not goods then return nil end
  157. local goodsData = goods[itemIndex + 1]
  158. if not goodsData then return nil end
  159. local item = gridView:NewListViewItem('RuneShopGoodsItem')
  160. local itemLua = CommonUtil.BindGridViewItem2Lua(self, 'RuneShopGoodsItem', item.gameObject)
  161. if itemLua then
  162. itemLua.itemIcon.image.sprite = nil
  163. itemLua.itemIcon.image.enabled = false
  164. local cfgData = goodsData.cfgData
  165. itemLua.presentPrice.icon:SetActive(true)
  166. itemLua.presentPrice.currency:SetActive(false)
  167. if cfgData then
  168. CommonUtil.LoadIcon(self, cfgData.GoodsPic, function(sprite)
  169. if sprite then
  170. itemLua.itemIcon.image.sprite = sprite
  171. itemLua.itemIcon.image.enabled = true
  172. end
  173. end, itemLua, 'ItemIcon')
  174. local itemCfgData = ManagerContainer.CfgMgr:GetItemById(cfgData.PayForType)
  175. CommonUtil.LoadIcon(self, itemCfgData.MiniIcon, function (sprite)
  176. itemLua.presentPrice.icon.image.sprite = sprite
  177. end, itemLua, 'CurrenyIcon')
  178. itemLua.itemName.text.text = string.formatbykey(cfgData.GoodsName)
  179. --LogError("====================="..cfgData.GoodsName.."========================")
  180. itemLua.discount:SetActive(false)
  181. self:RefreshGiftsItem(itemLua, nil, nil, cfgData.Reward)
  182. else
  183. itemLua.discount:SetActive(false)
  184. itemLua.itemName.text.text = ''
  185. itemLua.specialReward:SetActive(false)
  186. itemLua.goodsItems:SetActive(false)
  187. end
  188. local remainBuyNum = goodsData:GetRemainBuyNum()
  189. if remainBuyNum >= 0 then
  190. itemLua.purchaseLimit:SetActive(true)
  191. itemLua.purchaseLimit.text.text = string.formatbykey('RuneShopLimitBuy', tostring(remainBuyNum))
  192. else
  193. itemLua.purchaseLimit:SetActive(false)
  194. end
  195. local price = goodsData.curPrice
  196. if price <= 0 then
  197. itemLua.presentPrice.number.text.text = string.formatbykey('Free')
  198. else
  199. itemLua.presentPrice.number.text.text = FloatToPrice(price)
  200. end
  201. if goodsData:IsSoldout() then
  202. itemLua.soldout:SetActive(true)
  203. itemLua.button.onClick:RemoveAllListeners()
  204. itemLua.presentPrice.button.onClick:RemoveAllListeners()
  205. else
  206. itemLua.soldout:SetActive(false)
  207. self.uiBase:AddButtonUniqueEventListener(itemLua.button, self, self.OnClickGiftItem, goodsData)
  208. self.uiBase:AddButtonUniqueEventListener(itemLua.presentPrice.button, self, self.OnClickGiftPrice, goodsData)
  209. end
  210. end
  211. return item
  212. end
  213. function UIREShopView:RefreshGiftsItem(itemLua, bgPic, numPic, rewards)
  214. if numPic and numPic ~= '' then
  215. itemLua.specialReward.bg.image.sprite = nil
  216. itemLua.specialReward.bg.image.enabled = false
  217. CommonUtil.LoadIcon(self, bgPic, function(sprite)
  218. if sprite then
  219. itemLua.specialReward.bg.image.sprite = sprite
  220. itemLua.specialReward.bg.image.enabled = true
  221. end
  222. end, itemLua, 'SpecialRewardBgIcon')
  223. itemLua.specialReward.num.image.sprite = nil
  224. itemLua.specialReward.num.image.enabled = false
  225. CommonUtil.LoadIcon(self, numPic, function(sprite)
  226. if sprite then
  227. itemLua.specialReward.num.image.sprite = sprite
  228. itemLua.specialReward.num.image.enabled = true
  229. end
  230. end, itemLua, 'SpecialRewardNumIcon')
  231. itemLua.specialReward:SetActive(true)
  232. itemLua.goodsItems:SetActive(false)
  233. else
  234. itemLua.specialReward:SetActive(false)
  235. if rewards then
  236. itemLua.goodsItems:SetActive(true)
  237. local reward = rewards[1]
  238. if reward then
  239. itemLua.iconSmallItem1:SetActive(true)
  240. CommonUtil.UpdateItemPrefab(self, itemLua.iconSmallItem1, {cfgId = reward[1], num = reward[2]}, Enum.ItemIEnterType.Bag)
  241. else
  242. itemLua.iconSmallItem1:SetActive(false)
  243. end
  244. reward = rewards[2]
  245. if reward then
  246. itemLua.iconSmallItem2:SetActive(true)
  247. CommonUtil.UpdateItemPrefab(self, itemLua.iconSmallItem2, {cfgId = reward[1], num = reward[2]}, Enum.ItemIEnterType.Bag)
  248. else
  249. itemLua.iconSmallItem2:SetActive(false)
  250. end
  251. reward = rewards[3]
  252. if reward then
  253. itemLua.iconSmallItem3:SetActive(true)
  254. CommonUtil.UpdateItemPrefab(self, itemLua.iconSmallItem3, {cfgId = reward[1], num = reward[2]}, Enum.ItemIEnterType.Bag)
  255. else
  256. itemLua.iconSmallItem3:SetActive(false)
  257. end
  258. else
  259. itemLua.goodsItems:SetActive(false)
  260. end
  261. end
  262. end
  263. function UIREShopView:DisposeCurrencyItems()
  264. if not self.currencys then return end
  265. for _, currency in pairs(self.currencys) do
  266. if currency then
  267. local go = currency.gameObject
  268. CommonUtil.ClearGridViewItem(self, currency)
  269. CommonUtil.DestroyGO(go)
  270. end
  271. end
  272. self.currencys = nil
  273. end
  274. function UIREShopView:OnClickGiftItem(button, params)
  275. local goodsData = params[0]
  276. self.controller:SetCurGoodsData(goodsData)
  277. if self.shopBuyTips1 then
  278. self.shopBuyTips1:Show(goodsData)
  279. end
  280. end
  281. function UIREShopView:OnRedEnvelopeBtnClick(button, params)
  282. if self.shopGoldBuyTips then
  283. self.shopGoldBuyTips:Show()
  284. end
  285. end
  286. function UIREShopView:OnClickPlayRuleBtn()
  287. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIPlayRule, {'PlayExplainTitle', 'CashShopExplain'})
  288. end
  289. function UIREShopView:OnClickGiftPrice(button, params)
  290. local goodsData = params[0]
  291. self.controller:SetCurGoodsData(goodsData)
  292. self:OnClickBuyBtn()
  293. end
  294. function UIREShopView:OnClickBuyBtn()
  295. local errorCode, itemCfgId = self.controller:SendShopBuyItemReq()
  296. if errorCode ~= 0 then
  297. if errorCode == 391 then
  298. CommonUtil.ItemNotEnoughHandle(itemCfgId, Enum.UIPageName.UIShop)
  299. else
  300. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay(errorCode)
  301. end
  302. end
  303. if self.shopBuyTips1 then
  304. self.shopBuyTips1:Hide()
  305. end
  306. end
  307. function UIREShopView:ShowItemTips(button, params)
  308. ManagerContainer.LuaUIMgr:OpenTips(params[0])
  309. end
  310. function UIREShopView:OnShopDataChanged(shopId)
  311. local cfgData = self.controller:GetCurShopCfgData()
  312. if cfgData and cfgData.Id ~= shopId then return end
  313. self.controller:RefreshCurShopData()
  314. self:RefreshRedEnvelope()
  315. self:RefreshResourceBoxs()
  316. self:RefreshGoods()
  317. end
  318. function UIREShopView:OnOpenActivityRefresh(actIds)
  319. if not actIds then return end
  320. local changed = false
  321. for _, actId in pairs(actIds) do
  322. local activityItem = ManagerContainer.DataMgr.ActsDataMgr:GetActivityItemById(actId)
  323. if activityItem and activityItem.type == Enum.ActivityType.ACTIVITY_TYPE_SHOP then
  324. changed = true
  325. break
  326. end
  327. end
  328. if not changed then return end
  329. self.controller:InitData()
  330. self.controller:RefreshCurShopData(true)
  331. self:RefreshRedEnvelope()
  332. self:RefreshResourceBoxs()
  333. self:RefreshGoods()
  334. end
  335. function UIREShopView:OnActivityDataChange(actId)
  336. if not actId then return end
  337. local activityItem = ManagerContainer.DataMgr.ActsDataMgr:GetActivityItemById(actId)
  338. if not activityItem then return end
  339. if activityItem.type ~= Enum.ActivityType.ACTIVITY_TYPE_SHOP then
  340. return
  341. end
  342. self.controller:InitData()
  343. self.controller:RefreshCurShopData(true)
  344. self:RefreshRedEnvelope()
  345. self:RefreshResourceBoxs()
  346. self:RefreshGoods()
  347. end
  348. function UIREShopView:RemoveEventListener()
  349. ManagerContainer.LuaEventMgr:Unregister(self.uiData.name)
  350. end
  351. function UIREShopView:AddUIEventListener()
  352. self.shopBuyTips1:AddUIEventListener()
  353. self.shopGoldBuyTips:AddUIEventListener()
  354. self.uiBase:AddButtonEventListener(self.btnClose.button, self, self.OnClickCloseBtn)
  355. self.uiBase:AddButtonEventListener(self.redEnvelope.btnSure.button, self, self.OnRedEnvelopeBtnClick)
  356. self.uiBase:AddButtonEventListener(self.btnInfo.button, self, self.OnClickPlayRuleBtn)
  357. end
  358. function UIREShopView:OnClickCloseBtn()
  359. ManagerContainer.LuaUIMgr:OpenSourceUI(self)
  360. end
  361. function UIREShopView:OnHide()
  362. end
  363. function UIREShopView:OnShow(data)
  364. if data then
  365. self.controller:SetData(data)
  366. end
  367. self.controller:InitData()
  368. self.controller:RefreshCurShopData(true)
  369. self:RefreshResourceBoxs()
  370. self:RefreshGoods()
  371. end
  372. function UIREShopView:OnClose()
  373. if self.shopBuyTips1 ~= nil then
  374. self.shopBuyTips1:Dispose()
  375. self.shopBuyTips1 = nil
  376. end
  377. if self.shopGoldBuyTips ~= nil then
  378. self.shopGoldBuyTips:Dispose()
  379. self.shopGoldBuyTips = nil
  380. end
  381. end
  382. function UIREShopView:OnDispose()
  383. self.goodsScrollView.loopGridView:Dispose()
  384. self:DisposeCurrencyItems()
  385. self.controller:OnDispose()
  386. end
  387. return UIREShopView