utils.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*封装部分公共方法
  2. * @auth zhjy
  3. */
  4. var CAIMEI = window.CAIMEI = {};
  5. var isWuHeng = isLocalStorageSupported();//无痕
  6. var AmtRegExp =/^(([1-9]\d{0,9})|0)(\.\d{1,2})?$/;
  7. CAIMEI.Storage = {
  8. setItem:function(key,val){
  9. if(isWuHeng){
  10. window.localStorage.setItem(key,val);
  11. }else{
  12. setCookie(key,val,{maxAge:300000});
  13. }
  14. },
  15. getItem:function(key){
  16. if(isWuHeng){
  17. return window.localStorage.getItem(key);
  18. }else{
  19. var name = getCookie(key);
  20. return name;
  21. }
  22. },
  23. removeItem:function(key){
  24. if(isWuHeng){
  25. return window.localStorage.removeItem(key);
  26. }else{
  27. var name = getCookie(key);
  28. return name;
  29. }
  30. },
  31. clear:function(){
  32. if(isWuHeng){
  33. window.localStorage.clear();
  34. }else{
  35. clearCookie();
  36. }
  37. }
  38. };
  39. function isLocalStorageSupported(){
  40. var testKey = 'testWu',
  41. storage = window.sessionStorage;
  42. try {
  43. storage.setItem(testKey, 'testValue');
  44. storage.removeItem(testKey);
  45. return true;
  46. } catch (error) {
  47. return false;
  48. }
  49. };
  50. function getCookiesObj(){
  51. var cookies = {};
  52. if(document.cookie){
  53. var objs = document.cookie.split('; ');
  54. for(var i in objs){
  55. var index = objs[i].indexOf('='),
  56. name = objs[i].substr(0, index),
  57. value = objs[i].substr(index + 1, objs[i].length);
  58. cookies[name] = value;
  59. }
  60. }
  61. return cookies;
  62. };
  63. function setCookie(name, value,opts){
  64. if(name && value){
  65. var cookie = encodeURIComponent(name) + '=' + encodeURIComponent(value);
  66. if(opts){
  67. if(opts.maxAge){
  68. cookie += '; max-age=' + opts.maxAge;
  69. }
  70. }
  71. document.cookie = cookie;
  72. }else{
  73. return '';
  74. }
  75. };
  76. //获取cookie
  77. function getCookie(name){
  78. return decodeURIComponent(getCookiesObj()[name]) || null;
  79. };
  80. //清除所有cookie
  81. function clearCookie(){
  82. var cookies = getCookiesObj();
  83. for(var key in cookies){
  84. document.cookie = key + '=; max-age=0';
  85. }
  86. };
  87. /*获取URL后的传递参数
  88. * @param key 参数的传递字段
  89. * @auth zhjy
  90. */
  91. CAIMEI.getUrlParam=function(key){
  92. var href = window.location.href;
  93. var param = href.substr(href.indexOf('?')+1).split('&'),obj={};
  94. for(var i=0;i<param.length;i++){
  95. var arr = param[i].split('=');
  96. obj[arr[0]] = arr[1];
  97. }
  98. return obj[key];
  99. };
  100. /*取消,确定提示弹窗
  101. * @param content 提示文字信息
  102. * @param cancelText 自定义取消按钮文字
  103. * @param confitmText 自定义确认按钮文字
  104. * @param callback 回调函数
  105. * @auth zhjy
  106. */
  107. CAIMEI.Modal = function(content,cancelText,confitmText,callback){
  108. $.confirm({
  109. boxWidth: (isPC?'300px':'70%'),
  110. title:'提示',
  111. content: content,
  112. closeIcon: true,
  113. animation: 'opacity',
  114. closeAnimation: 'opacity',
  115. useBootstrap: false,
  116. animateFromElement: false,
  117. buttons: {
  118. cancel: {
  119. text: cancelText,
  120. btnClass: 'btn-cancel',
  121. action:function () {}
  122. },
  123. confirm:{
  124. text: confitmText,
  125. btnClass: 'btn-confirm',
  126. action:function () {
  127. callback();
  128. }
  129. }
  130. }
  131. });
  132. };
  133. /*自定义双回调弹窗
  134. * @param params{
  135. * content, 提示文字信息
  136. * confitmBtnText,自定义确认按钮文字
  137. * confirmCallback, 回调函数
  138. * cancelBtnText, 自定义取消按钮文字
  139. * cancelCallback 回调函数
  140. * }
  141. * @auth charles
  142. */
  143. CAIMEI.Popup = function(params,confirmCallback, cancelCallback){
  144. document.body.style.overflow = 'hidden';
  145. $.confirm({
  146. boxWidth: (isPC?'300px':'70%'),
  147. title:'提示',
  148. content: params.content,
  149. closeIcon: params.closeIcon,
  150. animation: 'opacity',
  151. closeAnimation: 'opacity',
  152. useBootstrap: false,
  153. animateFromElement: false,
  154. buttons: {
  155. cancel: {
  156. text: params.cancelBtnText,
  157. btnClass: 'btn-cancel',
  158. action: cancelCallback
  159. },
  160. confirm:{
  161. text: params.confitmBtnText,
  162. btnClass: 'btn-confirm',
  163. action: confirmCallback
  164. }
  165. },
  166. onOpen(){
  167. document.body.style.overflow = 'hidden';
  168. },
  169. onClose(){
  170. document.body.style.overflow = 'auto';
  171. }
  172. });
  173. };
  174. /*单个确定提示弹窗
  175. * @param content 提示文字信息
  176. * @param confitmText 自定义按钮文字内容
  177. * @param flg 判断是否需要回调函数
  178. * @param callback 回调函数
  179. * @auth zhjy
  180. */
  181. CAIMEI.Alert = function(content,confitmText,flg,callback){
  182. $.confirm({
  183. boxWidth: (isPC?'300px':'70%'),
  184. title:'提示',
  185. content: content,
  186. closeIcon: true,
  187. animation: 'opacity',
  188. closeAnimation: 'opacity',
  189. useBootstrap: false,
  190. animateFromElement: false,
  191. buttons: {
  192. confirm:{
  193. text: confitmText,
  194. btnClass: 'btn-confirm',
  195. action:function () {
  196. if(flg){
  197. callback();
  198. }
  199. }
  200. },
  201. },
  202. onOpen(){
  203. document.body.style.overflow = 'hidden';
  204. },
  205. onClose(){
  206. document.body.style.overflow = 'auto';
  207. }
  208. });
  209. };
  210. /*封装的吐司提示
  211. * @param content 提示文字信息
  212. * @param flg 判断是否需要执行回调函数
  213. * @param callback 回调函数
  214. * @auth zhjy
  215. */
  216. CAIMEI.dialog = function(content,flg,callback){
  217. $.confirm({
  218. title: false,
  219. content: '<div class="dialog">'+content+'</div>',
  220. boxWidth: (isPC?'300px':'70%'),
  221. autoClose: 'close|2000',
  222. useBootstrap:false,
  223. buttons: {
  224. close:{
  225. isHidden: true,
  226. action: function () {
  227. if(flg){
  228. callback()
  229. }
  230. }
  231. }
  232. }
  233. });
  234. };
  235. /*对象合并 IE 兼容方法
  236. * @param
  237. * @auth zhjy
  238. */
  239. CAIMEI.returnedTarget = function(){
  240. if (typeof Object.assign != 'function') {
  241. Object.assign = function(target) {
  242. 'use strict';
  243. if (target == null) {throw new TypeError('Cannot convert undefined or null to object');}
  244. target = Object(target);
  245. for (var index = 1; index < arguments.length; index++) {
  246. var source = arguments[index];
  247. if (source != null) {
  248. for (var key in source) {
  249. if (Object.prototype.hasOwnProperty.call(source, key)) {
  250. target[key] = source[key];
  251. }
  252. }
  253. }
  254. }
  255. return target;
  256. };
  257. }
  258. };
  259. /*手机校验
  260. * @param m 输入的手机号
  261. * @auth zhjy
  262. */
  263. CAIMEI.isPhone = function(mobile){
  264. var reg = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/;
  265. return reg.test(mobile);
  266. };
  267. /*固话校验
  268. * @param m 输入的固定电话
  269. * @auth zhjy
  270. */
  271. CAIMEI.isTel = function(mobile){
  272. var reg = /^0\d{2,3}-?\d{7,8}$/;//固定电话
  273. return reg.test(mobile)
  274. };
  275. /*金额格式校验
  276. * @param m 输入的金额
  277. * @auth zhjy
  278. */
  279. CAIMEI.isMoney = function(m){
  280. if(!AmtRegExp.test(m)){
  281. return true;
  282. }else{
  283. return false;
  284. }
  285. };
  286. /*邮箱校验
  287. * @param m 输入的邮箱号
  288. * @auth zhjy
  289. */
  290. CAIMEI.isEmail = function(m){
  291. var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;//邮箱正则
  292. return reg.test(m)
  293. };
  294. CAIMEI.returnedTarget = function(){//对象合并 IE 兼容方法
  295. if (typeof Object.assign != 'function') {
  296. Object.assign = function(target) {
  297. 'use strict';
  298. if (target == null) {
  299. throw new TypeError('Cannot convert undefined or null to object');
  300. }
  301. target = Object(target);
  302. for (var index = 1; index < arguments.length; index++) {
  303. var source = arguments[index];
  304. if (source != null) {
  305. for (var key in source) {
  306. if (Object.prototype.hasOwnProperty.call(source, key)) {
  307. target[key] = source[key];
  308. }
  309. }
  310. }
  311. }
  312. return target;
  313. };
  314. }
  315. };
  316. /**
  317. * @description: 根据年份月份计算当月天数
  318. * @param year 年份
  319. * @param month 月份
  320. * @return 返回日期格式
  321. */
  322. function fetchDaysByYear(year, month) {
  323. // 该函数没有对参数进行校验 必须确保传入年份月份为正确的数字
  324. year = parseInt(year, 10);
  325. month = parseInt(month, 10);
  326. let days;
  327. switch (month) {
  328. case 1:
  329. case 3:
  330. case 5:
  331. case 7:
  332. case 8:
  333. case 10:
  334. case 12:
  335. days = 31;
  336. break;
  337. case 4:
  338. case 6:
  339. case 9:
  340. case 11:
  341. days = 30;
  342. break;
  343. case 2:
  344. // 判断是否闰年
  345. if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) {
  346. days = 29;
  347. } else {
  348. days = 28;
  349. }
  350. }
  351. return days;
  352. }
  353. /**
  354. * 防抖
  355. * @param {Function} func 需要包装的函数
  356. * @param {string} wait 等待执行时间
  357. * @param {string} immediate 是否是立即执行 默认不立即执行
  358. * @returns {Function} 返回包装后的函数
  359. */
  360. function debounce(func, wait = 200, immediate = false) {
  361. let timeout, result
  362. return function () {
  363. const context = this
  364. const args = arguments
  365. if (timeout) clearTimeout(timeout)
  366. if (immediate) {
  367. const callNow = !timeout
  368. timeout = setTimeout(function () {
  369. timeout = null
  370. }, wait)
  371. if (callNow) result = func.apply(context, args)
  372. } else {
  373. timeout = setTimeout(function () {
  374. func.apply(context, args)
  375. }, wait)
  376. }
  377. return result
  378. }
  379. }