util.js 4.9 KB

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