最近搜索

js工具类。

浏览:473
管理员 2022-10-21 02:21



js工具类


/**
 *  var time_str = getTime();
    console.log(time_str);
 *   2021-04-16 00:28:33
 */
function getTime(){
  var date = new Date();
  var year = date.getFullYear();
  var month = (date.getMonth() + 1).toString().padStart(2, "0");
  var day = date.getDate().toString().padStart(2, "0");
  var hour = date.getHours().toString().padStart(2, "0");
  var minute = date.getMinutes().toString().padStart(2, "0");
  var second = date.getSeconds().toString().padStart(2, "0");
  var currentTime = `${year}-${month}-${day} ${hour}:${minute}:${second}`;
  return currentTime;
}

function getCurrDate(){
    //返回当前时间  2022-01-05   有01 05
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth()+1;
    var day = date.getDate();
    var hour = date.getHours();
    var minute = date.getMinutes();
    var seconds = date.getSeconds();
    if(month>=1&&month<=9){
        month="0"+month;
    }
    if(day>=1&&day<=9){
        day = "0"+day;
    }
    if(hour>=1&&hour<=9){
        hour = "0"+hour;
    }
    if(minute>=1&&minute<=9){
        minute = "0"+minute;
    }
    if(seconds>=1&&seconds<=9){
        seconds = "0"+seconds;
    }
    var time =  year + "-" + month + "-" + day  ;
    return time;
}

function  getCurrDate2(){
    //返回当前时间  2022-1-5   没有01 05
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth()+1;
    var day = date.getDate();
    var hour = date.getHours();
    var minute = date.getMinutes();
    var seconds = date.getSeconds();
    var time =  year + "-" + month + "-" + day  ;
    return time;
}

function  dateDiff(date1,date2){
    //接受日期1 日期2 格式是 2022-01-05  2022-01-08
    //日期1 到日期2 相差几天。3天  返回3   还有可能返回 -10 这是超过10天了
    var k1 = new Date(date1);
    var k2 = new Date(date2);
    var days     = k2.getTime()-k1.getTime();
    var times = parseInt(days/(1000*60*60*24));
    return times;
}

//在字符串原型上添加方法也可  去掉所有空格 使用方法  num= num.trim();
String.prototype.trim = function()
{
    return this.replace(/\s+/g,"");
}
//在字符串原型上添加方法也可  去掉首 尾空格 使用方法  num= num.trim_two();
String.prototype.trim_shou_wei = function()
{
    return this.replace(/^\s+|\s+$/g,'');
}

/**
 * 检测是不是浮点小数或者是整数。
 * 整数是0也可以
 * 浮点是0.0也可以
 * 返回true是浮点小数
 * 返回 false不是浮点小数
 * if(test_float_num(orderNo)){
        }else{
           layer.msg("请输入数字");
            return;
        }
 */
function test_float_num(num){
    var reg=/(^\d{1,11}$)|(^[0-9]\d{0,11}[\.]{1}\d{0,11}$)/;
    if(reg.test(num)){
        return true;
    }else{
        return false;
    }
}

/**
 * 检测是不是整数
 * 第1位必须不是0的数字
 * 返回true是整数   返回 false不是整数
 * if(test_num(orderNo)){
        }else{
           layer.msg("请输入数字");
            return;
        }
 */
function test_num(num){
    var num_reg = /^[1-9]\d{0,9}$/;
    if(num_reg.test(num)){
        return true;
    }else{
        return false;
    }
}
/**
 *检测是不是整数
 * 第一位可以是0   05 01 06
 */
function test_num2(num){
    var num_reg = /^\d{1,9}$/;
    if(num_reg.test(num)){
        return true;
    }else{
        return false;
    }
}


function date_add_num(num) {
    //当前日期 加上num天。 返回格式是2022-10-05  ,num可以-1天。相当于-1天
    var dateTime = new Date();
    dateTime = dateTime.setDate(dateTime.getDate() + parseInt(num));
    dateTime = new Date(dateTime);
    var year = dateTime.getFullYear();
    var month = dateTime.getMonth() + 1;
    var day = dateTime.getDate();

    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (day >= 1 && day <= 9) {
        day = "0" + day;
    }
    return year + "-" + month + "-" + day;
}




/**
 小程序 方法。
 * 接受数组 和 字符串
 * 判断数组中有没有这些字符串, 有没有包含这些字符串
 * 字符串使用逗号分割。
 * 接受的数组和字符串 注意是字符类型,不能是数字。
 * 使用方法:
 * var state_arr = "1,2,3".split(',');
 var str = "11,5";
 console.log(contain(state_arr,str));
 *
 */
  function contain(ary,str) {
    var success = true;//默认有这些值。
    var temp_arr = str.split(',');
    temp_arr.forEach((item)=>{
        if(ary.includes(item)==false){
        success=false;
    }
})
    return success;
}


// 数组去重
function unique(ary) {
    let newAry = [];
    for (let i = 0; i<ary.length; i++) {
        if (newAry.indexOf(ary[i]) === -1) {
            newAry.push(ary[i]);
        }
    }
    return newAry;
}

//删除数组 指定元素。
//  removeByValue(select_drawing_id_array,drawingid);
function removeByValue(arr,val){
    for(var i = 0; i < arr.length; i++) {
        if(arr[i] == val) {
            arr.splice(i, 1);
            break;
        }
    }
}


/**
 * 判断 是否是空
 * @param b
 * @returns {boolean}
 */
function isEmpty(b) {
    if (b == null || b.trim() == "" || b == undefined || b == "undefined") {
        return true
    }
    return false
}

function isNotEmpty(b) {
    return !isEmpty(b);
}

/**
 * 返回随机数
 * c=1 b = 10
 * 返回 1到10之间的随机数 包含1也包含10
 */
function random(c, b) {
    return Math.floor((Math.random() * b) + c)
}

/**
 * 苹果输入   判断移动设备
 *  ["iPhone", "iPhone", index: 13, input: "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac… Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1", groups: undefined]
 * 三星输出
 * ["Android", "Android", index: 20, input: "Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LR… Gecko) Chrome/86.0.4240.198 Mobile Safari/537.36", groups: undefined]
 * ipad输出
 * ["iPad", "iPad", index: 13, input: "Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) Appl… Gecko) Version/11.0 Mobile/15A5341f Safari/604.1", groups: undefined]
 * moto g4
 *  ["Android", "Android", index: 20, input: "Mozilla/5.0 (Linux; Android 6.0.1; Moto G (4)) App… Gecko) Chrome/86.0.4240.198 Mobile Safari/537.36", groups: undefine
 *
 * 电脑输出 null
 */
 function isMobile() {
    return navigator.userAgent.match(/(Android|iPhone|SymbianOS|Windows Phone|iPad|iPod)/i)
}



/**
 *
 *   测试
 const email1 = 'example@email.com';
 const email2 = 'invalid_email.com';

 console.log(validateEmail(email1)); // true
 console.log(validateEmail(email2)); // false
 *
 * @param email
 * @returns {boolean}
 */
function validateEmail(email) {
    const emailPattern = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
    return emailPattern.test(email);
}




/** 
 * var num1 = 12.55;
 var num2 = 125235.55;
 console.log(convertToChineseNumber(num1)); // 十二元五角五分
 console.log(convertToChineseNumber(num2)); // 十二万五千二百三十五元五角五分

 * @param num
 * @returns {string}
 */
function convertToChineseNumber(num) {
    var fraction = ['角', '分'];
    var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
    var unit = [['元', '万', '亿'], ['', '拾', '佰', '仟']];
    var head = num < 0 ? '负' : '';
    num = Math.abs(num);

    var s = '';

    for (var i = 0; i < fraction.length; i++) {
        s += (digit[Math.floor(num * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
    }

    s = s || '整';
    num = Math.floor(num);

    for (var i = 0; i < unit[0].length && num > 0; i++) {
        var p = '';
        for (var j = 0; j < unit[1].length && num > 0; j++) {
            p = digit[num % 10] + unit[1][j] + p;
            num = Math.floor(num / 10);
        }
        s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
    }

    return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整');
}


小程序工具类。


import {
  baseURL
} from './config.js'

export function request(options) {
  //使用promise最大的好处是  防止出现回调地狱
  return new Promise((resolve, reject) => {
    wx.request({
      url: options.url,
      method: options.method || 'get',
      header: options.header || {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: options.data || {},
      success: function (res) {
        resolve(res)
      },
      fail: function (err) {
        reject(err)
      }
    })
  })
}

//返回 res   res.data.session_key  res.data.openid
export function wx_login() {
  return new Promise((resolve, reject) => {
    wx.login({
      success: (res) => {
        console.log(res)
        if (res.code) {
          wx.request({
            url: baseURL + '/api/xcx/login',
            method: 'post',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              code: res.code
            },
            success: function (res) {
              console.log(res)
              //console.log(res.data.openid)
              //res.data.session_key
              //resolve(res.data.openid)
              resolve(res)
            },
            fail: function (err) {
              reject(err)
            }
          })
        }
      },
      fail: function (err) {
        reject(err)
      }
    })
  })
}


//更新用户资料   或者  说是添加 微信用户 添加会员
export function uploadWXUserInfo(data, openid) {
  wx.request({
    url: baseURL + '/api/xcx/wx/user/update',
    method: 'post',
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    data: {
      data: data,
      openid: openid
    },
    success: function (res) {},
    fail: function (err) {}
  })
}



// 文件上传
export function uploadFile(url, filePath, name) {
  //使用promise最大的好处是  防止出现回调地狱
  return new Promise((resolve, reject) => {
    wx.uploadFile({
      url: url,
      filePath: filePath,
      name: name,
      success: function (res) {
        resolve(res);
      },
      fail: function (err) {
        reject(err)
      }
    })
  })
}



/**
 *  var time_str = getTime();
    console.log(time_str);
 *   2021-4-16 00:28:33
 */
export function getTime() {
  var date = new Date();
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  var minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  var second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  var currentTime = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
  return currentTime
}

export function formatTime(time_str) {
  var date = new Date(time_str * 1000);
  var year = date.getFullYear();
  var month = (date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1);
  var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  var minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  var second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  var currentTime = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
  return currentTime
}


export function get_curr_date() {
  //返回当前时间  2022-01-05   有01 05
  var date = new Date();
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate();
  var hour = date.getHours();
  var minute = date.getMinutes();
  var seconds = date.getSeconds();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (day >= 1 && day <= 9) {
    day = "0" + day;
  }
  if (hour >= 1 && hour <= 9) {
    hour = "0" + hour;
  }
  if (minute >= 1 && minute <= 9) {
    minute = "0" + minute;
  }
  if (seconds >= 1 && seconds <= 9) {
    seconds = "0" + seconds;
  }
  var time = year + "-" + month + "-" + day;
  return time;
}

export function date_add_num(num) {
  //当前日期 加上num天。 返回格式是2022-10-05  ,num可以-1天。相当于-1天
  var dateTime = new Date();
  dateTime = dateTime.setDate(dateTime.getDate() + parseInt(num));
  dateTime = new Date(dateTime);
  var year = dateTime.getFullYear();
  var month = dateTime.getMonth() + 1;
  var day = dateTime.getDate();

  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (day >= 1 && day <= 9) {
    day = "0" + day;
  }
  return year + "-" + month + "-" + day;
}


export function date_str_add_num(date_str,num) {
  //指定日期字符串 加上num 天   日期格式如  2022-10-10  也可以是2022-2-7  也可以2022-02-07
  //当前日期 加上num天。 返回格式是2022-10-05  ,num可以-1天。相当于-1天
  var dateTime = new Date(date_str);
  dateTime = dateTime.setDate(dateTime.getDate() + parseInt(num));
  dateTime = new Date(dateTime);
  var year = dateTime.getFullYear();
  var month = dateTime.getMonth() + 1;
  var day = dateTime.getDate();

  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (day >= 1 && day <= 9) {
    day = "0" + day;
  }
  return year + "-" + month + "-" + day;
}








// 数组去重
export function unique(ary) {
  let newAry = [];
  for (let i = 0; i < ary.length; i++) {
    if (newAry.indexOf(ary[i]) === -1) {
      newAry.push(ary[i]);
    }
  }
  return newAry;
}
//数组去重

// 取得2个时间差
export function getDiffHour(upload_wx_user_datetime) {
  var time1 = new Date(upload_wx_user_datetime);
  var time2 = new Date().getTime();
  var diff = time2 - time1;
  var hour = diff / (1000 * 60 * 60);
  console.log("相差" + hour);
  return hour;
}




//date_add_num    get_curr_date

/**
 * 接受数组 和 字符串
 * 判断数组中有没有这些字符串, 有没有包含这些字符串
 * 字符串使用逗号分割。
 * 接受的数组和字符串 注意是字符类型,不能是数字。
 * 使用方法:
 * var state_arr = "1,2,3".split(',');
    var str = "11,5";
    console.log(contain(state_arr,str));
 * 
 */
export function contain(ary,str) {
  var success = true;//默认有这些值。
  var temp_arr = str.split(',');
  temp_arr.forEach((item)=>{
    if(ary.includes(item)==false){
      success=false;
    }
  })
  return success;
}


/**
 * 删除数组中的指定元素。用法如下:
 *  //arr:[0,1,2,5,8,9,5,8]
 *  var ary2 = [0,1,8,8]
 *  var s = array_remove(this.data.arr,ary2);
 *   console.log(s);
 * //) [2, 5, 9, 5]  
 */
export function array_remove(ary,ary2) {
  for(var i = 0; i < ary.length; i++) {
    for(var j  in ary2){
      if(ary[i] == ary2[j]) {
        ary.splice(i, 1);
      }
    }
  }
  return  ary;
}

/**
 * 去掉空格,所有空格。中间空格和前后空格。
 * 
 */
export function str_trim(str) {
  str = str.replace(/\s+/g,"");
  return str;
}

 
/**
 * 判断 是否是空
 */
export function isEmpty(b) {
  if (b == null || b.trim() == "" || b == undefined || b == "undefined") {
      return true
  }
  return false
}

export function isNotEmpty(b) {
  return !isEmpty(b);
}







css样式 颜色

image.png

body{
   padding-top: 3px;
}

er {font-style: normal;color: #c00;}
ber{font-style: normal;color: #c00;font-weight: bold;}
eg {font-style: normal;color: #009688;}
beg {font-style: normal;font-weight: bold;color: #009688;}
eb {font-style: normal;color: #1E9FFF;}
beb {font-style: normal;font-weight: bold;color: #1E9FFF;}
zi {font-style: normal;color: #9C27B0;}
zib {font-style: normal;font-weight: bold;color: #9C27B0;}


lan {font-style: normal;color: #0000ce;}
lan_b {font-style: normal;font-weight: bold;color: #0000ce;}




senlin {font-style: normal;color: #39b54a;}
senlin_b {font-style: normal;font-weight: bold;color: #39b54a;}
tianqing {font-style: normal;color: #1cbbb4;}
tianqing_b {font-style: normal;font-weight: bold;color: #1cbbb4;}
ganlan {font-style: normal;color: #8dc63f;}
ganlan_b {font-style: normal;font-weight: bold;color: #8dc63f;}

chazi {font-style: normal;color: #6739b6;}
chazi_b {font-style: normal;font-weight: bold;color: #6739b6;}
mujin {font-style: normal;color: #9c26b0;}
mujin_b {font-style: normal;font-weight: bold;color: #9c26b0;}
fen {font-style: normal;color: #e03997;}
fen_b {font-style: normal;font-weight: bold;color: #e03997;}

yanhong {font-style: normal;color: #e54d42;}
yanhong_b {font-style: normal;font-weight: bold;color: #e54d42;}

orange {font-style: normal;color: #f37b1d;}
orange_b {font-style: normal;font-weight: bold;color: #f37b1d;}

huang {font-style: normal;color: #fbbd08;}
huang_b {font-style: normal;font-weight: bold;color: #fbbd08;}

he {font-style: normal;color: #a5673f;}
he_b {font-style: normal;font-weight: bold;color: #a5673f;}

hui {font-style: normal;color: #8799a3;}
hui_b {font-style: normal;font-weight: bold;color: #8799a3;}










.er {font-style: normal;color: #c00;}
.ber{font-style: normal;color: #c00;font-weight: bold;}
.eg {font-style: normal;color: #009688;}
.beg {font-style: normal;font-weight: bold;color: #009688;}
.eb {font-style: normal;color: #1E9FFF;}
.beb {font-style: normal;font-weight: bold;color: #1E9FFF;}
.zi {font-style: normal;color: #9C27B0;}
.zib {font-style: normal;font-weight: bold;color: #9C27B0;}

.senlin {font-style: normal;color: #39b54a;}
.senlin_b {font-style: normal;font-weight: bold;color: #39b54a;}
.tianqing {font-style: normal;color: #1cbbb4;}
.tianqing_b {font-style: normal;font-weight: bold;color: #1cbbb4;}
.ganlan {font-style: normal;color: #8dc63f;}
.ganlan_b {font-style: normal;font-weight: bold;color: #8dc63f;}

.chazi {font-style: normal;color: #6739b6;}
.chazi_b {font-style: normal;font-weight: bold;color: #6739b6;}
.mujin {font-style: normal;color: #9c26b0;}
.mujin_b {font-style: normal;font-weight: bold;color: #9c26b0;}
.fen {font-style: normal;color: #e03997;}
.fen_b {font-style: normal;font-weight: bold;color: #e03997;}

.yanhong {font-style: normal;color: #e54d42;}
.yanhong_b {font-style: normal;font-weight: bold;color: #e54d42;}

.orange {font-style: normal;color: #f37b1d;}
.orange_b {font-style: normal;font-weight: bold;color: #f37b1d;}

.huang {font-style: normal;color: #fbbd08;}
.huang_b {font-style: normal;font-weight: bold;color: #fbbd08;}

.he {font-style: normal;color: #a5673f;}
.he_b {font-style: normal;font-weight: bold;color: #a5673f;}

.hui {font-style: normal;color: #8799a3;}
.hui_b {font-style: normal;font-weight: bold;color: #8799a3;}





.layui-table-cell {
    padding-left: 0px;
    padding-right: 0px;
    text-align: center;
}



.layui-table-view {
   margin: 0px 0 !important;      
}


.layui-table-cell .layui-form-checkbox[lay-skin=primary] {
    top: 4px;
}




联系站长

站长微信:xiaomao0055

站长QQ:14496453