@gindow/vue 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/utils/get.ts DELETED
@@ -1,10 +0,0 @@
1
- export const get = (object: any, path: string | string[], defaultValue?: any): any => {
2
- if (object == null) return defaultValue
3
- const keys = Array.isArray(path) ? path : path.replace(/\[(\d+)]/g, '.$1').split('.').filter(Boolean)
4
- let result: any = object
5
- for (const key of keys) {
6
- result = result?.[key]
7
- if (result === undefined) return defaultValue
8
- }
9
- return result
10
- }
@@ -1,38 +0,0 @@
1
- export class Platform {
2
-
3
- static get userAgent() {
4
- return window.navigator.userAgent.toLowerCase()
5
- }
6
-
7
- static get isFlutter() {
8
- return Platform.userAgent.match(/flutter/i) !== null
9
- }
10
-
11
- static get isFlutterIOS() {
12
- return Platform.isFlutter && Platform.isIOS
13
- }
14
-
15
- static get isWeb() {
16
- return window.matchMedia('(min-width: 992px)').matches
17
- }
18
-
19
- static get isMobile() {
20
- return Platform.userAgent.match(/(phone|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i) !== null
21
- }
22
-
23
- static get isIOS() {
24
- return Platform.userAgent.match(/iphone|ipad|ipod|ios/i) !== null
25
- }
26
-
27
- static get isWechat() {
28
- return Platform.userAgent.match(/MicroMessenger/i) !== null
29
- }
30
-
31
- static get isWxwork() {
32
- return Platform.userAgent.match(/MicroMessenger/i) !== null && Platform.userAgent.match(/wxwork/i) !== null
33
- }
34
-
35
- static get isMiniprogram() {
36
- return Platform.userAgent.match(/MicroMessenger/i) !== null && Platform.userAgent.match(/miniprogram/i) !== null
37
- }
38
- }
@@ -1,146 +0,0 @@
1
- import type { AxiosInstance, AxiosResponse } from 'axios'
2
- import type { IModel } from '../types'
3
- import { Platform } from './platform'
4
- import cookie from 'js-cookie'
5
- import axios from 'axios'
6
-
7
- const $params = (params: IModel) => {
8
- const para = { ...params }
9
- if (typeof para?.search === 'string') return para
10
- if (para?.search)
11
- para.search = Object.keys(para.search)
12
- .filter(item => para.search[item] !== null && para.search[item] !== '')
13
- .map(item => item + ':' + para.search[item])
14
- .join(';')
15
- if (para?.searchFields)
16
- para.searchFields = Object.keys(para.searchFields)
17
- .filter(item => para.searchFields[item])
18
- .map(item => item + ':' + para.searchFields[item])
19
- .join(';')
20
- return para
21
- }
22
-
23
- export const request = {
24
-
25
- baseURL: '',
26
- publicKey: '',
27
- headers: { Accept: 'application/json' } as Record<string, string>,
28
- instance: null as AxiosInstance|null,
29
- accessToken: 'accessToken',
30
-
31
- init (config: { baseURL?: string, publicKey?: string } = {}) {
32
- this.baseURL = config.baseURL ?? ''
33
- this.publicKey = config.publicKey ?? ''
34
-
35
- const reload = () => {
36
- this.delToken()
37
- location.reload()
38
- }
39
-
40
- this.instance = axios.create({ baseURL: this.baseURL })
41
-
42
- this.instance.interceptors.request.use(config => {
43
- const language = localStorage.getItem('locale')
44
- if (language) config.headers['Accept-Language'] = language
45
- return config
46
- })
47
-
48
- this.instance.interceptors.response.use((response: AxiosResponse) => {
49
- if (response.config.responseType === 'blob') return response
50
- else if (response.data.code === 401) return reload()
51
- else if (response.data.code === 200) return response.data
52
- else return Promise.reject(response.data)
53
- }, ({ response }) => Promise.reject(response.data))
54
- },
55
-
56
- setToken (token: string, options: { expires?: number, domain?: string } = {}) {
57
- options = { ...options, domain: '.' + location.hostname }
58
- cookie.set(this.accessToken, token, options)
59
- },
60
-
61
- getToken () {
62
- return cookie.get(this.accessToken) || ''
63
- },
64
-
65
- delToken () {
66
- cookie.remove(this.accessToken, { domain: '.' + location.hostname })
67
- },
68
-
69
- get (url: string, params?: {}, auth = false) { return this.request({ method: 'GET', url, params }, auth) },
70
- put (url: string, data?: {}, auth = false) { return this.request({ method: 'PUT', url, data }, auth) },
71
- patch (url: string, data?: {}, auth = false) { return this.request({ method: 'PATCH', url, data }, auth) },
72
- delete (url: string, params?: {}, auth = false) { return this.request({ method: 'DELETE', url, params }, auth) },
73
- post (url: string, data?: {}, auth = false) { return this.request({ method: 'POST', url, data }, auth) },
74
- blob (url: string, params?: {}, auth = false) { return this.request({ method: 'GET', url, params, responseType: 'blob' }, auth) },
75
-
76
- request(para: any, auth: boolean = false) {
77
- const headers = para.headers ?? this.headers
78
- if (auth) headers.Authorization = 'Bearer ' + this.getToken()
79
- if (para.params) para.params = $params(para.params)
80
- return (this.instance as any)({ ...para, headers })
81
- },
82
-
83
- async download(url: string, params?: {}, filename?: string, auth = false) {
84
- const ret = await this.blob(url, params, auth)
85
- if (Platform.isFlutter) return ret
86
- const getResponseFileName = () => {
87
- const disposition = ret.headers['content-disposition']
88
- const parts = disposition.split("filename*=")
89
- const val = parts[1].split(";")[0].trim()
90
- return decodeURIComponent(val.replace(/^UTF-8''/i, ''))
91
- }
92
- let link = document.createElement('a')
93
- link.href = window.URL.createObjectURL(ret.data)
94
- link.download = filename ?? getResponseFileName()
95
- link.click()
96
- return ret
97
- },
98
-
99
- async upload(url: string, data = {}) {
100
- const headers = { ...this.headers, 'Content-Type': 'multipart/form-data' }
101
- return this.instance!.post(url, data, { headers })
102
- },
103
-
104
- sse (url: string, para?: {[key: string]: string | number | undefined | null | void} | null) {
105
- const params = para ? Object.keys(para).map(key => key +'='+ para[key]).join('&') : ''
106
- return new EventSource(this.baseURL + '/' + url + '?' + params)
107
- },
108
-
109
- setHeader(key: string, value: string) {
110
- this.headers[key] = value
111
- },
112
-
113
- delHeader(key: string) {
114
- delete this.headers[key]
115
- }
116
- }
117
-
118
- export class resource {
119
-
120
- url: string
121
- auth: boolean
122
-
123
- constructor(url: string, auth = false) {
124
- this.url = url
125
- this.auth = auth
126
- }
127
-
128
- select = (para?: {}) => request.get(this.url, para, this.auth)
129
-
130
- create = (para?: {}) => request.post(this.url, para, this.auth)
131
-
132
- find = (id: string, para?: {}) => request.get(this.url + '/' + id, para, this.auth)
133
-
134
- update = (id: string, para?: {}) => request.patch(this.url + '/' + id, para, this.auth)
135
-
136
- delete = (id: string, para?: {}) => request.delete(this.url + '/' + id, para, this.auth)
137
-
138
- async get(para: any) {
139
- para.size = 1
140
- const ret = await this.select(para)
141
- ret.data = ret.data[0] || {}
142
- return ret
143
- }
144
- }
145
-
146
- export { $params }