util.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import Config from "./config.js"
  2. import Vue from 'vue'
  3. const Util = {
  4. /**
  5. * 图片路径转换
  6. * @param {String} img_path 图片地址
  7. * @param {Object} params 参数,针对商品、相册里面的图片区分大中小,size: big、mid、small
  8. */
  9. img(img_path, params) {
  10. var path = ""
  11. if (img_path != undefined && typeof img_path == 'string' && img_path != "") {
  12. if (img_path.split(',').length > 1) {
  13. img_path = img_path.split(',')[0];
  14. }
  15. if (params && img_path != this.$store.getters.defaultGoodsImage) {
  16. // 过滤默认图
  17. let arr = img_path.split(".")
  18. let suffix = arr[arr.length - 1]
  19. arr.pop()
  20. arr[arr.length - 1] = arr[arr.length - 1] + "_" + params.size.toUpperCase()
  21. arr.push(suffix)
  22. img_path = arr.join(".")
  23. }
  24. if (img_path.indexOf("http://") == -1 && img_path.indexOf("https://") == -1) {
  25. path = Config.imgDomain + "/" + img_path
  26. } else {
  27. path = img_path
  28. }
  29. }
  30. return path
  31. },
  32. /**
  33. * 时间戳转日期格式
  34. * @param {Object} timeStamp
  35. */
  36. timeStampTurnTime(timeStamp) {
  37. if (timeStamp != undefined && timeStamp != "" && timeStamp > 0) {
  38. var date = new Date()
  39. date.setTime(timeStamp * 1000)
  40. var y = date.getFullYear()
  41. var m = date.getMonth() + 1
  42. m = m < 10 ? "0" + m : m
  43. var d = date.getDate()
  44. d = d < 10 ? "0" + d : d
  45. var h = date.getHours()
  46. h = h < 10 ? "0" + h : h
  47. var minute = date.getMinutes()
  48. var second = date.getSeconds()
  49. minute = minute < 10 ? "0" + minute : minute
  50. second = second < 10 ? "0" + second : second
  51. return y + "-" + m + "-" + d + " " + h + ":" + minute + ":" + second
  52. } else {
  53. return ""
  54. }
  55. },
  56. /**
  57. * 倒计时
  58. * @param {Object} seconds 秒
  59. */
  60. countDown(seconds) {
  61. let [day, hour, minute, second] = [0, 0, 0, 0]
  62. if (seconds > 0) {
  63. day = Math.floor(seconds / (60 * 60 * 24))
  64. hour = Math.floor(seconds / (60 * 60)) - day * 24
  65. minute = Math.floor(seconds / 60) - day * 24 * 60 - hour * 60
  66. second = Math.floor(seconds) - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60
  67. }
  68. if (day < 10) {
  69. // day = '0' + day
  70. }
  71. if (hour < 10) {
  72. // hour = '0' + hour
  73. }
  74. if (minute < 10) {
  75. // minute = '0' + minute
  76. }
  77. if (second < 10) {
  78. // second = '0' + second
  79. }
  80. return {
  81. d: day,
  82. h: hour,
  83. i: minute,
  84. s: second
  85. }
  86. },
  87. /**
  88. * 数值去重
  89. * @param {Array} arr 数组
  90. * @param {string} field 字段
  91. */
  92. unique(arr, field) {
  93. const res = new Map()
  94. return arr.filter(a => !res.has(a[field]) && res.set(a[field], 1))
  95. },
  96. /**
  97. * 判断值是否在数组中
  98. * @param {Object} elem
  99. * @param {Object} arr
  100. * @param {Object} i
  101. */
  102. inArray: function(elem, arr) {
  103. return arr == null ? -1 : arr.indexOf(elem)
  104. },
  105. /**
  106. * 获取某天日期
  107. * @param {Object} day
  108. */
  109. getDay: function(day) {
  110. var today = new Date()
  111. var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day
  112. today.setTime(targetday_milliseconds)
  113. const doHandleMonth = function(month) {
  114. var m = month
  115. if (month.toString().length == 1) {
  116. m = "0" + month
  117. }
  118. return m
  119. }
  120. var tYear = today.getFullYear()
  121. var tMonth = today.getMonth()
  122. var tDate = today.getDate()
  123. var tWeek = today.getDay()
  124. var time = parseInt(today.getTime() / 1000)
  125. tMonth = doHandleMonth(tMonth + 1)
  126. tDate = doHandleMonth(tDate)
  127. const week = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"]
  128. return {
  129. t: time,
  130. y: tYear,
  131. m: tMonth,
  132. d: tDate,
  133. w: week[tWeek]
  134. }
  135. },
  136. /**
  137. * 复制
  138. * @param {Object} message
  139. * @param {Object} callback
  140. */
  141. copy(value, callback) {
  142. var oInput = document.createElement("input") //创建一个隐藏input(重要!)
  143. oInput.value = value //赋值
  144. document.body.appendChild(oInput)
  145. oInput.select() // 选择对象
  146. document.execCommand("Copy") // 执行浏览器复制命令
  147. oInput.className = "oInput"
  148. oInput.style.display = "none"
  149. this.$message({
  150. message: "复制成功",
  151. type: "success"
  152. })
  153. typeof callback == "function" && callback()
  154. },
  155. /**
  156. * 深度拷贝对象
  157. * @param {Object} obj
  158. */
  159. deepClone(obj) {
  160. const isObject = function(obj) {
  161. return typeof obj == "object"
  162. }
  163. if (!isObject(obj)) {
  164. throw new Error("obj 不是一个对象!")
  165. }
  166. //判断传进来的是对象还是数组
  167. let isArray = Array.isArray(obj)
  168. let cloneObj = isArray ? [] : {}
  169. //通过for...in来拷贝
  170. for (let key in obj) {
  171. cloneObj[key] = isObject(obj[key]) ? this.deepClone(obj[key]) : obj[key]
  172. }
  173. return cloneObj
  174. },
  175. // 过滤价格
  176. filterPrice(value) {
  177. return value.toFixed(2)
  178. },
  179. pushToTab(url) {
  180. if (process.client) {
  181. if (typeof url == 'string') {
  182. let routeUrl = '';
  183. if (url.indexOf('http') != -1 || url.indexOf('https') != -1) {
  184. routeUrl = url;
  185. } else {
  186. routeUrl = Config.webDomain + url;
  187. }
  188. window.open(routeUrl, '_blank');
  189. } else if (typeof url == 'object') {
  190. let routeUrl = '';
  191. if (url.path.indexOf('http') != -1 || url.path.indexOf('https') != -1) {
  192. routeUrl = url.path;
  193. } else {
  194. routeUrl = Config.webDomain + url.path;
  195. }
  196. if (url.qurey) {
  197. Object.keys(url.qurey).forEach((value, index) => {
  198. url.qurey += (index == 0 ? '?' : '&') + value + '=' + url.qurey[value];
  199. })
  200. }
  201. window.open(routeUrl, '_blank');
  202. }
  203. }
  204. },
  205. /**
  206. * 验证手机号
  207. * @param {string} mobile 被验证的mobile
  208. * @return {object} 验证后的结果
  209. **/
  210. verifyMobile(mobile) {
  211. var parse = /^\d{11}$/.test(mobile);
  212. return parse;
  213. }
  214. }
  215. Vue.prototype.$util = Util;
  216. Vue.prototype.$img = Util.img
  217. Vue.prototype.$timeStampTurnTime = Util.timeStampTurnTime
  218. Vue.prototype.$copy = Util.copy