utils.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import pjson from '/package.json'
  2. function guid() {
  3. function S4() {
  4. return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
  5. }
  6. return (`${S4() + S4() + S4() + S4() + S4() + S4() + S4() + S4()}`)
  7. }
  8. function getStorage(name){
  9. let value = window.localStorage.getItem(pjson.name+'_'+name);
  10. try {
  11. const temp = JSON.parse(value);
  12. let nowDate = new Date();
  13. let expireDate = new Date(temp.expire);
  14. if (temp) {
  15. if (temp.expire) {
  16. if (nowDate < expireDate) {
  17. return temp.token
  18. } else {
  19. return null;
  20. }
  21. } else {
  22. return temp
  23. }
  24. } else {
  25. return null;
  26. }
  27. } catch(e) {
  28. return value;
  29. }
  30. }
  31. function setStorage(name, value){
  32. if (value.expire) {
  33. value.expire = new Date().getTime() + value.expire * 1000;
  34. }
  35. window.localStorage.setItem(pjson.name+'_'+name, JSON.stringify(value));
  36. }
  37. //格式化日期
  38. function formatDate(str){
  39. let date = new Date(str*1000);
  40. const year = date.getFullYear();
  41. const month = date.getMonth() + 1;
  42. const day = date.getDate();
  43. return [year, month, day].map(formatNumber).join('-');
  44. }
  45. //格式化时间
  46. function formatTime(str){
  47. let date = new Date(str*1000);
  48. const year = date.getFullYear();
  49. const month = date.getMonth() + 1;
  50. const day = date.getDate();
  51. const hour = date.getHours();
  52. const minute = date.getMinutes();
  53. const second = date.getSeconds();
  54. return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':');
  55. }
  56. function formatFileTime(){
  57. let date = new Date();
  58. const year = date.getFullYear();
  59. const month = date.getMonth() + 1;
  60. const day = date.getDate();
  61. const hour = date.getHours();
  62. const minute = date.getMinutes();
  63. const second = date.getSeconds();
  64. return [year, month, day, hour, minute, second].map(formatNumber).join('-');
  65. }
  66. //加0
  67. function formatNumber(n) {
  68. n = n.toString();
  69. return n[1] ? n : '0' + n;
  70. }
  71. //验证手机号
  72. const validateMobile = (rule, value, callback) => {
  73. if (!value) {
  74. return callback(new Error('手机号不能为空'));
  75. } else if (!/^1[3456789]\d{9}$/.test(value)) {
  76. callback('手机号格式不正确');
  77. } else {
  78. callback();
  79. }
  80. };
  81. //验证手机号不返回内容
  82. const validateMobileNo = (rule, value, callback) => {
  83. if (!value) {
  84. return callback(new Error(' '));
  85. } else if (!/^1[3456789]\d{9}$/.test(value)) {
  86. callback(' ');
  87. } else {
  88. callback();
  89. }
  90. };
  91. function getBase64Image(img) {
  92. var canvas = document.createElement("canvas");
  93. canvas.width = img.width;
  94. canvas.height = img.height;
  95. var ctx = canvas.getContext("2d");
  96. ctx.drawImage(img, 0, 0, img.width, img.height);
  97. var dataURL = canvas.toDataURL("image/png"); // 可选其他值 image/jpeg
  98. return dataURL;
  99. }
  100. function main(src, cb) {
  101. var image = new Image();
  102. image.src = src + '?v=' + Math.random(); // 处理缓存
  103. image.crossOrigin = "*"; // 支持跨域图片
  104. image.onload = function(){
  105. var base64 = getBase64Image(image);
  106. cb && cb(base64);
  107. }
  108. }
  109. // 处理文件大小
  110. function handleSize(size) {
  111. let number = '';
  112. let outStr = '';
  113. if ((size / 1024 / 1024) < 1) {
  114. number = (size / 1024).toFixed(2);
  115. outStr = (size / 1024).toFixed(2) + 'KB';
  116. } else {
  117. number = (size / 1024 / 1024).toFixed(2);
  118. outStr = (size / 1024 / 1024).toFixed(2) + 'MB';
  119. }
  120. if(isNaN(number)){
  121. return '-';
  122. }else{
  123. return outStr;
  124. }
  125. }
  126. export default{
  127. guid,
  128. formatDate,
  129. formatTime,
  130. formatFileTime,
  131. validateMobile,
  132. validateMobileNo,
  133. main,
  134. getStorage,
  135. setStorage,
  136. handleSize
  137. }