index.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 getAddressString = val => {
  97. return val ? `${val.provice}-${val.city}-${val.area}-${val.address}` : ''
  98. }
  99. export const DistanceNow = t => { // 计算动态距离现在的发布时间
  100. t = (t + '').length === 10 ? `${t}000` : t
  101. const d = Date.now() - Number(t)
  102. if (d >= 0 && d < 10800000) {
  103. return '刚刚发表'
  104. } else if (d >= 10800000 && d < 86400000) {
  105. return `${Math.floor(d / 3600000)}小时前`
  106. } else if (d >= 86400000) {
  107. return Math.floor(d / 86400000) > 9 ? formatDate(t) : `${Math.floor(d / 86400000)}天前`
  108. }
  109. }
  110. export function GetQueryString(name) {
  111. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  112. var r = window.location.search.substr(1).match(reg);
  113. if (r != null)
  114. return decodeURI(r[2]);
  115. return null;
  116. }
  117. export function dateFormatter(timestamp, fmt) {
  118. if (!timestamp) return ""
  119. timestamp = typeof timestamp === 'string' ? timestamp.replace(/-/g, '/') : timestamp
  120. fmt = fmt || "yyyy-MM-dd";
  121. const $this = new Date(timestamp);
  122. const o = {
  123. "M+": $this.getMonth() + 1,
  124. "d+": $this.getDate(),
  125. "h+": $this.getHours(),
  126. "m+": $this.getMinutes(),
  127. "s+": $this.getSeconds(),
  128. "q+": Math.floor(($this.getMonth() + 3) / 3),
  129. S: $this.getMilliseconds()
  130. };
  131. if (/(y+)/.test(fmt)) {
  132. fmt = fmt.replace(
  133. RegExp.$1,
  134. ($this.getFullYear() + "").substr(4 - RegExp.$1.length)
  135. );
  136. }
  137. for (var k in o) {
  138. if (new RegExp("(" + k + ")").test(fmt)) {
  139. fmt = fmt.replace(
  140. RegExp.$1,
  141. RegExp.$1.length === 1 ?
  142. o[k] :
  143. ("00" + o[k]).substr(("" + o[k]).length)
  144. );
  145. }
  146. }
  147. return fmt;
  148. }
  149. export const remove = val => {
  150. if (!val) {
  151. return false;
  152. }
  153. return val.replace(/&nbsp;/ig, '').replace(/&mdash;/ig, '').replace(/&ldquo;/ig, '').replace(/&lsquo;/ig, '')
  154. .replace(/&rdquo;/ig, '');
  155. }
  156. /*店铺使用*/
  157. export const blanceFmt = val => { //分转化为元
  158. var num = Number(val);
  159. if (!num) { //等于0
  160. return num + '.00';
  161. } else { //不等于0
  162. num = Math.round((num) * 100) / 10000;
  163. num = num.toFixed(2);
  164. num += ''; //转成字符串
  165. var reg = num.indexOf('.') > -1 ? /(\d{1,3})(?=(?:\d{3})+\.)/g : /(\d{1,3})(?=(?:\d{3})+$)/g; //千分符的正则
  166. return num.replace(reg, '$1,') //千分位格式化
  167. }
  168. };
  169. export const getName = (val, num) => { //店铺名字截取
  170. if (!val) return '';
  171. if (num) {
  172. return val.length > num ? val.slice(0, num) + '...' : val;
  173. } else {
  174. return val.length > 10 ? val.slice(0, 10) + '...' : val;
  175. }
  176. }
  177. export const hideMiddle = (val, num) => { //隐藏号码
  178. if (val) {
  179. return `${val.substring(0, num)}************${val.substring(val.length - num)}`;
  180. } else {
  181. return '';
  182. }
  183. }
  184. export const getStatus = val => { //显示订单的状态
  185. switch (val) {
  186. case 0:
  187. return '待付款';
  188. break;
  189. case 1:
  190. return '待发货';
  191. break;
  192. case 2:
  193. return '待收货';
  194. break;
  195. case 3:
  196. return '待评价';
  197. default:
  198. break;
  199. }
  200. }
  201. export const formatRichText = html => {
  202. //显示富文本信息控制小程序中图片大小
  203. if (html) {
  204. let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
  205. match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
  206. match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
  207. match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
  208. return match;
  209. });
  210. newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
  211. match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi,
  212. 'max-width:100%;');
  213. return match;
  214. });
  215. newContent = newContent.replace(/<br[^>]*\/>/gi, '');
  216. newContent = newContent.replace(/\<img/gi,
  217. '<img style="max-width:100%;height:auto;display:inline-block;margin:10rpx auto;"');
  218. return newContent;
  219. }
  220. }
  221. //总金额
  222. export const totalPrice = data => {
  223. let totalPrice = 0;
  224. data.map(i => {
  225. i.sku.map(j => {
  226. totalPrice += j.num * j.price;
  227. });
  228. });
  229. return totalPrice;
  230. }
  231. //总数量
  232. export const totalNum = data => {
  233. let totalNum = 0;
  234. data.map(i => {
  235. i.sku.map(j => {
  236. totalNum += j.num;
  237. });
  238. });
  239. return totalNum;
  240. }
  241. //小计数量
  242. export const subNum = data => {
  243. let subNum = 0;
  244. data.map(i => {
  245. subNum += i.num;
  246. });
  247. return subNum;
  248. }
  249. //小计金额
  250. export const subtotal = data => {
  251. let subtotal = 0;
  252. data.map(j => {
  253. subtotal += j.num * j.price;
  254. });
  255. return subtotal;
  256. }
  257. /*店铺使用end*/