LUA的一些工具备份

table.unpack遇到的问题

做了个中转的服务, socket+json 传递数据,
通过 {...} 封装不定参数然后 json.encode 传递到其他服务器,
然后其他服务器 json.decode之后再通过 table.unpack 解出之前 {...} 里面的参数,
如果这里是 table 还好,如果这里面是 n 个整型数据的话,后面会多了个 .0
例如, 1 会被转成 1.0,这会带来一些麻烦。
调查了许久,发现不是 json 导致的,而是在 table.unpack 导致的,
所以根据网上找到的方法,重写了一个 unpack 的方法以及一个判断数组的方法,如下:


function util.unpack(t,i)
    i = i or 1
    if t[i] ~= nil then
        if type(t[i]) == "number" and t[i]%1 == 0 then
            t[i] = t[i] >> 0
        end
        return t[i],util.unpack(t,i+1)
    end
end

--检查是否是数组类型
function util.checkArray(a)
    if type(a) ~= "table" then return false end
    local count = 0
    for k,v in pairs(a) do
        if type(k) ~= "number" then return false end
        count = count + 1
    end

    for i=1,count do
        if not a[i] and type(a[i]) ~= "nil" then return false end
    end

    return true
end

身份证检测



local CITIES = {
    [11]="北京",[12]="天津",[13]="河北",[14]="山西",[15]="内蒙古",[21]="辽宁",[22]="吉林",[23]="黑龙江",[31]="上海",
    [32]="江苏",[33]="浙江",[34]="安徽",[35]="福建",[36]="江西",[37]="山东",[41]="河南",[42]="湖北",[43]="湖南",[44]="广东",
    [45]="广西",[46]="海南",[50]="重庆",[51]="四川",[52]="贵州",[53]="云南",[54]="西藏",[61]="陕西",[62]="甘肃",[63]="青海",
    [64]="宁夏",[65]="新疆",[71]="台湾",[81]="香港",[82]="澳门",[91]="国外"} 
local WIEGHTS = {
    [1]=7,[2]=9,[3]=10,[4]=5,[5]=8,[6]=4,[7]=2,[8]=1,[9]=6,[10]=3,[11]=7,[12]=9,[13]=10,[14]=5,[15]=8,[16]=4,[17]=2
}
local MOD = {
    '1','0','a','9','8','7','6','5','4','3','2'
}
local function checkID(sId)
    local iSum=0 
    local info="" 
    local index = 1
    local result
    for a in string.gmatch( sId,"%d+" ) do
        -- print(index, a)
        result = a
        index = index + 1
    end
    -- print("index ",index)
    if index > 2 then return false,"[1]格式非法" end
    if #result < 17 then return false, "[2]格式非法" end
    if #result == 17 and sId:sub(18,18) ~= "x" then  return false, "[3]格式非法"   end
    sId = sId:lower(sId)
    sId = string.gsub(sId,"x","a")
    
    if CITIES[tonumber( string.sub( sId,1,2 ) )] == nil then
        return false,"非法地区"
    end 
    local bYear = tonumber(string.sub(sId,7,10))
    local bMonth = tonumber(string.sub(sId,11,12))
    local bDay = tonumber(string.sub(sId,13,14))
    
    if bMonth <= 0 or bMonth > 12 then return false, "[4]格式非法"   end
    if bDay <= 0 or bDay > 31 then return false, "[5]格式非法"   end
    
    local sBirthday = os.date("*t", os.time{year=bYear,month=bMonth,day=bDay})
    local sToday = os.date("*t", os.time())
    local age = sToday.year-sBirthday.year
    if age < 0 or age > 100 then return false, "[6]格式非法" end
    
    for i = 1,17 do 
        iSum = iSum + tonumber(sId:sub(i,i))*WIEGHTS[i]
    end
    local verifycode = sId:sub(18,18)
    local modecode = MOD[iSum%11 + 1]
    -- print(iSum,iSum%11+1,verifycode,modecode)
    if modecode ~= verifycode then  return false,"[7]格式非法"    end

    return true 
end

手机号检测


string.match(phoneno,"[1][3,4,5,7,8,9]%d%d%d%d%d%d%d%d%d") ~= phoneno

posted @ 2018-10-11 11:56  Ado_On  阅读(254)  评论(0编辑  收藏  举报