util.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // 日期补0
  2. const formatNumber = n => {
  3. n = n.toString()
  4. return n[1] ? n : '0' + n
  5. }
  6. // 年月日时分秒
  7. const formatTime = (date, word = false, datestring = '/', timestring = ":") => {
  8. if(date.constructor==String){
  9. var date = new Date(date.replace(/-/g, '/'));
  10. }
  11. const year = date.getFullYear()
  12. const month = date.getMonth() + 1
  13. const day = date.getDate()
  14. const hour = date.getHours()
  15. const minute = date.getMinutes()
  16. const second = date.getSeconds()
  17. if (word == false) {
  18. return [year, month, day].map(formatNumber).join(datestring) + ' ' + [hour, minute, second].map(formatNumber).join(
  19. timestring)
  20. }
  21. return [year].map(formatNumber) + '年' + [month].map(formatNumber) + '月' + [day].map(formatNumber) + '日' + ' ' + [
  22. hour
  23. ].map(formatNumber) + ':' + [minute].map(formatNumber) + ':' + [second].map(formatNumber)
  24. }
  25. // 年月日时分
  26. const formatSpotDayHourMinute = (date, word = false, datestring = '/', timestring = ":") => {
  27. if(date.constructor==String){
  28. var date = new Date(date.replace(/-/g, '/'));
  29. }
  30. const year = date.getFullYear()
  31. const month = date.getMonth() + 1
  32. const day = date.getDate()
  33. const hour = date.getHours()
  34. const minute = date.getMinutes()
  35. if (word == false) {
  36. return [year, month, day].map(formatNumber).join(datestring) + ' ' + [hour, minute].map(formatNumber).join(
  37. timestring)
  38. }
  39. return [year].map(formatNumber) + '年' + [month].map(formatNumber) + '月' + [day].map(formatNumber) + '日' + ' ' + [
  40. hour
  41. ].map(formatNumber) + ':' + [minute].map(formatNumber)
  42. }
  43. // 年月日
  44. const formatTimeDay = (date, word = false, datestring = '/') => {
  45. if(date.constructor==String){
  46. var date = new Date(date.replace(/-/g, '/'));
  47. }
  48. const year = date.getFullYear()
  49. const month = date.getMonth() + 1
  50. const day = date.getDate()
  51. if (word == false) {
  52. return [year, month, day].map(formatNumber).join(datestring)
  53. }
  54. return [year].map(formatNumber) + '年' + [month].map(formatNumber) + '月' + [day].map(formatNumber) + '日'
  55. }
  56. // 年月
  57. const formatWordsYM = (date, word = false, datestring = '/') => {
  58. if(date.constructor==String){
  59. var date = new Date(date.replace(/-/g, '/'));
  60. }
  61. const year = date.getFullYear()
  62. const month = date.getMonth() + 1
  63. if (word == false) {
  64. return [year, month].map(formatNumber).join(datestring)
  65. }
  66. return [year].map(formatNumber) + '年' + [month].map(formatNumber) + '月'
  67. }
  68. // 月日
  69. const formatWordsYD = (date, word = false, datestring = '/') => {
  70. if(date.constructor==String){
  71. var date = new Date(date.replace(/-/g, '/'));
  72. }
  73. const month = date.getMonth() + 1
  74. const day = date.getDate()
  75. if (word == false) {
  76. return [month, day].map(formatNumber).join(datestring)
  77. }
  78. return [month].map(formatNumber) + '月' + [day].map(formatNumber) + '日'
  79. }
  80. // 时分
  81. const formatTimeHourMonth = (date, word = false, timestring = ":") => {
  82. if(date.constructor==String){
  83. var date = new Date(date.replace(/-/g, '/'));
  84. }
  85. const hour = date.getHours()
  86. const minute = date.getMinutes()
  87. if (word == false) {
  88. return [hour, minute].map(formatNumber).join(timestring)
  89. }
  90. return [hour].map(formatNumber) + '时' + [minute].map(formatNumber) + '分'
  91. }
  92. // 更新数组年月份日期
  93. function getfullDateStr(dayCount = 0,word=false,datestring="-") {
  94. var dd = new Date();
  95. dd.setDate(dd.getDate() + dayCount); //设置日期
  96. const year = dd.getFullYear()
  97. const month = dd.getMonth() + 1
  98. const day = dd.getDate()
  99. if(!word){
  100. return [year, month, day].map(formatNumber).join(datestring)
  101. }
  102. return [year].map(formatNumber) + '年' + [month].map(formatNumber) + '月' + [day].map(formatNumber) + '日'
  103. }
  104. //计算两个日期差
  105. function daysBetween(sDate1, sDate2) {
  106. //Date.parse() 解析一个日期时间字符串,并返回1970/1/1 午夜距离该日期时间的毫秒数
  107. var time1 = (new Date(sDate1)).getTime();
  108. var time2 = (new Date(sDate2)).getTime();
  109. var nDays = parseInt((time2 - time1) / 1000 / 3600 / 24);
  110. return nDays;
  111. };
  112. // 更新数组月份日期
  113. function getDateStr(dayCount = 0) {
  114. var dd = new Date();
  115. dd.setDate(dd.getDate() + dayCount); //设置日期
  116. var m = dd.getMonth() + 1; //获取当前月份的日期
  117. var d = dd.getDate();
  118. return `${m}月${d<10?'0'+d:d}日`
  119. }
  120. // 转化时间几分钟前 小时前 昨天 多少天前 '-'年月日
  121. const formatTimeHours = (timespan,word=false) => {
  122. if(timespan.constructor==String){
  123. var dateTimeStamp = new Date(timespan.replace(/-/g, '/'));
  124. }else{
  125. var dateTimeStamp = timespan
  126. }
  127. var minute = 1000 * 60;
  128. var hour = minute * 60;
  129. var day = hour * 24;
  130. var result = '';
  131. var now = new Date().getTime();
  132. var diffValue = now - dateTimeStamp;
  133. if (diffValue < 0) {
  134. console.log("时间不对劲,服务器创建时间与当前时间不同步");
  135. return result = "刚刚";
  136. }
  137. var dayC = diffValue / day;
  138. var hourC = diffValue / hour;
  139. var minC = diffValue / minute;
  140. if (parseInt(dayC) > 7) {
  141. if(word==false){
  142. result = "" + formatSpotDayHourMinute(dateTimeStamp,false,'-',':')
  143. }else{
  144. result = "" + formatSpotDayHourMinute(dateTimeStamp,true)
  145. }
  146. } else if (parseInt(dayC) > 1) {
  147. result = "" + parseInt(dayC) + "天前" + ' ' + formatTimeHourMonth(dateTimeStamp,word);
  148. } else if (parseInt(dayC) == 1) {
  149. result = "昨天" + ' ' + formatTimeHourMonth(dateTimeStamp,word);
  150. } else if (hourC >= 1) {
  151. result = "" + parseInt(hourC) + "小时前";
  152. } else if (minC >= 5) {
  153. result = "" + parseInt(minC) + "分钟前";
  154. } else {
  155. result = "刚刚";
  156. }
  157. return result;
  158. };
  159. //更新数组星期
  160. function getWeekDateStr(dayCount = 0) {
  161. //更新数组星期
  162. var weekStr = "周" + "日一二三四五六".charAt((new Date().getDay() + dayCount) % 7);
  163. return weekStr;
  164. }
  165. //手机号是否合法
  166. function isPhone(val) {
  167. const regexp = /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/;
  168. return regexp.test(val)
  169. // if (!(/^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/.test(phone)) || phone == '') {
  170. // return true;
  171. // }
  172. }
  173. //银行卡是否合法
  174. function isIDcard(value) {
  175. if (!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(value)) {
  176. return true
  177. }
  178. }
  179. /**
  180. * 处理富文本里的图片宽度自适应
  181. * 1.去掉img标签里的style、width、height属性
  182. * 2.img标签添加style属性:max-width:100%;height:auto
  183. * 3.修改所有style里的width属性为max-width:100%
  184. * 4.去掉<br/>标签
  185. * @param html
  186. * @returns {void|string|*}
  187. */
  188. function formatRichText(html) {
  189. let newContent = html.replace(/<img[^>]*>/gi, function (match, capture) {
  190. match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
  191. match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
  192. match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
  193. return match;
  194. });
  195. newContent = newContent.replace(/style="[^"]+"/gi, function (match, capture) {
  196. match = match.replace(/width:[^;]+;/gi, 'width:100%;').replace(/width:[^;]+;/gi, 'width:100%;');
  197. return match;
  198. });
  199. newContent = newContent.replace(/<br[^>]*\/>/gi, '');
  200. newContent = newContent.replace(/\<img/gi, '<img style="width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"');
  201. return newContent;
  202. };
  203. //图片转base64
  204. function toImageBase64(url) {
  205. return 'data:image/jpg;base64,' + wx.getFileSystemManager().readFileSync(url, "base64")
  206. }
  207. // 生成随机数字
  208. function RandomNumBoth(Min, Max) {
  209. var Range = Max - Min;
  210. var Rand = Math.random();
  211. var num = Min + Math.round(Rand * Range); //四舍五入
  212. return num;
  213. }
  214. // 随机字符串
  215. function randomName(len) {
  216. len = len || 23;
  217. var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
  218. var maxPos = chars.length;
  219. var str = '';
  220. for (var i = 0; i < len; i++) {
  221. str += chars.charAt(Math.floor(Math.random() * maxPos));
  222. }
  223. return new Date().getTime() + str;
  224. }
  225. // 随机颜色值
  226. function buildColor() {
  227. var color = "#" + Math.floor(Math.random() * 0xFFFFFF).toString(16).padStart(6, "0");
  228. return color;
  229. }
  230. function path(val){
  231. return '/static/images/'+val
  232. }
  233. // 格式化手机号 3-3-4
  234. function formatPhone(val) {
  235. if(val.length==11){
  236. const matches = /^(\d{3})(\d{4})(\d{4})$/.exec(val)
  237. if (matches) {
  238. return matches[1] + '-' + matches[2] + '-' + matches[3]
  239. }
  240. }else{
  241. return val
  242. }
  243. }
  244. const testPwd = (val) => {
  245. const regexp = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,}$/;
  246. return regexp.test(val)
  247. }
  248. module.exports = {
  249. formatWordsYM: formatWordsYM, //年月
  250. formatTime: formatTime, // 年月日时分秒
  251. formatTimeDay: formatTimeDay, // 年月日
  252. formatSpotDayHourMinute: formatSpotDayHourMinute, //年月日时分
  253. formatWordsYD: formatWordsYD,// 月日
  254. formatTimeHourMonth: formatTimeHourMonth, // 时分
  255. formatTimeHours:formatTimeHours, // 时间处理刚刚,几天前
  256. daysBetween: daysBetween, //计算两个日期差
  257. isPhone: isPhone, //判断手机号是否合法
  258. isIDcard: isIDcard, //身份证号验证
  259. formatRichText, //富文本解析
  260. formatNumber: formatNumber, //日期补0
  261. toImageBase64: toImageBase64, //本地图片转base64
  262. getDateStr: getDateStr, //获取距今天几天后的月日
  263. getWeekDateStr: getWeekDateStr, //获取距今天几天后的星期几
  264. getfullDateStr: getfullDateStr, ////获取距今天几天后格式化的年月日
  265. RandomNumBoth: RandomNumBoth, //范围内的随机数字
  266. randomName: randomName, //随机字符串
  267. buildColor:buildColor,// 随机颜色值
  268. path:path,
  269. formatPhone,
  270. testPwd
  271. }