--合成setter getter 方法 function cc.exports.CC_SYNTHESIZE(__class, __var, __functionName, __value) __class["get"..__functionName] = function(__fclass) assert(__fclass~=nil, "方法调用错误,请使用:") return __fclass[__var] end __class["set"..__functionName] = function(__fclass, __varS) assert(__fclass~=nil, "方法调用错误,请使用:") __fclass[__var] = __varS end __class[__var] = __value --赋初始值 end function cc.exports.addButtonListener(__sender,__function) local function onTouchEvent(sender, eventType, touch) if eventType == ccui.TouchEventType.ended then if (__function) then __function(__sender, __sender:getName()) end end return true; end __sender:setTouchEnabled(true) __sender:addTouchEventListener(onTouchEvent) end function cc.exports.findNodeByName(root, name) local res = root:getChildByName(name) if res then return res else local children = root:getChildren() for _, ch in pairs(children) do res = findNodeByName(ch, name) if res then return res end end end end function cc.exports.findNodeByTag(root, tag) local res = root:getChildByTag(tag) if res then return res else local children = root:getChildren() for _, ch in pairs(children) do res = findNodeByTag(ch, tag) if res then return res end end end end -- 格式化时间戳 function cc.exports.EapFormatTime( timeStamp, type) -- os.date("%Y:%m:%d:%H:%M:%S", timeStamp) type = type or 1 local res = "%Y:%m:%d %H:%M:%S" if type == 1 then --年月日 时分秒 res = "%Y-%m-%d %H:%M:%S" elseif type == 2 then --年月日 res = "%Y-%m-%d" elseif type == 3 then --时分秒 res = "%H:%M:%S" elseif type == 4 then --时分 res = "%H:%M" elseif type == 5 then --分秒 res = "%M:%S" elseif type == 6 then --年月 res = "%Y-%m" elseif type == 7 then --月日 res = "%m-%d" elseif type == 8 then --日时分 res = "%d:%H:%M" elseif type == 9 then --月日 时分 res = "%m-%d %H:%M" end return os.date(res, timeStamp) end -- 当前时间距离某一时间的倒计时 -- 输出格式 00:00:00 function cc.exports.CDTime( timeStamp ) local curTime = os.time() local spareTime = timeStamp - curTime if spareTime < 0 then spareTime = 0 end local s = spareTime%60 local m = math.floor(spareTime/60)%60 local h = math.floor(spareTime/3600) return string.format("%02d:%02d:%02d", h, m, s) end --倒计时(时) function cc.exports.GetCountDownTime(endTimeStamp) local curTime = os.time() local gap = endTimeStamp - curTime gap = gap > 0 and gap or 0 local m = math.floor(gap/60)%60 local h = math.floor(gap/3600)%24 local day = math.floor(gap/(24*3600)) -- if gap <= 3600 then -- return string.format("%d分", m) -- elseif gap <= 24 * 3600 then -- return string.format("%d小时%d分", h, m) -- end -- return string.format("%d天%d小时%d分", day, h, m) if gap <= 3600 then return string.format("00:00:%02d", m) elseif gap <= 24 * 3600 then return string.format("00:%02d:%02d", h, m) end return string.format("%02d:%02d:%02d", day, h, m) end --时间段 function cc.exports.EapGetDuration(__seconds) __seconds = __seconds > 0 and __seconds or 0 local m = math.floor(__seconds/60)%60 local h = math.floor(__seconds/3600)%24 return string.format("%02d:%02d", h, m) end function cc.exports.sendHttpReq(__url, __callback) --http请求,商城 print("sendHttpReq url = ", __url) local function onUpdate(status , info) -- status = progress,successed,failed print("global sendHttpReq = ", tostring(status)) if status ~= "successed" then __callback(false) return end __callback(true, info) end converUrlToIp(__url, function(urlNew, header) cc.CURLManager:getInstance():createHeaderTask(urlNew ,header, onUpdate , 0 , 0 , 0); end) end --处理中文字符 function cc.exports.urlEncode(s) if not s or type(s) ~= "string" then return end s = string.gsub(s, "([^%w%.%- ])", function(c) return string.format("%%%02X", string.byte(c)) end) return string.gsub(s, " ", "+") end function cc.exports.getUserImage(__url, __saveName, __imgView, __oriSize, __isClip, __texType) --获取图片 local url = __url print("getUserImage url = ", url) if not url or url == "" then return end local picName = "EapPic"..__saveName getImageFromUrlWithTime(url, picName, nil, function(res) print("global getUserImage = ", res) if res then if __isClip then createClipImage(picName, __imgView, __oriSize) else createUserImage(picName, __imgView, __oriSize, __texType) end end end) end function cc.exports.createUserImage(__filepath, __imgView, __oriSize, __texType) __texType = __texType or 1 if __imgView and not tolua.isnull(__imgView) then if cc.FileUtils:getInstance():isFileExist(__filepath) then print("createUserImage isFileExist = ", __filepath, type(__imgView)) local texture = loadTextureFromFile(__filepath, false) if not texture then return end -- 判断头像节点是否还有效 if tolua.isnull(__imgView) then return end -- 设置头像 if __texType == 1 then __imgView:setTexture(texture) else __imgView:setTexture2(texture) end texture = nil __imgView:setVisible(true) if __oriSize then local img_w = __imgView:getSize().width local img_h = __imgView:getSize().height print(" createUserImage ", img_w, img_h) local fixw_r = __oriSize.width/img_w __imgView:setScale(fixw_r) end end end end function cc.exports.createClipImage(__filepath, __imgView, __oriSize) if __imgView then if cc.FileUtils:getInstance():isFileExist(__filepath) then print("createUserImage isFileExist = ", __filepath, type(__imgView)) local texture = loadTextureFromFile(__filepath, true) if not texture then return end -- 判断头像节点是否还有效 if tolua.isnull(__imgView) then return end -- 设置头像 __imgView:setTexture2(texture) texture = nil __imgView:setVisible(true) if __oriSize then local img_w = __imgView:getSize().width local img_h = __imgView:getSize().height print(" createUserImage ", img_w, img_h) local fixw_r = __oriSize.width/img_w __imgView:setScale(fixw_r) end end end end function cc.exports.EapEncodeJson (tbl) local resStr = '' if tbl and type(tbl) == "table" then resStr = '{' for k,v in pairs(tbl) do local strCell = '"'..k..'"'..':' if type(v) == "string" then strCell = strCell..'"'..v..'"'..',' elseif type(v) == "number" then strCell = strCell..v..',' elseif type(v) == "table" then end resStr = resStr..strCell end resStr = string.sub(resStr, 1, -2) resStr = resStr..'}' end print("eapEncodeJson = ", resStr) return resStr end function cc.exports.filterSpecChars(s) if not s then return end local ss = {} local k = 1 while true do if k > #s then break end local c = string.byte(s,k) if not c then break end if c<192 then if (c>=32 and c<=126) then table.insert(ss, string.char(c)) end k = k + 1 elseif c<224 then k = k + 2 elseif c<240 then if c>=228 and c<=233 then local c1 = string.byte(s,k+1) local c2 = string.byte(s,k+2) if c1 and c2 then local a1,a2,a3,a4 = 128,191,128,191 if c == 228 then a1 = 184 elseif c == 233 then a2,a4 = 190,c1 ~= 190 and 191 or 165 end if c1>=a1 and c1<=a2 and c2>=a3 and c2<=a4 then table.insert(ss, string.char(c,c1,c2)) end end end k = k + 3 elseif c<248 then k = k + 4 elseif c<252 then k = k + 5 elseif c<254 then k = k + 6 end end return table.concat(ss) end