UIShopView.lua 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. local UIShopView = require("UIShop/UIShopView_Generate")
  2. function UIShopView:OnAwake(data)
  3. self.controller = require("UIShop/UIShopCtr"):new()
  4. self.controller:Init(self)
  5. self.controller:SetData(data)
  6. end
  7. function UIShopView:AddEventListener()
  8. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name, UIEventNames.SHOP_DATA_CHANGED, self, self.OnShopDataChanged)
  9. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name, UIEventNames.EID_Open_Activity_Refresh, self, self.OnOpenActivityRefresh)
  10. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name, UIEventNames.EID_Activity_Data_Change, self, self.OnActivityDataChange)
  11. end
  12. function UIShopView:FillContent(data, uiBase)
  13. self.uiBase = uiBase
  14. local gameObject = self.uiBase:GetRoot()
  15. if gameObject ~= nil then
  16. self.gameObject = gameObject
  17. self.transform = gameObject.transform
  18. end
  19. self:InitGenerate(self.transform, data)
  20. self:Init()
  21. end
  22. function UIShopView:Init()
  23. self.controller:InitData()
  24. self.controller:RefreshCurShopData(true)
  25. self.shopCurrencyItem:SetActive(false)
  26. self:InitLabels()
  27. self:RefreshLabels(true)
  28. self:RefreshResourceBoxs()
  29. self:RefreshGirlDialogue()
  30. self:InitGoods()
  31. self:RefreshGoods(true)
  32. end
  33. function UIShopView:RemoveEventListener()
  34. ManagerContainer.LuaEventMgr:Unregister(self.uiData.name)
  35. self.labelScrollView.loopGridView.mOnBeginDragAction = nil
  36. self.labelScrollView.loopGridView.mOnSnapItemFinished = nil
  37. end
  38. function UIShopView:AddUIEventListener()
  39. self.uiBase:AddButtonUniqueEventListener(self.btnClose.button, self, self.OnClickCloseBtn)
  40. self.uiBase:AddButtonUniqueEventListener(self.btnRefresh.button, self, self.OnClickRefreshBtn)
  41. self.labelScrollView.loopGridView.mOnBeginDragAction = function(gridView)
  42. gridView.ItemSnapEnable = false
  43. end
  44. self.labelScrollView.loopGridView.mOnSnapItemFinished = function(gridView, gridViewItem)
  45. gridView.ItemSnapEnable = false
  46. end
  47. end
  48. function UIShopView:OnHide()
  49. end
  50. function UIShopView:OnShow(data)
  51. if data then
  52. self.controller:SetData(data)
  53. end
  54. self.controller:InitData()
  55. self.controller:RefreshCurShopData(true)
  56. self:RefreshLabels()
  57. self:RefreshResourceBoxs()
  58. self:RefreshGirlDialogue()
  59. self:RefreshGoods()
  60. end
  61. function UIShopView:OnClose()
  62. end
  63. function UIShopView:OnDispose()
  64. if self.needRefreshTimer then
  65. self.needRefreshTimer:Stop()
  66. self.needRefreshTimer = nil
  67. end
  68. if self.labelRefreshTimer then
  69. self.labelRefreshTimer:Stop()
  70. self.labelRefreshTimer = nil
  71. end
  72. if self.dialogueTimer then
  73. self.dialogueTimer:Stop()
  74. self.dialogueTimer = nil
  75. end
  76. self:DisposeRefreshTimer()
  77. self:DisposeShopGoodsItemTimer()
  78. self.goodsScrollView.loopGridView:Dispose()
  79. self.labelScrollView.loopGridView:Dispose()
  80. self:DisposeCurrencyItems()
  81. self.controller:OnDispose()
  82. end
  83. function UIShopView:OnShopDataChanged(shopId)
  84. local cfgData = self.controller:GetCurShopCfgData()
  85. if cfgData and cfgData.Id ~= shopId then return end
  86. self.controller:RefreshCurShopData()
  87. self:RefreshResourceBoxs()
  88. self:RefreshGoods()
  89. end
  90. function UIShopView:OnOpenActivityRefresh(actIds)
  91. if not actIds then return end
  92. local changed = false
  93. for _, actId in pairs(actIds) do
  94. local activityItem = ManagerContainer.DataMgr.ActsDataMgr:GetActivityItemById(actId)
  95. if activityItem and activityItem.type == Enum.ActivityType.ACTIVITY_TYPE_SHOP then
  96. changed = true
  97. break
  98. end
  99. end
  100. if not changed then return end
  101. self.controller:InitData()
  102. self.controller:RefreshCurShopData(true)
  103. self:RefreshLabels()
  104. self:RefreshResourceBoxs()
  105. self:RefreshGirlDialogue()
  106. self:RefreshGoods()
  107. end
  108. function UIShopView:OnActivityDataChange(actId)
  109. if not actId then return end
  110. local activityItem = ManagerContainer.DataMgr.ActsDataMgr:GetActivityItemById(actId)
  111. if not activityItem then return end
  112. if activityItem.type ~= Enum.ActivityType.ACTIVITY_TYPE_SHOP then
  113. return
  114. end
  115. self.controller:InitData()
  116. self.controller:RefreshCurShopData(true)
  117. self:RefreshLabels()
  118. self:RefreshResourceBoxs()
  119. self:RefreshGirlDialogue()
  120. self:RefreshGoods()
  121. end
  122. function UIShopView:OnClickCloseBtn()
  123. ManagerContainer.LuaUIMgr:OpenSourceUI(self)
  124. end
  125. function UIShopView:OnClickRefreshBtn()
  126. local cfgData = self.controller:GetCurShopCfgData()
  127. if not cfgData then
  128. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay(397)
  129. return
  130. end
  131. -- if cfgData.ShopType ~= 2 then
  132. -- ManagerContainer.LuaUIMgr:ErrorNoticeDisplay(397)
  133. -- return
  134. -- end
  135. local costNum, costCfgId = self.controller:GetRefreshCostInfo()
  136. local costName
  137. if costCfgId then
  138. local itemCfgData = ManagerContainer.CfgMgr:GetItemById(costCfgId)
  139. if itemCfgData then
  140. costName = itemCfgData.Name
  141. end
  142. end
  143. local shopData = self.controller:GetCurShopData()
  144. local refreshNum = shopData and shopData:GetRefreshCount() or 0
  145. local refreshMaxNum = cfgData.RefreshNum or 0
  146. local data = {"ShopRefreshSure", {tostring(costNum), tostring(costName)}, nil, self, self.SureRefreshShop, nil, {(refreshMaxNum - refreshNum)}}
  147. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UINoticeTips, data)
  148. end
  149. function UIShopView:SureRefreshShop()
  150. local errorCode, itemCfgId = self.controller:SendRefreshShopReq()
  151. if errorCode ~= 0 then
  152. -- if errorCode == 396 then
  153. -- CommonUtil.ItemNotEnoughHandle(itemCfgId, self)
  154. -- else
  155. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay(errorCode)
  156. -- end
  157. -- else
  158. -- ManagerContainer.LuaUIMgr:ErrorNoticeDisplay("ShopTips")
  159. end
  160. end
  161. function UIShopView:OnClickLabelItem(btn, params)
  162. local itemIndex = params[0]
  163. local idx = itemIndex + 1
  164. local lastIdx = self.controller:GetCurSelectIdx()
  165. if not self.controller:SetCurSelectIdx(idx) then return end
  166. self:RefreshLabelSelectState(lastIdx - 1, false)
  167. self:RefreshLabelSelectState(itemIndex, true)
  168. self:RefreshLabelsSnap(itemIndex)
  169. self:RefreshResourceBoxs()
  170. self:RefreshGirlDialogue()
  171. self:RefreshGoods(true)
  172. end
  173. function UIShopView:InitLabels()
  174. local initParam = SuperScrollView.LoopGridViewInitParam()
  175. initParam.mSmoothDumpRate = 0.05
  176. self.labelScrollView.loopGridView.ItemSnapEnable = false
  177. self.labelScrollView.loopGridView:InitGridView(0, function(gridView, itemIndex, row, column)
  178. return self:GetLabelItemByRowColumn(gridView, itemIndex, row, column)
  179. end, nil, nil, initParam)
  180. end
  181. function UIShopView:RefreshLabelsSnap(itemIndex)
  182. local loopGridView = self.labelScrollView.loopGridView
  183. local pair1 = loopGridView:GetRowColumnByItemIndex(itemIndex)
  184. local pair2 = loopGridView:GetRowColumnByItemIndex(itemIndex + 1)
  185. local offset = loopGridView.ContainerTrans.anchoredPosition
  186. local itemPosStart = loopGridView:GetItemPos(pair1.mRow, pair1.mColumn) + offset
  187. local itemPosEnd = loopGridView:GetItemPos(pair2.mRow, pair2.mColumn) + offset
  188. local rect = loopGridView.ScrollRect.viewport.rect
  189. local isStart = (itemPosStart.x >= rect.xMin) and (itemPosStart.x <= rect.xMax)
  190. local isEnd = (itemPosEnd.x >= rect.xMin) and (itemPosEnd.x <= rect.xMax)
  191. if isStart and isEnd then
  192. return
  193. end
  194. loopGridView.ItemSnapEnable = true
  195. loopGridView:SetSnapTargetItemRowColumn(pair1.mRow, pair1.mColumn)
  196. if isEnd then
  197. loopGridView.ItemSnapPivot = Vector2.zero
  198. loopGridView.ViewPortSnapPivot = Vector2.zero
  199. return
  200. end
  201. loopGridView.ItemSnapPivot = Vector2.one
  202. loopGridView.ViewPortSnapPivot = Vector2.one
  203. end
  204. function UIShopView:RefreshLabels(resetPos)
  205. local datas = self.controller:GetShopLs()
  206. local length = datas and #datas or 0
  207. if resetPos then
  208. local idx = self.controller:GetCurSelectIdx()
  209. self.labelScrollView.loopGridView:RefreshListByIndex(length, idx and (idx - 1) or 0)
  210. else
  211. self.labelScrollView.loopGridView:RefreshListByIndex(length)
  212. end
  213. local nearestRemainTime = self.controller:GetNearestRemainTime()
  214. if nearestRemainTime then
  215. if nearestRemainTime <= 0 then
  216. nearestRemainTime = 1
  217. end
  218. if not self.labelRefreshTimer then
  219. self.labelRefreshTimer = Timer.New(slot(self.RefreshLabelsTimer, self), nearestRemainTime, -1)
  220. else
  221. self.labelRefreshTimer.time = 0
  222. self.labelRefreshTimer.duration = nearestRemainTime
  223. end
  224. if not self.labelRefreshTimer.running then
  225. self.labelRefreshTimer:Start()
  226. end
  227. else
  228. if self.labelRefreshTimer and self.labelRefreshTimer.running then
  229. self.labelRefreshTimer:Stop()
  230. end
  231. end
  232. end
  233. function UIShopView:RefreshLabelsTimer()
  234. if self.labelRefreshTimer and self.labelRefreshTimer.running then
  235. self.labelRefreshTimer:Stop()
  236. end
  237. self.controller:InitData()
  238. self:RefreshLabels()
  239. self:RefreshGirlDialogue()
  240. self:RefreshGoods()
  241. end
  242. function UIShopView:RefreshLabelSelectState(itemIndex, state)
  243. local item = self.labelScrollView.loopGridView:GetShownItemByItemIndex(itemIndex)
  244. if not item then return end
  245. local itemLua = CommonUtil.BindGridViewItem2Lua(self, 'ShopLabelToggleItem', item.gameObject)
  246. if not itemLua then return end
  247. if state then
  248. itemLua.off:SetActive(false)
  249. itemLua.on:SetActive(true)
  250. itemLua.button.onClick:RemoveAllListeners()
  251. itemLua.button.interactable = false
  252. else
  253. itemLua.off:SetActive(true)
  254. itemLua.on:SetActive(false)
  255. itemLua.button.interactable = true
  256. self.uiBase:AddButtonUniqueEventListener(itemLua.button, self, self.OnClickLabelItem, itemIndex)
  257. end
  258. end
  259. function UIShopView:GetLabelItemByRowColumn(gridView, itemIndex, row, column)
  260. local idx = itemIndex + 1
  261. local itemData = self.controller:GetShopByIdx(idx)
  262. if not itemData then
  263. return nil
  264. end
  265. local item = gridView:NewListViewItem('ShopLabelToggleItem')
  266. local itemLua = CommonUtil.BindGridViewItem2Lua(self, 'ShopLabelToggleItem', item.gameObject)
  267. itemLua.icon.image.sprite = nil
  268. itemLua.icon.image.enabled = false
  269. CommonUtil.LoadIcon(self, itemData.Icon, function(sprite)
  270. if sprite then
  271. itemLua.icon.image.sprite = sprite
  272. itemLua.icon.image.enabled = true
  273. end
  274. end)
  275. itemLua.nameTxt.uILocalizeScript:SetContent(itemData.Name)
  276. if (self.controller:GetCurSelectIdx() == idx) then
  277. itemLua.off:SetActive(false)
  278. itemLua.on:SetActive(true)
  279. itemLua.button.onClick:RemoveAllListeners()
  280. itemLua.button.interactable = false
  281. else
  282. itemLua.off:SetActive(true)
  283. itemLua.on:SetActive(false)
  284. itemLua.button.interactable = true
  285. self.uiBase:AddButtonUniqueEventListener(itemLua.button, self, self.OnClickLabelItem, itemIndex)
  286. end
  287. return item
  288. end
  289. function UIShopView:RefreshResourceBoxs()
  290. local cfgData = self.controller:GetCurShopCfgData()
  291. local currencyLs = cfgData and cfgData.Currency or nil
  292. local showIdx = 0
  293. local currencys = self.currencys
  294. if not currencys then
  295. currencys = {}
  296. self.currencys = currencys
  297. end
  298. if currencyLs then
  299. for i = 1, #currencyLs do
  300. showIdx = i
  301. local itemId = currencyLs[i]
  302. local itemLua = currencys[i]
  303. if not itemLua then
  304. itemLua = self:CreateCurrencyItem()
  305. currencys[i] = itemLua
  306. end
  307. itemLua:SetActive(true)
  308. itemLua.icon.image.sprite = nil
  309. itemLua.icon.image.enabled = false
  310. local itemCfgData = ManagerContainer.CfgMgr:GetItemById(itemId)
  311. if itemCfgData then
  312. CommonUtil.LoadIcon(self, itemCfgData.MiniIcon, function(sprite)
  313. itemLua.icon.image.sprite = sprite
  314. itemLua.icon.image.enabled = true
  315. end)
  316. itemLua.number.text.text = CommonUtil.FormatNumber(CommonUtil.GetOwnResCountByItemId(itemId))
  317. else
  318. itemLua.number.text.text = '0'
  319. end
  320. end
  321. end
  322. for i = showIdx + 1, #currencys do
  323. local currency = currencys[i]
  324. if currency then
  325. currency:SetActive(false)
  326. end
  327. end
  328. end
  329. function UIShopView:CreateCurrencyItem()
  330. local go = CommonUtil.Instantiate(self.shopCurrencyItem.gameObject, self.resourceBox.transform)
  331. return CommonUtil.BindGridViewItem2Lua(self, 'ShopCurrencyItem', go)
  332. end
  333. function UIShopView:DisposeCurrencyItems()
  334. if not self.currencys then return end
  335. for _, currency in pairs(self.currencys) do
  336. if currency then
  337. local go = currency.gameObject
  338. CommonUtil.ClearGridViewItem(self, currency)
  339. CommonUtil.DestroyGO(go)
  340. end
  341. end
  342. self.currencys = nil
  343. end
  344. function UIShopView:RefreshGirlDialogue()
  345. local cfgData = self.controller:GetCurShopCfgData()
  346. local endTime = self.controller:GeCurEndTime()
  347. if endTime and endTime > 0 then
  348. local remainTime = endTime - ManagerContainer.LuaTimerMgr:GetTimeSecond()
  349. if remainTime > 0 then
  350. self.talkBoxTxt.uILocalizeScript:SetContentAndValues(cfgData and cfgData.Desc or '', {DateTimeUtil.convertSeconds2TimeStr1(remainTime, true, true, true)})
  351. local validTime = nil
  352. if remainTime >= 86400 then
  353. validTime = remainTime % 3600
  354. if validTime == 0 then validTime = 3600 end
  355. elseif remainTime >= 3600 then
  356. validTime = remainTime % 60
  357. if validTime == 0 then validTime = 60 end
  358. else
  359. validTime = 1
  360. end
  361. if not self.dialogueTimer then
  362. self.dialogueTimer = Timer.New(slot(self.RefreshGirlDialogue, self), validTime, -1)
  363. else
  364. self.dialogueTimer.duration = validTime
  365. end
  366. if not self.dialogueTimer.running then
  367. self.dialogueTimer:Start()
  368. end
  369. return
  370. end
  371. end
  372. if self.dialogueTimer and self.dialogueTimer.running then
  373. self.dialogueTimer:Stop()
  374. end
  375. self.talkBoxTxt.uILocalizeScript:SetContentAndValues(cfgData and cfgData.Desc or '', {DateTimeUtil.convertSeconds2TimeStr1(0, true, true, true)})
  376. end
  377. function UIShopView:InitGoods()
  378. self.goodsScrollView.loopGridView:InitGridView(0, function(gridView, itemIndex, row, column)
  379. return self:GetShopGoodsItemByRowColumn(gridView, itemIndex, row, column)
  380. end, nil)
  381. end
  382. function UIShopView:RefreshGoods(resetPos)
  383. local cfgData = self.controller:GetCurShopCfgData()
  384. local shopData = self.controller:GetCurShopData()
  385. local shopType = cfgData and cfgData.ShopType or nil
  386. self.timeBox:SetActive(shopType == 2)
  387. if shopType == 2 then
  388. local remainTime = 0
  389. local refreshTime = shopData and shopData:GetRefreshTime() or 0
  390. if refreshTime and refreshTime > 0 then
  391. remainTime = ManagerContainer.LuaTimerMgr:GetRemainSeconds(refreshTime)
  392. end
  393. self.textTime.text.text = string.formatbykey('ShopOutTime', DateTimeUtil.convertSeconds2TimeStr1(remainTime, true, true, false))
  394. if not remainTime or remainTime <= 0 then
  395. self:StopRefreshTimer()
  396. else
  397. self:StartRefreshTimer()
  398. end
  399. else
  400. self.textTime.text.text = ''
  401. self:StopRefreshTimer()
  402. end
  403. local length = 0
  404. if shopData then
  405. local goods = shopData:GetShowGoodsDatas()
  406. if goods then
  407. length = #goods
  408. end
  409. -- 处理在时间段显示的商品,或不需要通过服务器就能刷新的数据
  410. -- local remain = ManagerContainer.LuaTimerMgr:GetRemainSeconds(shopData:GetNeedRefreshTime())
  411. -- if self.needRefreshTimer then
  412. -- self.needRefreshTimer:Stop()
  413. -- end
  414. -- if remain > 0 then
  415. -- self.needRefreshTimer = Timer.New(function()
  416. -- ManagerContainer.LuaUIMgr:ErrorNoticeDisplay("ShopTips")
  417. -- local curShopData = self.controller:GetCurShopData()
  418. -- if curShopData then
  419. -- curShopData:RefreshShowGoodsDatas()
  420. -- end
  421. -- self:RefreshGoods(true)
  422. -- end, remain + 1, 1)
  423. -- if not self.needRefreshTimer.running then
  424. -- self.needRefreshTimer:Start()
  425. -- end
  426. -- end
  427. end
  428. if resetPos then
  429. self.goodsScrollView.loopGridView:RefreshListByIndex(length, 0)
  430. else
  431. self.goodsScrollView.loopGridView:RefreshListByIndex(length)
  432. end
  433. end
  434. function UIShopView:StartRefreshTimer()
  435. local timer = self.refreshTimer
  436. if not timer then
  437. timer = Timer.New(function()
  438. self:RefreshTimer()
  439. end, 1, -1)
  440. self.refreshTimer = timer
  441. end
  442. if not timer.running then
  443. timer:Start()
  444. end
  445. end
  446. function UIShopView:StopRefreshTimer()
  447. if self.refreshTimer then
  448. self.refreshTimer:Stop()
  449. end
  450. end
  451. function UIShopView:DisposeRefreshTimer()
  452. self:StopRefreshTimer()
  453. self.refreshTimer = nil
  454. end
  455. function UIShopView:RefreshTimer()
  456. local cfgData = self.controller:GetCurShopCfgData()
  457. local shopData = self.controller:GetCurShopData()
  458. if not cfgData or not shopData then
  459. self:StopRefreshTimer()
  460. return
  461. end
  462. local remainTime = 0
  463. local refreshTime = shopData:GetRefreshTime()
  464. if refreshTime and refreshTime > 0 then
  465. remainTime = ManagerContainer.LuaTimerMgr:GetRemainSeconds(refreshTime)
  466. end
  467. self.textTime.text.text = I18N.SetLanguageValue('ShopOutTime', DateTimeUtil.convertSeconds2TimeStr1(remainTime, true, true, false))
  468. if not remainTime or remainTime <= 0 then
  469. self:StopRefreshTimer()
  470. else
  471. if self.refreshTimer then
  472. local validTime = nil
  473. if remainTime >= 86400 then
  474. validTime = remainTime % 3600
  475. if validTime == 0 then validTime = 3600 end
  476. elseif remainTime >= 3600 then
  477. validTime = remainTime % 60
  478. if validTime == 0 then validTime = 60 end
  479. else
  480. validTime = 1
  481. end
  482. if validTime then
  483. self.refreshTimer.duration = validTime
  484. else
  485. self:StopRefreshTimer()
  486. end
  487. end
  488. end
  489. end
  490. function UIShopView:GetShopGoodsItemByRowColumn(gridView, itemIndex, row, column)
  491. local shopData = self.controller:GetCurShopData()
  492. if not shopData then return nil end
  493. local goods = shopData:GetShowGoodsDatas()
  494. if not goods then return nil end
  495. local goodsData = goods[itemIndex + 1]
  496. if not goodsData then return nil end
  497. local item = gridView:NewListViewItem('ShopGoodsItem')
  498. local itemLua = CommonUtil.BindGridViewItem2Lua(self, 'ShopGoodsItem', item.gameObject)
  499. local goodsCfgData = goodsData:GetGoodsCfgData()
  500. local data = {cfgId = goodsCfgData.GoodsItem}
  501. -- CommonUtil.UpdateItemPrefab(self, itemLua.iconItem, data, nil, self, self.ShowItemTips, data)
  502. CommonUtil.UpdateItemPrefab(self, itemLua.iconItem, data, nil)
  503. local itemCfgData = ManagerContainer.CfgMgr:GetItemById(goodsCfgData.PayForType)
  504. CommonUtil.LoadIcon(self, itemCfgData.MiniIcon, function (sprite)
  505. itemLua.presentPrice.icon.image.sprite = sprite
  506. itemLua.originalPrice.icon.image.sprite = sprite
  507. end, itemLua, 'CurrenyIcon')
  508. itemLua.itemName.text.text = goodsCfgData.GoodsName
  509. itemLua.presentPrice.number.text.text = goodsData.curPrice --现价
  510. if goodsData:IsDiscount() then --折扣
  511. itemLua.discount:SetActive(true)
  512. itemLua.originalPrice:SetActive(true)
  513. local discountNum = goodsData.disPercent
  514. --不是中文环境
  515. if not (ManagerContainer.LuaGameMgr.CurLangKey == "_cn"
  516. or ManagerContainer.LuaGameMgr.CurLangKey == "_tw") then
  517. discountNum = 100 - goodsData.disPercent
  518. else
  519. discountNum = goodsData.disPercent * 0.1
  520. end
  521. itemLua.discount.text.text.text = I18N.SetLanguageValue("ShopDiscount", tostring(discountNum)) -- {0}折,中文是:{0}折,非中文是{0}%off
  522. itemLua.originalPrice.number.text.text = goodsData.price
  523. elseif goodsData.hot then --热销
  524. itemLua.discount:SetActive(true)
  525. itemLua.originalPrice:SetActive(false)
  526. itemLua.discount.text.text.text = I18N.T("ShopSellwell")
  527. else
  528. itemLua.discount:SetActive(false)
  529. itemLua.originalPrice:SetActive(false)
  530. end
  531. local limitType = goodsData.limitType
  532. if limitType == Enum.LimitBuyType.NoLimit then
  533. itemLua.purchaseLimit:SetActive(false)
  534. itemLua.limitBox:SetActive(false)
  535. self:StopShopGoodsItemTimer(itemLua)
  536. elseif limitType == Enum.LimitBuyType.Forever then
  537. itemLua.purchaseLimit:SetActive(true)
  538. local remainNum = (goodsData.count - goodsData.buyNum)
  539. if remainNum < 0 then remainNum = 0 end
  540. itemLua.purchaseLimit.text.text = string.formatbykey("ShopForeverBuy", remainNum)
  541. itemLua.limitBox:SetActive(false)
  542. self:StopShopGoodsItemTimer(itemLua)
  543. elseif limitType == Enum.LimitBuyType.Daily then
  544. itemLua.purchaseLimit:SetActive(true)
  545. local remainNum = (goodsData.count - goodsData.buyNum)
  546. if remainNum < 0 then remainNum = 0 end
  547. itemLua.purchaseLimit.text.text = string.formatbykey("ShopDayBuy", remainNum)
  548. itemLua.limitBox:SetActive(true)
  549. itemLua.timeLimit.text.text = goodsData:CalculateLimitTime()
  550. self:StartShopGoodsItemTimer(itemLua, goodsData)
  551. elseif limitType == Enum.LimitBuyType.Week then
  552. itemLua.purchaseLimit:SetActive(true)
  553. local remainNum = (goodsData.count - goodsData.buyNum)
  554. if remainNum < 0 then remainNum = 0 end
  555. itemLua.purchaseLimit.text.text = string.formatbykey("ShopLoopBuy", remainNum)
  556. itemLua.limitBox:SetActive(true)
  557. itemLua.timeLimit.text.text = goodsData:CalculateLimitTime()
  558. self:StartShopGoodsItemTimer(itemLua, goodsData)
  559. elseif limitType == Enum.LimitBuyType.Time then
  560. itemLua.purchaseLimit:SetActive(true)
  561. local remainNum = (goodsData.count - goodsData.buyNum)
  562. if remainNum < 0 then remainNum = 0 end
  563. itemLua.purchaseLimit.text.text = string.formatbykey("ShopLimitBuy", remainNum)
  564. itemLua.limitBox:SetActive(true)
  565. itemLua.timeLimit.text.text = goodsData:CalculateLimitTime()
  566. self:StartShopGoodsItemTimer(itemLua, goodsData)
  567. elseif limitType == Enum.LimitBuyType.Special then
  568. itemLua.purchaseLimit:SetActive(true)
  569. local remainNum = (goodsData.count - goodsData.buyNum)
  570. if remainNum < 0 then remainNum = 0 end
  571. itemLua.purchaseLimit.text.text = string.formatbykey("ShopLimitBuy", remainNum)
  572. itemLua.limitBox:SetActive(false)
  573. self:StopShopGoodsItemTimer(itemLua)
  574. else
  575. itemLua.purchaseLimit:SetActive(false)
  576. itemLua.limitBox:SetActive(false)
  577. self:StopShopGoodsItemTimer(itemLua)
  578. end
  579. itemLua.soldout:SetActive(goodsData:IsSoldout())
  580. self.uiBase:AddButtonUniqueEventListener(itemLua.button, self, self.OnClickShopGoodsItem, goodsData)
  581. self.uiBase:AddButtonUniqueEventListener(itemLua.presentPrice.button, self, self.OnClickShopGoodsItem, goodsData)
  582. return item
  583. end
  584. -- function UIShopView:ShowItemTips(button, params)
  585. -- local data = params[0]
  586. -- ManagerContainer.LuaUIMgr:OpenTips(data)
  587. -- end
  588. function UIShopView:OnClickShopGoodsItem(btn, params)
  589. local shopData = self.controller:GetCurShopData()
  590. if not shopData then return end
  591. -- if ManagerContainer.LuaTimerMgr:GetRemainSeconds(shopData:GetNeedRefreshDataTime()) <= 0 then
  592. -- local errorCode = self.controller:SendShopInfoReq()
  593. -- if errorCode ~= 0 then
  594. -- ManagerContainer.LuaUIMgr:ErrorNoticeDisplay(errorCode)
  595. -- else
  596. -- ManagerContainer.LuaUIMgr:ErrorNoticeDisplay("ShopTips")
  597. -- end
  598. -- end
  599. local goodsData = params[0]
  600. if not goodsData then return end
  601. -- if goodsData:IsCanBuy() then
  602. if not goodsData:IsSoldout() then
  603. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIShopBuyTips, {shopData.shopId, goodsData.id})
  604. end
  605. end
  606. function UIShopView:StartShopGoodsItemTimer(itemLua, goodsData)
  607. local timer = self.shopGoodsItemTimer
  608. if not timer then
  609. timer = Timer.New(function()
  610. self:RefreshShopGoodsItemTimer()
  611. end, 1, -1)
  612. self.shopGoodsItemTimer = timer
  613. end
  614. local updateItemMap = timer.updateItemMap
  615. if not updateItemMap then
  616. updateItemMap = {}
  617. timer.updateItemMap = updateItemMap
  618. end
  619. updateItemMap[itemLua] = goodsData
  620. if not timer.running then
  621. timer:Start()
  622. end
  623. end
  624. function UIShopView:StopShopGoodsItemTimer(itemLua)
  625. local timer = self.shopGoodsItemTimer
  626. if timer then
  627. local updateItemMap = timer.updateItemMap
  628. if updateItemMap then
  629. updateItemMap[itemLua] = nil
  630. end
  631. if not updateItemMap or table_is_empty(updateItemMap) then
  632. timer:Stop()
  633. end
  634. end
  635. end
  636. function UIShopView:DisposeShopGoodsItemTimer()
  637. if self.shopGoodsItemTimer then
  638. self.shopGoodsItemTimer:Stop()
  639. self.shopGoodsItemTimer.updateItemMap = nil
  640. self.shopGoodsItemTimer = nil
  641. end
  642. end
  643. function UIShopView:RefreshShopGoodsItemTimer()
  644. local timer = self.shopGoodsItemTimer
  645. if not timer then return end
  646. local updateItemMap = timer.updateItemMap
  647. if not updateItemMap or table_is_empty(updateItemMap) then
  648. timer:Stop()
  649. return
  650. end
  651. for itemLua, goodsData in pairs(updateItemMap) do
  652. itemLua.limitBox:SetActive(true)
  653. itemLua.timeLimit.text.text = goodsData:CalculateLimitTime()
  654. end
  655. end
  656. return UIShopView