index.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import {
  2. formatTimestamp
  3. } from '@/common/util/utils'
  4. export const getYear = value => { //时间戳转年份过滤器
  5. return formatTimestamp(+value)[0]
  6. }
  7. export const getMonth = value => { //时间戳转月份过滤器
  8. const month = formatTimestamp(+value)[1] + ''
  9. return month.length === 1 ? '0' + month : month //月份只有一位数字时补零
  10. }
  11. export const getDay = value => { //时间戳转周过滤器
  12. return formatTimestamp(+value)[2]
  13. }
  14. export const getDate = value => { //时间戳转 日 过滤器
  15. const date = formatTimestamp(+value)[3] + ''
  16. return date.length === 1 ? '0' + date : date //日期份只有一位数字时补零
  17. }
  18. export const getHousr = value => { //时间戳转 时 过滤器
  19. const hour = formatTimestamp(+value)[4] + ''
  20. return hour.length === 1 ? '0' + hour : hour // 小时只有一位时补零
  21. }
  22. export const getMinute = value => { //时间戳转 分 过滤器
  23. const minute = formatTimestamp(+value)[5] + ''
  24. return minute.length === 1 ? '0' + minute : minute // 分钟只有一位时补零
  25. }
  26. export const getSecond = value => { //时间戳转 秒 过滤器
  27. return formatTimestamp(+value)[6]
  28. }
  29. export const formatTime = value => { //格式化时间
  30. return `${getYear(value)}-${getMonth(value)}-${getDate(value)} ${getHousr(value)}:${getMinute(value)}`
  31. }
  32. export const formatDate = value => { //格式化日期
  33. return `${getYear(value)}-${getMonth(value)}-${getDate(value)}`
  34. }
  35. export const formatHourMin = value => { //格式化时分
  36. return `${getHousr(value)}:${getMinute(value)}`
  37. }
  38. export const formatActivity = value => { //格式化时分
  39. return `${getMonth(value)}月${getDate(value)}日 ${getHousr(value)}:${getMinute(value)}`
  40. }
  41. export const numDot = num => {
  42. if (!num) return 0
  43. return (num + '').replace(/(?=(?:\d{3})+(?!\d))/g, ',').replace(/^,/, '') || ''
  44. }
  45. export const getOrderStatus = val => {
  46. if (+val === 0) {
  47. return '待付款'
  48. } else if (+val === 1) {
  49. return '待发货'
  50. } else if (+val === 2) {
  51. return '配送中'
  52. } else if (+val === 3) {
  53. return '已完成'
  54. } else if (+val === 4) {
  55. return '已取消'
  56. } else {
  57. return ''
  58. }
  59. }
  60. export const getAddressString = val => {
  61. return val ? `${val.provice}-${val.city}-${val.area}-${val.address}` : ''
  62. }
  63. export const DistanceNow = t => { // 计算动态距离现在的发布时间
  64. t = (t + '').length === 10 ? `${t}000` : t
  65. const d = Date.now() - Number(t)
  66. if (d >= 0 && d < 10800000) {
  67. return '刚刚发表'
  68. } else if (d >= 10800000 && d < 86400000) {
  69. return `${Math.floor(d / 3600000)}小时前`
  70. } else if (d >= 86400000) {
  71. return Math.floor(d / 86400000) > 9 ? formatDate(t) : `${Math.floor(d / 86400000)}天前`
  72. }
  73. }
  74. export function GetQueryString(name) {
  75. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  76. var r = window.location.search.substr(1).match(reg);
  77. if (r != null)
  78. return decodeURI(r[2]);
  79. return null;
  80. }
  81. export function dateFormatter(timestamp, fmt) {
  82. if (!timestamp) return ""
  83. timestamp = typeof timestamp === 'string' ? timestamp.replace(/-/g, '/') : timestamp
  84. fmt = fmt || "yyyy-MM-dd";
  85. const $this = new Date(timestamp);
  86. const o = {
  87. "M+": $this.getMonth() + 1,
  88. "d+": $this.getDate(),
  89. "h+": $this.getHours(),
  90. "m+": $this.getMinutes(),
  91. "s+": $this.getSeconds(),
  92. "q+": Math.floor(($this.getMonth() + 3) / 3),
  93. S: $this.getMilliseconds()
  94. };
  95. if (/(y+)/.test(fmt)) {
  96. fmt = fmt.replace(
  97. RegExp.$1,
  98. ($this.getFullYear() + "").substr(4 - RegExp.$1.length)
  99. );
  100. }
  101. for (var k in o) {
  102. if (new RegExp("(" + k + ")").test(fmt)) {
  103. fmt = fmt.replace(
  104. RegExp.$1,
  105. RegExp.$1.length === 1 ?
  106. o[k] :
  107. ("00" + o[k]).substr(("" + o[k]).length)
  108. );
  109. }
  110. }
  111. return fmt;
  112. }
  113. export const remove = val => {
  114. if (!val) {
  115. return false;
  116. }
  117. return val.replace(/&nbsp;/ig, '').replace(/&mdash;/ig, '').replace(/&ldquo;/ig, '').replace(/&lsquo;/ig, '')
  118. .replace(/&rdquo;/ig, '');
  119. }
  120. /*店铺使用*/
  121. export const blanceFmt = val => { //分转化为元
  122. if (!val) {
  123. return '0.00'
  124. }
  125. var num = Number(val);
  126. if (!num) { //等于0
  127. return num + '.00';
  128. } else { //不等于0
  129. num = Math.round((num) * 100) / 10000;
  130. num = num.toFixed(2);
  131. num += ''; //转成字符串
  132. var reg = num.indexOf('.') > -1 ? /(\d{1,3})(?=(?:\d{3})+\.)/g : /(\d{1,3})(?=(?:\d{3})+$)/g; //千分符的正则
  133. return num.replace(reg, '$1,') //千分位格式化
  134. }
  135. };
  136. export const getName = (val, num) => { //店铺名字截取
  137. if (!val) return '';
  138. if (num) {
  139. return val.length > num ? val.slice(0, num) + '...' : val;
  140. } else {
  141. return val.length > 10 ? val.slice(0, 10) + '...' : val;
  142. }
  143. }
  144. export const hideMiddle = (val, num) => { //隐藏号码
  145. if (val === null || !val) {
  146. return '';
  147. } else {
  148. return `${val.substring(0, num)}************${val.substring(val.length - num)}`;
  149. }
  150. }
  151. export const getStatus = val => { //显示订单的状态
  152. switch (val) {
  153. case 0:
  154. return '待付款';
  155. break;
  156. case 1:
  157. return '待发货';
  158. break;
  159. case 2:
  160. return '待收货';
  161. break;
  162. case 3:
  163. return '待评价';
  164. default:
  165. break;
  166. }
  167. }
  168. export const formatRichText = html => {
  169. //显示富文本信息控制小程序中图片大小
  170. if (html) {
  171. let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
  172. match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
  173. match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
  174. match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
  175. return match;
  176. });
  177. newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
  178. match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi,
  179. 'max-width:100%;');
  180. return match;
  181. });
  182. newContent = newContent.replace(/<br[^>]*\/>/gi, '');
  183. newContent = newContent.replace(/\<img/gi,
  184. '<img style="max-width:100%;height:auto;display:inline-block;margin:10rpx auto;"');
  185. return newContent;
  186. }
  187. }
  188. //总金额
  189. export const totalPrice = data => {
  190. let totalPrice = 0;
  191. data.map(i => {
  192. i.sku.map(j => {
  193. totalPrice += j.num * j.price;
  194. });
  195. });
  196. return totalPrice;
  197. }
  198. //总数量
  199. export const totalNum = data => {
  200. let totalNum = 0;
  201. data.map(i => {
  202. i.sku.map(j => {
  203. totalNum += j.num;
  204. });
  205. });
  206. return totalNum;
  207. }
  208. //小计数量
  209. export const subNum = data => {
  210. let subNum = 0;
  211. data.map(i => {
  212. subNum += i.num;
  213. });
  214. return subNum;
  215. }
  216. //小计金额
  217. export const subtotal = data => {
  218. let subtotal = 0;
  219. data.map(j => {
  220. subtotal += j.num * j.price;
  221. });
  222. return subtotal;
  223. }
  224. /*店铺使用end*/