util.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. const formatTime = 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('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  9. }
  10. const formatNumber = n => {
  11. n = n.toString()
  12. return n[1] ? n : '0' + n
  13. }
  14. // 格式化时间// 20180101101010 => 2018-01-01 10:10:10
  15. function formatDate(time) {
  16. return `${time.substring(8, 10)}:${time.substring(10, 12)}`
  17. }
  18. /** 检测是否有定位权限BY 小程序 **/
  19. function checkHasLocationPermissionByMP() {
  20. return new Promise(function (resolve, reject) {
  21. wx.getSetting({
  22. success(sd) {
  23. if (!sd.authSetting['scope.userLocation']) {
  24. wx.authorize({
  25. scope: 'scope.userLocation',
  26. success(e) {
  27. resolve(e)
  28. },
  29. fail(e) {
  30. reject()
  31. }
  32. })
  33. } else {
  34. resolve(sd)
  35. }
  36. }
  37. })
  38. })
  39. }
  40. function picInit(value) {
  41. let baseUrl = "../../static/images/"
  42. value = value || '';
  43. return baseUrl + "room.png"
  44. if (value.startsWith("300") || value.startsWith("310") || value.startsWith("311")) {
  45. return baseUrl + "ic311.png"
  46. } else if (value.startsWith("312")) {
  47. return baseUrl + "ic312.png"
  48. } else if (value.startsWith("313")) {
  49. return baseUrl + "ic313.png"
  50. } else if (value.startsWith("314")) {
  51. return baseUrl + "ic314.png"
  52. } else if (value.startsWith("315")) {
  53. return baseUrl + "ic315.png"
  54. } else if (value.startsWith("320") || value.startsWith("321")) {
  55. return baseUrl + "ic321.png"
  56. } else if (value.startsWith("322")) {
  57. return baseUrl + "ic322.png"
  58. } else if (value.startsWith("323")) {
  59. return baseUrl + "ic323.png"
  60. } else if (value.startsWith("33")) {
  61. return baseUrl + "ic331.png"
  62. } else {
  63. return baseUrl + "ic313.png"
  64. }
  65. }
  66. // 前面补零
  67. function PrefixZero(num, n = 2) {
  68. return (Array(n).join(0) + num).slice(-n);
  69. }
  70. function formatMsgTime(dateStr) {
  71. // let dateObj = dateStr.replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '').replace(/(-)/g, '/')
  72. let targetDate = new Date(dateStr);
  73. let year = targetDate.getFullYear();
  74. let month = targetDate.getMonth() + 1;
  75. let day = targetDate.getDate();
  76. let hour = targetDate.getHours();
  77. let minute = targetDate.getMinutes();
  78. // let second = targetDate.getSeconds();
  79. let nowDate = new Date();
  80. let now_new = Date.parse(nowDate.toString());
  81. let milliseconds = 0;
  82. let timeSpanStr;
  83. milliseconds = now_new - targetDate;
  84. if (milliseconds <= 1000 * 60 * 1) {
  85. timeSpanStr = '刚刚';
  86. }
  87. else if (1000 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60) {
  88. timeSpanStr = Math.round((milliseconds / (1000 * 60))) + '分钟前';
  89. }
  90. else if (1000 * 60 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24) {
  91. timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60)) + '小时前';
  92. }
  93. else if (1000 * 60 * 60 * 24 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24 * 15) {
  94. timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60 * 24)) + '天前';
  95. }
  96. else if (milliseconds > 1000 * 60 * 60 * 24 * 15 && year == nowDate.getFullYear()) {
  97. timeSpanStr = month + '.' + day + ' ' + (hour < 10 ? `0${hour}` : hour) + ':' + (minute < 10 ? `0${minute}` : minute);
  98. } else {
  99. timeSpanStr = year + '-' + month + '-' + day;
  100. }
  101. return timeSpanStr;
  102. }
  103. function buildTree(value) {
  104. let list = JSON.parse(JSON.stringify(value));
  105. // let temp = {};
  106. let tree = [];
  107. list.forEach(item => {
  108. let temp = {};
  109. let children = {};
  110. temp.projectId = item.projectId;
  111. temp.projectName = item.projectName;
  112. temp.children = [];
  113. tree.push(temp)
  114. });
  115. let hash = {};
  116. tree = tree.reduce((item, next) => {
  117. hash[next.projectId] ? '' : hash[next.projectId] = true && item.push(next);
  118. return item;
  119. }, []);
  120. value.forEach((item) => {
  121. tree.forEach(items => {
  122. if (item.projectId === items.projectId) {
  123. // items.children=[];
  124. let children = {};
  125. children.tenantId = item.tenantId;
  126. children.projectId = item.projectId;
  127. children.tenantName = item.tenantName;
  128. children.remote = item.remote;
  129. items.children.push(children);
  130. }
  131. })
  132. })
  133. return tree;
  134. }
  135. // base64解码
  136. var Base64 = {
  137. // 转码表
  138. tables: [
  139. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  140. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  141. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  142. 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
  143. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  144. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  145. 'w', 'x', 'y', 'z', '0', '1', '2', '3',
  146. '4', '5', '6', '7', '8', '9', '+', '/'
  147. ],
  148. UTF16ToUTF8: function (str) {
  149. let results = [], len = str.length;
  150. for (let i = 0; i < len; i++) {
  151. let code = str.charCodeAt(i);
  152. if (code > 0x0000 && code <= 0x007F) {
  153. /* 一字节,不考虑0x0000,因为是空字节
  154. U+00000000 – U+0000007F 0xxxxxxx
  155. */
  156. results.push(str.charAt(i));
  157. } else if (code >= 0x0080 && code <= 0x07FF) {
  158. /* 二字节
  159. U+00000080 – U+000007FF 110xxxxx 10xxxxxx
  160. 110xxxxx
  161. */
  162. let byte1 = 0xC0 | ((code >> 6) & 0x1F);
  163. // 10xxxxxx
  164. let byte2 = 0x80 | (code & 0x3F);
  165. results.push(
  166. String.fromCharCode(byte1),
  167. String.fromCharCode(byte2)
  168. );
  169. } else if (code >= 0x0800 && code <= 0xFFFF) {
  170. /* 三字节
  171. U+00000800 – U+0000FFFF 1110xxxx 10xxxxxx 10xxxxxx
  172. 1110xxxx
  173. */
  174. let byte1 = 0xE0 | ((code >> 12) & 0x0F);
  175. // 10xxxxxx
  176. let byte2 = 0x80 | ((code >> 6) & 0x3F);
  177. // 10xxxxxx
  178. let byte3 = 0x80 | (code & 0x3F);
  179. results.push(
  180. String.fromCharCode(byte1),
  181. String.fromCharCode(byte2),
  182. String.fromCharCode(byte3)
  183. );
  184. } else if (code >= 0x00010000 && code <= 0x001FFFFF) {
  185. // 四字节
  186. // U+00010000 – U+001FFFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  187. } else if (code >= 0x00200000 && code <= 0x03FFFFFF) {
  188. // 五字节
  189. // U+00200000 – U+03FFFFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  190. } else /** if (code >= 0x04000000 && code <= 0x7FFFFFFF)*/ {
  191. // 六字节
  192. // U+04000000 – U+7FFFFFFF 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  193. }
  194. }
  195. return results.join('');
  196. },
  197. UTF8ToUTF16: function (str) {
  198. let results = [], len = str.length;
  199. let i = 0;
  200. for (let i = 0; i < len; i++) {
  201. let code = str.charCodeAt(i);
  202. // 第一字节判断
  203. if (((code >> 7) & 0xFF) == 0x0) {
  204. // 一字节
  205. // 0xxxxxxx
  206. results.push(str.charAt(i));
  207. } else if (((code >> 5) & 0xFF) == 0x6) {
  208. // 二字节
  209. // 110xxxxx 10xxxxxx
  210. let code2 = str.charCodeAt(++i);
  211. let byte1 = (code & 0x1F) << 6;
  212. let byte2 = code2 & 0x3F;
  213. let utf16 = byte1 | byte2;
  214. results.push(Sting.fromCharCode(utf16));
  215. } else if (((code >> 4) & 0xFF) == 0xE) {
  216. // 三字节
  217. // 1110xxxx 10xxxxxx 10xxxxxx
  218. let code2 = str.charCodeAt(++i);
  219. let code3 = str.charCodeAt(++i);
  220. let byte1 = (code << 4) | ((code2 >> 2) & 0x0F);
  221. let byte2 = ((code2 & 0x03) << 6) | (code3 & 0x3F);
  222. let utf16 = ((byte1 & 0x00FF) << 8) | byte2
  223. results.push(String.fromCharCode(utf16));
  224. } else if (((code >> 3) & 0xFF) == 0x1E) {
  225. // 四字节
  226. // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  227. } else if (((code >> 2) & 0xFF) == 0x3E) {
  228. // 五字节
  229. // 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  230. } else /** if (((code >> 1) & 0xFF) == 0x7E)*/ {
  231. // 六字节
  232. // 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  233. }
  234. }
  235. return results.join('');
  236. },
  237. encode: function (str) {
  238. if (!str) {
  239. return '';
  240. }
  241. let utf8 = this.UTF16ToUTF8(str); // 转成UTF-8
  242. let i = 0; // 遍历索引
  243. let len = utf8.length;
  244. let results = [];
  245. while (i < len) {
  246. let c1 = utf8.charCodeAt(i++) & 0xFF;
  247. results.push(this.tables[c1 >> 2]);
  248. // 补2个=
  249. if (i == len) {
  250. results.push(this.tables[(c1 & 0x3) << 4]);
  251. results.push('==');
  252. break;
  253. }
  254. let c2 = utf8.charCodeAt(i++);
  255. // 补1个=
  256. if (i == len) {
  257. results.push(this.tables[((c1 & 0x3) << 4) | ((c2 >> 4) & 0x0F)]);
  258. results.push(this.tables[(c2 & 0x0F) << 2]);
  259. results.push('=');
  260. break;
  261. }
  262. let c3 = utf8.charCodeAt(i++);
  263. results.push(this.tables[((c1 & 0x3) << 4) | ((c2 >> 4) & 0x0F)]);
  264. results.push(this.tables[((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6)]);
  265. results.push(this.tables[c3 & 0x3F]);
  266. }
  267. return results.join('');
  268. },
  269. decode: function (str) {
  270. //判断是否为空
  271. if (!str) {
  272. return '';
  273. }
  274. let len = str.length;
  275. let i = 0;
  276. let results = [];
  277. //循环解出字符数组
  278. while (i < len) {
  279. let code1 = this.tables.indexOf(str.charAt(i++));
  280. let code2 = this.tables.indexOf(str.charAt(i++));
  281. let code3 = this.tables.indexOf(str.charAt(i++));
  282. let code4 = this.tables.indexOf(str.charAt(i++));
  283. let c1 = (code1 << 2) | (code2 >> 4);
  284. results.push(String.fromCharCode(c1));
  285. if (code3 != -1) {
  286. let c2 = ((code2 & 0xF) << 4) | (code3 >> 2);
  287. results.push(String.fromCharCode(c2));
  288. }
  289. if (code4 != -1) {
  290. let c3 = ((code3 & 0x3) << 6) | code4;
  291. results.push(String.fromCharCode(c3));
  292. }
  293. }
  294. return this.UTF8ToUTF16(results.join(''));
  295. }
  296. }
  297. // 解析参数
  298. function getUrlParams(url, key) {
  299. // debugger
  300. // let url = url //获取url中"?"符后的字串
  301. let theRequest = {}
  302. url = "?" + url
  303. if (url.indexOf('?') != -1) {
  304. let str = url.substr(1)
  305. let strs = str.split('&')
  306. for (var i = 0; i < strs.length; i++) {
  307. theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1]
  308. }
  309. }
  310. let value = theRequest[key] || ''
  311. return value
  312. }
  313. module.exports = {
  314. formatTime: formatTime,
  315. formatDate: formatDate,
  316. checkHasLocationPermissionByMP: checkHasLocationPermissionByMP,
  317. PrefixZero: PrefixZero,
  318. buildTree: buildTree,
  319. Base64: Base64,
  320. getUrlParams,
  321. picInit: picInit,
  322. formatMsgTime: formatMsgTime,
  323. }