paySrv.lua 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. --[[
  2. Descripttion:支付服务
  3. version:
  4. Author: Neo,Huang
  5. Date: 2021-08-24 19:40:15
  6. LastEditors: Neo,Huang
  7. LastEditTime: 2021-09-24 10:26:38
  8. --]]
  9. local nodeMgr = require "nodeMgr"
  10. local baseService = require "baseService"
  11. local util_order = require("utils.util_order")
  12. local sessionData = require("data.session")
  13. local CMD = {}
  14. local INTERVAL = 180 -- 每3分钟轮询一次
  15. local orderReissue = {} -- {uid --> {order1, order2}}
  16. -- 加载未处理的订单
  17. local function loadUnprocessedOrder()
  18. local ret = util_order:get_unhandle_order_list()
  19. if ret and not table.empty(ret) then
  20. for _, v in ipairs(ret) do
  21. local uid = tonumber(v.uid)
  22. orderReissue[uid] = orderReissue[uid] or {}
  23. if not table.key_find(orderReissue[uid], "id", v.id) then
  24. table.insert(orderReissue[uid], v)
  25. end
  26. end
  27. end
  28. collectgarbage "collect"
  29. create_timeout(
  30. INTERVAL * 100,
  31. function()
  32. loadUnprocessedOrder()
  33. end
  34. )
  35. end
  36. -- 玩家登录处理订单
  37. function CMD.loginOrder(uid)
  38. -- log.print("玩家登录处理订单 uid:%s, %s", uid, tostring(orderReissue[uid]))
  39. local orderList = orderReissue[uid]
  40. if not orderList then
  41. return
  42. end
  43. local nodeInfo = sessionData:user_get_cluster_info(uid, "game")
  44. if nodeMgr.is_node_info_valid(nodeInfo) then
  45. -- 回调到逻辑服处理
  46. for _, orderAction in pairs(orderList) do
  47. local msg = {
  48. orderAction = orderAction,
  49. goodsId = tonumber(orderAction.itemId)
  50. }
  51. nodeMgr.send_with_node_info(nodeInfo, "doCmd", uid, "shop", "on_pay", msg)
  52. end
  53. end
  54. end
  55. -- 完成订单处理
  56. function CMD.finishOrder(uid, id)
  57. if not uid or not id then
  58. return
  59. end
  60. util_order:update_order_status(id, 1)
  61. local orderList = orderReissue[uid]
  62. if not orderList then
  63. return
  64. end
  65. log.print("完成订单处理 uid:%s, id:%s", uid, id)
  66. table.key_delete(orderList, "id", id)
  67. if table.empty(orderList) then
  68. orderReissue[uid] = nil
  69. end
  70. end
  71. -- 同步订单
  72. function CMD.syncOrder(uid, orderAction)
  73. log.print("同步订单 uid:%s, orderAction:%s", uid, tostring(orderAction))
  74. orderReissue[uid] = orderReissue[uid] or {}
  75. table.insert(orderReissue[uid], orderAction)
  76. end
  77. function CMD.onStart()
  78. loadUnprocessedOrder()
  79. end
  80. baseService.start(CMD, ".paySrv", true)