util.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 buildTree(value) {
  71. let list = JSON.parse(JSON.stringify(value));
  72. // let temp = {};
  73. let tree = [];
  74. list.forEach(item => {
  75. let temp = {};
  76. let children = {};
  77. temp.projectId = item.projectId;
  78. temp.projectName = item.projectName;
  79. temp.children = [];
  80. tree.push(temp)
  81. });
  82. let hash = {};
  83. tree = tree.reduce((item, next) => {
  84. hash[next.projectId] ? '' : hash[next.projectId] = true && item.push(next);
  85. return item;
  86. }, []);
  87. value.forEach((item) => {
  88. tree.forEach(items => {
  89. if (item.projectId === items.projectId) {
  90. // items.children=[];
  91. let children = {};
  92. children.tenantId = item.tenantId;
  93. children.projectId = item.projectId;
  94. children.tenantName = item.tenantName;
  95. children.remote = item.remote;
  96. items.children.push(children);
  97. }
  98. })
  99. })
  100. return tree;
  101. }
  102. // base64解码
  103. var Base64 = {
  104. // 转码表
  105. tables: [
  106. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  107. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  108. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  109. 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
  110. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  111. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  112. 'w', 'x', 'y', 'z', '0', '1', '2', '3',
  113. '4', '5', '6', '7', '8', '9', '+', '/'
  114. ],
  115. UTF16ToUTF8 : function (str) {
  116. let results = [], len = str.length;
  117. for (let i = 0; i < len; i++) {
  118. let code = str.charCodeAt(i);
  119. if (code > 0x0000 && code <= 0x007F) {
  120. /* 一字节,不考虑0x0000,因为是空字节
  121. U+00000000 – U+0000007F 0xxxxxxx
  122. */
  123. results.push(str.charAt(i));
  124. } else if (code >= 0x0080 && code <= 0x07FF) {
  125. /* 二字节
  126. U+00000080 – U+000007FF 110xxxxx 10xxxxxx
  127. 110xxxxx
  128. */
  129. let byte1 = 0xC0 | ((code >> 6) & 0x1F);
  130. // 10xxxxxx
  131. let byte2 = 0x80 | (code & 0x3F);
  132. results.push(
  133. String.fromCharCode(byte1),
  134. String.fromCharCode(byte2)
  135. );
  136. } else if (code >= 0x0800 && code <= 0xFFFF) {
  137. /* 三字节
  138. U+00000800 – U+0000FFFF 1110xxxx 10xxxxxx 10xxxxxx
  139. 1110xxxx
  140. */
  141. let byte1 = 0xE0 | ((code >> 12) & 0x0F);
  142. // 10xxxxxx
  143. let byte2 = 0x80 | ((code >> 6) & 0x3F);
  144. // 10xxxxxx
  145. let byte3 = 0x80 | (code & 0x3F);
  146. results.push(
  147. String.fromCharCode(byte1),
  148. String.fromCharCode(byte2),
  149. String.fromCharCode(byte3)
  150. );
  151. } else if (code >= 0x00010000 && code <= 0x001FFFFF) {
  152. // 四字节
  153. // U+00010000 – U+001FFFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  154. } else if (code >= 0x00200000 && code <= 0x03FFFFFF) {
  155. // 五字节
  156. // U+00200000 – U+03FFFFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  157. } else /** if (code >= 0x04000000 && code <= 0x7FFFFFFF)*/ {
  158. // 六字节
  159. // U+04000000 – U+7FFFFFFF 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  160. }
  161. }
  162. return results.join('');
  163. },
  164. UTF8ToUTF16 : function (str) {
  165. let results = [], len = str.length;
  166. let i = 0;
  167. for (let i = 0; i < len; i++) {
  168. let code = str.charCodeAt(i);
  169. // 第一字节判断
  170. if (((code >> 7) & 0xFF) == 0x0) {
  171. // 一字节
  172. // 0xxxxxxx
  173. results.push(str.charAt(i));
  174. } else if (((code >> 5) & 0xFF) == 0x6) {
  175. // 二字节
  176. // 110xxxxx 10xxxxxx
  177. let code2 = str.charCodeAt(++i);
  178. let byte1 = (code & 0x1F) << 6;
  179. let byte2 = code2 & 0x3F;
  180. let utf16 = byte1 | byte2;
  181. results.push(Sting.fromCharCode(utf16));
  182. } else if (((code >> 4) & 0xFF) == 0xE) {
  183. // 三字节
  184. // 1110xxxx 10xxxxxx 10xxxxxx
  185. let code2 = str.charCodeAt(++i);
  186. let code3 = str.charCodeAt(++i);
  187. let byte1 = (code << 4) | ((code2 >> 2) & 0x0F);
  188. let byte2 = ((code2 & 0x03) << 6) | (code3 & 0x3F);
  189. let utf16 = ((byte1 & 0x00FF) << 8) | byte2
  190. results.push(String.fromCharCode(utf16));
  191. } else if (((code >> 3) & 0xFF) == 0x1E) {
  192. // 四字节
  193. // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  194. } else if (((code >> 2) & 0xFF) == 0x3E) {
  195. // 五字节
  196. // 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  197. } else /** if (((code >> 1) & 0xFF) == 0x7E)*/ {
  198. // 六字节
  199. // 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  200. }
  201. }
  202. return results.join('');
  203. },
  204. encode : function (str) {
  205. if (!str) {
  206. return '';
  207. }
  208. let utf8 = this.UTF16ToUTF8(str); // 转成UTF-8
  209. let i = 0; // 遍历索引
  210. let len = utf8.length;
  211. let results = [];
  212. while (i < len) {
  213. let c1 = utf8.charCodeAt(i++) & 0xFF;
  214. results.push(this.tables[c1 >> 2]);
  215. // 补2个=
  216. if (i == len) {
  217. results.push(this.tables[(c1 & 0x3) << 4]);
  218. results.push('==');
  219. break;
  220. }
  221. let c2 = utf8.charCodeAt(i++);
  222. // 补1个=
  223. if (i == len) {
  224. results.push(this.tables[((c1 & 0x3) << 4) | ((c2 >> 4) & 0x0F)]);
  225. results.push(this.tables[(c2 & 0x0F) << 2]);
  226. results.push('=');
  227. break;
  228. }
  229. let c3 = utf8.charCodeAt(i++);
  230. results.push(this.tables[((c1 & 0x3) << 4) | ((c2 >> 4) & 0x0F)]);
  231. results.push(this.tables[((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6)]);
  232. results.push(this.tables[c3 & 0x3F]);
  233. }
  234. return results.join('');
  235. },
  236. decode: function (str) {
  237. //判断是否为空
  238. if (!str) {
  239. return '';
  240. }
  241. let len = str.length;
  242. let i = 0;
  243. let results = [];
  244. //循环解出字符数组
  245. while (i < len) {
  246. let code1 = this.tables.indexOf(str.charAt(i++));
  247. let code2 = this.tables.indexOf(str.charAt(i++));
  248. let code3 = this.tables.indexOf(str.charAt(i++));
  249. let code4 = this.tables.indexOf(str.charAt(i++));
  250. let c1 = (code1 << 2) | (code2 >> 4);
  251. results.push(String.fromCharCode(c1));
  252. if (code3 != -1) {
  253. let c2 = ((code2 & 0xF) << 4) | (code3 >> 2);
  254. results.push(String.fromCharCode(c2));
  255. }
  256. if (code4 != -1) {
  257. let c3 = ((code3 & 0x3) << 6) | code4;
  258. results.push(String.fromCharCode(c3));
  259. }
  260. }
  261. return this.UTF8ToUTF16(results.join(''));
  262. }
  263. }
  264. module.exports = {
  265. formatTime: formatTime,
  266. formatDate: formatDate,
  267. checkHasLocationPermissionByMP: checkHasLocationPermissionByMP,
  268. PrefixZero: PrefixZero,
  269. buildTree: buildTree,
  270. Base64:Base64,
  271. picInit:picInit
  272. }