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.

211 lines
5.3 KiB

  1. local MailProtocolPhp = class("MailProtocolPhp")
  2. local MailCmd = require("luaScript.Protocol.Mail.MailCmd")
  3. local MailEvent = require("luaScript.Protocol.Mail.MailEvent")
  4. function MailProtocolPhp:ctor()
  5. -- 添加分发事件的组件
  6. cc.GameObject.extend(self)
  7. self:addComponent(require("luaScript.cc.components.behavior.EventProtocol"):new()):exportMethods()
  8. self.mails={}
  9. end
  10. --发送请求
  11. function MailProtocolPhp:post(action, param, callback)
  12. local phpAddress = app.config.RomSetting.phpUrl
  13. local msg = {}
  14. msg.action = action
  15. msg.uid = app.user.loginInfo.uid
  16. msg.app = getAppId()
  17. if param then
  18. table.merge(msg,param)
  19. end
  20. logD("MailProtocolPhp:post",table.tostring(msg))
  21. httpPost(phpAddress, msg, function(status, response)
  22. if status == "failed" then
  23. showPHPFailedResult("请求数据失败! code = " .. response)
  24. else
  25. callback(response)
  26. end
  27. end)
  28. end
  29. --请求邮件
  30. function MailProtocolPhp:requestMail(localData)
  31. local param = {
  32. id = #self.mails>0 and self.mails[#self.mails].id or 0,
  33. type = localData.type
  34. }
  35. self:post(MailCmd.PHP_GET_MESS,param,function(data)
  36. local response = json.decode(data)
  37. if type(response) ~= "table" then
  38. showPHPFailedResult("数据错误!")
  39. return
  40. end
  41. if cc.Application:getInstance():getTargetPlatform() == 0 then
  42. logD("MailProtocolPhp:requestMail",table.tostring(response))
  43. end
  44. if response.code==200 then
  45. local result = response.result or {}
  46. self.mails = result
  47. self:dispatchEvent({name = MailEvent.UPDATE_MAIL});
  48. else
  49. self:showError(MailCmd.PHP_GET_MESS,response.code,response)
  50. end
  51. end)
  52. end
  53. --阅读邮件
  54. function MailProtocolPhp:requestReadMail(localData)
  55. local param = {
  56. ids = localData.ids,
  57. }
  58. self:post(MailCmd.PHP_READ,param,function(data)
  59. local response = json.decode(data)
  60. if type(response) ~= "table" then
  61. showPHPFailedResult("数据错误!")
  62. return
  63. end
  64. if cc.Application:getInstance():getTargetPlatform() == 0 then
  65. logD("MailProtocolPhp:requestMail",table.tostring(response))
  66. end
  67. if response.code==200 then
  68. local result = response.result or {}
  69. local idList = string.split(result.ids,",")
  70. if idList then
  71. for k,v in ipairs(idList) do
  72. self:updateMailReadStatus(tonumber(v),result.flag)
  73. end
  74. self:dispatchEvent({name = MailEvent.UPDATE_MAIL});
  75. end
  76. else
  77. self:showError(MailCmd.PHP_READ,response.code,response)
  78. end
  79. end)
  80. end
  81. function MailProtocolPhp:reciveMail(id)
  82. local param = {
  83. id = id
  84. }
  85. self:post(MailCmd.PHP_TAKE,param,function(data)
  86. local response = json.decode(data)
  87. if type(response) ~= "table" then
  88. showPHPFailedResult("数据错误!")
  89. return
  90. end
  91. if cc.Application:getInstance():getTargetPlatform() == 0 then
  92. logD("MailProtocolPhp:reciveMail",table.tostring(response))
  93. end
  94. if response.code==200 then
  95. if response.result.flag==1 then
  96. self:updateMailStatus(response.result.id,1)
  97. showTooltip("领取成功,请到微信兑换红包!");
  98. self:dispatchEvent({name = "reciveMail"});
  99. self:dispatchEvent({name = "updateMail"});
  100. else
  101. showTooltip("提取失败!code:"..response.result.flag);
  102. end
  103. else
  104. self:showError(MailCmd.PHP_TAKE,response.code,response)
  105. end
  106. end)
  107. end
  108. function MailProtocolPhp:delMail(ids)
  109. local param = {
  110. ids = ids
  111. }
  112. self:post(MailCmd.PHP_DEL,param,function(data)
  113. local response = json.decode(data)
  114. if type(response) ~= "table" then
  115. showPHPFailedResult("数据错误!")
  116. return
  117. end
  118. if cc.Application:getInstance():getTargetPlatform() == 0 then
  119. logD("MailProtocolPhp:reciveMail",table.tostring(response))
  120. end
  121. if response.code==200 then
  122. if response.result.flag==1 then
  123. local ids=string.split(response.result.ids,",")
  124. for k,v in pairs(ids) do
  125. self:removeMailById(tonumber(v))
  126. end
  127. showTooltip("删除成功!");
  128. self:dispatchEvent({name = "updateMail"});
  129. else
  130. showTooltip("删除失败! code:"..response.result.flag);
  131. end
  132. else
  133. self:showError(MailCmd.PHP_DEL,response.code,response)
  134. end
  135. end)
  136. end
  137. function MailProtocolPhp:isCanReciveById(id)
  138. for k,v in pairs(self.mails) do
  139. if v.id == id and v.awards and #v.awards>0 then
  140. for _,award in pairs(v.awards) do
  141. if award.havegot == 0 then
  142. return true
  143. end
  144. end
  145. end
  146. end
  147. return false
  148. end
  149. function MailProtocolPhp:isCanRecive()
  150. for k,v in pairs(self.mails) do
  151. if v.awards and #v.awards>0 then
  152. for _,award in pairs(v.awards) do
  153. if award.havegot == 0 then
  154. return true
  155. end
  156. end
  157. end
  158. end
  159. return false
  160. end
  161. function MailProtocolPhp:removeMailById(id)
  162. for k,v in pairs(self.mails) do
  163. if v.id == id then
  164. table.remove(self.mails,k)
  165. break
  166. end
  167. end
  168. end
  169. function MailProtocolPhp:updateMailStatus(id,status)
  170. for k,v in pairs(self.mails) do
  171. if v.id == id and v.awards and #v.awards>0 then
  172. for _,award in pairs(v.awards) do
  173. award.havegot=status
  174. end
  175. end
  176. end
  177. end
  178. function MailProtocolPhp:updateMailReadStatus(id,flag)
  179. local noReadCount = 0
  180. for k,v in pairs(self.mails) do
  181. if v.id == id then
  182. v.read_state = flag
  183. end
  184. if tonumber(v.read_state) == 0 then
  185. noReadCount = noReadCount + 1
  186. end
  187. end
  188. if noReadCount == 0 then
  189. app.msgManager:removeMsg(nil,ACTIVITY_REDPOINT.SYSTEM_MESSAGE)
  190. end
  191. end
  192. return MailProtocolPhp