shop.lua 5.4 KB

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