293 lines
7.8 KiB
Lua
293 lines
7.8 KiB
Lua
---@class _DBUser
|
|
---@field loginNo
|
|
---@field loginName
|
|
---@field groupId
|
|
---@field imageUrl string 头像
|
|
---@field PY
|
|
---@field PYFirst string 首字母
|
|
---@field isHeadPy
|
|
|
|
DBUser = {}
|
|
local db = {}
|
|
local icons = {}
|
|
local poplist = {}
|
|
local rolePermission = {}
|
|
local getIconCallback = {}
|
|
local isDownLoading = {}
|
|
|
|
DBUser.FilterGroup = {
|
|
user = "user",
|
|
group = "group"
|
|
}
|
|
|
|
function DBUser.clean()
|
|
db = {}
|
|
poplist = {}
|
|
rolePermission = {}
|
|
end
|
|
|
|
function DBUser.onGetUsers(userlist, groupList, permissions)
|
|
local options = ArrayList()
|
|
local values = ArrayList()
|
|
db.filters = {}
|
|
local name
|
|
db.filters[DBUser.FilterGroup.user] = {}
|
|
|
|
--------------------------------------------
|
|
for i, v in ipairs(userlist) do
|
|
v.PY = CLUIFormUtl.GetChineseSpell(v.loginName) -- 拼音首字母
|
|
v.PYFirst = string.sub(v.PY, 1, 1) -- 设置首字母
|
|
if v.loginName == "系统生成" then
|
|
name = joinStr(v.loginNo, "_", v.loginName)
|
|
else
|
|
name = joinStr(v.loginName)
|
|
end
|
|
|
|
table.insert(db.filters[DBUser.FilterGroup.user], {name = name, value = v.loginNo})
|
|
db[v.loginNo] = v
|
|
options:Add(name)
|
|
values:Add(v.loginNo)
|
|
end
|
|
poplist[DBUser.FilterGroup.user] = {
|
|
options = options,
|
|
values = values
|
|
}
|
|
--------------------------------------------
|
|
local options = ArrayList()
|
|
local values = ArrayList()
|
|
db.filters[DBUser.FilterGroup.group] = {}
|
|
for i, v in ipairs(groupList or {}) do
|
|
table.insert(db.filters[DBUser.FilterGroup.group], {name = v.name, value = v.id})
|
|
options:Add(v.name)
|
|
values:Add(v.id)
|
|
end
|
|
poplist[DBUser.FilterGroup.group] = {
|
|
options = options,
|
|
values = values
|
|
}
|
|
--------------------------------------------
|
|
rolePermission = {}
|
|
for i, v in ipairs(permissions) do
|
|
rolePermission[v] = true
|
|
end
|
|
DBUser.wrapUserList(userlist, groupList)
|
|
end
|
|
|
|
function DBUser.wrapUserList(userlist, groupList)
|
|
table.sort(
|
|
userlist,
|
|
function(a, b)
|
|
return string.byte(a.PY) < string.byte(b.PY)
|
|
end
|
|
)
|
|
|
|
db.userList = userlist
|
|
db.userPyIndexs = {} -- 拼音字母所在的index
|
|
db.groupTree = {}
|
|
db.userGroupList = {}
|
|
|
|
local char = ""
|
|
---@param v _DBUser
|
|
for i, v in ipairs(userlist) do
|
|
if char == "" or char ~= v.PYFirst then
|
|
char = v.PYFirst
|
|
v.isHeadPy = true
|
|
db.userPyIndexs[v.PYFirst] = i
|
|
else
|
|
v.isHeadPy = false
|
|
end
|
|
|
|
local list = db.userGroupList[v.groupId] or {}
|
|
table.insert(list, v)
|
|
db.userGroupList[v.groupId] = list
|
|
end
|
|
|
|
--//TODO:组装部门的数据
|
|
end
|
|
|
|
function DBUser.getUserList()
|
|
return db.userList
|
|
end
|
|
|
|
function DBUser.getGroupList(parentGId)
|
|
parentGId = parentGId or -1
|
|
return db.groupTree[parentGId]
|
|
end
|
|
|
|
function DBUser.getUsersByGId(groupId)
|
|
return db.userGroupList[groupId]
|
|
end
|
|
|
|
---public 取得拼音字母所在的index
|
|
function DBUser.getUserPyIndex(pyChar)
|
|
return db.userPyIndexs[pyChar]
|
|
end
|
|
|
|
function DBUser.getFilters(group)
|
|
return db.filters[group]
|
|
end
|
|
|
|
function DBUser.getPopList(group)
|
|
return poplist[group]
|
|
end
|
|
|
|
function DBUser.refreshUserInfor(loginNo, loginName)
|
|
local infor = DBUser.getPopList(DBUser.FilterGroup.user)
|
|
if infor and infor.values then
|
|
local count = infor.values.Count
|
|
for i = 0, count - 1 do
|
|
local v = infor.values[i]
|
|
if v == loginNo then
|
|
if loginName == "系统生成" then
|
|
infor.options[i] = joinStr(v, "_", loginName)
|
|
else
|
|
infor.options[i] = loginName
|
|
end
|
|
break
|
|
end
|
|
end
|
|
end
|
|
|
|
local list = DBUser.getFilters(DBUser.FilterGroup.user) or {}
|
|
for i, v in ipairs(list or {}) do
|
|
if v.value == loginNo then
|
|
v.name = loginName
|
|
if loginName == "系统生成" then
|
|
v.name = joinStr(v.value, "_", loginName)
|
|
else
|
|
v.name = loginName
|
|
end
|
|
break
|
|
end
|
|
end
|
|
end
|
|
|
|
---@return _DBUser
|
|
function DBUser.getUserById(loginNo)
|
|
local user = db[loginNo]
|
|
if user == nil then
|
|
printe("get user is nil=", loginNo)
|
|
end
|
|
return user
|
|
end
|
|
|
|
function DBUser.getIcon(loginNo, callback)
|
|
---@type _DBUser
|
|
local user = DBUser.getUserById(loginNo)
|
|
if user == nil or isNilOrEmpty(user.imageUrl) then
|
|
Utl.doCallback(callback, nil)
|
|
return
|
|
end
|
|
if icons[user.imageUrl] then
|
|
Utl.doCallback(callback, icons[user.imageUrl])
|
|
return
|
|
end
|
|
|
|
local callbacks = getIconCallback[user.imageUrl] or {}
|
|
callbacks[callback] = callback
|
|
getIconCallback[user.imageUrl] = callbacks
|
|
|
|
if isDownLoading[user.imageUrl] then
|
|
return
|
|
end
|
|
isDownLoading[user.imageUrl] = true
|
|
WWWEx.get(
|
|
user.imageUrl,
|
|
nil,
|
|
CLAssetType.texture,
|
|
function(content, orgs)
|
|
isDownLoading[user.imageUrl] = nil
|
|
if content then
|
|
content.name = user.imageUrl
|
|
icons[user.imageUrl] = content
|
|
end
|
|
local callbacks = getIconCallback[user.imageUrl]
|
|
for k, callback in pairs(callbacks) do
|
|
Utl.doCallback(callback, content)
|
|
end
|
|
getIconCallback[user.imageUrl] = {}
|
|
end,
|
|
function()
|
|
isDownLoading[user.imageUrl] = nil
|
|
printe("取得头像失败")
|
|
end,
|
|
nil,
|
|
true,
|
|
1
|
|
)
|
|
end
|
|
|
|
---public 是否有权限
|
|
function DBUser.hadPermission(key)
|
|
return rolePermission[key] or false
|
|
end
|
|
|
|
---public 是否白名单
|
|
function DBUser.isWhiteUser(userName, callback)
|
|
if isNilOrEmpty(userName) then
|
|
Utl.doCallback(callback, false)
|
|
return false
|
|
end
|
|
if DBUser.whiteNames == nil then
|
|
local url = Utl.urlAddTimes(joinStr(CLVerManager.self.baseUrl, "/whitList.json"))
|
|
WWWEx.get(
|
|
url,
|
|
CLAssetType.text,
|
|
function(content)
|
|
DBUser.whiteNames = json.decode(content)
|
|
Utl.doCallback(callback, DBUser.whiteNames[userName])
|
|
end,
|
|
function()
|
|
Utl.doCallback(callback, false)
|
|
end,
|
|
nil,
|
|
true,
|
|
1
|
|
)
|
|
return false
|
|
else
|
|
Utl.doCallback(callback, DBUser.whiteNames[userName])
|
|
return DBUser.whiteNames[userName]
|
|
end
|
|
end
|
|
---public 包装我的个人信息
|
|
function DBUser.wrapMyinforData(myinfor)
|
|
local currGroup = Prefs.getCurrGroup(Prefs.getUserName())
|
|
local companyInfro = json.decode(currGroup)
|
|
local user = DBUser.getUserById(companyInfro.login_no)
|
|
myinfor.company_id = companyInfro.company_id
|
|
myinfor.company_name = companyInfro.company_name
|
|
myinfor.phoneNo = Prefs.getUserName()
|
|
if user then
|
|
myinfor.loginNo = user.loginNo
|
|
myinfor.imageUrl = user.imageUrl
|
|
end
|
|
return myinfor
|
|
end
|
|
|
|
function DBUser.onPersonalData(result)
|
|
db.myinfor = result
|
|
local currGroup = Prefs.getCurrGroup(Prefs.getUserName())
|
|
local companyInfro = json.decode(currGroup)
|
|
DBUser.refreshUserInfor(companyInfro.login_no, db.myinfor.loginName)
|
|
local user = DBUser.getUserById(companyInfro.login_no)
|
|
if user then
|
|
-- 刷新数据
|
|
user.loginName = db.myinfor.loginName
|
|
user.imageUrl = db.myinfor.imageUrl
|
|
Prefs.setUserName(db.myinfor.phoneNo)
|
|
end
|
|
db.myinfor = DBUser.wrapMyinforData(db.myinfor)
|
|
end
|
|
|
|
---public 取得我的个人信息
|
|
function DBUser.getMyInfor()
|
|
if db.myinfor == nil then
|
|
db.myinfor = DBUser.wrapMyinforData({})
|
|
NetProto.send.personal_data_query()
|
|
end
|
|
return db.myinfor
|
|
end
|
|
|
|
return DBUser
|