util.js 8.0 KB

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