123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- import pjson from '/package.json'
- function guid() {
- function S4() {
- return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
- }
- return (`${S4() + S4() + S4() + S4() + S4() + S4() + S4() + S4()}`)
- }
- function getStorage(name){
- let value = window.localStorage.getItem(pjson.name+'_'+name);
-
- try {
- const temp = JSON.parse(value);
-
- let nowDate = new Date();
- let expireDate = new Date(temp.expire);
- if (temp) {
- if (temp.expire) {
- if (nowDate < expireDate) {
- return temp.token
- } else {
- return null;
- }
- } else {
- return temp
- }
- } else {
- return null;
- }
- } catch(e) {
- return value;
- }
- }
- function setStorage(name, value){
- if (value.expire) {
- value.expire = new Date().getTime() + value.expire * 1000;
- }
- window.localStorage.setItem(pjson.name+'_'+name, JSON.stringify(value));
- }
- //格式化日期
- function formatDate(str){
- let date = new Date(str*1000);
- const year = date.getFullYear();
- const month = date.getMonth() + 1;
- const day = date.getDate();
- return [year, month, day].map(formatNumber).join('-');
- }
- //格式化时间
- function formatTime(str){
- let date = new Date(str*1000);
- const year = date.getFullYear();
- const month = date.getMonth() + 1;
- const day = date.getDate();
- const hour = date.getHours();
- const minute = date.getMinutes();
- const second = date.getSeconds();
- return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':');
- }
- function formatFileTime(){
- let date = new Date();
- const year = date.getFullYear();
- const month = date.getMonth() + 1;
- const day = date.getDate();
- const hour = date.getHours();
- const minute = date.getMinutes();
- const second = date.getSeconds();
- return [year, month, day, hour, minute, second].map(formatNumber).join('-');
- }
- //加0
- function formatNumber(n) {
- n = n.toString();
- return n[1] ? n : '0' + n;
- }
- //验证手机号
- const validateMobile = (rule, value, callback) => {
- if (!value) {
- return callback(new Error('手机号不能为空'));
- } else if (!/^1[3456789]\d{9}$/.test(value)) {
- callback('手机号格式不正确');
- } else {
- callback();
- }
- };
- //验证手机号不返回内容
- const validateMobileNo = (rule, value, callback) => {
- if (!value) {
- return callback(new Error(' '));
- } else if (!/^1[3456789]\d{9}$/.test(value)) {
- callback(' ');
- } else {
- callback();
- }
- };
- function getBase64Image(img) {
- var canvas = document.createElement("canvas");
- canvas.width = img.width;
- canvas.height = img.height;
- var ctx = canvas.getContext("2d");
- ctx.drawImage(img, 0, 0, img.width, img.height);
- var dataURL = canvas.toDataURL("image/png"); // 可选其他值 image/jpeg
- return dataURL;
- }
- function main(src, cb) {
- var image = new Image();
- image.src = src + '?v=' + Math.random(); // 处理缓存
- image.crossOrigin = "*"; // 支持跨域图片
- image.onload = function(){
- var base64 = getBase64Image(image);
- cb && cb(base64);
- }
- }
- // 处理文件大小
- function handleSize(size) {
- let number = '';
- let outStr = '';
- if ((size / 1024 / 1024) < 1) {
- number = (size / 1024).toFixed(2);
- outStr = (size / 1024).toFixed(2) + 'KB';
- } else {
- number = (size / 1024 / 1024).toFixed(2);
- outStr = (size / 1024 / 1024).toFixed(2) + 'MB';
- }
-
- if(isNaN(number)){
- return '-';
- }else{
- return outStr;
- }
- }
- export default{
- guid,
- formatDate,
- formatTime,
- formatFileTime,
- validateMobile,
- validateMobileNo,
- main,
- getStorage,
- setStorage,
- handleSize
- }
|