util.js 4.5 KB

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