MailProxy.lua 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. local RemoteProxy = requireProxy("remote/RemoteProxy")
  2. local MailProxy = class("MailProxy", RemoteProxy)
  3. MailProxy.NAME = global.ProxyTable.MailProxy
  4. function MailProxy:ctor()
  5. self.mailIdList = {} --服务器发过来的邮件列表
  6. self.mailCount = 0 --邮件个数
  7. self.newMailCount = 0 --新邮件个数
  8. self.curMailId = 0 --当前打开的邮件
  9. MailProxy.super.ctor(self)
  10. end
  11. function MailProxy:resetList()
  12. self.mailIdList = {}
  13. self.mailCount = 0
  14. self.newMailCount = 0
  15. end
  16. --获取邮件的邮件列表
  17. function MailProxy:getMailList()
  18. return self.mailIdList
  19. end
  20. --根据邮件的mailId得到邮件
  21. function MailProxy:getMailByMailId(mailId)
  22. return self.mailIdList[mailId]
  23. end
  24. --添加邮件
  25. function MailProxy:addMail(mail)
  26. if mail == nil then
  27. return
  28. end
  29. -- {"Id":"1","btType":"1","sChrName":"张三","sSendName":"系统","sLable":"测试邮件","sMemo":"这是正文","sItem":[{"Index":6,"Count":30,"Bind":false}],"dCreateTime":"2010-01-00 00:00:00","btReadFlag","0","btRecvFlag","0"}
  30. -- btType:邮件类型,1:系统,2:战报,3:团队,4:行会
  31. -- dCreateTime:发送时间
  32. -- btReadFlag:读标志:0未读,1已读
  33. -- btRecvFlag:附件领取标志:0:未领取,1:已领取
  34. self.mailCount = self.mailCount + 1
  35. mail.Id = tonumber(mail.Id)
  36. mail.btType = tonumber(mail.btType)
  37. mail.btReadFlag = tonumber(mail.btReadFlag)
  38. mail.btRecvFlag = tonumber(mail.btRecvFlag)
  39. mail.sItem = mail.sItem or {}
  40. mail.index = self.mailCount
  41. self.mailIdList[mail.Id] = mail
  42. end
  43. --设置当前打开的邮件
  44. function MailProxy:setCurMailId(mailId)
  45. if mailId == nil then
  46. return
  47. end
  48. self.curMailId = mailId
  49. end
  50. function MailProxy:getCurMailId()
  51. return self.curMailId
  52. end
  53. --获取邮件数量
  54. function MailProxy:getMailCount()
  55. return self.mailCount
  56. end
  57. --获取新邮件数量
  58. function MailProxy:getNewMailCount()
  59. return self.newMailCount
  60. end
  61. --获取邮件状态
  62. function MailProxy:getMailState(mailId)
  63. if self.mailIdList[mailId] == nil then
  64. return false
  65. end
  66. return self.mailIdList[mailId].btReadFlag
  67. end
  68. --是否有未读邮件
  69. function MailProxy:haveUnread()
  70. for k, v in pairs(self.mailIdList) do
  71. if v.btReadFlag == 0 or (#v.sItem > 0 and v.btRecvFlag == 0) then
  72. return true
  73. end
  74. end
  75. return false
  76. end
  77. --是否有邮件可以删除
  78. function MailProxy:isHaveDeleteMail()
  79. for k, v in pairs(self.mailIdList) do
  80. if v.btReadFlag == 1 and (#v.sItem == 0 or (#v.sItem > 0 and v.btRecvFlag == 1)) then
  81. return true
  82. end
  83. end
  84. return false
  85. end
  86. --左下角显示未读邮件的提醒
  87. function MailProxy:showMainToolsBar(flag)
  88. local show = false
  89. if flag ~= nil then
  90. show = flag
  91. else
  92. show = self:haveUnread()
  93. end
  94. local function callback()
  95. JUMPTO(16)
  96. end
  97. local bubbleTips = {}
  98. bubbleTips.id = 2
  99. bubbleTips.status = show
  100. bubbleTips.callback = callback
  101. global.Facade:sendNotification(global.NoticeTable.BubbleTipsStatusChange, bubbleTips)
  102. end
  103. --读邮件
  104. function MailProxy:readMail(mailId)
  105. if self.mailIdList[mailId] == nil then
  106. return false
  107. end
  108. LuaSendMsg(global.MsgType.MSG_CS_EMAIL_READ_REQUEST, mailId)
  109. self.mailIdList[mailId].btReadFlag = 1
  110. self:showMainToolsBar()
  111. end
  112. --提取附件
  113. function MailProxy:getMailItems(mailId)
  114. if self.mailIdList[mailId] == nil then
  115. return false
  116. end
  117. LuaSendMsg(global.MsgType.MSG_CS_EMAIL_GET_REQUEST, 1, mailId)
  118. end
  119. --全部提取
  120. function MailProxy:getAllMailItems()
  121. LuaSendMsg(global.MsgType.MSG_CS_EMAIL_GET_REQUEST, 2)
  122. end
  123. --删除邮件
  124. function MailProxy:deleteMail(mailId)
  125. if self.mailIdList[mailId] == nil then
  126. return false
  127. end
  128. global.Facade:sendNotification(global.NoticeTable.SystemTips, GET_STRING(50000409))
  129. LuaSendMsg(global.MsgType.MSG_CS_EMAIL_DELETE_REQUEST, mailId)
  130. self.mailIdList[mailId] = nil
  131. self.mailCount = self.mailCount - 1
  132. if self.mailIdList == nil then
  133. self.mailIdList = {}
  134. end
  135. global.Facade:sendNotification(global.NoticeTable.Layer_Mail_DelOneMailSucc, mailId)
  136. end
  137. --删除已读
  138. function MailProxy:deleteReadMail()
  139. if self.mailIdList and next(self.mailIdList) then
  140. LuaSendMsg(global.MsgType.MSG_CS_EMAIL_DELETE_ALL_REQUEST)
  141. else
  142. global.Facade:sendNotification(global.NoticeTable.SystemTips, GET_STRING(50000410))
  143. end
  144. end
  145. --请求获取邮件列表 一次十条
  146. function MailProxy:RequestMailList()
  147. self:resetList()
  148. LuaSendMsg(global.MsgType.MSG_CS_EMAIL_LIST_REQUEST)
  149. end
  150. function MailProxy:handle_MSG_SC_EMAIL_LIST_RESPONSE(msg)
  151. local header = msg:GetHeader()
  152. local len = msg:GetDataLength()
  153. local page = header.recog
  154. local data = msg:GetData()
  155. if len > 0 then
  156. local sliceStr = data:ReadString(len)
  157. local cjson = require("cjson")
  158. local jsonData = cjson.decode(sliceStr)
  159. for _, mail in pairs(jsonData) do
  160. self:addMail(mail)
  161. end
  162. global.Facade:sendNotification(global.NoticeTable.Layer_Mail_DelAllMailSucc)
  163. else
  164. self:resetList()
  165. global.Facade:sendNotification(global.NoticeTable.Layer_Mail_DelAllMailSucc)
  166. end
  167. self:showMainToolsBar()
  168. end
  169. function MailProxy:handle_MSG_SC_EMAIL_DELETE_ALL_RESPONSE(msg)
  170. global.Facade:sendNotification(global.NoticeTable.SystemTips, GET_STRING(50000409))
  171. self:RequestMailList()
  172. end
  173. function MailProxy:handle_MSG_SC_EMAIL_GET_RESPONSE(msg)
  174. local header = msg:GetHeader()
  175. if header.recog == 1 then
  176. local mailId = header.param1
  177. if self.mailIdList[mailId] then
  178. self.mailIdList[mailId].btRecvFlag = 1
  179. end
  180. SLBridge:onLUAEvent(LUA_EVENT_MAIL_UPDATE)
  181. self:showMainToolsBar()
  182. end
  183. if header.recog == 2 then
  184. self:RequestMailList()
  185. end
  186. end
  187. function MailProxy:handle_MSG_SC_EMAIL_NEW_NOTI(msg)
  188. self.newMailCount = self.newMailCount + 1
  189. self:showMainToolsBar(true)
  190. end
  191. function MailProxy:handle_MSG_SC_EMAIL_TAKE_STATE_RESPONSE(msg)
  192. local header = msg:GetHeader()
  193. local mailId = header.recog
  194. if self.mailIdList[mailId] then
  195. local param = header.param1 -- 1 收货确认 2 拒绝收货
  196. if param == 1 then
  197. self.mailIdList[mailId].btRecvFlag = 0 --btRecvFlag:附件领取标志:0:未领取,1:已领取
  198. self.mailIdList[mailId].btType = 9999
  199. SLBridge:onLUAEvent(LUA_EVENT_MAIL_UPDATE)
  200. elseif param == 2 then
  201. if self.mailIdList[mailId] == nil then
  202. return false
  203. end
  204. self.mailIdList[mailId] = nil
  205. self.mailCount = self.mailCount - 1
  206. if self.mailIdList == nil then
  207. self.mailIdList = {}
  208. end
  209. SLBridge:onLUAEvent(LUA_EVENT_MAIL_UPDATE)
  210. end
  211. end
  212. end
  213. function MailProxy:RegisterMsgHandler()
  214. MailProxy.super.RegisterMsgHandler(self)
  215. local msgType = global.MsgType
  216. LuaRegisterMsgHandler(msgType.MSG_SC_EMAIL_LIST_RESPONSE, handler(self, self.handle_MSG_SC_EMAIL_LIST_RESPONSE)) -- 邮件列表
  217. LuaRegisterMsgHandler(msgType.MSG_SC_EMAIL_DELETE_ALL_RESPONSE, handler(self, self.handle_MSG_SC_EMAIL_DELETE_ALL_RESPONSE)) -- 删除已读的邮件
  218. LuaRegisterMsgHandler(msgType.MSG_SC_EMAIL_GET_RESPONSE, handler(self, self.handle_MSG_SC_EMAIL_GET_RESPONSE)) --提取附件成功
  219. LuaRegisterMsgHandler(msgType.MSG_SC_EMAIL_NEW_NOTI, handler(self, self.handle_MSG_SC_EMAIL_NEW_NOTI)) -- 新邮件到来
  220. LuaRegisterMsgHandler(msgType.MSG_SC_EMAIL_TAKE_STATE_RESPONSE, handler(self, self.handle_MSG_SC_EMAIL_TAKE_STATE_RESPONSE)) -- 邮件收货状态
  221. end
  222. return MailProxy