连续复制
一键复制
一键打包
mcake.js
(function (w) {
var m = {
debug:true
};
m.user = {
isLogin: util.cookie.get('user_auth') ? true : false,
url: {
login: '/home/user/login'
},
login: function (params, tips) {
util.ajax.post(this.url.login, params, tips, null, function (ret) {
switch (ret.code) {
case 0:
mcake.msg.error(ret.msg);
break;
case 1:
mcake.msg.success(ret.msg);
ret.data.timeout = (new Date()).getTime() + ret.data.expires * 1000;
util.cookie.set('user_auth', util.Base64.encode(JSON.stringify(ret.data)), ret.data.expires);
window.location.reload();
break;
}
});
}
};
m.api = {
get: function (hash, params, tips, callBack) {
var url = (m.debug?config.api.url:'/home/api/index?api=') + hash;
// 判断是否要登录
if (config.api.needLogin.indexOf(hash) > 0 && !util.cookie.get('user_auth')) {
mcake.msg.error('请登录后再操作!');
return false;
}
util.ajax.get(url, params, tips, true, callBack ? callBack : null);
return true;
},
post: function (hash, params, tips, callBack) {
var url = (m.debug?config.api.url:'/home/api/index?api=') + hash;
// 判断是否要登录
if (config.api.needLogin.indexOf(hash) > 0 && !util.cookie.get('user_auth')) {
mcake.msg.error('请登录后再操作!');
return false;
}
util.ajax.post(url, params, tips, true, callBack ? callBack : function(ret){
switch (ret.code) {
case 1:
mcake.msg.success(ret.msg);
break;
default:
mcake.msg.error(ret.msg);
}
});
return true;
}
};
/**
* @description 信息提示 @todo 临时信息提示窗口
* @type {{success: success, error: error}}
*/
m.msg = {
/**
* @description 警告弹窗
* @param tips
* @param confirm
* @param cancel
*/
warn: function (tips, confirm, cancel) {
layer.confirm(tips, {
icon: 3,
title: '提示信息',
btn: ['确定', '取消']
}, confirm ? confirm : null, cancel ? cancel : null);
},
success: function (tips) {
layer.msg(tips);
},
confirm:function(tips, confirm, cancel){
layer.confirm(tips, {
icon: 3,
title: '提示信息',
btn: ['确定', '取消']
}, confirm ? confirm : null, cancel ? cancel : null);
},
error: function (tips) {
layer.msg(tips);
}
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = m;
} else if (typeof exports !== 'undefined') {
exports.mcake = m;
} else if (typeof define === 'function' && define.amd) {
define([], function () {
return m;
});
} else if (typeof w.mcake === 'undefined') {
w.mcake = m;
}
})(window);
util.js
(function (w) {
var u = {};
u.ajax = {
get: function (url, params, tips, header, callBack) {
this.request(url, 'GET', params, tips, header, callBack);
},
post: function (url, params, tips, header, callBack) {
this.request(url, 'POST', params, tips, header, callBack);
},
/**
*@description 请求
* @param url
* @param method 请求方法
* @param params 请求参数
* @param tips 提示信息
* @param header
* @param callBack
*/
request: function (url, method, params, tips, header, callBack) {
method = method ? method : 'POST';
$.ajax({
url: url,
type: method,
data: params,
async: false,
headers: header ? this.header(header) : {},
success: callBack ? callBack : function (ret) {
switch (ret.code) {
case 1:
mcake.msg.success(tips);
break;
default:
mcake.msg.error(ret.msg);
break;
}
},
error:function (xhr, status, errorThrown) {
console.log(123);
console.log(xhr);
console.log(status);
console.log(errorThrown);
}
});
},
header: function (header) {
if (typeof header !== 'object' && header) {
header = {};
}
var userToken = util.cookie.get('user_auth') ? JSON.parse(util.Base64.decode(util.cookie.get('user_auth'))).token : false;
header.version = 'v1.0';
if (!util.cookie.get('access_token')) {
this.request('/home/index/getAccessToken', 'GET', null, '', '', function (ret) {
switch (ret.code) {
case 1:
util.cookie.set('access_token', ret.data.access_token, 7000);
break;
default:
break;
}
});
}
header['access-token'] = util.cookie.get('access_token');
if (userToken) {
header['user-token'] = userToken;
}
return header;
}
};
u.cookie = {
set: function (key, value, second) {
var second = second ? second : 30 * 24 * 60 * 60;
var exp = new Date();
exp.setTime(exp.getTime() + second * 1000);
document.cookie = key + '=' + value + ';expires=' + exp.toGMTString() + ';path=/';
},
get: function (key) {
var arr, reg = new RegExp('(^| )' + key + '=([^;]*)(;|$)');
if (arr = document.cookie.match(reg)) {
return arr[2];
} else {
return null;
}
},
del: function (key) {
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var value = this.get(key);
if (value != null) {
document.cookie = key + '=' + value + ';expires=' + exp.toGMTString() + ';path=/';
}
},
clear: function () {
var strCookie = document.cookie, arrCookie = strCookie.split('; ');
for (var i = 0; i < arrCookie.length; i++) {
var arr = arrCookie[i].split('=');
if (arr.length > 0) {
u.cookie.del(arr[0]);
}
}
}
};
u.request = {
get: function (name) {
var url = location.search.replace(/^\?/, '')
.split('&');
var obj = {};
for (var i = 0, iLen = url.length; i < iLen; i++) {
var param = url[i].split('=');
obj[param[0]] = param[1];
}
if (name) {
return obj[name] || '';
}
return obj;
}
};
u.str ={
length:function (str) {
return str.replace(/[\u0391-\uFFE5]/g,"aa").length; //先把中文替换成两个字节的英文,在计算长度
}
};
u.UTF8 = {
encode: function ($input) {
$input = $input.replace(/\r\n/g, '\n');
var $output = '';
for (var $n = 0; $n < $input.length; $n++) {
var $c = $input.charCodeAt($n);
if ($c < 128) {
$output += String.fromCharCode($c);
} else if (($c > 127) && ($c < 2048)) {
$output += String.fromCharCode(($c >> 6) | 192);
$output += String.fromCharCode(($c & 63) | 128);
} else {
$output += String.fromCharCode(($c >> 12) | 224);
$output += String.fromCharCode((($c >> 6) & 63) | 128);
$output += String.fromCharCode(($c & 63) | 128);
}
}
return $output;
},
decode: function ($input) {
var $output = '';
var $i = 0;
var $c = $c1 = $c2 = 0;
while ($i < $input.length) {
$c = $input.charCodeAt($i);
if ($c < 128) {
$output += String.fromCharCode($c);
$i++;
} else if (($c > 191) && ($c < 224)) {
$c2 = $input.charCodeAt($i + 1);
$output += String.fromCharCode((($c & 31) << 6) | ($c2 & 63));
$i += 2;
} else {
$c2 = $input.charCodeAt($i + 1);
$c3 = $input.charCodeAt($i + 2);
$output += String.fromCharCode((($c & 15) << 12) | (($c2 & 63) << 6) | ($c3 & 63));
$i += 3;
}
}
return $output;
}
};
u.tmpl =function(dom,tmpl,obj,key,callBack){
$(dom).html('');
$(tmpl).tmpl(obj).appendTo(dom);
callBack?callBack():null;
if (key){
u.watch(obj,key,function (obj) {
$(dom).html('');
$(tmpl).tmpl(obj).appendTo(dom);
callBack();
});
}
};
u.watch = function(res,key,callBack){
var intValue =res[key];
Object.defineProperty(res, key, {
get: function () {
return intValue
}, set: function (value) {
intValue = value;
callBack?callBack(res):null;
}
});
};
u.Base64 = {
base64: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
encode: function ($input) {
if (!$input) {
return false;
}
$input = u.UTF8.encode($input);
var $output = '';
var $chr1, $chr2, $chr3;
var $enc1, $enc2, $enc3, $enc4;
var $i = 0;
do {
$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 += this.base64.charAt($enc1) + this.base64.charAt($enc2) + this.base64.charAt($enc3) + this.base64.charAt($enc4);
} while ($i < $input.length);
return $output;
},
decode: function ($input) {
if (!$input) {
return false;
}
$input = $input.replace(/[^A-Za-z0-9\+\/\=]/g, '');
var $output = '';
var $enc1, $enc2, $enc3, $enc4;
var $i = 0;
do {
$enc1 = this.base64.indexOf($input.charAt($i++));
$enc2 = this.base64.indexOf($input.charAt($i++));
$enc3 = this.base64.indexOf($input.charAt($i++));
$enc4 = this.base64.indexOf($input.charAt($i++));
$output += String.fromCharCode(($enc1 << 2) | ($enc2 >> 4));
if ($enc3 != 64) {
$output += String.fromCharCode((($enc2 & 15) << 4) | ($enc3 >> 2));
}
if ($enc4 != 64) {
$output += String.fromCharCode((($enc3 & 3) << 6) | $enc4);
}
} while ($i < $input.length);
return u.UTF8.decode($output);
}
};
u.Hex = {
hex: '0123456789abcdef',
encode: function ($input) {
if (!$input) {
return false;
}
var $output = '';
var $k;
var $i = 0;
do {
$k = $input.charCodeAt($i++);
$output += this.hex.charAt(($k >> 4) & 0xf) + this.hex.charAt($k & 0xf);
} while ($i < $input.length);
return $output;
},
decode: function ($input) {
if (!$input) {
return false;
}
$input = $input.replace(/[^0-9abcdef]/g, '');
var $output = '';
var $i = 0;
do {
$output += String.fromCharCode(((this.hex.indexOf($input.charAt($i++)) << 4) & 0xf0) | (this.hex.indexOf($input.charAt($i++)) & 0xf));
} while ($i < $input.length);
return $output;
}
};
u.ASN1Data = function ($data) {
this.error = false;
this.parse = function ($data) {
if (!$data) {
this.error = true;
return null;
}
var $result = [];
while ($data.length > 0) {
// get the tag
var $tag = $data.charCodeAt(0);
$data = $data.substr(1);
// get length
var $length = 0;
// ignore any null tag
if (($tag & 31) == 0x5) {
$data = $data.substr(1);
} else {
if ($data.charCodeAt(0) & 128) {
var $lengthSize = $data.charCodeAt(0) & 127;
$data = $data.substr(1);
if ($lengthSize > 0) {
$length = $data.charCodeAt(0);
}
if ($lengthSize > 1) {
$length = (($length << 8) | $data.charCodeAt(1));
}
if ($lengthSize > 2) {
this.error = true;
return null;
}
$data = $data.substr($lengthSize);
} else {
$length = $data.charCodeAt(0);
$data = $data.substr(1);
}
}
// get value
var $value = '';
if ($length) {
if ($length > $data.length) {
this.error = true;
return null;
}
$value = $data.substr(0, $length);
$data = $data.substr($length);
}
if ($tag & 32) {
$result.push(this.parse($value));
}// sequence
else {
$result.push(this.value(($tag & 128) ? 4 : ($tag & 31), $value));
}
}
return $result;
};
this.value = function ($tag, $data) {
if ($tag == 1) {
return $data ? true : false;
} else if ($tag == 2) //integer
{
return $data;
} else if ($tag == 3) //bit string
{
return this.parse($data.substr(1));
} else if ($tag == 5) //null
{
return null;
} else if ($tag == 6) { //ID
var $res = [];
var $d0 = $data.charCodeAt(0);
$res.push(Math.floor($d0 / 40));
$res.push($d0 - $res[0] * 40);
var $stack = [];
var $powNum = 0;
var $i;
for ($i = 1; $i < $data.length; $i++) {
var $token = $data.charCodeAt($i);
$stack.push($token & 127);
if ($token & 128) {
$powNum++;
} else {
var $j;
var $sum = 0;
for ($j = 0; $j < $stack.length; $j++) {
$sum += $stack[$j] * Math.pow(128, $powNum--);
}
$res.push($sum);
$powNum = 0;
$stack = [];
}
}
return $res.join('.');
}
return null;
};
this.data = this.parse($data);
};
u.RSA = {
getPublicKey: function ($pem) {
if ($pem.length < 50) {
return false;
}
if ($pem.substr(0, 26) != '-----BEGIN PUBLIC KEY-----') {
return false;
}
$pem = $pem.substr(26);
if ($pem.substr($pem.length - 24) != '-----END PUBLIC KEY-----') {
return false;
}
$pem = $pem.substr(0, $pem.length - 24);
$pem = new ASN1Data(util.Base64.decode($pem));
if ($pem.error) {
return false;
}
$pem = $pem.data;
if ($pem[0][0][0] == '1.2.840.113549.1.1.1') {
return new util.RSAPublicKey($pem[0][1][0][0], $pem[0][1][0][1]);
}
return false;
},
encrypt: function ($data, $pubkey) {
if (!$pubkey) {
return false;
}
var bytes = ($pubkey.modulus.bitLength() + 7) >> 3;
$data = this.pkcs1pad2($data, bytes);
if (!$data) {
return false;
}
$data = $data.modPowInt($pubkey.encryptionExponent, $pubkey.modulus);
if (!$data) {
return false;
}
$data = $data.toString(16);
while ($data.length < bytes * 2) {
$data = '0' + $data;
}
return util.Base64.encode(Hex.decode($data));
},
pkcs1pad2: function ($data, $keysize) {
if ($keysize < $data.length + 11) {
return null;
}
var $buffer = [];
var $i = $data.length - 1;
while ($i >= 0 && $keysize > 0) {
$buffer[--$keysize] = $data.charCodeAt($i--);
}
$buffer[--$keysize] = 0;
while ($keysize > 2) {
$buffer[--$keysize] = Math.floor(Math.random() * 254) + 1;
}
$buffer[--$keysize] = 2;
$buffer[--$keysize] = 0;
return new BigInteger($buffer);
}
};
u.RSAPublicKey = function ($modulus, $encryptionExponent) {
this.modulus = new BigInteger(util.Hex.encode($modulus), 16);
this.encryptionExponent = new BigInteger(util.Hex.encode($encryptionExponent), 16);
};
u.validate = {
//判断输入内容是否为空
IsNull: function(str) {
if (str == null || str == "" || str == "undefined") {
return false;
}
return true;
},
// 判断日期类型是否为YYYY-MM-DD格式的类型
IsDate: function(str) {
if (str.length != 0) {
var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/;
var r = str.match(reg);
if (r == null)
return false;
}
return true;
},
// 判断日期类型是否为YYYY-MM-DD hh:mm:ss格式的类型
IsDateTime: function(str) {
if (str.length != 0) {
var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
var r = str.match(reg);
if (r == null)
return false;
}
return true;
},
// 判断日期类型是否为hh:mm:ss格式的类型
IsTime: function(str) {
if (str.length != 0) {
reg = /^((20|21|22|23|[0-1]\d)\:[0-5][0-9])(\:[0-5][0-9])?$/;
if (!reg.test(str)) {
return false;
}
}
return true;
},
// 判断输入的字符是否为英文字母
IsLetter: function(str) {
if (str.length != 0) {
reg = /^[a-zA-Z]+$/;
if (!reg.test(str)) {
return false;
}
}
return true;
},
// 判断输入的字符是否为整数
IsInteger: function(str) {
if (str.length != 0) {
reg = /^[-+]?\d*$/;
if (!reg.test(str)) {
return false;
}
}
return true;
},
// 判断输入的字符是否为双精度
IsDouble: function(str) {
if (str.length != 0) {
reg = /^[-\+]?\d+(\.\d+)?$/;
if (!reg.test(str)) {
return false;
}
}
return true;
},
// 判断输入的字符是否为:a-z,A-Z,0-9
IsString: function(str) {
if (str.length != 0) {
reg = /^[a-zA-Z0-9_]+$/;
if (!reg.test(str)) {
return false;
}
}
return true;
},
// 判断输入的字符是否为中文
IsChinese: function(str) {
if (str.length != 0) {
reg = /^[\u0391-\uFFE5]+$/;
if (!reg.test(str)) {
return false;
}
}
return true;
},
// 判断输入的EMAIL格式是否正确
IsEmail: function(str) {
if (str.length != 0) {
reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
if (!reg.test(str)) {
return false;
}
}
return true;
},
// 判断输入的邮编(只能为六位)是否正确
IsZIP: function(str) {
if (str.length != 0) {
reg = /^\d{6}$/;
if (!reg.test(str)) {
return false;
}
}
},
// 判断输入的数字不大于某个特定的数字
MaxValue: function(str, value) {
if (str.length != 0) {
reg = /^[-+]?\d*$/;
if (reg.test(str)) { // 判断是否为数字类型
if (val > parseInt(value)) // “123”为自己设定的最大值
{
return false;
}
}
}
return true;
},
// 判断输入的数字不小于某个特定的数字
MinValue: function(str, value) {
if (str.length != 0) {
reg = /^[-+]?\d*$/;
if (reg.test(str)) { // 判断是否为数字类型
if (val < parseInt(value)) // value为自己设定的最小值
{
return false;
}
}
}
return true;
},
//判断输入内容是否为手机号码
IsMobile: function(str) {
if (str.length != 0) {
var reg = /^(13[0-9]|15[0-9]|17[0-9]|18[0-9]|14[0-9])[0-9]{8}$/;
if (!reg.test(str)) {
return false;
}
}
return true;
}
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = u;
} else if (typeof exports !== 'undefined') {
exports.util = u;
} else if (typeof define === 'function' && define.amd) {
define([], function () {
return u;
});
} else if (typeof w.util === 'undefined') {
w.util = u;
}
// defineProperty compatible start
var origDefineProperty = Object.defineProperty;
var arePropertyDescriptorsSupported = function() {
var obj = {};
try {
origDefineProperty(obj, "x", { enumerable: false, value: obj });
for (var _ in obj) {
return false;
}
return obj.x === obj;
} catch (e) {
/* this is IE 8. */
return false;
}
};
var supportsDescriptors =
origDefineProperty && arePropertyDescriptorsSupported();
if (!supportsDescriptors) {
Object.defineProperty = function(a, b, c) {
//IE8支持修改元素节点的属性
if (origDefineProperty && a.nodeType == 1) {
return origDefineProperty(a, b, c);
} else {
a[b] = c.value || (c.get && c.get());
}
};
}
// defineProperty compatible end
if (!Array.indexOf) {
Array.prototype.indexOf = function (obj) {
for (var i = 0; i < this.length; i++) {
if (this[i] == obj) {
return i;
}
}
return -1;
};
}
})(window);
文件目录
└── js
├── mcake.js
├── util.js
评论已关闭