在項(xiàng)目開發(fā)過程中,常遇到一樣的方法,這時(shí)候就可以封裝起來
PS:其實(shí)也過了好久,這里都已經(jīng)不是最新的了
'use strict';
var?JavascriptCommon = {
????/**
????* 剪切字符串
????* @ str 原字符串
????* @ 最大長度 ??
????*/
????MsgCutWord: function?(str, num) {
????????if?(str == null) { str = ""; }
????????var?result = str;
????????var?wordLength = str.length;
????????if?(wordLength > num) {
????????????result = str.substr(0, num);
????????????result += "...";
????????}
????????return?(result);
????},
????/**
????* ?修改日期格式年月日
????* ?@ jsondate json日期
????* ?@ splitchar 分隔符
????*/
????ChangeDateFormat: function?(jsondate, splitchar) {
????????if?(!jsondate) { return?""; }
????????jsondate = jsondate.replace("/Date(", "").replace(")/", "");
????????if?(jsondate.indexOf("+") > 0) {
????????????jsondate = jsondate.substring(0, jsondate.indexOf("+"));
????????}
????????else?if?(jsondate.indexOf("-") > 0) {
????????????jsondate = jsondate.substring(0, jsondate.indexOf("-"));
????????}
????????var?date = new?Date(parseInt(jsondate, 10));
????????var?month = date.getMonth() + 1 < 10 ? "0"?+ (date.getMonth() + 1) : date.getMonth() + 1;
????????var?currentDate = date.getDate() < 10 ? "0"?+ date.getDate() : date.getDate();
????????return?date.getFullYear() + splitchar + month + splitchar + currentDate;
????},
????/**
????* ?修改日期格式年月日
????* ?@ jsondate json日期
????* ?@ splitchar 分隔符
????*/
????ChangeDateFormat_hhmmss: function?(jsondate, splitchar) {
????????if?(!jsondate) { return?""; }
????????jsondate = jsondate.replace("/Date(", "").replace(")/", "");
????????if?(jsondate.indexOf("+") > 0) {
????????????jsondate = jsondate.substring(0, jsondate.indexOf("+"));
????????}
????????else?if?(jsondate.indexOf("-") > 0) {
????????????jsondate = jsondate.substring(0, jsondate.indexOf("-"));
????????}
????????var?date = new?Date(parseInt(jsondate, 10));
????????var?month = date.getMonth() + 1 < 10 ? "0"?+ (date.getMonth() + 1) : date.getMonth() + 1;
????????var?currentDate = date.getDate() < 10 ? "0"?+ date.getDate() : date.getDate();
????????var?hh = date.getHours() < 10 ? "0"?+ date.getHours() : date.getHours();
????????var?mm = date.getMinutes() < 10 ? "0"?+ date.getMinutes() : date.getMinutes();
????????return?date.getFullYear() + splitchar + month + splitchar + currentDate + " "?+ hh + ":"?+ mm;
????},
????/**
????* ?獲得當(dāng)天對應(yīng)的天數(shù)
????* ?@ jsondate json日期
????* ?@ splitchar 分隔符
????*/
????GetDateDay: function?(jsondate) {
????????if?(!jsondate) { return?""; }
????????jsondate = jsondate.replace("/Date(", "").replace(")/", "");
????????if?(jsondate.indexOf("+") > 0) {
????????????jsondate = jsondate.substring(0, jsondate.indexOf("+"));
????????}
????????else?if?(jsondate.indexOf("-") > 0) {
????????????jsondate = jsondate.substring(0, jsondate.indexOf("-"));
????????}
????????var?date = new?Date(parseInt(jsondate, 10));
????????return?date.getDate();
????},
????/**
????* 設(shè)置Cookie 【js版】
????* @ name 名稱
????* @ value 內(nèi)容
????* @ time 保存時(shí)間
????* @ path [可選] 路徑
????* 刪除時(shí)time傳 -1
????*/
????MsgSetCookie: function?(name, value, time, path) {
????????path = path || "";
????????var?d = new?Date();
????????d.setTime(d.getTime() + (time * 24 * 60 * 60 * 1000));
????????var?expires = "expires="?+ d.toUTCString();
????????path = "path="?+ path;
????????document.cookie = name + "="?+ value + "; "?+ expires + "; "?+ path;
????},
????/**
????* 讀取Cookie 【js版】
????* @ name 名稱
????*/
????MsgGetCookie: function?(name) {
????????name = (name + "=");
????????var?ca = document.cookie.split(';');
????????for?(var?i = 0; i < ca.length; i++) {
????????????var?c = ca[i];
????????????while?(c.charAt(0) == ' ') c = c.substring(1);
????????????if?(c.indexOf(name) != -1) return?c.substring(name.length, c.length);
????????}
????????return?"";
????},
????/**
????* 生成Guid 唯一主鍵
????* 把英文和-全部替換成純數(shù)字
????*/
????Guid: function?() {
????????var?result = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function?(c) {
????????????var?r = Math.random() * 16 | 0, v = c == 'x'?? r : (r & 0x3 | 0x8);
????????????return?v.toString(16);
????????});
????????return?result.replace(/-/ig, parseInt(Math.random() * 10)).replace(/[a-zA-Z]/ig, parseInt(Math.random() * 10));
????},
????/**
????* Base64加密解密類
????* @ .encode() 加密
????* @ .decode() 解密
????*/
????Base64: function?() {
????????var?_keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
????????this.encode = function?(input) {
????????????input = input || "";
????????????var?output = "";
????????????var?chr1, chr2, chr3, enc1, enc2, enc3, enc4;
????????????var?i = 0;
????????????input = _utf8_encode(input);
????????????while?(i < input.length) {
????????????????chr1 = input.charCodeAt(i++);
????????????????chr2 = input.charCodeAt(i++);
????????????????chr3 = input.charCodeAt(i++);
????????????????enc1 = chr1 >> 2;
????????????????enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
????????????????enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
????????????????enc4 = chr3 & 63;
????????????????if?(isNaN(chr2)) {
????????????????????enc3 = enc4 = 64;
????????????????} else?if?(isNaN(chr3)) {
????????????????????enc4 = 64;
????????????????}
????????????????output = output +
????_keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
????_keyStr.charAt(enc3) + _keyStr.charAt(enc4);
????????????}
????????????return?output;
????????};
????????this.decode = function?(input) {
????????????input = input || "";
????????????var?output = "";
????????????var?chr1, chr2, chr3;
????????????var?enc1, enc2, enc3, enc4;
????????????var?i = 0;
????????????input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
????????????while?(i < input.length) {
????????????????enc1 = _keyStr.indexOf(input.charAt(i++));
????????????????enc2 = _keyStr.indexOf(input.charAt(i++));
????????????????enc3 = _keyStr.indexOf(input.charAt(i++));
????????????????enc4 = _keyStr.indexOf(input.charAt(i++));
????????????????chr1 = (enc1 << 2) | (enc2 >> 4);
????????????????chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
????????????????chr3 = ((enc3 & 3) << 6) | enc4;
????????????????output = output + String.fromCharCode(chr1);
????????????????if?(enc3 != 64) {
????????????????????output = output + String.fromCharCode(chr2);
????????????????}
????????????????if?(enc4 != 64) {
????????????????????output = output + String.fromCharCode(chr3);
????????????????}
????????????}
????????????output = _utf8_decode(output);
????????????return?output;
????????};
????????var?_utf8_encode = function?(string) {
????????????string = string.replace(/\r\n/g, "\n");
????????????var?utftext = "";
????????????for?(var?n = 0; n < string.length; n++) {
????????????????var?c = string.charCodeAt(n);
????????????????if?(c < 128) {
????????????????????utftext += String.fromCharCode(c);
????????????????} else?if?((c > 127) && (c < 2048)) {
????????????????????utftext += String.fromCharCode((c >> 6) | 192);
????????????????????utftext += String.fromCharCode((c & 63) | 128);
????????????????} else?{
????????????????????utftext += String.fromCharCode((c >> 12) | 224);
????????????????????utftext += String.fromCharCode(((c >> 6) & 63) | 128);
????????????????????utftext += String.fromCharCode((c & 63) | 128);
????????????????}
????????????}
????????????return?utftext;
????????};
????????var?_utf8_decode = function?(utftext) {
????????????var?string = "";
????????????var?i = 0;
????????????var?c = c1 = c2 = 0;
????????????while?(i < utftext.length) {
????????????????c = utftext.charCodeAt(i);
????????????????if?(c < 128) {
????????????????????string += String.fromCharCode(c);
????????????????????i++;
????????????????} else?if?((c > 191) && (c < 224)) {
????????????????????c2 = utftext.charCodeAt(i + 1);
????????????????????string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
????????????????????i += 2;
????????????????} else?{
????????????????????c2 = utftext.charCodeAt(i + 1);
????????????????????c3 = utftext.charCodeAt(i + 2);
????????????????????string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
????????????????????i += 3;
????????????????}
????????????}
????????????return?string;
????????};
????},
????/**
????* 打印對象屬性和方法
????* @ obj 對象
????*/
????ShowObjectAttr: function?(obj) {
????????for?(var?name in?obj) {
????????????console.log(name + ": "?+ obj[name]);
????????}
????},
????/**
????* 獲取純文字
????* @ html 富文本/html標(biāo)簽
????* @ length 長度 可選[默認(rèn)全部]
????*/
????MsgGetPureText: function?(html, length) {
????????var?text = html.replace(/<[^>]+>/g, "");
????????length = isNaN(length) ? text.length : length;
????????return?text.substring(0, length) + "...";
????},
????/**
????* 翻譯狀態(tài)
????* @ state
????*/
????TranslateState: function?(state) {
????????switch?(state) {
????????????case?"0": return?"待受理";
????????????case?"1": return?"待受理";
????????????case?"2": return?"已受理";
????????????case?"3": return?"不受理";
????????????case?"4": return?"已取消";
????????????case?"5": return?"被取消";
????????????default: return?"未定義";
????????}
????},
????/**
????* 郵件判斷
????*/
????ValidateEmail: function?(email) {
????????var?myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
????????return?myreg.test(email);
????},
????/**
????* 電話判斷
????* @ phone
????*/
????ValidatePhone: function?(phone) {
????????var?myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
????????return?myreg.test(phone);
????}
};