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.

386 lines
9.6 KiB

1 year ago
  1. const getDateString = date => {
  2. const year = date.getFullYear()
  3. const month = date.getMonth() + 1
  4. const day = date.getDate()
  5. const hour = date.getHours()
  6. const minute = date.getMinutes()
  7. const second = date.getSeconds()
  8. return [year, month, day].map(formatNumber).join('-')
  9. }
  10. const getTimeString = date => {
  11. const hour = date.getHours()
  12. const minute = date.getMinutes()
  13. return [hour, minute].map(formatNumber).join(':')
  14. }
  15. const formatTime = date => {
  16. const year = date.getFullYear()
  17. const month = date.getMonth() + 1
  18. const day = date.getDate()
  19. const hour = date.getHours()
  20. const minute = date.getMinutes()
  21. const second = date.getSeconds()
  22. return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  23. }
  24. const formatDate = date => {
  25. const year = date.getFullYear()%100
  26. const month = date.getMonth() + 1
  27. const day = date.getDate()
  28. const hour = date.getHours()
  29. const minute = date.getMinutes()
  30. return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute].map(formatNumber).join(':')
  31. }
  32. const formatNumber = n => {
  33. n = n.toString()
  34. return n[1] ? n : '0' + n
  35. }
  36. // 显示繁忙提示
  37. var showBusy = text => wx.showToast({
  38. title: text,
  39. icon: 'loading',
  40. duration: 10000
  41. })
  42. // 显示成功提示
  43. var showSuccess = text => wx.showToast({
  44. title: text,
  45. icon: 'success'
  46. })
  47. // 显示失败提示
  48. var showModel = (title, content) => {
  49. wx.hideToast();
  50. wx.showModal({
  51. title,
  52. content: JSON.stringify(content),
  53. showCancel: false
  54. })
  55. }
  56. // ArrayBuffer转16进度字符串示例
  57. const ab2hex = buffer => {
  58. var hexArr = Array.prototype.map.call(
  59. new Uint8Array(buffer),
  60. function(bit) {
  61. return ('00' + bit.toString(16)).slice(-2)
  62. }
  63. )
  64. return hexArr.join('');
  65. }
  66. function isPrime(n) {
  67. if (isNaN(n) || !isFinite(n) || n % 1 || n < 2) return false;
  68. if (n == leastFactor(n)) return true;
  69. return false;
  70. }
  71. // leastFactor(n)
  72. // returns the smallest prime that divides n
  73. // NaN if n is NaN or Infinity
  74. // 0 if n=0
  75. // 1 if n=1, n=-1, or n is not an integer
  76. function leastFactor(n) {
  77. if (isNaN(n) || !isFinite(n)) return NaN;
  78. if (n == 0) return 0;
  79. if (n % 1 || n * n < 2) return 1;
  80. if (n % 2 == 0) return 2;
  81. if (n % 3 == 0) return 3;
  82. if (n % 5 == 0) return 5;
  83. var m = Math.sqrt(n);
  84. for (var i = 7; i <= m; i += 30) {
  85. if (n % i == 0) return i;
  86. if (n % (i + 4) == 0) return i + 4;
  87. if (n % (i + 6) == 0) return i + 6;
  88. if (n % (i + 10) == 0) return i + 10;
  89. if (n % (i + 12) == 0) return i + 12;
  90. if (n % (i + 16) == 0) return i + 16;
  91. if (n % (i + 22) == 0) return i + 22;
  92. if (n % (i + 24) == 0) return i + 24;
  93. }
  94. return n;
  95. }
  96. function randomPrime() {
  97. //取一个8位数的随机数
  98. var random = Math.floor(Math.random() * 90000000 + 10000000);
  99. if (isPrime(random)) return random;
  100. //取rondom最近的一个素数
  101. var index = 0
  102. var prime = 10000019;
  103. while (true) {
  104. index++;
  105. if (isPrime(random + index)) {
  106. prime = random + index;
  107. break;
  108. }
  109. if (isPrime(random - index)) {
  110. prime = random - index;
  111. break;
  112. }
  113. }
  114. return prime;
  115. }
  116. function randomPrime6() {
  117. //取一个6位数的随机数
  118. var random = Math.floor(Math.random() * 900000 + 100000);
  119. if (isPrime(random)) return random;
  120. //取rondom最近的一个素数
  121. var index = 0
  122. var prime = 100003;
  123. while (true) {
  124. index++;
  125. if (isPrime(random + index)) {
  126. prime = random + index;
  127. break;
  128. }
  129. if (isPrime(random - index)) {
  130. prime = random - index;
  131. break;
  132. }
  133. }
  134. return prime;
  135. }
  136. function randomN() {
  137. //return 5
  138. //取一个4~15之间的随机数
  139. var random = Math.floor(Math.random() * 15 + 4);
  140. if (isPrime(random)) return random;
  141. //取rondom最近的一个素数
  142. var index = 0
  143. var prime = 3;
  144. while (true) {
  145. index++;
  146. if (isPrime(random + index)) {
  147. prime = random + index;
  148. break;
  149. }
  150. if (isPrime(random - index)) {
  151. prime = random - index;
  152. break;
  153. }
  154. }
  155. return prime;
  156. }
  157. // Convert a hex string to a byte array
  158. const hexToBytes = hex => {
  159. for (var bytes = [], c = 0; c < hex.length; c += 2)
  160. bytes.push(parseInt(hex.substr(c, 2), 16));
  161. return bytes;
  162. }
  163. // Convert a byte array to a hex string
  164. const bytesToHex = bytes => {
  165. for (var hex = [], i = 0; i < bytes.length; i++) {
  166. hex.push((bytes[i] >>> 4).toString(16));
  167. hex.push((bytes[i] & 0xF).toString(16));
  168. }
  169. return hex.join("");
  170. }
  171. const macToInt = mac => {
  172. var macNew = mac.replace(/:/g, '').substring(4)
  173. //console.log('macNew', macNew)
  174. var high = parseInt(macNew.substr(0, 4), 16)
  175. //console.log('high', high)
  176. var low = parseInt(macNew.substr(4, 4), 16)
  177. //console.log('low', low)
  178. return (high << 16) | low
  179. }
  180. const hexToInt = hex => {
  181. var high = parseInt(hex.substr(0, 4), 16)
  182. //console.log('high', high)
  183. var low = parseInt(hex.substr(4, 4), 16)
  184. //console.log('low', low)
  185. return (high << 16) | low
  186. }
  187. const getLockModel = name => {
  188. if (name == undefined || name == null) return 0
  189. if (name.length == 9) {
  190. return 1
  191. } else {
  192. if (name.indexOf("WSL_A") == 0) {
  193. return 1 + parseInt(name.substr(5, 6))
  194. } else if (name.indexOf("WSL_H") == 0) {
  195. return 10 + parseInt(name.substr(5, 6))
  196. } else if (name.indexOf("WSL_B") == 0) {
  197. return 21 + parseInt(name.substr(5, 6))
  198. } else if (name.indexOf("WSL_N") == 0) {
  199. return 30 + parseInt(name.substr(5, 6))
  200. } else if (name.indexOf("WSL_M") == 0) {
  201. return 40 + parseInt(name.substr(5, 6))
  202. } else if (name.indexOf("WSL_U") == 0) {
  203. return 50 + parseInt(name.substr(5, 6))
  204. } else if (name.indexOf("WSL_J") == 0) {
  205. return 60 + parseInt(name.substr(5, 6))
  206. } else if (name.indexOf("WSL_F") == 0) {
  207. return 70 + parseInt(name.substr(5, 6))
  208. } else if (name.indexOf("WSL_C") == 0) {
  209. return 80 + parseInt(name.substr(5, 6))
  210. } else if (name.indexOf("WSL_O") == 0) {
  211. return 90 + parseInt(name.substr(5, 6))
  212. } else if (name.indexOf("WSL_D") == 0) {
  213. return 100 + parseInt(name.substr(5, 6))
  214. } else if (name.indexOf("WSJ_Q") == 0) {
  215. return 10000 + parseInt(name.substr(5, 6))
  216. } else {
  217. return 0
  218. }
  219. }
  220. }
  221. const isNbLock = model => {
  222. if (model > 30 && model <= 49) {
  223. return true
  224. } else {
  225. return false
  226. }
  227. }
  228. const isUxLock = model => {
  229. if (model > 30 && model <= 59) {
  230. return true
  231. } else {
  232. return false
  233. }
  234. }
  235. const isFxLock = model => {
  236. if (model > 70 && model <= 79) {
  237. return true
  238. } else {
  239. return false
  240. }
  241. }
  242. const getLockType = model => {
  243. if (model > 0 && model <= 9) {
  244. return '蓝牙密码锁'
  245. } else if (model > 10 && model <= 19) {
  246. return '家庭锁系列'
  247. } else if (model > 20 && model <= 29) {
  248. return '酒店密码锁'
  249. } else if (model > 30 && model <= 49 || model > 100 && model <= 109) {
  250. return '蓝牙NB锁'
  251. } else if (model > 50 && model <= 59 || model > 90 && model <= 99) {
  252. return '蓝牙密码锁'
  253. } else if (model > 60 && model <= 69) {
  254. return '蓝牙NB机柜锁'
  255. } else if (model > 70 && model <= 79) {
  256. return '蓝牙指纹锁'
  257. } else if (model > 80 && model <= 89) {
  258. return 'NB指纹锁'
  259. } else if (model > 10000 && model <= 10009) {
  260. return '取电开关'
  261. }
  262. }
  263. const getLockName = (mac, model) => {
  264. var name = mac.replace(/:/g, '_').slice(mac.length - 5, mac.length)
  265. if (model == 1) {
  266. return `WSL_${name}`
  267. } else if (model > 1 && model <= 9) {
  268. return `WSL_A${model % 10-1}_${name}`
  269. } else if (model > 20 && model <= 29) {
  270. return `WSL_B${model % 10-1}_${name}`
  271. } else if (model > 10 && model <= 19) {
  272. return `WSL_H${model % 10}_${name}`
  273. } else if (model > 30 && model <= 39) {
  274. return `WSL_N${model % 10}_${name}`
  275. } else if (model > 40 && model <= 49) {
  276. return `WSL_M${model % 10}_${name}`
  277. } else if (model > 50 && model <= 59) {
  278. return `WSL_U${model % 10}_${name}`
  279. } else if (model > 60 && model <= 69) {
  280. return `WSL_J${model % 10}_${name}`
  281. } else if (model > 70 && model <= 79) {
  282. return `WSL_F${model % 10}_${name}`
  283. } else if (model > 80 && model <= 89) {
  284. return `WSL_C${model % 10}_${name}`
  285. } else if (model > 90 && model <= 99) {
  286. return `WSL_O${model % 10}_${name}`
  287. } else if (model > 100 && model <= 109) {
  288. return `WSL_D${model % 10}_${name}`
  289. } else if (model > 10000 && model <= 10009) {
  290. return `WSJ_Q${model % 10}_${name}`
  291. }
  292. }
  293. //得到标准时区的时间的函数
  294. function getLocalTime(i) {
  295. //参数i为时区值数字,比如北京为东八区则输进8,西5输入-5
  296. if (typeof i !== 'number') return;
  297. var d = new Date();
  298. //得到1970年一月一日到现在的秒数
  299. var len = d.getTime();
  300. //本地时间与GMT时间的时间偏移差
  301. var offset = d.getTimezoneOffset() * 60000;
  302. //得到现在的格林尼治时间
  303. var utcTime = len + offset;
  304. return new Date(utcTime + 3600000 * i);
  305. }
  306. //得到标准时区的时间的函数
  307. function getLocalTimeByZone(d, i) {
  308. //参数i为时区值数字,比如北京为东八区则输进8,西5输入-5
  309. if (typeof i !== 'number') return;
  310. //var d = new Date();
  311. //得到1970年一月一日到现在的秒数
  312. var len = d.getTime();
  313. //本地时间与GMT时间的时间偏移差
  314. var offset = d.getTimezoneOffset() * 60000;
  315. //得到现在的格林尼治时间
  316. var utcTime = len + offset;
  317. return new Date(utcTime + 3600000 * i);
  318. }
  319. module.exports = {
  320. getDateString,
  321. getTimeString,
  322. formatTime,
  323. formatDate,
  324. showBusy,
  325. showSuccess,
  326. showModel,
  327. isPrime,
  328. randomPrime,
  329. randomPrime6,
  330. randomN,
  331. ab2hex,
  332. bytesToHex,
  333. hexToBytes,
  334. macToInt,
  335. hexToInt,
  336. getLockModel,
  337. getLockType,
  338. getLockName,
  339. isUxLock,
  340. isNbLock,
  341. isFxLock,
  342. getLocalTime
  343. }