KLMailPanel.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. ---@class KLMailPanel:UIKmlLuaPanelBase
  2. ---@field view KLMailPanelView
  3. ---@field selectBtn KLMailItemItem[]
  4. local KLMailPanel = class(UIKmlLuaPanelBase)
  5. local this = KLMailPanel
  6. ---创建时调用一次
  7. function this:Init()
  8. SL:HideMainPanel()
  9. self.selectBtn = {}
  10. end
  11. ---创建或者刷新界面数据时调用
  12. function this:Refresh()
  13. self.info = self:LoadData()
  14. if table.count(self.info) == 0 then
  15. GUI:SetActive(self.view.null_bg, true)
  16. GUI:SetActive(self.view.left_panel, false)
  17. GUI:SetActive(self.view.mail_content_rt, false)
  18. else
  19. self.selectid = 0
  20. self.mailId = 0
  21. self:RefreshUI()
  22. self.all = false
  23. end
  24. end
  25. function this:RefreshUI()
  26. GUI:SetActive(self.view.null_bg, false)
  27. GUI:SetActive(self.view.left_panel, true)
  28. GUI:SetActive(self.view.mail_content_rt, true)
  29. GUI:DataListInitData(self.view.mail_data_list, function()
  30. return self:DataListMailCountFunc()
  31. end, function(realIndex)
  32. return self:DataListMailGetFunc(realIndex)
  33. end, function(realIndex, kmlcontrol)
  34. return self:DataListMailInitFunc(realIndex, kmlcontrol)
  35. end, function(realIndex, kmlcontrol)
  36. return self:DataListMailUpdateFunc(realIndex, kmlcontrol)
  37. end)
  38. GUI:DataListUpdateData(self.view.mail_data_list)
  39. self:SelectMail(1, self.info[1].mailId)
  40. end
  41. ---注册UI事件和服务器消息
  42. function this:RegistEvents()
  43. SL:RegisterLuaNetMsg(MessageDef.ResGetMailItemMessage, self.ResGetMailItemMessage, self)
  44. SL:RegisterLUAEvent(LUA_EVENT_MAIL_STATE_CHANGE, self.CheckState, self)
  45. SL:RegisterLUAEvent(LUA_EVENT_RECEIVE_EMAIL, self.Refresh, self)
  46. GUI:AddOnClickEvent(self.view.get_all_btn, self, self.AllReceive)
  47. GUI:AddOnClickEvent(self.view.del_all_btn, self, self.AllDelMail)
  48. GUI:AddOnClickEvent(self.view.del_btn, self, self.DelMail)
  49. GUI:AddOnClickEvent(self.view.detail_btn, self, function()
  50. SL:GetMailItems(self.mailId)
  51. end)
  52. GUI:AddOnClickEvent(self.view.des_btn, self, function()
  53. local helpCfg = SL:GetConfig("cfg_rule_text", 15,"id")
  54. if helpCfg then
  55. SL:CommonStrTipsMessage({ title = helpCfg.menutxt, str = helpCfg.location })
  56. end
  57. end)
  58. GUI:AddOnClickEvent(self.view.closeBtn, self, function()
  59. GUI:UIPanel_Close("dev/ui/Mail/Panel/KLMail/KLMailPanel")
  60. end)
  61. end
  62. ---@param message MailProto.MailIdMsg
  63. function this:ResGetMailItemMessage(_, message)
  64. local items = {}
  65. if table.count(message.mailIds) == 0 then
  66. return
  67. end
  68. local mails = SL.Mail:GetAllMail()
  69. for i, v in pairs(message.mailIds) do
  70. local info = mails[v]
  71. for _, k in pairs(info.items) do
  72. if items[k.cfgId] then
  73. items[k.cfgId] = items[k.cfgId] + k.count
  74. else
  75. items[k.cfgId] = k.count
  76. end
  77. end
  78. end
  79. local reward = {}
  80. for i, v in pairs(items) do
  81. reward[#reward + 1] = { cfgId = i, count = v }
  82. end
  83. SL:OpenRewardTips(reward, 0)
  84. GUI:SetActive(self.view.del_btn, true)
  85. GUI:SetActive(self.view.detail_btn, false)
  86. self:SelectMail(nil, message.mailIds[1])
  87. if self.all then
  88. self:Refresh()
  89. self.all = false
  90. end
  91. end
  92. function this:DataListMailCountFunc()
  93. return table.count(SL.Mail:GetAllMail())
  94. end
  95. function this:DataListMailGetFunc(realIndex)
  96. local item = GUI:UIPanel_Open("dev/ui/Mail/Item/KLMailItem/KLMailItemItem", self.view.mail_data_list, self, nil, true)
  97. self.selectBtn[realIndex + 1] = item
  98. return item.view.root
  99. end
  100. function this:DataListMailInitFunc(realIndex, kmlcontrol)
  101. end
  102. function this:DataListMailUpdateFunc(realIndex, kmlcontrol)
  103. local luaind = realIndex + 1
  104. local data = self.info[luaind]
  105. local title = self.selectBtn[realIndex + 1].view.mail_title_
  106. GUI:Text_setString(title, data.title)
  107. local icon = self.selectBtn[realIndex + 1].view.mail_icon_
  108. local atlas = "Atlas/UIEmailPanel.spriteatlas"
  109. if data.state == 0 then
  110. if data.item then
  111. GUI:Image_loadTexture(icon, "icon_haveitem", atlas)
  112. else
  113. GUI:Image_loadTexture(icon, "icon_unread", atlas)
  114. end
  115. elseif data.state == 1 then
  116. GUI:Image_loadTexture(icon, "icon_unclaimed", atlas)
  117. else
  118. if data.item then
  119. GUI:Image_loadTexture(icon, "icon_received", atlas)
  120. else
  121. GUI:Image_loadTexture(icon, "icon_read", atlas)
  122. end
  123. end
  124. local timestr = Time.FormatTimeYMDHMS(data.time)
  125. local time = self.selectBtn[realIndex + 1].view .mail_time_
  126. GUI:Text_setString(time, timestr)
  127. self.selectBtn[realIndex + 1].index = luaind
  128. self.selectBtn[realIndex + 1].callback = self.SelectMail
  129. self.selectBtn[realIndex + 1].ui = self
  130. self.selectBtn[realIndex + 1].mailId = data.mailId
  131. GUI:setVisible(self.selectBtn[realIndex + 1].view.redpoint,data.state == 0 or (data.item and data.state ~= 2))
  132. end
  133. function this:LoadData()
  134. local data = {}
  135. for i, v in pairs(SL.Mail:GetAllMail()) do
  136. local time = v.overdueTime - Time.GetServerTime() / 1000
  137. if time > 0 then
  138. local info = { mailId = v.mailId, title = v.title, time = v.sendTime, state = v.state, item = table.count(v.items) ~= 0,hasItems = v.state ~= 2 and v.items and #v.items > 0 }
  139. table.insert(data, info)
  140. end
  141. end
  142. table.sort(data, function(a, b)
  143. local timeCompare = a.time > b.time
  144. local wa = timeCompare and 1 or 0
  145. local wb = timeCompare and 0 or 1
  146. if a.state == 0 then
  147. wa = wa + 10
  148. end
  149. if b.state == 0 then
  150. wb = wb + 10
  151. end
  152. if a.hasItems then
  153. wa = wa + 100
  154. end
  155. if b.hasItems then
  156. wb = wb + 100
  157. end
  158. return wa > wb
  159. end)
  160. return data
  161. end
  162. function this:SelectMail(index, mailId)
  163. if index == nil then
  164. index = self.selectid
  165. else
  166. self.selectid = index
  167. end
  168. local mails = SL.Mail:GetAllMail()
  169. for i, v in pairs(self.selectBtn) do
  170. --v.SelectMail(v,)
  171. if i == index then
  172. v.Selected(v, true)
  173. local icon = v.view.mail_icon_
  174. if table.count(mails[mailId].items) > 0 and mails[mailId].state ~= 2 then
  175. GUI:Image_loadTexture(icon, "icon_unclaimed", "Atlas/UIEmailPanel.spriteatlas")
  176. elseif table.count(mails[mailId].items) > 0 then
  177. GUI:Image_loadTexture(icon, "icon_received", "Atlas/UIEmailPanel.spriteatlas")
  178. else
  179. GUI:Image_loadTexture(icon, "icon_read", "Atlas/UIEmailPanel.spriteatlas")
  180. end
  181. GUI:setVisible(v.view.redpoint,mails[mailId].state == 0 or (#mails[mailId].items>0 and mails[mailId].state ~= 2))
  182. else
  183. --local select = v.view.selected
  184. --GUI:SetActive(select, false)
  185. v.Selected(v, false)
  186. end
  187. end
  188. if mails[mailId].state == 0 then
  189. SL:RequestReadMail({ mailId })
  190. end
  191. self:LoadPanel(mailId)
  192. end
  193. --- 页面加载
  194. function this:LoadPanel(mailId)
  195. self.mailId = mailId
  196. local data = SL.Mail:GetAllMail()[mailId]
  197. --标题
  198. GUI:Text_setString(self.view.content_title, data.title)
  199. -- 时间
  200. local time = data.overdueTime - Time.GetServerTime() / 1000
  201. local timeText = ""
  202. if time <= 0 then
  203. timeText = "已过期"
  204. else
  205. timeText = "有效期:" .. Time.FormatTimeDH(time)
  206. end
  207. GUI:Text_setString(self.view.content_time, timeText)
  208. -- 内容
  209. GUI:Text_setString(self.view.content_text, data.content)
  210. -- 状态
  211. if table.count(data.items) > 0 and data.state ~= 2 and time > 0 then
  212. GUI:SetActive(self.view.del_btn, false)
  213. GUI:SetActive(self.view.detail_btn, true)
  214. --GUI:AddOnClickEvent(self.view.detail_btn, self, function()
  215. -- SL:GetMailItems(data.mailId)
  216. --end)
  217. else
  218. GUI:SetActive(self.view.del_btn, true)
  219. GUI:SetActive(self.view.detail_btn, false)
  220. end
  221. -- 附件
  222. if table.count(data.items) == 0 then
  223. GUI:SetActive(self.view.received_attachment, false)
  224. else
  225. GUI:SetActive(self.view.received_attachment, true)
  226. end
  227. GUI:DataListInitData(self.view.items_list, function()
  228. return table.count(data.items)
  229. end, function(realIndex)
  230. return self:DataListItemGetFunc(realIndex)
  231. end, function(realIndex, kmlcontrol)
  232. return self:DataListItemInitFunc(realIndex, kmlcontrol)
  233. end, function(realIndex, kmlcontrol)
  234. return self:DataListItemUpdateFunc(realIndex, kmlcontrol, data)
  235. end)
  236. GUI:DataListUpdateData(self.view.items_list)
  237. end
  238. function this:DataListItemGetFunc(realIndex)
  239. end
  240. function this:DataListItemInitFunc(realIndex, kmlcontrol)
  241. end
  242. function this:DataListItemUpdateFunc(realIndex, kmlcontrol, data)
  243. local itemTbl = data.items[realIndex + 1]
  244. --local item2 = self.view.items_list:GetChildControl(realIndex, "item2")
  245. local item2 = GUI:GetChildControl(self.view.items_list,realIndex, "item2")
  246. GUI:Item_UpdataData(item2, {
  247. itemid = itemTbl.cfgId,
  248. --itemcount = itemTbl.count,
  249. })
  250. --local itemcount = self.view.items_list:GetChildControl(realIndex, "itemcount")
  251. local itemcount = GUI:GetChildControl(self.view.items_list,realIndex,"itemcount")
  252. local count = SL:GetSimpleNumber(itemTbl.count, 1)
  253. GUI:Text_setString(itemcount, tostring(count))
  254. --local click_item = self.view.items_list:GetChildControl(realIndex, "click_item")
  255. local click_item = GUI:GetChildControl(self.view.items_list,realIndex,"click_item")
  256. --local item_received = self.view.items_list:GetChildControl(realIndex, "item_received")
  257. local item_received = GUI:GetChildControl(self.view.items_list,realIndex,"item_received")
  258. GUI:AddOnClickEvent(click_item, self, function()
  259. SL:OpenTips("mail", itemTbl.cfgId, itemTbl.id, itemTbl.count)
  260. end)
  261. if data.state ~= 2 then
  262. GUI:SetActive(item_received, false)
  263. else
  264. --GUI:AddOnClickEvent(click_item, self, function()
  265. -- SL:OpenTips("mail", itemTbl.cfgId, itemTbl.id,itemTbl.count)
  266. --end)
  267. GUI:SetActive(item_received, true)
  268. end
  269. end
  270. function this:DelMail()
  271. SL.Mail:ReqDeleteMail({ self.mailId })
  272. SL.Mail:GetAllMail()[self.mailId] = nil
  273. self:Refresh()
  274. end
  275. function this:AllDelMail()
  276. local data = {}
  277. local mails = SL.Mail:GetAllMail()
  278. for i, v in pairs(mails) do
  279. if v.state == 2 then
  280. table.insert(data, i)
  281. end
  282. end
  283. SL.Mail:ReqDeleteMail(data)
  284. self.all = true
  285. --self:RefreshUI()
  286. end
  287. function this:AllReceive()
  288. SL:GetAllMailItems()
  289. self.all = true
  290. end
  291. function this:Close()
  292. SL:UnRegisterLuaNetMsg(MessageDef.ResGetMailItemMessage, self.ResGetMailItemMessage, self)
  293. SL:UnRegisterLUAEvent(LUA_EVENT_MAIL_STATE_CHANGE, self.CheckState, self)
  294. SL:UnRegisterLUAEvent(LUA_EVENT_RECEIVE_EMAIL, self.Refresh, self)
  295. SL:ShowMainPanel()
  296. end
  297. function this:CheckState()
  298. if self.all == false then
  299. return
  300. end
  301. local data = SL.Mail:GetAllMail()
  302. self:Refresh()
  303. end
  304. return this