File size: 5,963 Bytes
81d8726
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { reactive } from 'vue'
import JSRsaEncrypt from 'jsencrypt'
import CryptoJS from 'crypto-js'
import socketIo from 'socket.io-client'
import useStore from '../store'

export const EventBus = reactive({})

EventBus.$emit = (event, data) => {
  if (EventBus[event]) {
    EventBus[event].forEach(callback => callback(data))
  }
}

EventBus.$on = (event, callback) => {
  if (!EventBus[event]) {
    EventBus[event] = []
  }
  EventBus[event].push(callback)
}

EventBus.$off = (event, callback) => {
  if (!EventBus[event]) return
  if (!callback) {
    delete EventBus[event]
  } else {
    EventBus[event] = EventBus[event].filter(cb => cb !== callback)
  }
}

export const randomStr = (e) =>{
  e = e || 16
  let str = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678',
    a = str.length,
    res = ''
  for (let i = 0; i < e; i++) res += str.charAt(Math.floor(Math.random() * a))
  return res
}

export const jwtExpireToTimestamp = (str) => {
  const match = /^(\d+)([smhd])$/.exec(str)
  if (!match) throw new Error('jwt过期格式错误,必须是 [xx]s / [xx]h / [xx]d')

  const n = Number(match[1])
  const unit = match[2]

  const map = {
    s: 1000,
    m: 60 * 1000,
    h: 60 * 60 * 1000,
    d: 24 * 60 * 60 * 1000
  }

  return n * map[unit] + Date.now()
}

// rsa公钥加密
export const RSAEncrypt = (text) => {
  const publicKey = localStorage.getItem('publicKey')
  if (!publicKey) return -1 // 公钥不存在
  const RSAPubEncrypt = new JSRsaEncrypt() // 生成实例
  RSAPubEncrypt.setPublicKey(publicKey) // 配置公钥(不是将公钥实例化时传入!!!)
  const ciphertext = RSAPubEncrypt.encrypt(text) // 加密
  // console.log('rsa公钥加密:', ciphertext)
  return ciphertext
}

// aes加密
export const AESEncrypt = (text, secretKey) => {
  let ciphertext = CryptoJS.AES.encrypt(text, secretKey).toString()
  return ciphertext
}

// aes解密
export const AESDecrypt = (ciphertext, secretKey) => {
  let bytes = CryptoJS.AES.decrypt(ciphertext, secretKey)
  let originalText = bytes.toString(CryptoJS.enc.Utf8)
  return originalText
}

export const sortString = (arr = []) => {
  return arr.sort((a, b) => {
    let c1 = ''
    let c2 = ''
    let temp = a.length > b.length ? b : a
    for (let i = 0; i < temp.length; i++) {
      c1 = a[i].toLowerCase()
      c2 = b[i].toLowerCase()
      if (c1 !== c2) break
    }
    return c1.charCodeAt() - c2.charCodeAt()
  })
}

export const dirType = ['d', 'l',] // 文件夹或者链接文件夹

export const fileType = ['-',] // 文本文件或者二进制文件

export const isDir = (type) => dirType.includes(type)

export const isFile = (type) => fileType.includes(type)

export const sortDirTree = (tree = []) => {
  const dirsAndlinks = tree.filter(item => isDir(item.type))
  const others = tree.filter(item => !(isDir(item.type)))
  const sort = (arr = []) => {
    return arr.sort((a, b) => {
      const { name: aName } = a
      const { name: bName } = b
      let c1 = ''
      let c2 = ''
      let temp = aName.length > bName.length ? bName : aName
      for (let i = 0; i < temp.length; i++) {
        c1 = aName[i].toLowerCase()
        c2 = bName[i].toLowerCase()
        if (c1 !== c2) break
      }
      return c1.charCodeAt() - c2.charCodeAt()
    })
  }
  sort(dirsAndlinks)
  sort(others)
  let res = [].concat(dirsAndlinks, others)
  let homeDirIndex = res.findIndex(item => item.name === 'home')
  if (homeDirIndex !== -1) {
    let homeDir = res[homeDirIndex]
    res.splice(homeDirIndex, 1)
    res.unshift(homeDir)
  }
  let rootDirIndex = res.findIndex(item => item.name === 'root')
  if (rootDirIndex !== -1) {
    let rootDir = res[rootDirIndex]
    res.splice(rootDirIndex, 1)
    res.unshift(rootDir)
  }
  return res
}

export const downloadFile = ({ buffer, name }) => {
  let contentUrl = window.URL.createObjectURL(new Blob([buffer,])) // params:object 可选: File对象、Blob对象、MediaSource对象。
  let link = document.createElement('a')
  link.style.display = 'none'
  link.href = contentUrl
  console.log(name)
  link.setAttribute('download', name) // 文件名称
  document.body.appendChild(link)
  link.click()
  setTimeout(() => {
    document.body.removeChild(link)
    window.URL.revokeObjectURL(contentUrl)
  })
}

export const getSuffix = (name = '') => {
  return String(name).split(/\./).pop()
}

export const exportFile = (data, filename, mimeType = 'application/json') =>{
  const blob = new Blob([JSON.stringify(data, null, 2),], { type: mimeType })
  const url = window.URL.createObjectURL(blob)

  const link = document.createElement('a')
  link.href = url
  link.setAttribute('download', filename)

  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
  window.URL.revokeObjectURL(url)
}

export const isHttps = () => {
  return window.location.protocol === 'https:'
}

export const isMobile = () => {
  let userAgent = navigator.userAgent || navigator.vendor || window.opera
  return /android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(userAgent)
}

export const handlePlusSupport = () => {
  window.open('https://en.221022.xyz/buy-plus', '_blank')
}

export const isDockerId = (id) => {
  return /^[a-f0-9]{12}|[a-f0-9]{64}$/.test(id)
}

export const isDockerComposeYml = (str) => {
  return /^docker-compose\.ya?ml$/.test(str)
}

export const isValidDate = (dateString) => {
  if (!dateString) return false
  const date = new Date(dateString)
  return !isNaN(date.getTime()) && date instanceof Date
}

const { protocol, hostname } = location
const serviceURI = import.meta.env.DEV ? `${ protocol }//${ hostname }:8082` : location.origin
export const generateSocketInstance = (path, config = { forceNew: false, reconnection: true, reconnectionAttempts: 3 }, query = {}) => {
  return socketIo(serviceURI, {
    withCredentials: true,
    auth: {
      token: useStore().token
    },
    path,
    query: {
      ...query
    },
    ...config
  })
}