add
This commit is contained in:
206
Assets/trCRM/upgradeRes4Dev/priority/lua/net/CLLNet.lua
Normal file
206
Assets/trCRM/upgradeRes4Dev/priority/lua/net/CLLNet.lua
Normal file
@@ -0,0 +1,206 @@
|
||||
--- 网络下行数据调度器
|
||||
require("bio.BioUtl")
|
||||
require("net.NetProtoUsermgrClient")
|
||||
require("net.NetPtMonstersClient")
|
||||
CLLNet = {}
|
||||
local protoClient = NetPtMonsters
|
||||
|
||||
local strLen = string.len
|
||||
local strSub = string.sub
|
||||
local strPack = string.pack
|
||||
--local strbyte = string.byte
|
||||
--local maxPackSize = 64 * 1024 - 1
|
||||
--local subPackSize = 64 * 1024 - 1 - 50
|
||||
---@type Coolape.Net
|
||||
local csSelf = Net.self
|
||||
--local __maxLen = 1024 * 1024
|
||||
local timeOutSec = 30 -- 超时秒
|
||||
local NetSuccess = NetSuccess
|
||||
local __httpBaseUrl = PStr.b():a("http://"):a(Net.self.gateHost):a(":"):a(tostring(Net.self.gatePort)):e()
|
||||
local baseUrlUsermgr = joinStr(__httpBaseUrl, "/usermgr/postbio")
|
||||
|
||||
function CLLNet.refreshBaseUrl(host)
|
||||
__httpBaseUrl = PStr.b():a("http://"):a(host):a(":"):a(tostring(Net.self.gatePort)):e()
|
||||
baseUrlUsermgr = joinStr(__httpBaseUrl, "/usermgr/postbio")
|
||||
end
|
||||
|
||||
--function CLLNet.init()
|
||||
-- csSelf = Net.self
|
||||
--end
|
||||
|
||||
local httpPostBio = function(url, postData, callback, orgs)
|
||||
WWWEx.postBytes(Utl.urlAddTimes(url), postData, CLAssetType.bytes, callback, CLLNet.httpError, orgs, true)
|
||||
end
|
||||
|
||||
function CLLNet.httpPostUsermgr(data, callback)
|
||||
local postData = BioUtl.writeObject(data)
|
||||
httpPostBio(baseUrlUsermgr, postData, CLLNet.onResponsedUsermgr, {callback = callback, data = data})
|
||||
end
|
||||
|
||||
function CLLNet.onResponsedUsermgr(content, orgs)
|
||||
local callback = orgs.callback
|
||||
local map = nil
|
||||
if content then
|
||||
map = BioUtl.readObject(content)
|
||||
end
|
||||
if map then
|
||||
local cmd = map[0]
|
||||
local dispatchInfor = NetProtoUsermgr.dispatch[cmd]
|
||||
if dispatchInfor then
|
||||
local data = dispatchInfor.onReceive(map)
|
||||
if callback then
|
||||
callback(data)
|
||||
else
|
||||
CLLNet.dispatch(data)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function CLLNet.httpError(content, orgs)
|
||||
local callback = orgs.callback
|
||||
local data = orgs.data
|
||||
local map = {}
|
||||
local ret = {}
|
||||
local cmd = data[0]
|
||||
ret.code = number2bio(2)
|
||||
ret.msg = "http error"
|
||||
map.retInfor = ret
|
||||
map.cmd = cmd
|
||||
if callback then
|
||||
callback(map, data)
|
||||
else
|
||||
CLLNet.dispatch(map)
|
||||
end
|
||||
end
|
||||
|
||||
--============================================================
|
||||
|
||||
function CLLNet.dispatchGame(map)
|
||||
if (map == nil) then
|
||||
return
|
||||
end
|
||||
if type(map) == "string" then
|
||||
if map == "connectCallback" then
|
||||
CLPanelManager.topPanel:procNetwork("connectCallback", 1, "connectCallback", nil)
|
||||
--csSelf:invoke4Lua(CLLNet.heart, nil, timeOutSec, true);
|
||||
InvokeEx.invoke(CLLNet.heart, timeOutSec)
|
||||
elseif map == "outofNetConnect" then
|
||||
--csSelf:cancelInvoke4Lua(CLLNet.heart)
|
||||
InvokeEx.cancelInvoke(CLLNet.heart)
|
||||
CLPanelManager.topPanel:procNetwork("outofNetConnect", -9999, "outofNetConnect", nil)
|
||||
|
||||
-- 处理断线处理
|
||||
if GameMode.none ~= MyCfg.mode then
|
||||
local ok, result = pcall(procOffLine)
|
||||
if not ok then
|
||||
printe(result)
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
local dispatchInfor = protoClient.dispatch[map[0]]
|
||||
if dispatchInfor then
|
||||
local data = dispatchInfor.onReceive(map)
|
||||
CLLNet.dispatch(data)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function CLLNet.dispatch(map)
|
||||
local cmd = map.cmd -- 接口名
|
||||
if protoClient and cmd == protoClient.cmds.heart then
|
||||
-- 心跳不处理
|
||||
return
|
||||
end
|
||||
local retInfor = map.retInfor
|
||||
-- 解密bio
|
||||
retInfor.code = BioUtl.bio2int(retInfor.code)
|
||||
local succ = retInfor.code
|
||||
local msg = retInfor.msg
|
||||
|
||||
if MyCfg.self.isEditMode then
|
||||
print(joinStr("cmd=", cmd, "==succ==", succ, "==msg==", msg))
|
||||
end
|
||||
|
||||
if (succ ~= NetSuccess) then
|
||||
retInfor.msg = Localization.Get(joinStr("Error_", succ))
|
||||
CLAlert.add(retInfor.msg, Color.red, 1)
|
||||
hideHotWheel()
|
||||
else
|
||||
-- success
|
||||
CLLNet.cacheData(cmd, map)
|
||||
end
|
||||
|
||||
-- 通知所有显示的页面
|
||||
local panels4Retain = CLPanelManager.panels4Retain
|
||||
if (panels4Retain ~= nil and panels4Retain.Length > 0) then
|
||||
for i = 0, panels4Retain.Length - 1 do
|
||||
panels4Retain[i]:procNetwork(cmd, succ, msg, map)
|
||||
end
|
||||
else
|
||||
if (CLPanelManager.topPanel ~= nil) then
|
||||
CLPanelManager.topPanel:procNetwork(cmd, succ, msg, map)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--数据缓存的function
|
||||
local cacheDataFuncs = {
|
||||
[protoClient.cmds.sendNetCfg] = function(netData)
|
||||
---@type NetPtMonsters.RC_sendNetCfg
|
||||
local data = netData
|
||||
-- 初始化时间
|
||||
local systime = bio2number(data.systime)
|
||||
DateEx.init(systime)
|
||||
---@type CLLNetSerialize
|
||||
local netSerialize = csSelf.luaTable
|
||||
if netSerialize then
|
||||
netSerialize.setCfg(data.netCfg)
|
||||
end
|
||||
end,
|
||||
[protoClient.cmds.login] = function(netData)
|
||||
---@type NetPtMonsters.RC_login
|
||||
local data = netData
|
||||
protoClient.__sessionID = bio2number(data.session)
|
||||
-- 初始化时间
|
||||
local systime = bio2number(data.systime)
|
||||
DateEx.init(systime)
|
||||
--//TODO:
|
||||
--[[
|
||||
---@type protoClient.ST_player
|
||||
local player = data.player
|
||||
local p = IDDBPlayer.new(player)
|
||||
IDDBPlayer.myself = p
|
||||
---@type protoClient.ST_city
|
||||
local city = data.city
|
||||
local curCity = IDDBCity.new(city)
|
||||
curCity:initDockyardShips()
|
||||
IDDBCity.curCity = curCity
|
||||
--]]
|
||||
end
|
||||
}
|
||||
|
||||
function CLLNet.cacheData(cmd, netData)
|
||||
if protoClient == nil then
|
||||
return
|
||||
end
|
||||
local func = cacheDataFuncs[cmd]
|
||||
if func then
|
||||
func(netData)
|
||||
end
|
||||
end
|
||||
|
||||
-- 心跳
|
||||
function CLLNet.heart()
|
||||
net:send(protoClient.send.heart())
|
||||
InvokeEx.invoke(CLLNet.heart, timeOutSec)
|
||||
--csSelf:invoke4Lua(CLLNet.heart, nil, timeOutSec, true)
|
||||
end
|
||||
|
||||
function CLLNet.cancelHeart()
|
||||
--csSelf:cancelInvoke4Lua(CLLNet.heart)
|
||||
InvokeEx.cancelInvoke(CLLNet.heart)
|
||||
end
|
||||
|
||||
return CLLNet
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f3d7aee8b8a754618b615b8ca13fe3d8
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
200
Assets/trCRM/upgradeRes4Dev/priority/lua/net/CLLNetSerialize.lua
Normal file
200
Assets/trCRM/upgradeRes4Dev/priority/lua/net/CLLNetSerialize.lua
Normal file
@@ -0,0 +1,200 @@
|
||||
-- 组包
|
||||
require("public.CLLInclude")
|
||||
require("bio.BioUtl")
|
||||
require("toolkit.CLLPrintEx")
|
||||
require("toolkit.BitUtl")
|
||||
|
||||
---@class CLLNetSerialize
|
||||
local CLLNetSerialize = {}
|
||||
|
||||
local strLen = string.len
|
||||
local strSub = string.sub
|
||||
local strPack = string.pack
|
||||
local strbyte = string.byte
|
||||
local strchar = string.char
|
||||
local insert = table.insert
|
||||
local concat = table.concat
|
||||
local maxPackSize = 64 * 1024 - 1
|
||||
local subPackSize = 64 * 1024 - 1 - 50
|
||||
local __maxLen = 1024 * 1024
|
||||
|
||||
local currPack = {} --处理分包用
|
||||
local netCfg = {} --配置数据
|
||||
local index = 0
|
||||
--============================================================
|
||||
local EncryptType = {
|
||||
clientEncrypt = 1,
|
||||
serverEncrypt = 2,
|
||||
both = 3,
|
||||
none = 0,
|
||||
}
|
||||
function CLLNetSerialize.setCfg(cfg)
|
||||
--[[
|
||||
cfg.encryptType:加密类别,1:只加密客户端,2:只加密服务器,3:前后端都加密,0及其它情况:不加密
|
||||
cfg.secretKey:密钥
|
||||
cfg.checkTimeStamp:检测时间戳
|
||||
cfg.systime:系统时间 long"
|
||||
]]
|
||||
netCfg = cfg
|
||||
end
|
||||
|
||||
---@public 添加时间戳
|
||||
function CLLNetSerialize.addTimestamp(bytes)
|
||||
if bytes == nil then
|
||||
return nil
|
||||
end
|
||||
index = index + 1
|
||||
if index > 100 then
|
||||
index = 0
|
||||
end
|
||||
local ts = DateEx.nowMS + index
|
||||
return BioUtl.number2bio(ts) .. bytes
|
||||
end
|
||||
|
||||
---@public 安全加固
|
||||
local securityReinforce = function(bytes)
|
||||
if netCfg.checkTimeStamp then
|
||||
bytes = CLLNetSerialize.addTimestamp(bytes)
|
||||
end
|
||||
local enType = bio2Int(netCfg.encryptType)
|
||||
if enType == EncryptType.clientEncrypt or enType == EncryptType.both then
|
||||
bytes = CLLNetSerialize.encrypt(bytes, netCfg.secretKey)
|
||||
end
|
||||
return bytes
|
||||
end
|
||||
--============================================================
|
||||
function CLLNetSerialize.packMsg(data, tcp)
|
||||
local bytes = BioUtl.writeObject(data)
|
||||
if bytes == nil or tcp == nil or tcp.socket == nil then
|
||||
return nil
|
||||
end
|
||||
local len = strLen(bytes)
|
||||
if len > maxPackSize then
|
||||
-- 处理分包
|
||||
--local packList = ArrayList()
|
||||
local subPackgeCount = math.floor(len / subPackSize)
|
||||
local left = len % subPackSize
|
||||
local count = subPackgeCount
|
||||
if left > 0 then
|
||||
count = subPackgeCount + 1
|
||||
end
|
||||
for i = 1, subPackgeCount do
|
||||
local subPackg = {}
|
||||
subPackg.__isSubPack = true
|
||||
subPackg.count = count
|
||||
subPackg.i = i
|
||||
subPackg.content = strSub(bytes, ((i - 1) * subPackSize) + 1, i * subPackSize)
|
||||
local _bytes = securityReinforce(BioUtl.writeObject(subPackg))
|
||||
local package = strPack(">s2", _bytes)
|
||||
tcp.socket:SendAsync(package)
|
||||
end
|
||||
if left > 0 then
|
||||
local subPackg = {}
|
||||
subPackg.__isSubPack = true
|
||||
subPackg.count = count
|
||||
subPackg.i = count
|
||||
subPackg.content = strSub(bytes, len - left + 1, len)
|
||||
local _bytes = securityReinforce(BioUtl.writeObject(subPackg))
|
||||
local package = strPack(">s2", _bytes)
|
||||
tcp.socket:SendAsync(package)
|
||||
end
|
||||
else
|
||||
local _bytes = securityReinforce(bytes)
|
||||
local package = strPack(">s2", _bytes)
|
||||
tcp.socket:SendAsync(package)
|
||||
end
|
||||
end
|
||||
|
||||
--============================================================
|
||||
-- 完整的接口都是table,当有分包的时候会收到list。list[1]=共有几个分包,list[2]=第几个分包,list[3]= 内容
|
||||
local function isSubPackage(m)
|
||||
if m.__isSubPack then
|
||||
--判断有没有cmd
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
local function unPackSubMsg(m)
|
||||
-- 是分包
|
||||
local count = m.count
|
||||
local index = m.i
|
||||
if m.content == nil then
|
||||
printe("the m.content is nil")
|
||||
return
|
||||
end
|
||||
currPack[index] = m.content
|
||||
if (#currPack == count) then
|
||||
-- 说明分包已经取完整
|
||||
local map = BioUtl.readObject(table.concat(currPack, ""))
|
||||
currPack = {}
|
||||
return map
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
--============================================================
|
||||
---@public 解包
|
||||
function CLLNetSerialize.unpackMsg(buffer, tcp)
|
||||
local ret = nil
|
||||
local oldPos = buffer.Position
|
||||
buffer.Position = 0
|
||||
local totalLen = buffer.Length
|
||||
local needLen = buffer:ReadByte() * 256 + buffer:ReadByte()
|
||||
if (needLen <= 0 and needLen > __maxLen) then
|
||||
--// 网络Number据错误。断isOpen网络
|
||||
tcp.socket:close()
|
||||
return nil
|
||||
end
|
||||
local usedLen = buffer.Position
|
||||
if (usedLen + needLen <= totalLen) then
|
||||
local lessBuff = Utl.read4MemoryStream(buffer, 0, needLen)
|
||||
local enType = bio2Int(netCfg.encryptType)
|
||||
if enType == EncryptType.serverEncrypt or enType == EncryptType.both then
|
||||
lessBuff = CLLNetSerialize.decrypt(lessBuff, netCfg.secretKey)
|
||||
end
|
||||
ret = BioUtl.readObject(lessBuff)
|
||||
else
|
||||
--说明长度不够
|
||||
buffer.Position = oldPos
|
||||
end
|
||||
|
||||
if ret and isSubPackage(ret) then
|
||||
return unPackSubMsg(ret)
|
||||
else
|
||||
return ret
|
||||
end
|
||||
end
|
||||
|
||||
--============================================================
|
||||
local secretKey = ""
|
||||
---@public 加密
|
||||
function CLLNetSerialize.encrypt(bytes, key)
|
||||
return CLLNetSerialize.xor(bytes, key)
|
||||
end
|
||||
|
||||
---@public 解密
|
||||
function CLLNetSerialize.decrypt(bytes, key)
|
||||
return CLLNetSerialize.xor(bytes, key)
|
||||
end
|
||||
|
||||
function CLLNetSerialize.xor(bytes, key)
|
||||
key = key or secretKey
|
||||
if key == nil or key == "" then
|
||||
return bytes
|
||||
end
|
||||
local len = #bytes
|
||||
local keyLen = #key
|
||||
local byte, byte2
|
||||
local keyIdx = 0
|
||||
local result = {}
|
||||
for i = 1, len do
|
||||
byte = strbyte(bytes, i)
|
||||
keyIdx = i % keyLen + 1
|
||||
byte2 = BitUtl.xorOp(byte, strbyte(key, keyIdx))
|
||||
insert(result, strchar(byte2))
|
||||
end
|
||||
return concat(result)
|
||||
end
|
||||
--------------------------------------------
|
||||
return CLLNetSerialize
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e5d249971c4b94f4c9a7a4a83f2a7835
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
508
Assets/trCRM/upgradeRes4Dev/priority/lua/net/NetProto.lua
Normal file
508
Assets/trCRM/upgradeRes4Dev/priority/lua/net/NetProto.lua
Normal file
@@ -0,0 +1,508 @@
|
||||
NetProto = {}
|
||||
|
||||
local onReceiveCmdCallback = nil
|
||||
local PanelListener = {}
|
||||
---@type CLLQueue
|
||||
local ListenerQueue = CLLQueue.new()
|
||||
NetProto.send = {}
|
||||
---------------------------------------------------------------------------------------
|
||||
---------------------------------------------------------------------------------------
|
||||
---------------------------------------------------------------------------------------
|
||||
-- local baseUrl = "http://app.ttf-cti.com:29000/open_api/"
|
||||
local baseUrl = "http://47.111.20.34:29004/open_api/"
|
||||
-- local baseUrl2 = "http://47.111.20.34:29004/open_api/"
|
||||
-- local socketUrl = "ws://app.ttf-cti.com:29000/tr_socket/websocket/"
|
||||
local socketUrl = "ws://47.111.20.34:29004/tr_socket/websocket/"
|
||||
---@type Dist.SpringWebsocket.Client
|
||||
local socket = SocketClient.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
|
||||
CLAlert.add(msg, Color.yellow, 1)
|
||||
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
|
||||
CLAlert.add("与服务器失去联络,请重试!", Color.yellow, 1)
|
||||
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
|
||||
CLAlert.add(data.msg, Color.yellow, 1)
|
||||
Utl.doCallback(callback, false)
|
||||
end
|
||||
end,
|
||||
function()
|
||||
CLAlert.add("取得会话ID失败!", Color.yellow, 1)
|
||||
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" -- 更新客户信息
|
||||
}
|
||||
---------------------------------------------------------------------------------------
|
||||
---------------------------------------------------------------------------------------
|
||||
---------------------------------------------------------------------------------------
|
||||
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 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
|
||||
-- 重连失败提示
|
||||
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)
|
||||
InvokeEx.invoke(NetProto.doSendMsg, contentStr, 0.1)
|
||||
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.phone = 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
|
||||
------------------------------------------------------
|
||||
return NetProto
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 914cc9ac1476d4c96ae8a5d7d5be2965
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user