utils.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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?cancelCallback:function(){
  159. document.body.style.overflow = 'auto';
  160. }
  161. },
  162. confirm:{
  163. text: params.confitmBtnText,
  164. btnClass: 'btn-confirm',
  165. action: confirmCallback?confirmCallback:function(){
  166. document.body.style.overflow = 'auto';
  167. }
  168. }
  169. }
  170. });
  171. };
  172. /*单个确定提示弹窗
  173. * @param content 提示文字信息
  174. * @param confitmText 自定义按钮文字内容
  175. * @param flg 判断是否需要回调函数
  176. * @param callback 回调函数
  177. * @auth zhjy
  178. */
  179. CAIMEI.Alert = function(content,confitmText,flg,callback){
  180. document.body.style.overflow = 'hidden';
  181. $.confirm({
  182. boxWidth: (isPC?'300px':'70%'),
  183. title:'提示',
  184. content: content,
  185. closeIcon: true,
  186. animation: 'opacity',
  187. closeAnimation: 'opacity',
  188. useBootstrap: false,
  189. animateFromElement: false,
  190. buttons: {
  191. confirm:{
  192. text: confitmText,
  193. btnClass: 'btn-confirm',
  194. action:function () {
  195. document.body.style.overflow = 'auto';
  196. if(flg){
  197. callback();
  198. }
  199. }
  200. }
  201. }
  202. });
  203. };
  204. /*封装的吐司提示
  205. * @param content 提示文字信息
  206. * @param flg 判断是否需要执行回调函数
  207. * @param callback 回调函数
  208. * @auth zhjy
  209. */
  210. CAIMEI.dialog = function(content,flg,callback){
  211. $.confirm({
  212. title: false,
  213. content: '<div class="dialog">'+content+'</div>',
  214. boxWidth: (isPC?'300px':'70%'),
  215. autoClose: 'close|2000',
  216. useBootstrap:false,
  217. buttons: {
  218. close:{
  219. isHidden: true,
  220. action: function () {
  221. if(flg){
  222. callback()
  223. }
  224. }
  225. }
  226. }
  227. });
  228. };
  229. /*对象合并 IE 兼容方法
  230. * @param
  231. * @auth zhjy
  232. */
  233. CAIMEI.returnedTarget = function(){
  234. if (typeof Object.assign != 'function') {
  235. Object.assign = function(target) {
  236. 'use strict';
  237. if (target == null) {throw new TypeError('Cannot convert undefined or null to object');}
  238. target = Object(target);
  239. for (var index = 1; index < arguments.length; index++) {
  240. var source = arguments[index];
  241. if (source != null) {
  242. for (var key in source) {
  243. if (Object.prototype.hasOwnProperty.call(source, key)) {
  244. target[key] = source[key];
  245. }
  246. }
  247. }
  248. }
  249. return target;
  250. };
  251. }
  252. };
  253. /*手机校验
  254. * @param m 输入的手机号
  255. * @auth zhjy
  256. */
  257. CAIMEI.isPhone = function(mobile){
  258. var reg = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/;
  259. return reg.test(mobile);
  260. };
  261. /*固话校验
  262. * @param m 输入的固定电话
  263. * @auth zhjy
  264. */
  265. CAIMEI.isTel = function(mobile){
  266. var reg = /^0\d{2,3}-?\d{7,8}$/;//固定电话
  267. return reg.test(mobile)
  268. };
  269. /*金额格式校验
  270. * @param m 输入的金额
  271. * @auth zhjy
  272. */
  273. CAIMEI.isMoney = function(m){
  274. if(!AmtRegExp.test(m)){
  275. return true;
  276. }else{
  277. return false;
  278. }
  279. };
  280. /*邮箱校验
  281. * @param m 输入的邮箱号
  282. * @auth zhjy
  283. */
  284. CAIMEI.isEmail = function(m){
  285. var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;//邮箱正则
  286. return reg.test(m)
  287. };
  288. CAIMEI.returnedTarget = function(){//对象合并 IE 兼容方法
  289. if (typeof Object.assign != 'function') {
  290. Object.assign = function(target) {
  291. 'use strict';
  292. if (target == null) {
  293. throw new TypeError('Cannot convert undefined or null to object');
  294. }
  295. target = Object(target);
  296. for (var index = 1; index < arguments.length; index++) {
  297. var source = arguments[index];
  298. if (source != null) {
  299. for (var key in source) {
  300. if (Object.prototype.hasOwnProperty.call(source, key)) {
  301. target[key] = source[key];
  302. }
  303. }
  304. }
  305. }
  306. return target;
  307. };
  308. }
  309. };
  310. /**
  311. * @description: 根据年份月份计算当月天数
  312. * @param year 年份
  313. * @param month 月份
  314. * @return 返回日期格式
  315. */
  316. function fetchDaysByYear(year, month) {
  317. // 该函数没有对参数进行校验 必须确保传入年份月份为正确的数字
  318. year = parseInt(year, 10);
  319. month = parseInt(month, 10);
  320. let days;
  321. switch (month) {
  322. case 1:
  323. case 3:
  324. case 5:
  325. case 7:
  326. case 8:
  327. case 10:
  328. case 12:
  329. days = 31;
  330. break;
  331. case 4:
  332. case 6:
  333. case 9:
  334. case 11:
  335. days = 30;
  336. break;
  337. case 2:
  338. // 判断是否闰年
  339. if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) {
  340. days = 29;
  341. } else {
  342. days = 28;
  343. }
  344. }
  345. return days;
  346. }