util.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import CONSTANT from '@/constant'
  2. import { mobileReg } from '@/utils/tools/validate'
  3. import { getMiniAppUrl, isWxBrowser } from '@/utils/common'
  4. // 是否为空
  5. const isEmpty = (val) => {
  6. if (val instanceof Array) {
  7. if (val.length === 0) return true
  8. } else if (val instanceof Object) {
  9. if (!Object.keys(val).length) return true
  10. } else {
  11. if (val === 'null' || val === null || val === 'undefined' || val === undefined || val === '') return true
  12. return false
  13. }
  14. return false
  15. }
  16. const numberFormat =(number,type)=> {
  17. if(number > 10000) {
  18. let num = (number / 10000 + '').split('.')
  19. return `${num[0]}.${(''+ num[1]).slice(0,1)} 万`
  20. }else {
  21. return number
  22. }
  23. }
  24. // 截取图片上传字符串
  25. const imgSubstr = (val) => {
  26. if (isEmpty(val)) return []
  27. if (isArray(val)) {
  28. return val.map(e => {
  29. let index = e.indexOf('/uploads/')
  30. e = e.substring(index)
  31. return e
  32. })
  33. } else {
  34. let index = val.indexOf('/uploads/')
  35. return val.substring(index)
  36. }
  37. }
  38. // 是否为数组
  39. const isArray = (arr) => {
  40. return (typeof arr == 'object') && arr.constructor == Array
  41. }
  42. // 是否为字符串
  43. const isString = (str) => {
  44. return (typeof str == 'string') && str.constructor == String
  45. }
  46. // 是否为对象
  47. const isObject = (obj) => {
  48. return (typeof obj == 'object') && obj.constructor == Object
  49. }
  50. // 是否为数字
  51. const isNumber = (num) => {
  52. return (typeof num == 'number') && num.constructor == Number
  53. }
  54. // 是否为日期类型
  55. const isDate = (date) => {
  56. return (typeof date == 'object') && date.constructor == Date
  57. }
  58. // 是否为函数
  59. const isFunction = (obj) => {
  60. return (typeof obj == 'object') && obj.constructor == Function
  61. }
  62. const checkMobile = (num) => {
  63. return mobileReg.test(num)
  64. }
  65. const checkName = (str) => {
  66. return /^[\u4E00-\u9FA5A-Za-z\s]+(·[\u4E00-\u9FA5A-Za-z]+)*$/.test(str)
  67. }
  68. /** *
  69. * 检查登录
  70. * @parmas query 拼接得参数对象
  71. * @parmas isFrist 为true时开头为&
  72. */
  73. export async function getCheckLogin() {
  74. let status = false
  75. let token = uni.getStorageSync('token')
  76. if (token) {
  77. status = true
  78. } else {
  79. // #ifdef H5
  80. if (isWxBrowser()) {
  81. let option = JSON.parse(uni.getStorageSync('currentQuery')) || {}
  82. console.log('option', option)
  83. // console.log('option', getMiniAppUrl(option, true))
  84. // return false
  85. location.href = `${CONSTANT.hostUrl}/auth/prepare?url=${encodeURIComponent(getMiniAppUrl(option, true))}`
  86. } else {
  87. // uni.showToast({
  88. // title: '请用微信浏览器打开!',
  89. // icon: 'none',
  90. // mask: true
  91. // })
  92. }
  93. // #endif
  94. // #ifndef H5
  95. // #endif
  96. }
  97. return status
  98. }
  99. export const floatFormat = (f, n) => {
  100. let m = Math.pow(10, n)
  101. return parseInt(f * m, 10) / m
  102. }
  103. /** *
  104. * 对象参数转为url参数
  105. * @parmas query 拼接得参数对象
  106. * @parmas isFrist 为true时开头为&
  107. */
  108. export const parse = (query, isFrist = false) => {
  109. let str = Object.keys(query)
  110. .filter(key => !isEmpty(query[key]))
  111. .reduce((result, key) => {
  112. const value = query[key]
  113. // in查询特殊处理
  114. if (Array.isArray(value) && !isEmpty(value)) {
  115. return `${result}&${value.reduce((val, cVal) => `${val ? `${val}&` : val}${key}=${cVal}`, '')}`
  116. }
  117. // between查询做特殊处理
  118. if (typeof value === 'object' && !isEmpty(value)) {
  119. const [start, end] = value
  120. return `${result}&${key}[]=${start}&${key}[]=${end}`
  121. }
  122. return `${result}&${key}=${value}`
  123. }, '')
  124. return isFrist ? str : str.replace(/^&/, '?')
  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(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '\u661f\u671f' : '\u5468') : '') + week[date.getDay() + ''])
  160. }
  161. for (let k in o) {
  162. if (new RegExp('(' + k + ')').test(fmt)) {
  163. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
  164. }
  165. }
  166. return fmt
  167. }
  168. /** *
  169. * 全局公共按钮跳转
  170. * @parmas item.funtion 为可执行函数
  171. * @parmas item.url 为跳转的路径
  172. * @parmas item.query 为跳转携带的参数
  173. * @parmas item.type 为跳转调用的函数 1为navigateTo, 2为redirectTo, 3为reLaunch, 4为switchTab
  174. */
  175. export const pageTo = (item) => {
  176. if (isNumber(item)) {
  177. uni.navigateBack({
  178. delta: item
  179. })
  180. }
  181. if (isString(item)) {
  182. uni.navigateTo({
  183. url: item
  184. })
  185. return false
  186. }
  187. if (item.funtion) {
  188. item.funtion()
  189. }
  190. if (item.url) {
  191. let query = item.query ? parse(item.query) : ''
  192. let allUrl = `${item.url}${query}`
  193. if (item.type == 2) {
  194. uni.redirectTo({
  195. url: allUrl
  196. })
  197. } else if (item.type == 3) {
  198. uni.reLaunch({
  199. url: allUrl
  200. })
  201. } else if (item.type == 4) {
  202. console.log('switchTab', item.query)
  203. uni.setStorageSync('switchTabQuery', item.query)
  204. uni.switchTab({
  205. url: item.url
  206. })
  207. } else {
  208. uni.navigateTo({
  209. url: allUrl
  210. })
  211. }
  212. }
  213. }
  214. /** *
  215. * 是否登录
  216. */
  217. export const isLogin = () => {
  218. let token = uni.getStorageSync('token')
  219. if (token) {
  220. return true
  221. }
  222. return false
  223. }
  224. export default {
  225. isEmpty,
  226. imgSubstr,
  227. isArray,
  228. parse,
  229. checkMobile,
  230. checkName,
  231. $formatDate,
  232. floatFormat,
  233. pageTo,
  234. isLogin,
  235. getCheckLogin,
  236. numberFormat
  237. }