utils.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**
  2. * 格式化数字,数字每隔三位加个逗号
  3. * params: num 需要处理的数字
  4. * params: len 需要保留的小数点位数
  5. */
  6. export function formattedNumber(num, len = 2) {
  7. let result = "",
  8. counter = 0;
  9. let formatNum;
  10. if (num < 0) {
  11. formatNum = formatNub(Math.abs(num) || 0, len);
  12. } else {
  13. formatNum = formatNub(num || 0, len);
  14. }
  15. let stringNum = formatNum.toString().split(".");
  16. for (let i = stringNum[0].length - 1; i >= 0; i--) {
  17. counter++;
  18. result = stringNum[0].charAt(i) + result;
  19. if (!(counter % 3) && i != 0) {
  20. result = "," + result;
  21. }
  22. }
  23. result = num < 0 ? "-" + result : result;
  24. if (stringNum[1]) {
  25. return "¥" + result + "." + stringNum[1];
  26. } else {
  27. return "¥" + result;
  28. }
  29. };
  30. /**
  31. * 不四舍五入保留n位小数
  32. * params: val 需要处理的数字
  33. * params: len 需要保留的小数点位数
  34. */
  35. export function formatNub(num, n = 2) {
  36. if (typeof num != "number" && !Number(num)) {
  37. return "0.00";
  38. }
  39. num = Number(num).toString();
  40. let result = "";
  41. let zeroResult = function(n) {
  42. let zero = "";
  43. for (let i = 0; i < n; i++) {
  44. zero += "0";
  45. }
  46. return zero;
  47. };
  48. if (num % 1 == 0) {
  49. //整数
  50. result = num + "." + zeroResult(n);
  51. } else {
  52. //小数
  53. let num1 = num.split(".");
  54. if (num1[1].length < n) {
  55. result = num1[0] + "." + num1[1] + zeroResult(n - num1[1].length);
  56. } else {
  57. result = num1[0] + "." + num1[1].substring(0, n);
  58. }
  59. }
  60. return result;
  61. };
  62. // 获取【加载更多】组件状态
  63. export function loadStatus(page, pageSize, total) {
  64. if (total / pageSize > page) {
  65. return 'loadmore';
  66. } else {
  67. return 'nomore';
  68. }
  69. }
  70. /**
  71. * 获取zhi日期前后N天的日期
  72. * n 获取自定天 负数指定前n天,正数,指定后n天
  73. * */
  74. export function funDate(n) {
  75. let date1 = new Date();
  76. let time1 =
  77. date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate(); // time1表示当前时间
  78. let date2 = new Date(date1);
  79. date2.setDate(date1.getDate() + n);
  80. let time2 =
  81. date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate();
  82. return time2;
  83. }
  84. // 指定月最后一天
  85. export function getCurrentMonthLast(time) {
  86. // console.log(time)
  87. let date = ''
  88. if (!time) {
  89. date = new Date();
  90. } else {
  91. date = new Date(time + '-01');
  92. }
  93. let currentMonth = date.getMonth();
  94. let nextMonth = ++currentMonth;
  95. let nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1);
  96. let oneDay = 1000 * 60 * 60 * 24;
  97. let lastTime = new Date(nextMonthFirstDay - oneDay);
  98. let month = parseInt(lastTime.getMonth() + 1);
  99. let day = lastTime.getDate();
  100. if (month < 10) {
  101. month = '0' + month
  102. }
  103. if (day < 10) {
  104. day = '0' + day
  105. }
  106. return date.getFullYear() + '-' + month + '-' + day;
  107. }
  108. /**
  109. * 时间转换为10位时间戳
  110. * */
  111. export function timeByTimestamp(time) {
  112. let date1 = new Date(time).getTime() / 1000;
  113. return date1;
  114. }
  115. /**
  116. * 本月第一天
  117. * */
  118. export function showMonthFirstDay() {
  119. var Nowdate = new Date();
  120. var MonthFirstDay = new Date(Nowdate.getFullYear(), Nowdate.getMonth(), 1);
  121. const M = Number(MonthFirstDay.getMonth()) + 1;
  122. return (
  123. MonthFirstDay.getFullYear() +
  124. "-" +
  125. M +
  126. "-" +
  127. MonthFirstDay.getDate() +
  128. " 00:00:00"
  129. );
  130. }
  131. /**
  132. * 判断当前页面,是使用搜索引擎接口还是使用列表接口
  133. */
  134. export const isSerch = (obj) => {
  135. let isKey = false;
  136. for (let i in obj) {
  137. let item = obj[i];
  138. if (Array.isArray(item)) {
  139. if (item.length > 0) {
  140. isKey = true;
  141. break;
  142. }
  143. } else if (
  144. typeof item === "string" ||
  145. typeof item === "number" ||
  146. typeof item === "boolean"
  147. ) {
  148. if (item) {
  149. isKey = true;
  150. break;
  151. }
  152. } else if (typeof item === "object") {
  153. if (item && JSON.stringify(item) !== "{}") {
  154. isKey = true;
  155. break;
  156. }
  157. }
  158. }
  159. return isKey;
  160. };
  161. /**
  162. * 数组去重
  163. * 按数组对象的某一个属性或两个或三个属性去重
  164. * params: arr 数组
  165. * params: property 对象属性
  166. */
  167. export const unique = (arr = [], property = []) => {
  168. for (let i = 0, len = arr.length; i < len; i++) {
  169. for (let j = i + 1; j < len; j++) {
  170. if (property.length === 1) {
  171. if (arr[i][property[0]] === arr[j][property[0]]) {
  172. arr.splice(j, 1);
  173. // splice 会改变数组长度,所以要将数组长度 len 和下标 j 减一
  174. len--;
  175. j--;
  176. }
  177. } else if (property.length === 2) {
  178. if (
  179. arr[i][property[0]] === arr[j][property[0]] &&
  180. arr[i][property[1]] === arr[j][property[1]]
  181. ) {
  182. arr.splice(j, 1);
  183. // splice 会改变数组长度,所以要将数组长度 len 和下标 j 减一
  184. len--;
  185. j--;
  186. }
  187. } else if (property.length === 3) {
  188. if (
  189. arr[i][property[0]] === arr[j][property[0]] &&
  190. arr[i][property[1]] === arr[j][property[1]] &&
  191. arr[i][property[2]] === arr[j][property[2]]
  192. ) {
  193. arr.splice(j, 1);
  194. // splice 会改变数组长度,所以要将数组长度 len 和下标 j 减一
  195. len--;
  196. j--;
  197. }
  198. } else {
  199. if (arr[i] === arr[j]) {
  200. arr.splice(j, 1);
  201. // splice 会改变数组长度,所以要将数组长度 len 和下标 j 减一
  202. len--;
  203. j--;
  204. }
  205. }
  206. }
  207. }
  208. return arr;
  209. };
  210. /**
  211. * @description 时间格式化 时间戳转换为时间
  212. * @param date
  213. * @param fmt
  214. * @return {*}
  215. */
  216. export function formatDate(date, fmt) {
  217. if (!date) {
  218. return ''
  219. }
  220. // if (typeof date !== 'string') {
  221. // return ''
  222. // }
  223. date *= (date.toString().length === 10 ? 1000 : 1)
  224. let _date = new Date(date)
  225. let _fmt = fmt || 'yyyy-MM-dd hh:mm:ss'
  226. let o = {
  227. 'M+': _date.getMonth() + 1,
  228. 'd+': _date.getDate(),
  229. 'h+': _date.getHours(),
  230. 'm+': _date.getMinutes(),
  231. 's+': _date.getSeconds()
  232. }
  233. if (/(y+)/.test(_fmt)) {
  234. _fmt = _fmt.replace(RegExp.$1, (_date.getFullYear() + '').substr(4 - RegExp.$1.length))
  235. }
  236. for (let k in o) {
  237. if (new RegExp('(' + k + ')').test(_fmt)) {
  238. _fmt = _fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k])
  239. .length)))
  240. }
  241. }
  242. return _fmt
  243. }