You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

304 lines
9.1 KiB

  1. --合成setter getter 方法
  2. function cc.exports.CC_SYNTHESIZE(__class, __var, __functionName, __value)
  3. __class["get"..__functionName] = function(__fclass)
  4. assert(__fclass~=nil, "方法调用错误,请使用:")
  5. return __fclass[__var]
  6. end
  7. __class["set"..__functionName] = function(__fclass, __varS)
  8. assert(__fclass~=nil, "方法调用错误,请使用:")
  9. __fclass[__var] = __varS
  10. end
  11. __class[__var] = __value --赋初始值
  12. end
  13. function cc.exports.addButtonListener(__sender,__function)
  14. local function onTouchEvent(sender, eventType, touch)
  15. if eventType == ccui.TouchEventType.ended then
  16. if (__function) then
  17. __function(__sender, __sender:getName())
  18. end
  19. end
  20. return true;
  21. end
  22. __sender:setTouchEnabled(true)
  23. __sender:addTouchEventListener(onTouchEvent)
  24. end
  25. function cc.exports.findNodeByName(root, name)
  26. local res = root:getChildByName(name)
  27. if res then
  28. return res
  29. else
  30. local children = root:getChildren()
  31. for _, ch in pairs(children) do
  32. res = findNodeByName(ch, name)
  33. if res then
  34. return res
  35. end
  36. end
  37. end
  38. end
  39. function cc.exports.findNodeByTag(root, tag)
  40. local res = root:getChildByTag(tag)
  41. if res then
  42. return res
  43. else
  44. local children = root:getChildren()
  45. for _, ch in pairs(children) do
  46. res = findNodeByTag(ch, tag)
  47. if res then
  48. return res
  49. end
  50. end
  51. end
  52. end
  53. -- 格式化时间戳
  54. function cc.exports.EapFormatTime( timeStamp, type)
  55. -- os.date("%Y:%m:%d:%H:%M:%S", timeStamp)
  56. type = type or 1
  57. local res = "%Y:%m:%d %H:%M:%S"
  58. if type == 1 then --年月日 时分秒
  59. res = "%Y-%m-%d %H:%M:%S"
  60. elseif type == 2 then --年月日
  61. res = "%Y-%m-%d"
  62. elseif type == 3 then --时分秒
  63. res = "%H:%M:%S"
  64. elseif type == 4 then --时分
  65. res = "%H:%M"
  66. elseif type == 5 then --分秒
  67. res = "%M:%S"
  68. elseif type == 6 then --年月
  69. res = "%Y-%m"
  70. elseif type == 7 then --月日
  71. res = "%m-%d"
  72. elseif type == 8 then --日时分
  73. res = "%d:%H:%M"
  74. elseif type == 9 then --月日 时分
  75. res = "%m-%d %H:%M"
  76. end
  77. return os.date(res, timeStamp)
  78. end
  79. -- 当前时间距离某一时间的倒计时
  80. -- 输出格式 00:00:00
  81. function cc.exports.CDTime( timeStamp )
  82. local curTime = os.time()
  83. local spareTime = timeStamp - curTime
  84. if spareTime < 0 then spareTime = 0 end
  85. local s = spareTime%60
  86. local m = math.floor(spareTime/60)%60
  87. local h = math.floor(spareTime/3600)
  88. return string.format("%02d:%02d:%02d", h, m, s)
  89. end
  90. --倒计时(时)
  91. function cc.exports.GetCountDownTime(endTimeStamp)
  92. local curTime = os.time()
  93. local gap = endTimeStamp - curTime
  94. gap = gap > 0 and gap or 0
  95. local m = math.floor(gap/60)%60
  96. local h = math.floor(gap/3600)%24
  97. local day = math.floor(gap/(24*3600))
  98. -- if gap <= 3600 then
  99. -- return string.format("%d分", m)
  100. -- elseif gap <= 24 * 3600 then
  101. -- return string.format("%d小时%d分", h, m)
  102. -- end
  103. -- return string.format("%d天%d小时%d分", day, h, m)
  104. if gap <= 3600 then
  105. return string.format("00:00:%02d", m)
  106. elseif gap <= 24 * 3600 then
  107. return string.format("00:%02d:%02d", h, m)
  108. end
  109. return string.format("%02d:%02d:%02d", day, h, m)
  110. end
  111. --时间段
  112. function cc.exports.EapGetDuration(__seconds)
  113. __seconds = __seconds > 0 and __seconds or 0
  114. local m = math.floor(__seconds/60)%60
  115. local h = math.floor(__seconds/3600)%24
  116. return string.format("%02d:%02d", h, m)
  117. end
  118. function cc.exports.sendHttpReq(__url, __callback) --http请求,商城
  119. print("sendHttpReq url = ", __url)
  120. local function onUpdate(status , info)
  121. -- status = progress,successed,failed
  122. print("global sendHttpReq = ", tostring(status))
  123. if status ~= "successed" then
  124. __callback(false)
  125. return
  126. end
  127. __callback(true, info)
  128. end
  129. converUrlToIp(__url, function(urlNew, header)
  130. cc.CURLManager:getInstance():createHeaderTask(urlNew ,header, onUpdate , 0 , 0 , 0);
  131. end)
  132. end
  133. --处理中文字符
  134. function cc.exports.urlEncode(s)
  135. if not s or type(s) ~= "string" then
  136. return
  137. end
  138. s = string.gsub(s, "([^%w%.%- ])", function(c) return string.format("%%%02X", string.byte(c)) end)
  139. return string.gsub(s, " ", "+")
  140. end
  141. function cc.exports.getUserImage(__url, __saveName, __imgView, __oriSize, __isClip, __texType) --获取图片
  142. local url = __url
  143. print("getUserImage url = ", url)
  144. if not url or url == "" then
  145. return
  146. end
  147. local picName = "EapPic"..__saveName
  148. getImageFromUrlWithTime(url, picName, nil, function(res)
  149. print("global getUserImage = ", res)
  150. if res then
  151. if __isClip then
  152. createClipImage(picName, __imgView, __oriSize)
  153. else
  154. createUserImage(picName, __imgView, __oriSize, __texType)
  155. end
  156. end
  157. end)
  158. end
  159. function cc.exports.createUserImage(__filepath, __imgView, __oriSize, __texType)
  160. __texType = __texType or 1
  161. if __imgView and not tolua.isnull(__imgView) then
  162. if cc.FileUtils:getInstance():isFileExist(__filepath) then
  163. print("createUserImage isFileExist = ", __filepath, type(__imgView))
  164. local texture = loadTextureFromFile(__filepath, false)
  165. if not texture then
  166. return
  167. end
  168. -- 判断头像节点是否还有效
  169. if tolua.isnull(__imgView) then
  170. return
  171. end
  172. -- 设置头像
  173. if __texType == 1 then
  174. __imgView:setTexture(texture)
  175. else
  176. __imgView:setTexture2(texture)
  177. end
  178. texture = nil
  179. __imgView:setVisible(true)
  180. if __oriSize then
  181. local img_w = __imgView:getSize().width
  182. local img_h = __imgView:getSize().height
  183. print(" createUserImage ", img_w, img_h)
  184. local fixw_r = __oriSize.width/img_w
  185. __imgView:setScale(fixw_r)
  186. end
  187. end
  188. end
  189. end
  190. function cc.exports.createClipImage(__filepath, __imgView, __oriSize)
  191. if __imgView then
  192. if cc.FileUtils:getInstance():isFileExist(__filepath) then
  193. print("createUserImage isFileExist = ", __filepath, type(__imgView))
  194. local texture = loadTextureFromFile(__filepath, true)
  195. if not texture then
  196. return
  197. end
  198. -- 判断头像节点是否还有效
  199. if tolua.isnull(__imgView) then
  200. return
  201. end
  202. -- 设置头像
  203. __imgView:setTexture2(texture)
  204. texture = nil
  205. __imgView:setVisible(true)
  206. if __oriSize then
  207. local img_w = __imgView:getSize().width
  208. local img_h = __imgView:getSize().height
  209. print(" createUserImage ", img_w, img_h)
  210. local fixw_r = __oriSize.width/img_w
  211. __imgView:setScale(fixw_r)
  212. end
  213. end
  214. end
  215. end
  216. function cc.exports.EapEncodeJson (tbl)
  217. local resStr = ''
  218. if tbl and type(tbl) == "table" then
  219. resStr = '{'
  220. for k,v in pairs(tbl) do
  221. local strCell = '"'..k..'"'..':'
  222. if type(v) == "string" then
  223. strCell = strCell..'"'..v..'"'..','
  224. elseif type(v) == "number" then
  225. strCell = strCell..v..','
  226. elseif type(v) == "table" then
  227. end
  228. resStr = resStr..strCell
  229. end
  230. resStr = string.sub(resStr, 1, -2)
  231. resStr = resStr..'}'
  232. end
  233. print("eapEncodeJson = ", resStr)
  234. return resStr
  235. end
  236. function cc.exports.filterSpecChars(s)
  237. if not s then
  238. return
  239. end
  240. local ss = {}
  241. local k = 1
  242. while true do
  243. if k > #s then break end
  244. local c = string.byte(s,k)
  245. if not c then break end
  246. if c<192 then
  247. if (c>=32 and c<=126) then
  248. table.insert(ss, string.char(c))
  249. end
  250. k = k + 1
  251. elseif c<224 then
  252. k = k + 2
  253. elseif c<240 then
  254. if c>=228 and c<=233 then
  255. local c1 = string.byte(s,k+1)
  256. local c2 = string.byte(s,k+2)
  257. if c1 and c2 then
  258. local a1,a2,a3,a4 = 128,191,128,191
  259. if c == 228 then a1 = 184
  260. elseif c == 233 then a2,a4 = 190,c1 ~= 190 and 191 or 165
  261. end
  262. if c1>=a1 and c1<=a2 and c2>=a3 and c2<=a4 then
  263. table.insert(ss, string.char(c,c1,c2))
  264. end
  265. end
  266. end
  267. k = k + 3
  268. elseif c<248 then
  269. k = k + 4
  270. elseif c<252 then
  271. k = k + 5
  272. elseif c<254 then
  273. k = k + 6
  274. end
  275. end
  276. return table.concat(ss)
  277. end