|
- local MailProtocolPhp = class("MailProtocolPhp")
-
- local MailCmd = require("luaScript.Protocol.Mail.MailCmd")
- local MailEvent = require("luaScript.Protocol.Mail.MailEvent")
-
- function MailProtocolPhp:ctor()
- -- 添加分发事件的组件
- cc.GameObject.extend(self)
- self:addComponent(require("luaScript.cc.components.behavior.EventProtocol"):new()):exportMethods()
-
- self.mails={}
- end
-
- --发送请求
- function MailProtocolPhp:post(action, param, callback)
- local phpAddress = app.config.RomSetting.phpUrl
- local msg = {}
- msg.action = action
- msg.uid = app.user.loginInfo.uid
- msg.app = getAppId()
- if param then
- table.merge(msg,param)
- end
- logD("MailProtocolPhp:post",table.tostring(msg))
- httpPost(phpAddress, msg, function(status, response)
- if status == "failed" then
- showPHPFailedResult("请求数据失败! code = " .. response)
- else
- callback(response)
- end
- end)
- end
-
- --请求邮件
- function MailProtocolPhp:requestMail(localData)
- local param = {
- id = #self.mails>0 and self.mails[#self.mails].id or 0,
- type = localData.type
- }
-
- self:post(MailCmd.PHP_GET_MESS,param,function(data)
- local response = json.decode(data)
- if type(response) ~= "table" then
- showPHPFailedResult("数据错误!")
- return
- end
- if cc.Application:getInstance():getTargetPlatform() == 0 then
- logD("MailProtocolPhp:requestMail",table.tostring(response))
- end
- if response.code==200 then
- local result = response.result or {}
- self.mails = result
- self:dispatchEvent({name = MailEvent.UPDATE_MAIL});
- else
- self:showError(MailCmd.PHP_GET_MESS,response.code,response)
- end
- end)
- end
-
- --阅读邮件
- function MailProtocolPhp:requestReadMail(localData)
- local param = {
- ids = localData.ids,
- }
-
- self:post(MailCmd.PHP_READ,param,function(data)
- local response = json.decode(data)
- if type(response) ~= "table" then
- showPHPFailedResult("数据错误!")
- return
- end
- if cc.Application:getInstance():getTargetPlatform() == 0 then
- logD("MailProtocolPhp:requestMail",table.tostring(response))
- end
- if response.code==200 then
- local result = response.result or {}
- local idList = string.split(result.ids,",")
- if idList then
- for k,v in ipairs(idList) do
- self:updateMailReadStatus(tonumber(v),result.flag)
- end
- self:dispatchEvent({name = MailEvent.UPDATE_MAIL});
- end
- else
- self:showError(MailCmd.PHP_READ,response.code,response)
- end
- end)
- end
-
- function MailProtocolPhp:reciveMail(id)
- local param = {
- id = id
- }
-
- self:post(MailCmd.PHP_TAKE,param,function(data)
- local response = json.decode(data)
- if type(response) ~= "table" then
- showPHPFailedResult("数据错误!")
- return
- end
- if cc.Application:getInstance():getTargetPlatform() == 0 then
- logD("MailProtocolPhp:reciveMail",table.tostring(response))
- end
- if response.code==200 then
- if response.result.flag==1 then
- self:updateMailStatus(response.result.id,1)
- showTooltip("领取成功,请到微信兑换红包!");
- self:dispatchEvent({name = "reciveMail"});
- self:dispatchEvent({name = "updateMail"});
- else
- showTooltip("提取失败!code:"..response.result.flag);
- end
- else
- self:showError(MailCmd.PHP_TAKE,response.code,response)
- end
- end)
- end
-
- function MailProtocolPhp:delMail(ids)
- local param = {
- ids = ids
- }
-
- self:post(MailCmd.PHP_DEL,param,function(data)
- local response = json.decode(data)
- if type(response) ~= "table" then
- showPHPFailedResult("数据错误!")
- return
- end
- if cc.Application:getInstance():getTargetPlatform() == 0 then
- logD("MailProtocolPhp:reciveMail",table.tostring(response))
- end
- if response.code==200 then
- if response.result.flag==1 then
- local ids=string.split(response.result.ids,",")
- for k,v in pairs(ids) do
- self:removeMailById(tonumber(v))
- end
-
- showTooltip("删除成功!");
- self:dispatchEvent({name = "updateMail"});
- else
- showTooltip("删除失败! code:"..response.result.flag);
- end
- else
- self:showError(MailCmd.PHP_DEL,response.code,response)
- end
- end)
- end
-
- function MailProtocolPhp:isCanReciveById(id)
- for k,v in pairs(self.mails) do
- if v.id == id and v.awards and #v.awards>0 then
- for _,award in pairs(v.awards) do
- if award.havegot == 0 then
- return true
- end
- end
- end
- end
- return false
- end
-
- function MailProtocolPhp:isCanRecive()
- for k,v in pairs(self.mails) do
- if v.awards and #v.awards>0 then
- for _,award in pairs(v.awards) do
- if award.havegot == 0 then
- return true
- end
- end
- end
- end
- return false
- end
-
- function MailProtocolPhp:removeMailById(id)
- for k,v in pairs(self.mails) do
- if v.id == id then
- table.remove(self.mails,k)
- break
- end
- end
- end
-
- function MailProtocolPhp:updateMailStatus(id,status)
- for k,v in pairs(self.mails) do
- if v.id == id and v.awards and #v.awards>0 then
- for _,award in pairs(v.awards) do
- award.havegot=status
- end
- end
- end
- end
-
- function MailProtocolPhp:updateMailReadStatus(id,flag)
- local noReadCount = 0
- for k,v in pairs(self.mails) do
- if v.id == id then
- v.read_state = flag
- end
- if tonumber(v.read_state) == 0 then
- noReadCount = noReadCount + 1
- end
- end
- if noReadCount == 0 then
- app.msgManager:removeMsg(nil,ACTIVITY_REDPOINT.SYSTEM_MESSAGE)
- end
- end
-
- return MailProtocolPhp
|