mail.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. local skynet = require "skynet"
  2. require "skynet.manager"
  3. local logger = require "logger"
  4. local queue = require "skynet.queue"
  5. local md5 = require "md5"
  6. local synchronized = queue()
  7. local cjson = require "cjson"
  8. local asset = require "model.asset"
  9. local common_fun = require "model.common_fun"
  10. local mailbox
  11. local table_insert = table.insert
  12. local authz = {acc = "yytx", pwd = "lee@YY-Games.520"}
  13. -- Generate request data
  14. local content = {acc = authz.acc, pwd = authz.pwd, sign = false}
  15. content.sign = string.sub(md5.sumhexa(content.acc .. content.pwd), 9, 24)
  16. local function create_html_page(text)
  17. return string.format(
  18. "<!DOCTYPE html><html><head><meta charset='UTF-8'></head> <body>%s</body> </html>",
  19. string.gsub(text, "[\n]", "<br/>"))
  20. end
  21. local whitelist = {
  22. -- ["192.168.1.23"] = true,
  23. }
  24. local function func(args, ipaddr, header)
  25. logger.trace("处理来自主机 %s 的发送邮件请求", ipaddr)
  26. -- 验证主机id
  27. -- if not whitelist[ipaddr] then
  28. -- return cjson.encode({errno = 403, host = header.host, info = "不信任ip"})
  29. -- end
  30. -- 验证gm账号
  31. -- local code = string.sub(args.code, 1, 16)
  32. -- if code ~= content.sign then
  33. -- return cjson.encode({state = 403, msg = "账号或密码错误"})
  34. -- end
  35. local mtype = tonumber(args.ntype)
  36. local json = cjson.decode(args.json)
  37. local attach = json.attach or {}
  38. local detail = json.attach or {}
  39. local errno = 0
  40. local info = "成功"
  41. ----------------------------- THIS ----------------------------------
  42. -- if attach ~= nil then
  43. -- for i=1, #attach, 2 do
  44. -- local id = attach[i]
  45. -- local num = attach[i+1]
  46. -- if not id or not num or num <= 0 then
  47. -- errno = 1
  48. -- info = "格式错误"
  49. -- break
  50. -- end
  51. -- errno = common_fun.goods_check(id)
  52. -- if errno == 0 then
  53. -- table_insert(detail, {id = id, num = num})
  54. -- else
  55. -- info = "错误的道具id:"..id
  56. -- break
  57. -- end
  58. -- end
  59. -- else
  60. -- detail = {}
  61. -- end
  62. -- if errno ~= 0 then
  63. -- return cjson.encode({state = errno, msg = info})
  64. -- end
  65. for _, v in ipairs(detail) do
  66. if not v.id or not v.num or v.num <= 0 then
  67. errno = 1
  68. info = "格式错误"
  69. break
  70. end
  71. local goods_type = common_fun.goods_type(v.id)
  72. if goods_type== GOODS_NONE then
  73. errno = 1
  74. info = "错误的道具id"
  75. end
  76. end
  77. if errno ~= 0 then
  78. return cjson.encode({state = errno, msg = info})
  79. end
  80. local body = json.content
  81. local source = {
  82. module = "sendmail",
  83. brief = "GM发送的邮件",
  84. context = string.format("GM ip: %s tm = %s",
  85. ipaddr, os.time())
  86. }
  87. local receiver
  88. mailbox = mailbox or skynet.localname(".mailbox")
  89. local subject = tonumber(json.title)
  90. if type(subject) ~= 'number' then
  91. return cjson.encode({state = 400, msg = "错误的title"})
  92. end
  93. for k, v in pairs(detail) do
  94. logger.trace("=======k=%s--v=%s----", v.id, v.num)
  95. end
  96. if mtype == 2 then -- 全服邮件
  97. skynet.call(mailbox, "lua", "broadcast", subject, body, detail,
  98. source)
  99. elseif mtype == 1 then -- 给特定玩家
  100. receiver = json.roles
  101. skynet.call(mailbox, "lua", "off_line_mail", receiver, subject,
  102. body, detail, source)
  103. else
  104. return cjson.encode({state = 400, msg = "ntype不存在"})
  105. end
  106. return cjson.encode({state = 0, msg = "success"})
  107. end
  108. -- http://192.168.108.4:9002/mail?code=xxxx&ntype=3&json={"roles":["00-6527363693544497152","00-6527368552515657728"],"attach":[100041, 100, 100037, 100],"title":"这是标题","content":"这是正文"}
  109. local mail = function(args, ipaddr, header)
  110. return synchronized(function()
  111. -- return create_html_page(func(args, ipaddr, header))
  112. return func(args, ipaddr, header)
  113. end)
  114. end
  115. return mail