tool.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * @Descripttion: 工具集
  3. * @version: 1.2
  4. * @LastEditors: sakuya
  5. * @LastEditTime: 2022年5月24日00:28:56
  6. */
  7. // import CryptoJS from 'crypto-js';
  8. const tool = {};
  9. /* localStorage */
  10. tool.data = {
  11. set(key, data, datetime = 0) {
  12. let cacheValue = {
  13. content: data,
  14. datetime: parseInt(datetime) === 0 ? 0 : new Date().getTime() + parseInt(datetime) * 1000,
  15. };
  16. return localStorage.setItem(key, JSON.stringify(cacheValue));
  17. },
  18. get(key) {
  19. try {
  20. const value = JSON.parse(localStorage.getItem(key));
  21. if (value) {
  22. let nowTime = new Date().getTime();
  23. if (nowTime > value.datetime && value.datetime != 0) {
  24. localStorage.removeItem(key);
  25. return null;
  26. }
  27. return value.content;
  28. }
  29. return null;
  30. } catch (err) {
  31. return null;
  32. }
  33. },
  34. remove(key) {
  35. return localStorage.removeItem(key);
  36. },
  37. clear() {
  38. return localStorage.clear();
  39. },
  40. };
  41. /*sessionStorage*/
  42. tool.session = {
  43. set(table, settings) {
  44. var _set = JSON.stringify(settings);
  45. return sessionStorage.setItem(table, _set);
  46. },
  47. get(table) {
  48. var data = sessionStorage.getItem(table);
  49. try {
  50. data = JSON.parse(data);
  51. } catch (err) {
  52. return null;
  53. }
  54. return data;
  55. },
  56. remove(table) {
  57. return sessionStorage.removeItem(table);
  58. },
  59. clear() {
  60. return sessionStorage.clear();
  61. },
  62. };
  63. /*cookie*/
  64. tool.cookie = {
  65. set(name, value, config = {}) {
  66. var cfg = {
  67. expires: null,
  68. path: null,
  69. domain: null,
  70. secure: false,
  71. httpOnly: false,
  72. ...config,
  73. };
  74. var cookieStr = `${name}=${escape(value)}`;
  75. if (cfg.expires) {
  76. var exp = new Date();
  77. exp.setTime(exp.getTime() + parseInt(cfg.expires) * 1000);
  78. cookieStr += `;expires=${exp.toGMTString()}`;
  79. }
  80. if (cfg.path) {
  81. cookieStr += `;path=${cfg.path}`;
  82. }
  83. if (cfg.domain) {
  84. cookieStr += `;domain=${cfg.domain}`;
  85. }
  86. document.cookie = cookieStr;
  87. },
  88. get(name) {
  89. var arr = document.cookie.match(new RegExp('(^| )' + name + '=([^;]*)(;|$)'));
  90. if (arr != null) {
  91. return unescape(arr[2]);
  92. } else {
  93. return null;
  94. }
  95. },
  96. remove(name) {
  97. var exp = new Date();
  98. exp.setTime(exp.getTime() - 1);
  99. document.cookie = `${name}=;expires=${exp.toGMTString()}`;
  100. },
  101. };
  102. /* Fullscreen */
  103. tool.screen = function (element) {
  104. var isFull = !!(document.webkitIsFullScreen || document.mozFullScreen || document.msFullscreenElement || document.fullscreenElement);
  105. if (isFull) {
  106. if (document.exitFullscreen) {
  107. document.exitFullscreen();
  108. } else if (document.msExitFullscreen) {
  109. document.msExitFullscreen();
  110. } else if (document.mozCancelFullScreen) {
  111. document.mozCancelFullScreen();
  112. } else if (document.webkitExitFullscreen) {
  113. document.webkitExitFullscreen();
  114. }
  115. } else {
  116. if (element.requestFullscreen) {
  117. element.requestFullscreen();
  118. } else if (element.msRequestFullscreen) {
  119. element.msRequestFullscreen();
  120. } else if (element.mozRequestFullScreen) {
  121. element.mozRequestFullScreen();
  122. } else if (element.webkitRequestFullscreen) {
  123. element.webkitRequestFullscreen();
  124. }
  125. }
  126. };
  127. /* 复制对象 */
  128. tool.objCopy = function (obj) {
  129. return JSON.parse(JSON.stringify(obj));
  130. };
  131. /* 日期格式化 */
  132. tool.dateFormat = function (date, fmt = 'yyyy-MM-dd hh:mm:ss') {
  133. date = new Date(date);
  134. var o = {
  135. 'M+': date.getMonth() + 1, //月份
  136. 'd+': date.getDate(), //日
  137. 'h+': date.getHours(), //小时
  138. 'm+': date.getMinutes(), //分
  139. 's+': date.getSeconds(), //秒
  140. 'q+': Math.floor((date.getMonth() + 3) / 3), //季度
  141. S: date.getMilliseconds(), //毫秒
  142. };
  143. if (/(y+)/.test(fmt)) {
  144. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  145. }
  146. for (var k in o) {
  147. if (new RegExp('(' + k + ')').test(fmt)) {
  148. fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
  149. }
  150. }
  151. return fmt;
  152. };
  153. /* 千分符 */
  154. tool.groupSeparator = function (num) {
  155. num = num + '';
  156. if (!num.includes('.')) {
  157. num += '.';
  158. }
  159. return num
  160. .replace(/(\d)(?=(\d{3})+\.)/g, function ($0, $1) {
  161. return $1 + ',';
  162. })
  163. .replace(/\.$/, '');
  164. };
  165. // /* 常用加解密 */
  166. // tool.crypto = {
  167. // //MD5加密
  168. // MD5(data){
  169. // return CryptoJS.MD5(data).toString()
  170. // },
  171. // //BASE64加解密
  172. // BASE64: {
  173. // encrypt(data){
  174. // return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(data))
  175. // },
  176. // decrypt(cipher){
  177. // return CryptoJS.enc.Base64.parse(cipher).toString(CryptoJS.enc.Utf8)
  178. // }
  179. // },
  180. // //AES加解密
  181. // AES: {
  182. // encrypt(data, secretKey, config={}){
  183. // if(secretKey.length % 8 != 0){
  184. // console.warn("[SCUI error]: 秘钥长度需为8的倍数,否则解密将会失败。")
  185. // }
  186. // const result = CryptoJS.AES.encrypt(data, CryptoJS.enc.Utf8.parse(secretKey), {
  187. // iv: CryptoJS.enc.Utf8.parse(config.iv || ""),
  188. // mode: CryptoJS.mode[config.mode || "ECB"],
  189. // padding: CryptoJS.pad[config.padding || "Pkcs7"]
  190. // })
  191. // return result.toString()
  192. // },
  193. // decrypt(cipher, secretKey, config={}){
  194. // const result = CryptoJS.AES.decrypt(cipher, CryptoJS.enc.Utf8.parse(secretKey), {
  195. // iv: CryptoJS.enc.Utf8.parse(config.iv || ""),
  196. // mode: CryptoJS.mode[config.mode || "ECB"],
  197. // padding: CryptoJS.pad[config.padding || "Pkcs7"]
  198. // })
  199. // return CryptoJS.enc.Utf8.stringify(result);
  200. // }
  201. // }
  202. // }
  203. // 查找树
  204. tool.treeFind = (tree, func) => {
  205. for (const data of tree) {
  206. if (func(data)) return data;
  207. if (data.children) {
  208. const res = tool.treeFind(data.children, func);
  209. if (res) return res;
  210. }
  211. }
  212. return null;
  213. };
  214. export default tool;