index.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. import {
  2. formatTimestamp
  3. } from '@/common/util/utils'
  4. // 检测10位补0后转换,13位直接转换
  5. export function dateFormatterNew(timestamp, fmt) {
  6. if (!timestamp) return ""
  7. if (timestamp.toString().length == 10) {
  8. timestamp = timestamp * 1000
  9. }
  10. timestamp = typeof timestamp === 'string' ? timestamp.replace(/-/g, '/') : timestamp
  11. fmt = fmt || "yyyy-MM-dd";
  12. const $this = new Date(timestamp);
  13. const o = {
  14. "M+": $this.getMonth() + 1,
  15. "d+": $this.getDate(),
  16. "h+": $this.getHours(),
  17. "m+": $this.getMinutes(),
  18. "s+": $this.getSeconds(),
  19. "q+": Math.floor(($this.getMonth() + 3) / 3),
  20. S: $this.getMilliseconds()
  21. };
  22. if (/(y+)/.test(fmt)) {
  23. fmt = fmt.replace(
  24. RegExp.$1,
  25. ($this.getFullYear() + "").substr(4 - RegExp.$1.length)
  26. );
  27. }
  28. for (var k in o) {
  29. if (new RegExp("(" + k + ")").test(fmt)) {
  30. fmt = fmt.replace(
  31. RegExp.$1,
  32. RegExp.$1.length === 1 ?
  33. o[k] :
  34. ("00" + o[k]).substr(("" + o[k]).length)
  35. );
  36. }
  37. }
  38. return fmt;
  39. }
  40. export const getYear = value => { //时间戳转年份过滤器
  41. return formatTimestamp(+value)[0]
  42. }
  43. export const getMonth = value => { //时间戳转月份过滤器
  44. const month = formatTimestamp(+value)[1] + ''
  45. return month.length === 1 ? '0' + month : month //月份只有一位数字时补零
  46. }
  47. export const getDay = value => { //时间戳转周过滤器
  48. return formatTimestamp(+value)[2]
  49. }
  50. export const getDate = value => { //时间戳转 日 过滤器
  51. const date = formatTimestamp(+value)[3] + ''
  52. return date.length === 1 ? '0' + date : date //日期份只有一位数字时补零
  53. }
  54. export const getHousr = value => { //时间戳转 时 过滤器
  55. const hour = formatTimestamp(+value)[4] + ''
  56. return hour.length === 1 ? '0' + hour : hour // 小时只有一位时补零
  57. }
  58. export const getMinute = value => { //时间戳转 分 过滤器
  59. const minute = formatTimestamp(+value)[5] + ''
  60. return minute.length === 1 ? '0' + minute : minute // 分钟只有一位时补零
  61. }
  62. export const getSecond = value => { //时间戳转 秒 过滤器
  63. return formatTimestamp(+value)[6]
  64. }
  65. export const formatTime = value => { //格式化时间
  66. return `${getYear(value)}-${getMonth(value)}-${getDate(value)} ${getHousr(value)}:${getMinute(value)}`
  67. }
  68. export const formatDate = value => { //格式化日期
  69. return `${getYear(value)}-${getMonth(value)}-${getDate(value)}`
  70. }
  71. export const formatHourMin = value => { //格式化时分
  72. return `${getHousr(value)}:${getMinute(value)}`
  73. }
  74. export const formatActivity = value => { //格式化时分
  75. return `${getMonth(value)}月${getDate(value)}日 ${getHousr(value)}:${getMinute(value)}`
  76. }
  77. export const numDot = num => {
  78. if (!num) return 0
  79. return (num + '').replace(/(?=(?:\d{3})+(?!\d))/g, ',').replace(/^,/, '') || ''
  80. }
  81. export const getOrderStatus = val => {
  82. if (+val === 0) {
  83. return '待付款'
  84. } else if (+val === 1) {
  85. return '待发货'
  86. } else if (+val === 2) {
  87. return '配送中'
  88. } else if (+val === 3) {
  89. return '已完成'
  90. } else if (+val === 4) {
  91. return '已取消'
  92. } else {
  93. return ''
  94. }
  95. }
  96. export const willDeg = (value) => {
  97. if (value <= 200) {
  98. return '大学毕业'
  99. }
  100. if (value >= 201 && value <= 500) {
  101. return '学士毕业'
  102. } else if (value >= 501 && value <= 1000) {
  103. return '硕士毕业'
  104. } else if (value >= 1001 && value <= 1500) {
  105. return '博士毕业'
  106. } else if (value >= 1501) {
  107. return '成为学霸'
  108. }
  109. return ''
  110. }
  111. export const lessScore = (value) => {
  112. if (value <= 500) {
  113. return 500 - value
  114. } else if (value >= 501 && value <= 1000) {
  115. return 1000 - value
  116. } else if (value >= 1001 && value <= 1500) {
  117. return 1500 - value
  118. } else if (value >= 1501 && value <= 2000) {
  119. return 2000 - value
  120. } else if (value >= 2001) {
  121. return ''
  122. }
  123. return ''
  124. }
  125. export const getAddressString = val => {
  126. return val ? `${val.province ? val.province : val.provice}-${val.city}-${val.area}-${val.address}` : ''
  127. }
  128. export const DistanceNow = t => { // 计算动态距离现在的发布时间
  129. t = (t + '').length === 10 ? `${t}000` : t
  130. const d = Date.now() - Number(t)
  131. if (d >= 0 && d < 10800000) {
  132. return '刚刚发表'
  133. } else if (d >= 10800000 && d < 86400000) {
  134. return `${Math.floor(d / 3600000)}小时前`
  135. } else if (d >= 86400000) {
  136. return Math.floor(d / 86400000) > 9 ? formatDate(t) : `${Math.floor(d / 86400000)}天前`
  137. }
  138. }
  139. export function GetQueryString(name) {
  140. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  141. var r = window.location.search.substr(1).match(reg);
  142. if (r != null)
  143. return decodeURI(r[2]);
  144. return null;
  145. }
  146. export function dateFormatter(timestamp, fmt) {
  147. if (!timestamp) return ""
  148. timestamp = typeof timestamp === 'string' ? timestamp.replace(/-/g, '/') : timestamp
  149. fmt = fmt || "yyyy-MM-dd";
  150. const $this = new Date(timestamp);
  151. const o = {
  152. "M+": $this.getMonth() + 1,
  153. "d+": $this.getDate(),
  154. "h+": $this.getHours(),
  155. "m+": $this.getMinutes(),
  156. "s+": $this.getSeconds(),
  157. "q+": Math.floor(($this.getMonth() + 3) / 3),
  158. S: $this.getMilliseconds()
  159. };
  160. if (/(y+)/.test(fmt)) {
  161. fmt = fmt.replace(
  162. RegExp.$1,
  163. ($this.getFullYear() + "").substr(4 - RegExp.$1.length)
  164. );
  165. }
  166. for (var k in o) {
  167. if (new RegExp("(" + k + ")").test(fmt)) {
  168. fmt = fmt.replace(
  169. RegExp.$1,
  170. RegExp.$1.length === 1 ?
  171. o[k] :
  172. ("00" + o[k]).substr(("" + o[k]).length)
  173. );
  174. }
  175. }
  176. return fmt;
  177. }
  178. export const remove = val => {
  179. if (!val) {
  180. return false;
  181. }
  182. return val.replace(/&nbsp;/ig, '').replace(/&mdash;/ig, '').replace(/&ldquo;/ig, '').replace(/&lsquo;/ig, '')
  183. .replace(/&rdquo;/ig, '');
  184. }
  185. /*店铺使用*/
  186. export const blanceFmt = val => { //分转化为元
  187. var num = Number(val);
  188. if (!num) { //等于0
  189. return num + '.00';
  190. } else { //不等于0
  191. num = Math.round((num) * 100) / 10000;
  192. num = num.toFixed(2);
  193. num += ''; //转成字符串
  194. var reg = num.indexOf('.') > -1 ? /(\d{1,3})(?=(?:\d{3})+\.)/g : /(\d{1,3})(?=(?:\d{3})+$)/g; //千分符的正则
  195. return num.replace(reg, '$1,') //千分位格式化
  196. }
  197. };
  198. export const getName = (val, num) => { //店铺名字截取
  199. if (!val) return '';
  200. if (num) {
  201. return val.length > num ? val.slice(0, num) + '...' : val;
  202. } else {
  203. return val.length > 10 ? val.slice(0, 10) + '...' : val;
  204. }
  205. }
  206. export const hideMiddle = (val, num) => { //隐藏号码
  207. if (val === null || !val) {
  208. return '';
  209. } else {
  210. return `${val.substring(0, num)}************${val.substring(val.length - num)}`;
  211. }
  212. }
  213. export const getStatus = val => { //显示订单的状态
  214. switch (val) {
  215. case 0:
  216. return '待付款';
  217. break;
  218. case 1:
  219. return '待发货';
  220. break;
  221. case 2:
  222. return '待收货';
  223. break;
  224. case 3:
  225. return '待评价';
  226. default:
  227. break;
  228. }
  229. }
  230. export const formatRichText = html => {
  231. //显示富文本信息控制小程序中图片大小
  232. if (html) {
  233. let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
  234. match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
  235. match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
  236. match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
  237. return match;
  238. });
  239. newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
  240. match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi,
  241. 'max-width:100%;');
  242. return match;
  243. });
  244. newContent = newContent.replace(/<br[^>]*\/>/gi, '');
  245. newContent = newContent.replace(/\<img/gi,
  246. '<img style="max-width:100%;height:auto;display:inline-block;margin:10rpx auto;"');
  247. return newContent;
  248. }
  249. }
  250. export const sendDay = times => {
  251. //截取字符串年月日
  252. let timestr = '';
  253. var timearr = times
  254. .replace(' ', ':')
  255. .replace(/\:/g, '-')
  256. .split('-');
  257. var date = new Date();
  258. let month = date.getMonth() + 1;
  259. if (month != timearr[1]) { //如果不是当前月份,显示年月
  260. timestr = timearr[0] + '-' + timearr[1]
  261. } else {
  262. timestr = date.getDate() - Number(timearr[2]) > 0 ? date.getDate() - Number(timearr[2]) + '天前' : '今天'
  263. }
  264. return timestr;
  265. }
  266. //总金额
  267. export const totalPrice = data => {
  268. let totalPrice = 0;
  269. data.map(i => {
  270. i.sku.map(j => {
  271. totalPrice += j.num * j.price;
  272. });
  273. });
  274. return totalPrice;
  275. }
  276. //总数量
  277. export const totalNum = data => {
  278. let totalNum = 0;
  279. data.map(i => {
  280. i.sku.map(j => {
  281. totalNum += j.num;
  282. });
  283. });
  284. return totalNum;
  285. }
  286. //小计数量
  287. export const subNum = data => {
  288. let subNum = 0;
  289. data.map(i => {
  290. subNum += i.num;
  291. });
  292. return subNum;
  293. }
  294. //小计金额
  295. export const subtotal = data => {
  296. let subtotal = 0;
  297. data.map(j => {
  298. subtotal += j.num * j.price;
  299. });
  300. return subtotal;
  301. }
  302. /*店铺使用end*/
  303. export const format = num => {
  304. if (!num) return '';
  305. return '0'.repeat(2 - String(Math.floor(num / 60)).length) + Math.floor(num / 60) + ':' + '0'.repeat(2 -
  306. String(Math.floor(num % 60)).length) + Math.floor(num % 60)
  307. }
  308. export const moreNum = value => { //超出9999显示9999+
  309. if (!value) return 0
  310. if (value >= 9999) {
  311. return '9999+'
  312. } else {
  313. return value
  314. }
  315. }