123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- /*封装部分公共方法
- * @auth zhjy
- */
- var CAIMEI = window.CAIMEI = {};
- var isWuHeng = isLocalStorageSupported();//无痕
- var AmtRegExp =/^(([1-9]\d{0,9})|0)(\.\d{1,2})?$/;
- CAIMEI.Storage = {
- setItem:function(key,val){
- if(isWuHeng){
- window.localStorage.setItem(key,val);
- }else{
- setCookie(key,val,{maxAge:300000});
- }
- },
- getItem:function(key){
- if(isWuHeng){
- return window.localStorage.getItem(key);
- }else{
- var name = getCookie(key);
- return name;
- }
- },
- removeItem:function(key){
- if(isWuHeng){
- return window.localStorage.removeItem(key);
- }else{
- var name = getCookie(key);
- return name;
- }
- },
- clear:function(){
- if(isWuHeng){
- window.localStorage.clear();
- }else{
- clearCookie();
- }
- }
- };
- function isLocalStorageSupported(){
- var testKey = 'testWu',
- storage = window.sessionStorage;
- try {
- storage.setItem(testKey, 'testValue');
- storage.removeItem(testKey);
- return true;
- } catch (error) {
- return false;
- }
- };
- function getCookiesObj(){
- var cookies = {};
- if(document.cookie){
- var objs = document.cookie.split('; ');
- for(var i in objs){
- var index = objs[i].indexOf('='),
- name = objs[i].substr(0, index),
- value = objs[i].substr(index + 1, objs[i].length);
- cookies[name] = value;
- }
- }
- return cookies;
- };
- function setCookie(name, value,opts){
- if(name && value){
- var cookie = encodeURIComponent(name) + '=' + encodeURIComponent(value);
- if(opts){
- if(opts.maxAge){
- cookie += '; max-age=' + opts.maxAge;
- }
- }
- document.cookie = cookie;
- }else{
- return '';
- }
- };
- //获取cookie
- function getCookie(name){
- return decodeURIComponent(getCookiesObj()[name]) || null;
- };
- //清除所有cookie
- function clearCookie(){
- var cookies = getCookiesObj();
- for(var key in cookies){
- document.cookie = key + '=; max-age=0';
- }
- };
- /*获取URL后的传递参数
- * @param key 参数的传递字段
- * @auth zhjy
- */
- CAIMEI.getUrlParam=function(key){
- var href = window.location.href;
- var param = href.substr(href.indexOf('?')+1).split('&'),obj={};
- for(var i=0;i<param.length;i++){
- var arr = param[i].split('=');
- obj[arr[0]] = arr[1];
- }
- return obj[key];
- };
- /*取消,确定提示弹窗
- * @param content 提示文字信息
- * @param cancelText 自定义取消按钮文字
- * @param confitmText 自定义确认按钮文字
- * @param callback 回调函数
- * @auth zhjy
- */
- CAIMEI.Modal = function(content,cancelText,confitmText,callback){
- $.confirm({
- boxWidth: (isPC?'300px':'70%'),
- title:'提示',
- content: content,
- closeIcon: true,
- animation: 'opacity',
- closeAnimation: 'opacity',
- useBootstrap: false,
- animateFromElement: false,
- buttons: {
- cancel: {
- text: cancelText,
- btnClass: 'btn-cancel',
- action:function () {}
- },
- confirm:{
- text: confitmText,
- btnClass: 'btn-confirm',
- action:function () {
- callback();
- }
- }
- }
- });
- };
- /*自定义双回调弹窗
- * @param params{
- * content, 提示文字信息
- * confitmBtnText,自定义确认按钮文字
- * confirmCallback, 回调函数
- * cancelBtnText, 自定义取消按钮文字
- * cancelCallback 回调函数
- * }
- * @auth charles
- */
- CAIMEI.Popup = function(params,confirmCallback, cancelCallback){
- document.body.style.overflow = 'hidden';
- $.confirm({
- boxWidth: (isPC?'300px':'70%'),
- title:'提示',
- content: params.content,
- closeIcon: params.closeIcon,
- animation: 'opacity',
- closeAnimation: 'opacity',
- useBootstrap: false,
- animateFromElement: false,
- buttons: {
- cancel: {
- text: params.cancelBtnText,
- btnClass: 'btn-cancel',
- action: cancelCallback
- },
- confirm:{
- text: params.confitmBtnText,
- btnClass: 'btn-confirm',
- action: confirmCallback
- }
- },
- onOpen(){
- document.body.style.overflow = 'hidden';
- },
- onClose(){
- document.body.style.overflow = 'auto';
- }
- });
- };
- /*单个确定提示弹窗
- * @param content 提示文字信息
- * @param confitmText 自定义按钮文字内容
- * @param flg 判断是否需要回调函数
- * @param callback 回调函数
- * @auth zhjy
- */
- CAIMEI.Alert = function(content,confitmText,flg,callback){
- $.confirm({
- boxWidth: (isPC?'300px':'70%'),
- title:'提示',
- content: content,
- closeIcon: true,
- animation: 'opacity',
- closeAnimation: 'opacity',
- useBootstrap: false,
- animateFromElement: false,
- buttons: {
- confirm:{
- text: confitmText,
- btnClass: 'btn-confirm',
- action:function () {
- if(flg){
- callback();
- }
- }
- },
- },
- onOpen(){
- document.body.style.overflow = 'hidden';
- },
- onClose(){
- document.body.style.overflow = 'auto';
- }
- });
- };
- /*封装的吐司提示
- * @param content 提示文字信息
- * @param flg 判断是否需要执行回调函数
- * @param callback 回调函数
- * @auth zhjy
- */
- CAIMEI.dialog = function(content,flg,callback){
- $.confirm({
- title: false,
- content: '<div class="dialog">'+content+'</div>',
- boxWidth: (isPC?'300px':'70%'),
- autoClose: 'close|2000',
- useBootstrap:false,
- buttons: {
- close:{
- isHidden: true,
- action: function () {
- if(flg){
- callback()
- }
- }
- }
- }
- });
- };
- /*对象合并 IE 兼容方法
- * @param
- * @auth zhjy
- */
- CAIMEI.returnedTarget = function(){
- if (typeof Object.assign != 'function') {
- Object.assign = function(target) {
- 'use strict';
- if (target == null) {throw new TypeError('Cannot convert undefined or null to object');}
- target = Object(target);
- for (var index = 1; index < arguments.length; index++) {
- var source = arguments[index];
- if (source != null) {
- for (var key in source) {
- if (Object.prototype.hasOwnProperty.call(source, key)) {
- target[key] = source[key];
- }
- }
- }
- }
- return target;
- };
- }
- };
- /*手机校验
- * @param m 输入的手机号
- * @auth zhjy
- */
- CAIMEI.isPhone = function(mobile){
- var reg = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/;
- return reg.test(mobile);
- };
- /*固话校验
- * @param m 输入的固定电话
- * @auth zhjy
- */
- CAIMEI.isTel = function(mobile){
- var reg = /^0\d{2,3}-?\d{7,8}$/;//固定电话
- return reg.test(mobile)
- };
- /*金额格式校验
- * @param m 输入的金额
- * @auth zhjy
- */
- CAIMEI.isMoney = function(m){
- if(!AmtRegExp.test(m)){
- return true;
- }else{
- return false;
- }
- };
- /*邮箱校验
- * @param m 输入的邮箱号
- * @auth zhjy
- */
- CAIMEI.isEmail = function(m){
- var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;//邮箱正则
- return reg.test(m)
- };
- CAIMEI.returnedTarget = function(){//对象合并 IE 兼容方法
- if (typeof Object.assign != 'function') {
- Object.assign = function(target) {
- 'use strict';
- if (target == null) {
- throw new TypeError('Cannot convert undefined or null to object');
- }
- target = Object(target);
- for (var index = 1; index < arguments.length; index++) {
- var source = arguments[index];
- if (source != null) {
- for (var key in source) {
- if (Object.prototype.hasOwnProperty.call(source, key)) {
- target[key] = source[key];
- }
- }
- }
- }
- return target;
- };
- }
- };
- /**
- * @description: 根据年份月份计算当月天数
- * @param year 年份
- * @param month 月份
- * @return 返回日期格式
- */
- function fetchDaysByYear(year, month) {
- // 该函数没有对参数进行校验 必须确保传入年份月份为正确的数字
- year = parseInt(year, 10);
- month = parseInt(month, 10);
- let days;
- switch (month) {
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- days = 31;
- break;
- case 4:
- case 6:
- case 9:
- case 11:
- days = 30;
- break;
- case 2:
- // 判断是否闰年
- if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) {
- days = 29;
- } else {
- days = 28;
- }
- }
- return days;
- }
- /**
- * 防抖
- * @param {Function} func 需要包装的函数
- * @param {string} wait 等待执行时间
- * @param {string} immediate 是否是立即执行 默认不立即执行
- * @returns {Function} 返回包装后的函数
- */
- function debounce(func, wait = 200, immediate = false) {
- let timeout, result
- return function () {
- const context = this
- const args = arguments
- if (timeout) clearTimeout(timeout)
- if (immediate) {
- const callNow = !timeout
- timeout = setTimeout(function () {
- timeout = null
- }, wait)
- if (callNow) result = func.apply(context, args)
- } else {
- timeout = setTimeout(function () {
- func.apply(context, args)
- }, wait)
- }
- return result
- }
- }
|