Files
tianrunCRM/Assets/trCRM/upgradeRes4Dev/priority/lua/net/NetProto.lua
2020-07-16 23:06:09 +08:00

571 lines
20 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

NetProto = {}
local onReceiveCmdCallback = nil
local PanelListener = {}
---@type CLLQueue
local ListenerQueue = CLLQueue.new()
NetProto.send = {}
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
local host = "app.ttf-cti.com" -- "47.111.20.34"
local port = 29004
-- local host = "192.168.1.126"
-- local port = 29000
local baseUrl = joinStr("http://", host, ":", port, "/open_api/")
-- local baseUrl = "http://47.111.20.34/home/open_api/"
local socketUrl = joinStr("ws://", host, ":", port, "/tr_socket/websocket/")
---@type Dist.SpringWebsocket.Client
local socket = Client4Stomp.self
local appid = 2020158
local appsecret = "ea042bc86428ca968756a6e47b10742d"
local isDebug = true
local ErrCode = {
success = 1000,
invalidToken = 2019
}
local wrapSendData = function(map)
map = map or {}
map.appid = appid
map.timestamp = DateEx.nowMS + 1000
map.sign = NetProto.sign
local dList = {}
for k, v in pairs(map) do
if (type(v) == "number" or type(v) == "string" or type(v) == "boolean") then
table.insert(dList, joinStr(k, "=", Uri.EscapeDataString(v or "")))
end
end
return table.concat(dList, "&")
end
local dispatch = function(content, params)
hideHotWheel()
local cmd = params.cmd -- 接口名
local callback = params.callback
local orgs = params.orgs
local map = content or {}
-- 解密bio
local succ = map.success == nil and true or map.success
local msg = map.message or ""
local code = succ and (map.code or NetSuccess) or (map.code or -1)
-- if MyCfg.self.isEditMode then
-- print(joinStr("cmd:[", cmd, "]code:[", code, "]msg:", msg))
-- end
if code == ErrCode.invalidToken then
-- 重新设置token
Prefs.setToken("")
NetProto.init()
end
if (not succ) then
printe(joinStr("cmd:[", cmd, "]code:[", code, "]msg:", msg))
-- retInfor.msg = Localization.Get(joinStr("Error_", succ))
if not isNilOrEmpty(msg) then
MyUtl.toastW(msg)
end
hideHotWheel()
else
-- success
NetProto.onReceiveCMD(cmd, map)
end
Utl.doCallback(callback, map, orgs)
-- 线程安全考虑先把要处理的panle放到quque中
for k, p in pairs(PanelListener) do
ListenerQueue:enQueue(p)
end
local p
while (ListenerQueue:size() > 0) do
---@type coolape.Coolape.CLPanelBase
p = ListenerQueue:deQueue()
if p then
p:procNetwork(cmd, code, msg, map)
end
end
end
local dispatchHttp = function(content, params)
if isDebug then
print(content)
end
local map = json.decode(content)
dispatch(map, params)
end
local onFailedSend = function(content, params)
hideHotWheel()
local cmd = params.cmd -- 接口名
local failedCallback = params.failedCallback
local orgs = params.orgs
Utl.doCallback(failedCallback, nil, orgs)
end
NetProto.sendGet = function(cmd, map, callback, failedCallback, orgs, _baseUrl)
if isNilOrEmpty(NetProto.sign) and (cmd ~= NetProto.cmds.getTokenForAPI) then
MyUtl.toastW("与服务器失去联络,请重试!")
NetProto.init()
return
end
showHotWheel()
local _url = _baseUrl or baseUrl
local url = joinStr(_url, cmd, "?", wrapSendData(map))
local params = {
cmd = cmd,
orgs = orgs,
callback = callback,
failedCallback = failedCallback
}
url = Uri.EscapeUriString(url)
if isDebug then
print(url)
end
WWWEx.get(url, NetProto.httpHeader, CLAssetType.text, dispatchHttp, onFailedSend, params, true, 1)
end
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
local onGetToken = function(token)
Prefs.setToken(token)
NetProto.token = token
NetProto.sign = Utl.getSha256(joinStr(appid, "&", NetProto.token))
NetProto.httpHeader = {
Authorization = joinStr("Bearer ", NetProto.token)
}
-- print("token==" .. NetProto.token)
-- print("sign==" .. NetProto.sign)
end
function NetProto.init(callback)
-- NetProto.token = Prefs.getToken()
NetProto.getTokenForAPI(
appsecret,
function(data, orgs)
if data.success then
onGetToken(data.result)
Utl.doCallback(callback, true)
else
MyUtl.toastW(data.msg)
Utl.doCallback(callback, false)
end
end,
function()
MyUtl.toastW("取得会话ID失败")
Utl.doCallback(callback, false)
end
)
end
---@param p Coolape.CLPanelBase
function NetProto.addPanelListener(p)
if p then
PanelListener[p] = p
end
end
---@param p Coolape.CLPanelBase
function NetProto.removePanelListener(p)
if p then
PanelListener[p] = nil
end
end
function NetProto.cleanPanelListeners()
PanelListener = {}
end
function NetProto.setReceiveCMDCallback(cb)
onReceiveCmdCallback = cb
end
---public 缓存数据
function NetProto.onReceiveCMD(cmd, data)
if onReceiveCmdCallback then
onReceiveCmdCallback(cmd, data)
end
end
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
NetProto.getTokenForAPI = function(appsecret, callback, failedCallback, orgs)
local map = {
appsecret = appsecret
}
NetProto.sendGet(NetProto.cmds.getTokenForAPI, map, callback, failedCallback, orgs, baseUrl)
end
NetProto.login = function(map, callback, failedCallback, orgs)
-- local map = {
-- phone = phone,
-- password = password
-- }
NetProto.sendGet(NetProto.cmds.login, map, callback, failedCallback, orgs)
end
-- 取得短信验证码
NetProto.sendVerMsg = function(map, callback, failedCallback, orgs)
NetProto.sendGet(NetProto.cmds.sendVerMsg, map, callback, failedCallback, orgs)
end
NetProto.updateLoginNoPwd = function(map, callback, failedCallback, orgs)
NetProto.sendGet(NetProto.cmds.updateLoginNoPwd, map, callback, failedCallback, orgs)
end
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
NetProto.cmds = {
getTokenForAPI = "getTokenForAPI",
login = "login",
sendVerMsg = "sendVerMsg", -- 取得短信验证码
updateLoginNoPwd = "updateLoginNoPwd", -- 设置新密码
announcement_query = "announcement_query", -- 系统公告
booking_query = "booking_query", -- 待跟进客户
replenish_query = "replenish_query", -- 待补货
filter_customers = "filter_customers", -- 过滤条件
list_customers = "list_customers", -- 客户列表
person_view_query = "person_view_query", -- 头像
bi_call = "bi_call", -- 打电话
query_cust_calllog = "query_cust_calllog", -- 通话记录
sales_view_query = "sales_view_query", -- 今日成交&今日金额&本月金额
custtype_report = "custtype_report", -- 客户类型分布
order_report = "order_report", -- 客户类型分布
target_report = "target_report", -- 客户类型分布
update_customer = "update_customer", -- 更新客户信息
save_customer = "save_customer", -- 新建客户
create_followUp_record = "create_followUp_record", -- 新建跟进
load_wfTicket_Settings = "load_WfTicket_Settings", -- 工单配置信息
selectProductInfo = "selectProductInfo", -- 商品列表
createWfInfo = "createWfInfo", -- 创建订单
}
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
local __callbackInfor = {} -- 回调信息
local __callTimes = 1
---public 处理回调
local doCallbackSocket = function(result)
local callbackKey = result.callbackId
if callbackKey then
local cbinfor = __callbackInfor[callbackKey]
if cbinfor and cbinfor[1] then
pcall(cbinfor[1], result, cbinfor[2])
end
__callbackInfor[callbackKey] = nil
end
end
---public 超时处理
local timeOutCallback = function(param)
if __callbackInfor[param.callbackId] then
printe("timeOutCallback====")
-- 说明请求还没有返回
local result = {callbackId = param.callbackId, code = -100, success = false, message = "请求超时"}
doCallbackSocket(result)
dispatch(result, {cmd = param.action})
end
end
---public 设计回调信息
local setCallback = function(callback, orgs, timeOutSec)
local callbackKey
-- if callback then
callbackKey = os.time() + __callTimes
__callTimes = __callTimes + 1
__callbackInfor[callbackKey] = {callback, orgs}
-- end
if timeOutSec and timeOutSec > 0 then
InvokeEx.invoke(timeOutCallback, orgs, timeOutSec)
end
return callbackKey
end
---------------------------------------------------------------------------------------
NetProto.socketInit = function(comanyId, loginNo, groupId)
-- 网络
NetProto.comanyId = comanyId
NetProto.loginNo = loginNo
NetProto.groupId = groupId
socket:init(socketUrl, NetProto.OnReceiveStompMsg)
end
---@param frame Dist.SpringWebsocket.StompFrame
NetProto.OnReceiveStompMsg = function(frame)
-- if isDebug then
-- print(frame.Code, frame.Content)
-- end
if frame.Code == StompStatus.OPENSERVER then
socket:Connect(nil, NetProto.OnReceiveStompMsg)
elseif frame.Code == StompStatus.CONNECTED then
socket:Subscribe(
joinStr("socket.client.", NetProto.comanyId, ".", NetProto.loginNo, ".notice"),
NetProto.OnReceiveStompMsg
)
socket:Subscribe(
joinStr("socket.client.", NetProto.comanyId, ".", NetProto.loginNo, ".response"),
NetProto.OnReceiveStompMsg
)
if NetProto.isReconnect then
-- 说明是重连
NetProto.isReconnect = false
NetProto.reconnectTimes = 0
InvokeEx.invoke(NetProto.reSendWenConnected, 0.5)
else
dispatch({success = true}, {cmd = "connect"})
end
elseif frame.Code == StompStatus.MESSAGE then
local success, content = pcall(json.decode, frame.Content)
if not success then
printe(content)
return
end
if content then
local cmd = content.action
if not cmd then
local callbackKey = content.callbackId
if callbackKey then
local cbinfor = __callbackInfor[callbackKey]
if cbinfor then
local orgs = cbinfor[2]
cmd = orgs.action
end
end
end
if isDebug then
printw(cmd, frame.Content)
end
if cmd then
doCallbackSocket(content)
dispatch(content, {cmd = cmd})
end
end
elseif frame.Code == StompStatus.ERROR then
elseif frame.Code == StompStatus.SERVERCLOSED then
dispatch({success = false}, {cmd = "connect"})
NetProto.reconnectSocket()
elseif frame.Code == StompStatus.SERVERERROR then
dispatch({success = false}, {cmd = "connect"})
NetProto.reconnectSocket()
end
end
NetProto.reconnectSocket = function()
socket:close()
InvokeEx.cancelInvoke(NetProto.doReconnectSocket)
InvokeEx.invoke(NetProto.doReconnectSocket, 1)
end
---public 重连socket服务器
NetProto.doReconnectSocket = function()
NetProto.reconnectTimes = NetProto.reconnectTimes or 0
NetProto.isReconnect = true
if NetProto.reconnectTimes > 1 then
NetProto.reconnectTimes = 0
-- 重连失败提示
CLUIUtl.showConfirm("服务器连接失败,请重试", NetProto.doReconnectSocket)
else
NetProto.reconnectTimes = NetProto.reconnectTimes + 1
socket:init(socketUrl, NetProto.OnReceiveStompMsg)
end
end
---public 重新发送数据
NetProto.reSendWenConnected = function()
local list = {}
for k, v in pairs(__callbackInfor) do
table.insert(list, v)
end
__callbackInfor = {}
for i, v in ipairs(list) do
local callback = v[1]
local content = v[2]
NetProto.sendSocket(content, callback)
end
end
NetProto.sendSocket = function(content, callback, timeOutSec)
if (content == nil) then
printe("发送信息不能为空")
return
end
content.operator = NetProto.loginNo
content.loginNo = content.loginNo or NetProto.loginNo
content.companyId = NetProto.comanyId
content.callbackId = setCallback(callback, content, timeOutSec)
local contentStr = json.encode(content)
NetProto.doSendMsg(contentStr)
end
NetProto.doSendMsg = function(contentStr)
if isDebug then
print("send=", contentStr)
end
socket:Send("/tr_socket", {atytopic = "socket.server.operation"}, contentStr)
end
---public 取得系统公告
NetProto.send.announcement_query = function(callback, timeOutSec)
local content = {}
content.action = NetProto.cmds.announcement_query
NetProto.sendSocket(content, callback, timeOutSec)
end
---public 待跟进客户
NetProto.send.booking_query = function(callback, timeOutSec)
local content = {}
content.action = NetProto.cmds.booking_query
content.groupId = NetProto.groupId
NetProto.sendSocket(content, callback, timeOutSec)
end
---public 待补货
NetProto.send.replenish_query = function(callback, timeOutSec)
local content = {}
content.action = NetProto.cmds.replenish_query
NetProto.sendSocket(content, callback, timeOutSec)
end
---public 取得客户过滤条件
NetProto.send.filter_customers = function(callback, timeOutSec)
local content = {}
content.action = NetProto.cmds.filter_customers
content.groupId = NetProto.groupId
NetProto.sendSocket(content, callback, timeOutSec)
end
NetProto.send.list_customers = function(filters, queryKey, page, callback, timeOutSec)
local content = {}
content.action = NetProto.cmds.list_customers
content.filters = filters
content.keywords = queryKey
content.current_page = page
NetProto.sendSocket(content, callback, timeOutSec)
end
NetProto.send.person_view_query = function(callback, timeOutSec)
local content = {}
content.action = NetProto.cmds.person_view_query
content.loginNo = NetProto.loginNo
content.groupId = NetProto.groupId
NetProto.sendSocket(content, callback, timeOutSec)
end
NetProto.send.bi_call = function(custId, phoneNo, loginNo, callback, timeOutSec)
local content = {}
content.action = NetProto.cmds.bi_call
content.custId = custId
content.phoneNo = phoneNo
content.loginNo = loginNo or NetProto.loginNo
NetProto.sendSocket(content, callback, timeOutSec)
end
NetProto.send.query_cust_calllog = function(phoneNo, loginNo, current_page, callback, timeOutSec)
local content = {}
content.action = NetProto.cmds.query_cust_calllog
content.groupId = NetProto.groupId
content.loginNo = loginNo or NetProto.loginNo
content.phoneNo = phoneNo
content.current_page = current_page or 1
NetProto.sendSocket(content, callback, timeOutSec)
end
NetProto.send.sales_view_query = function(loginNo, callback, timeOutSec)
local content = {}
content.action = NetProto.cmds.sales_view_query
content.groupId = NetProto.groupId
content.loginNo = loginNo or NetProto.loginNo
NetProto.sendSocket(content, callback, timeOutSec)
end
---public 客户类型分布
NetProto.send.custtype_report = function(loginNo, callback, timeOutSec)
local content = {}
content.action = NetProto.cmds.custtype_report
content.groupId = NetProto.groupId
content.loginNo = loginNo or NetProto.loginNo
NetProto.sendSocket(content, callback, timeOutSec)
end
---public 客户类型分布
NetProto.send.order_report = function(loginNo, callback, timeOutSec)
local content = {}
content.action = NetProto.cmds.order_report
content.groupId = NetProto.groupId
content.loginNo = loginNo or NetProto.loginNo
NetProto.sendSocket(content, callback, timeOutSec)
end
---public 客户类型分布
NetProto.send.target_report = function(loginNo, callback, timeOutSec)
local content = {}
content.action = NetProto.cmds.target_report
content.groupId = NetProto.groupId
content.loginNo = loginNo or NetProto.loginNo
NetProto.sendSocket(content, callback, timeOutSec)
end
---public 更新客户信息
NetProto.send.update_customer = function(customer, callback, timeOutSec)
local content = {}
content.action = NetProto.cmds.update_customer
content.groupId = NetProto.groupId
content.customer = customer
NetProto.sendSocket(content, callback, timeOutSec)
end
NetProto.send.save_customer = function(customer, callback, timeOutSec)
local content = {}
content.action = NetProto.cmds.save_customer
content.loginNo = NetProto.loginNo
content.groupId = NetProto.groupId
content.customer = customer
NetProto.sendSocket(content, callback, timeOutSec)
end
NetProto.send.create_followUp_record = function(followUpRecordJson, callback, timeOutSec)
local content = {}
content.action = NetProto.cmds.create_followUp_record
followUpRecordJson.loginNo= NetProto.loginNo
followUpRecordJson.groupId = NetProto.groupId
followUpRecordJson.recordingTime = DateEx.nowString()
content.followUpRecordJson = followUpRecordJson
NetProto.sendSocket(content, callback, timeOutSec)
end
NetProto.send.load_wfTicket_Settings = function(callback, timeOutSec)
local content = {}
content.action = NetProto.cmds.load_wfTicket_Settings
content.loginNo = NetProto.loginNo
content.groupId = NetProto.groupId
NetProto.sendSocket(content, callback, timeOutSec)
end
NetProto.send.selectProductInfo = function(searchKey, page, callback, timeOutSec)
local content = {}
content.action = NetProto.cmds.selectProductInfo
content.loginNo = NetProto.loginNo
content.groupId = NetProto.groupId
content.current_page = page
content.search = searchKey
NetProto.sendSocket(content, callback, timeOutSec)
end
NetProto.send.createWfInfo = function(workFlowInfo, callback, timeOutSec)
local content = {}
content.action = NetProto.cmds.createWfInfo
local user = DBUser.getUserById(NetProto.loginNo)
content.wfStatus = "正常工单"
content.loginName = user.loginName
content.loginNo = NetProto.loginNo
content.groupId = NetProto.groupId
content.workFlowInfo = workFlowInfo
NetProto.sendSocket(content, callback, timeOutSec)
end
------------------------------------------------------
return NetProto