TextReviewProxy.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. local RemoteProxy = requireProxy("remote/RemoteProxy")
  2. local TextReviewProxy = class("TextReviewProxy", RemoteProxy)
  3. TextReviewProxy.NAME = global.ProxyTable.TextReviewProxy
  4. local sha1 = require( "sha1/init" )
  5. local cjson = require("cjson")
  6. function TextReviewProxy:ctor()
  7. TextReviewProxy.super.ctor(self)
  8. local envProxy = global.Facade:retrieveProxy( global.ProxyTable.GameEnvironment )
  9. self._isCheckText = tonumber(envProxy:GetCustomDataByKey("checkText")) == 1
  10. self._domain = "https://gtf.ai.xingzheai.cn/"
  11. -- self._domainAbroad = "https://gtf.ai-abroad.xingzheai.cn/"
  12. self._uri = "/v2.0/game_chat_ban/detect_text"
  13. self._url = "https://gtf.ai.xingzheai.cn/v2.0/game_chat_ban/detect_text"
  14. self._token = "9J0IKT8H6NF3UB24"
  15. self.CONTENT_TYPE = { --支持“chat”(聊天消息),“nick”(昵称),"post"(帖 子),"notice"(公告), "signature"(签名)5种
  16. "chat","nick","post","notice","signature"
  17. }
  18. local envProxy = global.Facade:retrieveProxy(global.ProxyTable.GameEnvironment)
  19. local TextReviewDoamin = envProxy:GetCustomDataByKey("TextReviewDoamin")
  20. if TextReviewDoamin and string.len(TextReviewDoamin) > 0 then
  21. self._domain = TextReviewDoamin .. "/"
  22. self._url = TextReviewDoamin .. "/v2.0/game_chat_ban/detect_text"
  23. end
  24. end
  25. function REQUEST_TEXT_REVIEW( contentType, content, autoTips, callbackCB)
  26. local TextReviewProxy = global.Facade:retrieveProxy( global.ProxyTable.TextReviewProxy )
  27. return TextReviewProxy:RequestReviewText( contentType, content, autoTips, callbackCB)
  28. end
  29. function TextReviewProxy:RequestReviewText(contentType, content, autoTips, callbackCB)
  30. local url = self._url
  31. if not (url and string.len(url) > 0) then
  32. return nil
  33. end
  34. if not contentType or not tonumber(contentType) or not content then
  35. return nil
  36. end
  37. if not self._isCheckText then
  38. if callbackCB then
  39. callbackCB(true)
  40. end
  41. return false
  42. end
  43. -- http数据请求回调
  44. local function httpCB(success, response)
  45. if not success then
  46. if callbackCB then
  47. callbackCB(false)
  48. end
  49. return false
  50. end
  51. local isOk, jsonData = pcall(function ( ... )
  52. -- body
  53. return cjson.decode(response)
  54. end)
  55. if not isOk then
  56. releasePrint("TextReview Error : json Data Parsing failed.")
  57. if callbackCB then
  58. callbackCB(false)
  59. end
  60. return false
  61. end
  62. dump(jsonData)
  63. self.code = jsonData.code
  64. self.msg = jsonData.msg
  65. if self.msg and self.msg ~= "success" or self.code == -1 then
  66. releasePrint("TextReview Error : "..self.msg)
  67. end
  68. self.data_id = jsonData.data_id
  69. self.data = jsonData.data
  70. -- dump(self.data)
  71. if self.data and self.data.suggestion then
  72. --
  73. if self.data.suggestion == "pass" then
  74. callbackCB(true)
  75. elseif self.data.suggestion == "review" or self.data.suggestion == "block" then
  76. if autoTips then
  77. ShowSystemTips(GET_STRING(700000010))
  78. end
  79. callbackCB(false)
  80. end
  81. end
  82. if self.data and self.data.label then
  83. --
  84. -- dump(self.data.label)
  85. end
  86. end
  87. local loginProxy = global.Facade:retrieveProxy(global.ProxyTable.Login)
  88. local role_id = loginProxy:GetSelectedRoleID()
  89. local originData = {
  90. {key="data_id", value= randDataID(role_id)},
  91. {key="context", value= content},
  92. {key="context_type", value= self.CONTENT_TYPE[tonumber(contentType)]},
  93. {key="token", value= self._token},
  94. -- {key="user_id", value=data.user_id},
  95. -- {key="role_id", value= role_id},
  96. }
  97. -- if data.room_id then
  98. -- table.insert( originData, {key="room_id", value=data.room_id} )
  99. -- end
  100. -- if data.area then
  101. -- table.insert( originData, {key="area", value=data.area} )
  102. -- end
  103. local suffix = self:calcDataParam(originData)
  104. local header = self:getHeader()
  105. self:HTTPRequestPost(url, httpCB, suffix, header)
  106. releasePrint("RequestReviewText url: " .. url .. " postData: " .. suffix)
  107. end
  108. function TextReviewProxy:HTTPRequestPost(url, callback, data, header)
  109. dump({url, callback, data, header},"ssss____")
  110. local httpRequest = cc.XMLHttpRequest:new()
  111. httpRequest.responseType = cc.XMLHTTPREQUEST_RESPONSE_JSON
  112. httpRequest.timeout = 100000 -- 10 s
  113. httpRequest:open("POST", url)
  114. if header then
  115. for k, v in pairs(header) do
  116. httpRequest:setRequestHeader(k, v)
  117. end
  118. end
  119. if callback then
  120. local function HttpResponseCB()
  121. local code = httpRequest.status
  122. local success = code >= 200 and code < 300
  123. local response = httpRequest.response
  124. releasePrint("Http Request Code:" .. tostring(code))
  125. -- print("!!!Http Request readyState: ".. httpRequest.readyState)
  126. callback(success, response, code)
  127. end
  128. -- httpRequest:setRequestHeader("Content-type", "application/x-www-form-urlencoded")
  129. httpRequest:registerScriptHandler(HttpResponseCB)
  130. end
  131. httpRequest:send(data)
  132. end
  133. function TextReviewProxy:getHeader()
  134. local header = {}
  135. header["Content-Type"] = "application/json"
  136. return header
  137. end
  138. function TextReviewProxy:calcDataParam(originData)
  139. local paramData = {}
  140. for i, v in ipairs(originData) do
  141. paramData[v.key] = v.value
  142. end
  143. local suffix = cjson.encode(paramData)
  144. return suffix
  145. end
  146. return TextReviewProxy