shop.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. -- 商城接口
  2. local code = require "code"
  3. local util_user = require("utils.util_user")
  4. local lib_game_mysql = require("lib_game_mysql")
  5. local util_order = require("utils.util_order")
  6. local shopAdapt = require("adapt.shopAdapt")
  7. local dataBag = require("data.bag")
  8. local root = {}
  9. -- 购买商品
  10. function root.shop_buy_goods(role, msg)
  11. local gid = msg.goodsId
  12. local conf = shopAdapt:goods_get_info(gid)
  13. if conf == nil or conf.price == nil then
  14. return code.PARAMTER_ERROR
  15. end
  16. local uid = role.uid
  17. local ok = dataBag:is_enough(uid, conf.price)
  18. if not ok then
  19. return code.RES.NOT_ENOUGH
  20. end
  21. local eventId = "shop-pay-" .. tostring(gid)
  22. dataBag:consume_items(uid, conf.price, eventId)
  23. return role.pay:dispatch_goods_items(role, gid)
  24. end
  25. -- 充值成功回调
  26. function root.on_pay(role, msg)
  27. local uid = role.uid
  28. log.print("充值成功回调 %s, %s", uid, tostring(msg))
  29. local order = msg.orderAction
  30. local logEvent = function(seasonId)
  31. local extraParams =
  32. table.concat(
  33. {
  34. order and order.orderNo or "",
  35. order and order.type or "",
  36. order and order.itemId or "",
  37. order and order.amount or "",
  38. order and order.status or "",
  39. seasonId or ""
  40. },
  41. ","
  42. )
  43. util_user:log_event(uid, "OrderError", extraParams)
  44. end
  45. local gid = msg.goodsId
  46. local conf = gid and shopAdapt:goods_get_info(gid)
  47. if is_empty(conf) then
  48. log.error("商城充值回调 gid =%s", gid)
  49. logEvent(1)
  50. return code.PARAMTER_ERROR
  51. end
  52. -- 订单先完成
  53. util_order:update_order_status(order and order.id, 1)
  54. -- 统计 - 充值金额
  55. local amount = math.floor(tonumber(msg.orderAction.amount) * 100)
  56. -- 增加物品
  57. local errCode, res = role.pay:dispatch_goods_items(role, gid, amount)
  58. if code.is_not_ok(errCode) then
  59. return errCode
  60. end
  61. -- 推送客户端
  62. util_order:on_shop_order(order)
  63. return code.OK, res
  64. end
  65. -- 1.5版本订单BUG,修复代码
  66. local function is_order(str)
  67. if str == nil or #str == 0 then
  68. return false
  69. end
  70. for i = 1, #str do
  71. local cur_byte = string.byte(str, i)
  72. if cur_byte > 127 then
  73. return false
  74. end
  75. end
  76. return true
  77. end
  78. -- 查询订单结果
  79. function root.shop_get_order(role, msg)
  80. if msg == nil or msg.orderId == nil then
  81. return code.PARAMTER_ERROR
  82. end
  83. local uid = role.uid
  84. local orderId = msg.orderId
  85. log.info("itf_get_order uid[%s] orderId[%s]", uid, orderId)
  86. if not is_order(orderId) then
  87. return code.SHOP.ALREADY_GET_ORDER
  88. end
  89. -- 订单是否已入库
  90. local sql =
  91. string.format(
  92. "SELECT `id`, `gid`, `status`, `resMsg`, `pay`, `amount` FROM `mdl_order` WHERE orderNo='%s';",
  93. orderId
  94. )
  95. local ret = lib_game_mysql:query(sql)
  96. if is_empty(ret) then
  97. return code.SHOP.NO_FOUND_ORDER
  98. end
  99. -- 修改订单状态
  100. sql = string.format("UPDATE `mdl_order` SET `readStatus`=99 WHERE `orderNo`='%s';", orderId)
  101. lib_game_mysql:query(sql)
  102. local id = tonumber(ret[1].id)
  103. local status = tonumber(ret[1].status)
  104. local goodsId = tonumber(ret[1].gid)
  105. local resMsg = ret[1].resMsg
  106. local payName = ret[1].pay
  107. local amount = ret[1].amount
  108. local order = {
  109. status = status,
  110. goodsId = goodsId,
  111. orderId = orderId,
  112. errMsg = resMsg,
  113. payName = payName,
  114. amount = tostring(amount)
  115. }
  116. if status ~= 1 then
  117. -- 订单支付失败
  118. return code.OK, {order = order}
  119. end
  120. local logEvent = function(seasonId)
  121. local extraParams =
  122. table.concat(
  123. {
  124. orderId,
  125. payName,
  126. goodsId,
  127. amount,
  128. status,
  129. seasonId or ""
  130. },
  131. ","
  132. )
  133. util_user:log_event(uid, "OrderError", extraParams)
  134. end
  135. local conf = goodsId and shopAdapt:goods_get_info(goodsId)
  136. if is_empty(conf) then
  137. log.error("商城充值回调 goodsId =%s", goodsId)
  138. logEvent(1)
  139. return code.PARAMTER_ERROR
  140. end
  141. -- TODO 订单回调是否重复
  142. -- 订单先完成
  143. util_order:update_order_status(id, 1)
  144. -- 增加物品
  145. local errCode, res = role.pay:dispatch_goods_items(role, goodsId)
  146. if errCode ~= code.OK then
  147. return errCode
  148. end
  149. -- 推送客户端
  150. util_order:on_shop_order(order)
  151. local ret = {orderInfo = util_order:pack_order_info(order)}
  152. return code.OK, ret
  153. end
  154. return root