源战役客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

410 lines
10 KiB

  1. MailModel = MailModel or BaseClass(EventDispatcher)
  2. function MailModel:__init()
  3. MailModel.Instance = self
  4. self.mail_list = {} --邮件的基本信息列表
  5. self.business_mail_list = {} --交易邮件的基本信息列表
  6. self.mail_tab_index = 1
  7. self.now_select_mail = nil --目前正在查看的邮件基本信息
  8. self.select_mail_list = {} --被选中的邮件列表
  9. self.attachment_time_list = {} --提取附件后的邮件列表,key为ID,value为提取时的时间
  10. end
  11. function MailModel:getInstance()
  12. if MailModel.Instance == nil then
  13. MailModel.New()
  14. end
  15. return MailModel.Instance
  16. end
  17. function MailModel:Clear()
  18. self:CancelTimer()
  19. self.now_select_mail = nil
  20. self.select_mail_list = {}
  21. end
  22. function MailModel:SetAllMailList(list)
  23. local mail_list = {}
  24. local business_mail_list = {}
  25. for k,v in pairs(list) do
  26. if v.type == 5 then
  27. table.insert(business_mail_list,v)
  28. else
  29. table.insert(mail_list,v)
  30. end
  31. end
  32. self:SetMailList(mail_list)
  33. self:SetBusinessMailList(business_mail_list)
  34. end
  35. --设置保存邮件的列表(接受所有存储在服务器的邮件,筛选出要显示的邮件)
  36. function MailModel:SetMailList(list)
  37. self.mail_list = list --邮件列表
  38. local delete_index = {} --要删除的邮件的索引集合
  39. for i, v in ipairs(self.mail_list) do
  40. local gap = tonumber(v.effect_et) - TimeUtil:getServerTime() --获取信件存储剩余时间
  41. if gap < 0 then
  42. table.insert(delete_index, i)
  43. end
  44. end
  45. --删除超出限制的邮件
  46. local del_num = 0
  47. for i, v in ipairs(delete_index) do
  48. local index = v - del_num
  49. table.remove(self.mail_list, index)
  50. del_num = del_num + 1
  51. end
  52. self:SortFun(1)
  53. self:Fire(MailEvent.UPDATE_MAIL_LIST) --更新邮件显示列表
  54. end
  55. function MailModel:SetBusinessMailList(list)
  56. self.business_mail_list = list --邮件列表
  57. local delete_index = {} --要删除的邮件的索引集合
  58. for i, v in ipairs(self.business_mail_list) do
  59. local gap = tonumber(v.effect_et) - TimeUtil:getServerTime() --获取信件存储剩余时间
  60. if gap < 0 then
  61. table.insert(delete_index, i)
  62. end
  63. end
  64. --删除超出限制的邮件
  65. local del_num = 0
  66. for i, v in ipairs(delete_index) do
  67. local index = v - del_num
  68. table.remove(self.business_mail_list, index)
  69. del_num = del_num + 1
  70. end
  71. self:SortFun(5)
  72. self:Fire(MailEvent.UPDATE_MAIL_LIST) --更新邮件显示列表
  73. end
  74. function MailModel:GetMailList()
  75. self.mail_list = self.mail_list or {}
  76. local delete_index = {} --要删除的邮件的索引集合
  77. for i, v in ipairs(self.mail_list) do
  78. local gap = tonumber(v.effect_et) - TimeUtil:getServerTime() --获取信件存储剩余时间
  79. if gap < 0 then
  80. table.insert(delete_index, i)
  81. end
  82. end
  83. --删除超出限制的邮件
  84. local del_num = 0
  85. for i, v in ipairs(delete_index) do
  86. local index = v - del_num
  87. table.remove(self.mail_list, index)
  88. del_num = del_num + 1
  89. end
  90. self:SortFun(1)
  91. return self.mail_list
  92. end
  93. function MailModel:GetBusinessMailList()
  94. self.business_mail_list = self.business_mail_list or {}
  95. local delete_index = {} --要删除的邮件的索引集合
  96. for i, v in ipairs(self.business_mail_list) do
  97. local gap = tonumber(v.effect_et) - TimeUtil:getServerTime() --获取信件存储剩余时间
  98. if gap < 0 then
  99. table.insert(delete_index, i)
  100. end
  101. end
  102. --删除超出限制的邮件
  103. local del_num = 0
  104. for i, v in ipairs(delete_index) do
  105. local index = v - del_num
  106. table.remove(self.business_mail_list, index)
  107. del_num = del_num + 1
  108. end
  109. self:SortFun(5)
  110. return self.business_mail_list
  111. end
  112. --对邮件进行排序(按接受时间排序,最新收到的邮件最高)
  113. --新 先是分为两部分,未读的在顶部,已读的在下面,两部分再分别按时间排序
  114. function MailModel:SortFun(type)
  115. local not_read = {}
  116. local has_read = {}
  117. if type == 5 then
  118. for i,v in ipairs(self.business_mail_list) do
  119. if v.state == 2 or ( v.is_attach == 1 and v.state ~= 3) then --未读
  120. v.client_read_state = 0
  121. table.insert(not_read, self.business_mail_list[i])
  122. elseif v.state == 1 or v.state == 3 then --已读 和 已领取
  123. v.client_read_state = 1
  124. table.insert(has_read, self.business_mail_list[i])
  125. end
  126. end
  127. else
  128. for i,v in ipairs(self.mail_list) do
  129. if v.state == 2 or ( v.is_attach == 1 and v.state ~= 3) then --未读
  130. v.client_read_state = 0
  131. table.insert(not_read, self.mail_list[i])
  132. elseif v.state == 1 or v.state == 3 then --已读 和 已领取
  133. v.client_read_state = 1
  134. table.insert(has_read, self.mail_list[i])
  135. end
  136. end
  137. end
  138. -- not_read = self:SortMore(not_read)
  139. -- has_read = self:SortMore(has_read)
  140. for i,v in ipairs(has_read) do
  141. table.insert(not_read, v)
  142. end
  143. local arg = {"important","client_read_state","time", "mail_id"}
  144. local condition = {Array.UPPER,Array.LOWER,Array.UPPER, Array.UPPER}
  145. SortTools.MoreKeysSorter(not_read, arg, condition)
  146. if type == 5 then
  147. self.business_mail_list = not_read
  148. else
  149. self.mail_list = not_read
  150. end
  151. end
  152. function MailModel:SortMore(list)
  153. if not list then return end
  154. local arg = {"time", "mail_id"}
  155. local condition = {Array.UPPER, Array.UPPER}
  156. SortTools.MoreKeysSorter(list, arg, condition)
  157. return list
  158. end
  159. --增加邮件
  160. function MailModel:AddMailList(list)
  161. if list then
  162. for i, v in ipairs(list) do
  163. if v.type == 5 then
  164. table.insert(self.business_mail_list, v)
  165. else
  166. table.insert(self.mail_list, v)
  167. end
  168. end
  169. end
  170. self:SortFun(1)
  171. self:SortFun(5)
  172. self:Fire(MailEvent.UPDATE_MAIL_LIST)
  173. end
  174. --删除邮件
  175. function MailModel:DeleteMail(mail_ids)
  176. local is_find = false
  177. if mail_ids then
  178. for i = 1, #mail_ids do
  179. for k, v in ipairs(self.mail_list) do
  180. if v.mail_id == mail_ids[i].mail_id then
  181. table.remove(self.mail_list, k)
  182. is_find = true
  183. break
  184. end
  185. end
  186. for k, v in ipairs(self.business_mail_list) do
  187. if v.mail_id == mail_ids[i].mail_id then
  188. table.remove(self.business_mail_list, k)
  189. break
  190. end
  191. end
  192. end
  193. self:Clear()
  194. self:SortFun(1)
  195. self:SortFun(5)
  196. -- self:Fire(MailEvent.UPDATE_MAIL_LIST)
  197. end
  198. end
  199. --更改邮件的数据
  200. function MailModel:UpdateMailData(data)
  201. if data == nil then return end
  202. if data.type ~= 5 then
  203. for i, v in ipairs(self.mail_list) do
  204. if v.mail_id == data.mail_id then
  205. v.type = data.type
  206. v.state = data.state
  207. v.title = data.title
  208. v.is_attach = data.is_attach
  209. v.time = data.time
  210. v.effect_et = data.effect_et
  211. self:Fire(MailEvent.UPDATE_MAIL_ITEM, v)
  212. break
  213. end
  214. end
  215. elseif data.type == 5 then
  216. for i, v in ipairs(self.business_mail_list) do
  217. if v.mail_id == data.mail_id then
  218. v.type = data.type
  219. v.state = data.state
  220. v.title = data.title
  221. v.is_attach = data.is_attach
  222. v.time = data.time
  223. v.effect_et = data.effect_et
  224. self:Fire(MailEvent.UPDATE_MAIL_ITEM, v)
  225. break
  226. end
  227. end
  228. end
  229. end
  230. --检查系统邮件的红点
  231. function MailModel:CheckSystemMailRedDot()
  232. local mail_list = self:GetMailList()
  233. if not mail_list or #mail_list <= 0 then
  234. return
  235. end
  236. local show = false
  237. for i,v in ipairs(mail_list) do
  238. if v.state == 2 or (v.state ~= 3 and v.is_attach == 1) then
  239. show = true
  240. break
  241. end
  242. end
  243. return show
  244. end
  245. --检查交易邮件的红点
  246. function MailModel:CheckBusinessMailRedDot()
  247. local business_mail_list = self:GetBusinessMailList()
  248. if not business_mail_list or #business_mail_list <= 0 then
  249. return
  250. end
  251. local show = false
  252. for i,v in ipairs(business_mail_list) do
  253. if v.state == 2 or (v.state ~= 3 and v.is_attach == 1) then
  254. show = true
  255. break
  256. end
  257. end
  258. return show
  259. end
  260. --判断邮件红点数量
  261. function MailModel:GetMailRedNum( )
  262. local red_num = 0
  263. local mail_list = self:GetMailList()
  264. local business_mail_list = self:GetBusinessMailList()
  265. for i,v in ipairs(mail_list) do
  266. if v.state == 2 or (v.state ~= 3 and v.is_attach == 1) then
  267. red_num = red_num + 1
  268. end
  269. end
  270. for i,v in ipairs(business_mail_list) do
  271. if v.state == 2 or (v.state ~= 3 and v.is_attach == 1) then
  272. red_num = red_num + 1
  273. end
  274. end
  275. return red_num
  276. end
  277. --再处理一次邮件的详细信息 避免有些标题空白的情况
  278. function MailModel:HandleMailDetail(vo)
  279. if not vo then return end
  280. local is_system_mail = false
  281. if vo.title == nil or vo.title == "" then
  282. for k,v in pairs(self.mail_list) do
  283. if vo.mail_id == v.mail_id then
  284. if v.type == 1 or v.type == 3 then
  285. is_system_mail = true
  286. end
  287. end
  288. end
  289. if is_system_mail then
  290. if vo.module_id and vo.sub_module_id then
  291. local cfg
  292. if vo.sub_module_id == 0 then
  293. cfg = Config.Moduleid[vo.module_id]
  294. vo.title = cfg.module_name.."内容"
  295. vo.content = cfg.module_name.."内容"
  296. else
  297. cfg = Config.Modulesub[vo.module_id.."@"..vo.sub_module_id]
  298. vo.title = cfg.sub_name.."内容"
  299. vo.content = cfg.module_name.."内容"
  300. end
  301. end
  302. end
  303. end
  304. return vo
  305. end
  306. -- 获取到邮件的类型
  307. function MailModel:GetMailType(mail_id)
  308. local type
  309. if self.mail_list and mail_id then
  310. for k,v in pairs(self.mail_list) do
  311. if v and v.mail_id == mail_id then
  312. type = v.type
  313. return type
  314. end
  315. end
  316. for k,v in pairs(self.business_mail_list) do
  317. if v and v.mail_id == mail_id then
  318. type = v.type
  319. return type
  320. end
  321. end
  322. end
  323. return 0
  324. end
  325. function MailModel:GetMailEffectTime(mail_id)
  326. for k,v in pairs(self.business_mail_list) do
  327. if mail_id ==v.mail_id then
  328. return v.effect_et
  329. end
  330. end
  331. for k,v in pairs(self.mail_list) do
  332. if mail_id ==v.mail_id then
  333. return v.effect_et
  334. end
  335. end
  336. end
  337. function MailModel:SortMailEffectTime()
  338. self:CancelTimer()
  339. local tb = {}
  340. for k,v in pairs(self.business_mail_list) do
  341. local temp = {}
  342. temp.mail_id = v.mail_id
  343. temp.runout_time = v.effect_et - TimeUtil:getServerTime()
  344. table.insert(tb,temp)
  345. end
  346. for k,v in pairs(self.mail_list) do
  347. local temp = {}
  348. temp.mail_id = v.mail_id
  349. temp.runout_time = v.effect_et - TimeUtil:getServerTime()
  350. table.insert(tb,temp)
  351. end
  352. local sort_func = function ( a, b )
  353. return a.runout_time < b.runout_time
  354. end
  355. table.sort(tb, sort_func)
  356. local timer_func = function()
  357. self:CancelTimer()
  358. self:Fire(MailEvent.UPDATE_TOP_BAR_RED)
  359. end
  360. if tb and tb[1] and tb[1].runout_time>0 then
  361. self.timer_id = GlobalTimerQuest:AddPeriodQuest(timer_func, tb[1].runout_time+2)
  362. end
  363. end
  364. function MailModel:CancelTimer()
  365. if self.timer_id then
  366. GlobalTimerQuest:CancelQuest(self.timer_id)
  367. self.timer_id = nil
  368. end
  369. end