【前端学习】整理一些实用的函数

1、将js中Date类型对象转换为“yyyy-MM-dd HH:MM:SS”格式
原博地址:https://cloud.tencent.com/developer/article/1484285

getNowFormatDate : function () {
     
        var date = new Date();
        var seperator1 = "-";
        var seperator2 = ":";
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
     
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
     
            strDate = "0" + strDate;
        }
        var hours = date.getHours();
        if(hours >=0 && hours <=9){
     
            hours = "0" + hours;
        }
        var minutes = date.getMinutes();
        if(minutes >=0 && minutes <=9){
     
            minutes = "0" + minutes;
        }
        var seconds = date.getSeconds();
        if(seconds >=0 && seconds <=9){
     
            seconds = "0" + seconds;
        }
        var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
                + " " + hours + seperator2 + minutes
                + seperator2 + seconds;
        return currentdate;
}

2、微信发送请求:

wx.request({
     
    url: 'http://127.0.0.1:8080/test',
    method: "POST",//指定请求方式,默认get
    data: {
      id: 2010140},
    header: {
     
       //默认值'Content-Type': 'application/json'
      'content-type': 'application/x-www-form-urlencoded' //post
    },
    success: function (res) {
     
      console.log(res.data)
    }
  });

3、python实现随机生成唯一id

def getMsgId():
    array = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
             "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
             "v", "w", "x", "y", "z",
             "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
             "V", "W", "X", "Y", "Z"
             ]
    id = str(uuid.uuid4()).replace("-", '')  # 注意这里需要用uuid4
    buffer = []
    for i in range(0, 8):
        start = i * 4
        end = i * 4 + 4
        val = int(id[start:end], 16)
        buffer.append(array[val % 62])
    return "".join(buffer)

4、JavaMD5加密算法

public static  String getMD5Str(String str) {
     
        byte[] digest = null;
        try {
     
            MessageDigest md5 = MessageDigest.getInstance("md5");
            digest  = md5.digest(str.getBytes("utf-8"));
        } catch (NoSuchAlgorithmException e) {
     
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
     
            e.printStackTrace();
        }
        String md5Str = new BigInteger(1, digest).toString(16);
        return md5Str;
    }

5、Java获取唯一id

 public static String getMsgId() {
     
    	String[] id=new String[10002];
        for (int i = 0; i < 10000; i++) {
     
            id[i]=UUID.randomUUID().toString().substring(24);
        }
 
        for (int j = 0; j < 10000; j++) {
     
            for (int i = 0; i < 10000&&i!=j; i++) {
     
               if(id[j].equals(id[i]))
                   System.out.println(id[j]);
            }
            }
        return id[9999];

    }

你可能感兴趣的:(js,javascript)