util.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. /** *
  117. * 对象参数转为url参数
  118. * @parmas query 拼接得参数对象
  119. * @parmas isFrist 为true时开头为&
  120. */
  121. export const parse = (query, isFrist = false) => {
  122. let str = Object.keys(query)
  123. .filter(key => !isEmpty(query[key]))
  124. .reduce((result, key) => {
  125. const value = query[key];
  126. // in查询特殊处理
  127. if (Array.isArray(value) && !isEmpty(value)) {
  128. return `${result}&${value.reduce(
  129. (val, cVal) => `${val ? `${val}&` : val}${key}=${cVal}`,
  130. ''
  131. )}`;
  132. }
  133. // between查询做特殊处理
  134. if (typeof value === 'object' && !isEmpty(value)) {
  135. const [start, end] = value;
  136. return `${result}&${key}[]=${start}&${key}[]=${end}`;
  137. }
  138. return `${result}&${key}=${value}`;
  139. }, '');
  140. return isFrist ? str : str.replace(/^&/, '?');
  141. };
  142. const $checkName = str => {
  143. return /^[\u4E00-\u9FA5A-Za-z\s]+(·[\u4E00-\u9FA5A-Za-z]+)*$/.test(str);
  144. };
  145. /** *
  146. * 全局时间转换
  147. * @parmas num 时间戳默认为11位时间戳
  148. * @parmas fmt 默认转换的时间格式
  149. * @parmas all 为true时time为13位时间戳
  150. */
  151. export const $formatDate = (num, fmt = 'YYYY-MM-DD HH:mm', all) => {
  152. num = all ? num : num * 1000;
  153. let date = new Date();
  154. date.setTime(num);
  155. let o = {
  156. 'M+': date.getMonth() + 1,
  157. 'D+': date.getDate(),
  158. 'h+': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12,
  159. 'H+': date.getHours(),
  160. 'm+': date.getMinutes(),
  161. 's+': date.getSeconds(),
  162. 'q+': Math.floor((date.getMonth() + 3) / 3),
  163. S: date.getMilliseconds()
  164. };
  165. let week = {
  166. '0': '\u65e5',
  167. '1': '\u4e00',
  168. '2': '\u4e8c',
  169. '3': '\u4e09',
  170. '4': '\u56db',
  171. '5': '\u4e94',
  172. '6': '\u516d'
  173. };
  174. if (/(Y+)/.test(fmt)) {
  175. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  176. }
  177. if (/(E+)/.test(fmt)) {
  178. fmt = fmt.replace(
  179. RegExp.$1,
  180. (RegExp.$1.length > 1 ? (RegExp.$1.length > 2 ? '\u661f\u671f' : '\u5468') : '') +
  181. week[date.getDay() + '']
  182. );
  183. }
  184. for (let k in o) {
  185. if (new RegExp('(' + k + ')').test(fmt)) {
  186. fmt = fmt.replace(
  187. RegExp.$1,
  188. RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
  189. );
  190. }
  191. }
  192. return fmt;
  193. };
  194. export function checkPhone(val) {
  195. return (/^1(3|4|5|6|7|8|9)\d{9}$/.test(val));
  196. }
  197. export default {
  198. isEmpty,
  199. copyObject,
  200. parse,
  201. pageTo,
  202. $checkName,
  203. $formatDate,
  204. getShortUrl,
  205. checkPhone
  206. };