filters.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. export function formatnumber(num, digits = 2, type = "", numstr = ",") {
  2. // digits >>> 小数位取值数
  3. // type >>> floor:向下取整;ceil:向上取整;0:取整数的部分;1:取小数的部分
  4. // numstr >>> 千位分隔符
  5. if (type == "floor") {
  6. num = Math.floor(num)
  7. } else if (type == "ceil") {
  8. num = Math.ceil(num)
  9. } else {
  10. num = Number(num).toFixed(digits)
  11. }
  12. const numa = (+num || 0).toString().split('.')
  13. let renum = (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, numstr))
  14. var digNum = ""
  15. if (numa.length > 1) {
  16. digNum = numa[1]
  17. if (numa[1].length < digits) {
  18. renum += '0'
  19. digNum += '0'
  20. }
  21. if (type === 1) {
  22. return digNum
  23. }
  24. } else if (digits > 0) {
  25. renum = renum + '.'
  26. for (let i = 1; i <= digits; i++) {
  27. renum += '0'
  28. digNum += '0'
  29. }
  30. if (type === 1) {
  31. return digNum
  32. }
  33. }
  34. if (type === 0) {
  35. return numa[0]
  36. } else {
  37. return renum
  38. }
  39. }
  40. export function getPriceNumber(num, type = 0) {
  41. if (!num) num = 0
  42. num = Number(num).toFixed(2)
  43. const numa = (+num || 0).toString().split('.')
  44. let renum = (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ","))
  45. if (numa.length > 1) {
  46. if (numa[1].length < 2) {
  47. renum = renum + '0'
  48. }
  49. } else {
  50. renum = renum + '.'
  51. for (let i = 1; i <= 2; i++) {
  52. renum = renum + '0'
  53. }
  54. }
  55. const arrnum = String(renum).split('.')
  56. return arrnum[type]
  57. }
  58. export function formatDate(datestr, temp = "yyyy-MM-dd hh:mm:ss", type = false, dayCount = 30) {
  59. var date
  60. var time = new Date().getTime();
  61. if (Object.prototype.toString.call(datestr) === '[object Date]') {
  62. date = new Date(datestr);
  63. } else if (datestr) {
  64. if (Object.prototype.toString.call(datestr) === '[object Number]') {
  65. date = new Date(parseInt(datestr))
  66. } else {
  67. var reg = new RegExp('-', "g");
  68. datestr = String(datestr).replace(reg, '/');
  69. date = new Date(datestr);
  70. }
  71. } else {
  72. date = new Date();
  73. }
  74. if (temp == 'getDate') {
  75. return date
  76. } else if (temp == 'getTime') {
  77. return date.getTime()
  78. } else if (!type) {
  79. // 返回标准日期格式
  80. return _formatDate(date, temp);
  81. } else {
  82. // 返回距离当前间隔时间
  83. var _date = date.getTime();
  84. var _time = parseInt((time - _date) / 1000);
  85. var s;
  86. if (_time < 60 * 10) { //十分钟内
  87. return '刚刚';
  88. } else if ((_time < 60 * 60) && (_time >= 60 * 10)) {
  89. //超过十分钟少于1小时
  90. s = Math.floor(_time / 60);
  91. return s + "分钟前";
  92. } else if ((_time < 60 * 60 * 24) && (_time >= 60 * 60)) {
  93. //超过1小时少于24小时
  94. s = Math.floor(_time / 60 / 60);
  95. return s + "小时前";
  96. } else if ((_time < 60 * 60 * 24 * dayCount) && (_time >= 60 * 60 * 24)) {
  97. //超过1天少于7天内
  98. s = Math.floor(_time / 60 / 60 / 24);
  99. return s + "天前";
  100. } else {
  101. //超过7天
  102. return _formatDate(date, temp);
  103. }
  104. }
  105. }
  106. export function imgUrl(url) {
  107. if (!url) {
  108. return ""
  109. }
  110. url = String(url)
  111. const appData = getApp().globalData
  112. if (url.toLowerCase().indexOf('https://') == 0 || url.toLowerCase().indexOf('https://') == 0) {
  113. return url
  114. }
  115. return appData.imgUrl + url;
  116. }
  117. export function statusName(state) {
  118. if (state == -1) {
  119. return '已驳回'
  120. } else if (state == 0) {
  121. return '禁用'
  122. } else if (state == 1) {
  123. return '启用'
  124. } else if (state == 2) {
  125. return '审核中'
  126. }
  127. }
  128. export function newState(val) {
  129. const state = ['待审核', '已审核', '已驳回']
  130. return state[val]
  131. }
  132. export function activityState(val) {
  133. if (val == -3) {
  134. return '已驳回'
  135. } else if (val == -2) {
  136. return '待审核'
  137. } else if (val == -1) {
  138. return '暂存'
  139. } else if (val == 1) {
  140. return '已发布'
  141. } else if (val == 2) {
  142. return '活动结束'
  143. } else if (val == 3) {
  144. return '活动未开始'
  145. }
  146. }
  147. export function orderState(state) {
  148. // 0未支付 1待发货 3待收货 4已收货 5取消 6支付超时
  149. switch (state) {
  150. case 0:
  151. return '待支付'
  152. break;
  153. case 1:
  154. return '待发货'
  155. break;
  156. case 2:
  157. return '待收货'
  158. break;
  159. case 3:
  160. return '已收货'
  161. break;
  162. case -1:
  163. return '已取消'
  164. break;
  165. case -2:
  166. return '支付超时'
  167. break;
  168. case -3:
  169. return '已退款'
  170. break;
  171. default:
  172. return ""
  173. break;
  174. }
  175. }
  176. export function hidUserName(uname) {
  177. if (!uname) {
  178. return ""
  179. }
  180. uname = String(uname)
  181. return uname.length > 2 ? uname.slice(0, 1) + "*" + uname.slice(-1) : uname.slice(0, 1) + "*";
  182. }
  183. export function getDateMath(datestr, type = "day", counts = 0, temp = "yyyy-MM-dd hh:mm:ss") {
  184. var date
  185. /*
  186. type类型:
  187. day:间隔天数
  188. month: 间隔月数
  189. year:间隔年份
  190. */
  191. if (datestr) {
  192. if (!isNaN(datestr)) {
  193. datestr = new Date(parseInt(datestr))
  194. }
  195. var reg = new RegExp('-', "g");
  196. datestr = String(datestr).replace(reg, '/');
  197. date = new Date(datestr);
  198. } else {
  199. date = new Date();
  200. }
  201. switch (type) {
  202. case "year":
  203. date.setFullYear(date.getFullYear() + counts);
  204. break;
  205. case "month":
  206. date.setMonth(date.getMonth() + counts);
  207. break;
  208. default:
  209. date.setDate(date.getDate() + counts);
  210. break;
  211. }
  212. return _formatDate(date, temp)
  213. }
  214. const _formatDate = function(_date, _temp = "yyyy-MM-dd hh:mm:ss") {
  215. var o = {
  216. "M+": _date.getMonth() + 1, //月份
  217. "d+": _date.getDate(), //日
  218. "h+": _date.getHours(), //小时
  219. "m+": _date.getMinutes(), //分
  220. "s+": _date.getSeconds(), //秒
  221. "q+": Math.floor((_date.getMonth() + 3) / 3), //季度
  222. "S": _date.getMilliseconds() //毫秒
  223. };
  224. if (/(y+)/.test(_temp)) {
  225. _temp = _temp.replace(RegExp.$1, (_date.getFullYear() + "").substr(4 - RegExp.$1.length));
  226. }
  227. for (var k in o) {
  228. if (new RegExp("(" + k + ")").test(_temp)) {
  229. _temp = _temp.replace(
  230. RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  231. }
  232. }
  233. return _temp;
  234. }
  235. export function sizeDeal(size, idx = 2) {
  236. const info = uni.getSystemInfoSync()
  237. this.scale = 750 / info.windowWidth;
  238. // 分离字体大小和单位,rpx 转 px
  239. let s = Number.isNaN(parseFloat(size)) ? 0 : parseFloat(size)
  240. let u = size.toString().replace(/[0-9]/g, '').replace('-', '')
  241. if (u == 'rpx') {
  242. s /= this.scale
  243. u = 'px'
  244. } else if (u == '') {
  245. u = 'px'
  246. } else if (u == 'vw') {
  247. u = 'px'
  248. s = s / 100 * 750 / this.scale
  249. }
  250. const data = [s, u, s + u]
  251. return data[idx]
  252. }
  253. export function removeHTMLTag(str) {
  254. str = str.replace(/<\/?[^>]*>/g, '') // 去除HTML tag
  255. str = str.replace(/[ | ]*\n/g, '\n') // 去除行尾空白
  256. str = str.replace(/\n[\s| | ]*\r/g, '\n'); //去除多余空行
  257. str = str.replace(/ /ig, '') // 去掉
  258. const arrEntities = {
  259. 'lt': '<',
  260. 'gt': '>',
  261. 'nbsp': ' ',
  262. 'amp': '&',
  263. 'quot': '"'
  264. } // 转义符换成普通字符
  265. str = str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function(all, t) {
  266. return arrEntities[t]
  267. })
  268. return str
  269. }
  270. //富文本转换
  271. export function formatHtml(html, imgnum = -1) {
  272. let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
  273. match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
  274. match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
  275. match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
  276. return match;
  277. });
  278. newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
  279. match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
  280. return match;
  281. });
  282. newContent = newContent.replace(/<br[^>]*\/>/gi, '');
  283. newContent = newContent.replace(/\<img/gi,
  284. '<img class="rich-img" style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"'
  285. );
  286. let imgReg = /<img.*?(?:>|\/>)/gi //匹配图片中的img标签
  287. let arr = newContent.match(imgReg)
  288. if (imgnum >= 0 && arr && arr.length > 0) {
  289. // console.log(arr)
  290. arr.forEach((item, index) => {
  291. if (index >= imgnum) {
  292. newContent = newContent.replace(item, '');
  293. }
  294. })
  295. }
  296. newContent = newContent.replace(/\<p/gi, '<P class="rich-p" '); //正则给p标签增加class
  297. return newContent;
  298. }
  299. //多行域文本转换
  300. export function formatText(val) {
  301. if (val) {
  302. let newString = val.replace(/\n/g, '_@').replace(/\r/g, '_#');
  303. newString = newString.replace(/_#_@/g, '<br/>');
  304. newString = newString.replace(/_@/g, '<br/>');
  305. newString = newString.replace(/\s/g, '&nbsp;');
  306. return newString;
  307. }
  308. return "";
  309. }
  310. /**
  311. * 数字转中文
  312. */
  313. export function numberToChinese(num) {
  314. if (isNaN(num)) return "Number is wrong!";
  315. let AA = new Array("零", "一", "二", "三", "四", "五", "六", "七", "八", "九");
  316. let BB = new Array("", "十", "百", "千", "万", "亿", "点", "");
  317. let a = ("" + num).replace(/(^0*)/g, "").split("."),
  318. k = 0,
  319. re = "";
  320. for (let i = a[0].length - 1; i >= 0; i--) {
  321. switch (k) {
  322. case 0:
  323. re = BB[7] + re;
  324. break;
  325. case 4:
  326. if (!new RegExp("0{4}\\d{" + (a[0].length - i - 1) + "}$").test(a[0]))
  327. re = BB[4] + re;
  328. break;
  329. case 8:
  330. re = BB[5] + re;
  331. BB[7] = BB[5];
  332. k = 0;
  333. break;
  334. }
  335. if (k % 4 == 2 && a[0].charAt(i + 2) != 0 && a[0].charAt(i + 1) == 0) re = AA[0] + re;
  336. if (a[0].charAt(i) != 0) re = AA[a[0].charAt(i)] + BB[k % 4] + re;
  337. k++;
  338. }
  339. if (a.length > 1) //加上小数部分(如果有小数部分)
  340. {
  341. re += BB[6];
  342. for (let i = 0; i < a[1].length; i++) re += AA[a[1].charAt(i)];
  343. }
  344. return re;
  345. }