util.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // import router from '@/router';
  2. import router from '@/router';
  3. // 是否为空
  4. const isEmpty = val => {
  5. if (val instanceof Array) {
  6. if (val.length === 0) return true;
  7. } else if (val instanceof Object) {
  8. if (!Object.keys(val).length) return true;
  9. } else {
  10. if (
  11. val === 'null' ||
  12. val === null ||
  13. val === 'undefined' ||
  14. val === undefined ||
  15. val === ''
  16. )
  17. return true;
  18. return false;
  19. }
  20. return false;
  21. };
  22. const getShortUrl = url => {
  23. if (isEmpty(url)) return '';
  24. let startIndex = url.indexOf('/uploads');
  25. let endIndex = url.indexOf('_small');
  26. let urlArr = url.split('.');
  27. return url.substring(startIndex, endIndex) + '.' + urlArr[urlArr.length - 1];
  28. };
  29. /**
  30. * 深拷贝对象
  31. * @param {要拷贝对象} obj
  32. */
  33. const copyObject = obj => {
  34. let str,
  35. newobj = Array.isArray(obj) === true ? [] : {};
  36. if (typeof obj !== "object") {
  37. return;
  38. } else if (JSON) {
  39. (str = JSON.stringify(obj)), //系列化对象
  40. (newobj = JSON.parse(str)); //还原
  41. } else {
  42. for (let i in obj) {
  43. newobj[i] = typeof obj[i] === "object" ? copyObject(obj[i]) : obj[i];
  44. }
  45. }
  46. return newobj;
  47. };
  48. /** *
  49. * 全局公共按钮跳转
  50. * @parmas item.funtion 为可执行函数
  51. * @parmas item.url 为跳转的路径
  52. * @parmas item.query 为跳转携带的参数
  53. * @parmas item.type 为跳转调用的函数 1为navigateTo, 2为redirectTo, 3为reLaunch, 4为switchTab
  54. */
  55. export const pageTo = item => {
  56. if (isNumber(item)) {
  57. // uni.navigateBack({
  58. // delta: item
  59. // })
  60. }
  61. if (isString(item)) {
  62. router.push({
  63. path: item
  64. });
  65. return false;
  66. }
  67. if (item.funtion) {
  68. item.funtion();
  69. }
  70. if (item.url) {
  71. let query = item.query ? parse(item.query) : '';
  72. let allUrl = `${item.url}${query}`;
  73. if (item.type == 2) {
  74. router.replace({
  75. path: allUrl
  76. });
  77. } else if (item.type == 3) {
  78. // uni.reLaunch({
  79. // url: allUrl
  80. // });
  81. } else if (item.type == 4) {
  82. // uni.switchTab({
  83. // url: allUrl
  84. // });
  85. } else {
  86. router.push({
  87. path: allUrl
  88. });
  89. }
  90. }
  91. };
  92. // 是否为数组
  93. const isArray = arr => {
  94. return typeof arr == 'object' && arr.constructor == Array;
  95. };
  96. // 是否为字符串
  97. const isString = str => {
  98. return typeof str == 'string' && str.constructor == String;
  99. };
  100. // 是否为对象
  101. const isObject = obj => {
  102. return typeof obj == 'object' && obj.constructor == Object;
  103. };
  104. // 是否为数字
  105. const isNumber = num => {
  106. return typeof num == 'number' && num.constructor == Number;
  107. };
  108. // 是否为日期类型
  109. const isDate = date => {
  110. return typeof date == 'object' && date.constructor == Date;
  111. };
  112. // 是否为函数
  113. const isFunction = obj => {
  114. return typeof obj == 'object' && obj.constructor == Function;
  115. };
  116. const isMoney = money =>{
  117. return /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/.test(money)
  118. }
  119. /** *
  120. * 对象参数转为url参数
  121. * @parmas query 拼接得参数对象
  122. * @parmas isFrist 为true时开头为&
  123. */
  124. export const parse = (query, isFrist = false) => {
  125. let str = Object.keys(query)
  126. .filter(key => !isEmpty(query[key]))
  127. .reduce((result, key) => {
  128. const value = query[key];
  129. // in查询特殊处理
  130. if (Array.isArray(value) && !isEmpty(value)) {
  131. return `${result}&${value.reduce(
  132. (val, cVal) => `${val ? `${val}&` : val}${key}=${cVal}`,
  133. ''
  134. )}`;
  135. }
  136. // between查询做特殊处理
  137. if (typeof value === 'object' && !isEmpty(value)) {
  138. const [start, end] = value;
  139. return `${result}&${key}[]=${start}&${key}[]=${end}`;
  140. }
  141. return `${result}&${key}=${value}`;
  142. }, '');
  143. return isFrist ? str : str.replace(/^&/, '?');
  144. };
  145. const $checkName = str => {
  146. return /^[\u4E00-\u9FA5A-Za-z\s]+(·[\u4E00-\u9FA5A-Za-z]+)*$/.test(str);
  147. };
  148. /** *
  149. * 全局时间转换
  150. * @parmas num 时间戳默认为11位时间戳
  151. * @parmas fmt 默认转换的时间格式
  152. * @parmas all 为true时time为13位时间戳
  153. */
  154. export const $formatDate = (num, fmt = 'YYYY-MM-DD HH:mm', all) => {
  155. num = all ? num : num * 1000;
  156. let date = new Date();
  157. date.setTime(num);
  158. let o = {
  159. 'M+': date.getMonth() + 1,
  160. 'D+': date.getDate(),
  161. 'h+': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12,
  162. 'H+': date.getHours(),
  163. 'm+': date.getMinutes(),
  164. 's+': date.getSeconds(),
  165. 'q+': Math.floor((date.getMonth() + 3) / 3),
  166. S: date.getMilliseconds()
  167. };
  168. let week = {
  169. '0': '\u65e5',
  170. '1': '\u4e00',
  171. '2': '\u4e8c',
  172. '3': '\u4e09',
  173. '4': '\u56db',
  174. '5': '\u4e94',
  175. '6': '\u516d'
  176. };
  177. if (/(Y+)/.test(fmt)) {
  178. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  179. }
  180. if (/(E+)/.test(fmt)) {
  181. fmt = fmt.replace(
  182. RegExp.$1,
  183. (RegExp.$1.length > 1 ? (RegExp.$1.length > 2 ? '\u661f\u671f' : '\u5468') : '') +
  184. week[date.getDay() + '']
  185. );
  186. }
  187. for (let k in o) {
  188. if (new RegExp('(' + k + ')').test(fmt)) {
  189. fmt = fmt.replace(
  190. RegExp.$1,
  191. RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
  192. );
  193. }
  194. }
  195. return fmt;
  196. };
  197. export function checkPhone(val) {
  198. return (/^1(3|4|5|6|7|8|9)\d{9}$/.test(val));
  199. }
  200. export default {
  201. isEmpty,
  202. copyObject,
  203. parse,
  204. pageTo,
  205. $checkName,
  206. $formatDate,
  207. getShortUrl,
  208. checkPhone,
  209. isMoney
  210. };