sh-tools 1.1.0 → 1.1.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/README.en.md ADDED
@@ -0,0 +1,36 @@
1
+ # sh-tools
2
+
3
+ #### Description
4
+ {**When you're done, you can delete the content in this README and update the file with details for others getting started with your repository**}
5
+
6
+ #### Software Architecture
7
+ Software architecture description
8
+
9
+ #### Installation
10
+
11
+ 1. xxxx
12
+ 2. xxxx
13
+ 3. xxxx
14
+
15
+ #### Instructions
16
+
17
+ 1. xxxx
18
+ 2. xxxx
19
+ 3. xxxx
20
+
21
+ #### Contribution
22
+
23
+ 1. Fork the repository
24
+ 2. Create Feat_xxx branch
25
+ 3. Commit your code
26
+ 4. Create Pull Request
27
+
28
+
29
+ #### Gitee Feature
30
+
31
+ 1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md
32
+ 2. Gitee blog [blog.gitee.com](https://blog.gitee.com)
33
+ 3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore)
34
+ 4. The most valuable open source project [GVP](https://gitee.com/gvp)
35
+ 5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help)
36
+ 6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)
package/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # sh-tools
2
+
3
+ #### 介绍
4
+ {**以下是 Gitee 平台说明,您可以替换此简介**
5
+ Gitee 是 OSCHINA 推出的基于 Git 的代码托管平台(同时支持 SVN)。专为开发者提供稳定、高效、安全的云端软件开发协作平台
6
+ 无论是个人、团队、或是企业,都能够用 Gitee 实现代码托管、项目管理、协作开发。企业项目请看 [https://gitee.com/enterprises](https://gitee.com/enterprises)}
7
+
8
+ #### 软件架构
9
+ 软件架构说明
10
+
11
+
12
+ #### 安装教程
13
+
14
+ 1. xxxx
15
+ 2. xxxx
16
+ 3. xxxx
17
+
18
+ #### 使用说明
19
+
20
+ 1. xxxx
21
+ 2. xxxx
22
+ 3. xxxx
23
+
24
+ #### 参与贡献
25
+
26
+ 1. Fork 本仓库
27
+ 2. 新建 Feat_xxx 分支
28
+ 3. 提交代码
29
+ 4. 新建 Pull Request
30
+
31
+
32
+ #### 特技
33
+
34
+ 1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md
35
+ 2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com)
36
+ 3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目
37
+ 4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目
38
+ 5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help)
39
+ 6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "sh-tools",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "基于xe-ajax和xe-utils二次封装",
5
- "main": "lib/sh-tools.common.js.gz",
5
+ "main": "packages/index.js",
6
6
  "scripts": {
7
7
  "lib": "vue-cli-service build --target lib packages/index.js --name sh-tools --dest lib"
8
8
  },
@@ -0,0 +1,99 @@
1
+ import XEAjax from 'xe-ajax'
2
+ import { Notice } from 'view-ui-plus'
3
+
4
+ class HttpRequest {
5
+ constructor(options = {}, gateWay = {}) {
6
+ this.instance = XEAjax
7
+ this.options = options
8
+ this.gateWay = gateWay
9
+ this.setOptions(options)
10
+ this.interceptors()
11
+ }
12
+ setOptions(options) {
13
+ this.instance.setup(options)
14
+ }
15
+ #setRequest(request) {
16
+ if (this.options && this.options?.setRequest && 'function' == typeof this.options.setRequest) {
17
+ this.options.setRequest(request)
18
+ }
19
+ return request
20
+ }
21
+ #setResponse(response) {
22
+ if (this.options && this.options?.setResponse && 'function' == typeof this.options.setResponse) {
23
+ this.options.setResponse(response)
24
+ }
25
+ return response
26
+ }
27
+ request(options) {
28
+ return this.instance.ajax(options)
29
+ }
30
+ get(url, params, options) {
31
+ return this.instance.get(url, params, options)
32
+ }
33
+ post(url, params, options) {
34
+ return this.instance.post(url, params, options)
35
+ }
36
+ put(url, params, options) {
37
+ return this.instance.put(url, params, options)
38
+ }
39
+ delete(url, params, options) {
40
+ return this.instance.delete(url, params, options)
41
+ }
42
+ patch(url, params, options) {
43
+ return this.instance.patch(url, params, options)
44
+ }
45
+ head(url, params, options) {
46
+ return this.instance.head(url, params, options)
47
+ }
48
+ jsonp(url, params, options) {
49
+ return this.instance.jsonp(url, params, options)
50
+ }
51
+ // 拦截器
52
+ interceptors() {
53
+ // 请求拦截
54
+ this.instance.interceptors.request.use((request, next) => {
55
+ if (this.gateWay && this.gateWay[serveName] && !request.origin) {
56
+ let serveName = request.url.split('/')[request.url.startsWith('/') ? 1 : 0]
57
+ request.origin = this.gateWay[serveName]
58
+ }
59
+ this.#setRequest(request)
60
+ next()
61
+ })
62
+ // 响应拦截
63
+ this.instance.interceptors.response.use((response, next) => {
64
+ this.responseNext(response, next)
65
+ })
66
+ }
67
+ // 错误处理
68
+ async responseNext(response, next) {
69
+ const { status, statusText, headers } = response
70
+ const duration = 5
71
+ let resBody = null
72
+ try {
73
+ resBody = await response.json()
74
+ } catch (e) {
75
+ resBody = response.__response || {}
76
+ }
77
+ let errorMsg = resBody.error || resBody.msg || resBody.message || resBody.result || statusText || '系统错误'
78
+ switch (status) {
79
+ case 0:
80
+ Notice.error({ title: '请求错误', desc: errorMsg, duration: duration })
81
+ break
82
+ case 200:
83
+ break
84
+ case 401:
85
+ Notice.error({ title: '系统提示 401', desc: '未授权,请重新登录', duration: duration })
86
+ return
87
+ case 504:
88
+ Notice.error({ title: '系统提示 网络超时', desc: errorMsg, duration: duration })
89
+ break
90
+ default:
91
+ Notice.error({ title: `系统提示 ${status}`, desc: errorMsg, duration: duration })
92
+ break
93
+ }
94
+ this.#setResponse(resBody)
95
+ next({ status: 200, body: resBody })
96
+ }
97
+ }
98
+
99
+ export default HttpRequest
@@ -0,0 +1,168 @@
1
+ const isPlainObject = value => {
2
+ if (!value || typeof value !== 'object' || {}.toString.call(value) !== '[object Object]') {
3
+ return false
4
+ }
5
+ var proto = Object.getPrototypeOf(value)
6
+ if (proto === null) {
7
+ return true
8
+ }
9
+ var Ctor = hasOwnProperty.call(proto, 'constructor') && proto.constructor
10
+ return typeof Ctor === 'function' && Ctor instanceof Ctor && Function.prototype.toString.call(Ctor) === Function.prototype.toString.call(Object)
11
+ }
12
+
13
+ const base64 = {
14
+ _keyStr: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
15
+ encode: function (input) {
16
+ if (!input && input !== 0) {
17
+ return input
18
+ }
19
+ var output = ''
20
+ var chr1, chr2, chr3, enc1, enc2, enc3, enc4
21
+ var i = 0
22
+ input = this._utf8_encode(input)
23
+ while (i < input.length) {
24
+ chr1 = input.charCodeAt(i++)
25
+ chr2 = input.charCodeAt(i++)
26
+ chr3 = input.charCodeAt(i++)
27
+ enc1 = chr1 >> 2
28
+ enc2 = ((chr1 & 3) << 4) | (chr2 >> 4)
29
+ enc3 = ((chr2 & 15) << 2) | (chr3 >> 6)
30
+ enc4 = chr3 & 63
31
+ if (isNaN(chr2)) {
32
+ enc3 = enc4 = 64
33
+ } else if (isNaN(chr3)) {
34
+ enc4 = 64
35
+ }
36
+ output = output + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4)
37
+ }
38
+ return output
39
+ },
40
+ decode: function (input) {
41
+ if (!input && input !== 0) {
42
+ return input
43
+ }
44
+ var output = ''
45
+ var chr1, chr2, chr3
46
+ var enc1, enc2, enc3, enc4
47
+ var i = 0
48
+ input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '')
49
+ while (i < input.length) {
50
+ enc1 = this._keyStr.indexOf(input.charAt(i++))
51
+ enc2 = this._keyStr.indexOf(input.charAt(i++))
52
+ enc3 = this._keyStr.indexOf(input.charAt(i++))
53
+ enc4 = this._keyStr.indexOf(input.charAt(i++))
54
+ chr1 = (enc1 << 2) | (enc2 >> 4)
55
+ chr2 = ((enc2 & 15) << 4) | (enc3 >> 2)
56
+ chr3 = ((enc3 & 3) << 6) | enc4
57
+ output = output + String.fromCharCode(chr1)
58
+ if (enc3 !== 64) {
59
+ output = output + String.fromCharCode(chr2)
60
+ }
61
+ if (enc4 !== 64) {
62
+ output = output + String.fromCharCode(chr3)
63
+ }
64
+ }
65
+ output = this._utf8_decode(output)
66
+ return output
67
+ },
68
+ _utf8_encode: function (string) {
69
+ if (!string && string !== 0) {
70
+ return string
71
+ }
72
+ string = String(string)
73
+ // console.log(string);
74
+ string = string.replace(/\r\n/g, '\n')
75
+ var utftext = ''
76
+ for (var n = 0; n < string.length; n++) {
77
+ var c = string.charCodeAt(n)
78
+ if (c < 128) {
79
+ utftext += String.fromCharCode(c)
80
+ } else if (c > 127 && c < 2048) {
81
+ utftext += String.fromCharCode((c >> 6) | 192)
82
+ utftext += String.fromCharCode((c & 63) | 128)
83
+ } else {
84
+ utftext += String.fromCharCode((c >> 12) | 224)
85
+ utftext += String.fromCharCode(((c >> 6) & 63) | 128)
86
+ utftext += String.fromCharCode((c & 63) | 128)
87
+ }
88
+ }
89
+ return utftext
90
+ },
91
+ _utf8_decode: function (utftext) {
92
+ if (!utftext && utftext !== 0) {
93
+ return utftext
94
+ }
95
+ var stringtext = ''
96
+ var i = 0
97
+ var c = 0
98
+ var c1 = 0
99
+ var c2 = 0
100
+ while (i < utftext.length) {
101
+ c = utftext.charCodeAt(i)
102
+ if (c < 128) {
103
+ stringtext += String.fromCharCode(c)
104
+ i++
105
+ } else if (c > 191 && c < 224) {
106
+ c2 = utftext.charCodeAt(i + 1)
107
+ stringtext += String.fromCharCode(((c & 31) << 6) | (c2 & 63))
108
+ i += 2
109
+ } else {
110
+ c2 = utftext.charCodeAt(i + 1)
111
+ let c3 = utftext.charCodeAt(i + 2)
112
+ stringtext += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63))
113
+ i += 3
114
+ }
115
+ }
116
+ return stringtext
117
+ },
118
+ /**
119
+ * 递归条用对参数进行编码
120
+ * @param parameters
121
+ * @returns {*}
122
+ */
123
+ encrypt: function (parameters, isRecursive) {
124
+ if (!parameters) {
125
+ return parameters
126
+ }
127
+ var curParam
128
+ if (isRecursive) {
129
+ // 递归调用
130
+ curParam = parameters
131
+ } else {
132
+ curParam = JSON.parse(JSON.stringify(parameters))
133
+ }
134
+ if (Array.isArray(curParam)) {
135
+ // 是数组
136
+ curParam.forEach((row, index) => {
137
+ if (!row && row !== 0) {
138
+ // 非空的不处理
139
+ return
140
+ }
141
+ if (isPlainObject(row) || Array.isArray(row)) {
142
+ // 对象和数组需要递归
143
+ this.encrypt(row, true)
144
+ } else {
145
+ // 其他直接加密
146
+ curParam[index] = this.encode(row)
147
+ }
148
+ })
149
+ } else if (isPlainObject(curParam)) {
150
+ // 对象
151
+ for (var key in curParam) {
152
+ if (!curParam[key] && curParam[key] !== 0) {
153
+ // 非空的不处理
154
+ continue
155
+ }
156
+ if (isPlainObject(curParam[key]) || Array.isArray(curParam[key])) {
157
+ // 对象和数组需要递归
158
+ this.encrypt(curParam[key], true)
159
+ } else {
160
+ curParam[key] = this.encode(curParam[key])
161
+ }
162
+ }
163
+ }
164
+ return curParam
165
+ }
166
+ }
167
+
168
+ export default base64
@@ -0,0 +1,92 @@
1
+ const hex = {
2
+ encode: function (b, pos, len) {
3
+ var hexCh = new Array(len * 2)
4
+ var hexCode = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
5
+ for (var i = pos, j = 0; i < len + pos; i++, j++) {
6
+ hexCh[j] = hexCode[(b[i] & 0xff) >> 4]
7
+ hexCh[++j] = hexCode[b[i] & 0x0f]
8
+ }
9
+ return hexCh.join('')
10
+ },
11
+ decode: function (hex) {
12
+ if (hex == null || hex === '') {
13
+ return null
14
+ }
15
+ if (hex.length % 2 !== 0) {
16
+ return null
17
+ }
18
+ var ascLen = hex.length / 2
19
+ var hexCh = this.toCharCodeArray(hex)
20
+ var asc = new Array(ascLen)
21
+ for (var i = 0; i < ascLen; i++) {
22
+ if (hexCh[2 * i] >= 0x30 && hexCh[2 * i] <= 0x39) {
23
+ asc[i] = (hexCh[2 * i] - 0x30) << 4
24
+ } else if (hexCh[2 * i] >= 0x41 && hexCh[2 * i] <= 0x46) {
25
+ // A-F : 0x41-0x46
26
+ asc[i] = (hexCh[2 * i] - 0x41 + 10) << 4
27
+ } else if (hexCh[2 * i] >= 0x61 && hexCh[2 * i] <= 0x66) {
28
+ // a-f : 0x61-0x66
29
+ asc[i] = (hexCh[2 * i] - 0x61 + 10) << 4
30
+ } else {
31
+ return null
32
+ }
33
+ if (hexCh[2 * i + 1] >= 0x30 && hexCh[2 * i + 1] <= 0x39) {
34
+ asc[i] = asc[i] | (hexCh[2 * i + 1] - 0x30)
35
+ } else if (hexCh[2 * i + 1] >= 0x41 && hexCh[2 * i + 1] <= 0x46) {
36
+ asc[i] = asc[i] | (hexCh[2 * i + 1] - 0x41 + 10)
37
+ } else if (hexCh[2 * i + 1] >= 0x61 && hexCh[2 * i + 1] <= 0x66) {
38
+ asc[i] = asc[i] | (hexCh[2 * i + 1] - 0x61 + 10)
39
+ } else {
40
+ return null
41
+ }
42
+ }
43
+ return asc
44
+ },
45
+ utf8StrToHex: function (utf8Str) {
46
+ var ens = encodeURIComponent(utf8Str)
47
+ var es = unescape(ens)
48
+ var esLen = es.length
49
+ // Convert
50
+ var words = []
51
+ for (var i = 0; i < esLen; i++) {
52
+ words[i] = es.charCodeAt(i).toString(16)
53
+ }
54
+ return words.join('')
55
+ },
56
+ utf8StrToBytes: function (utf8Str) {
57
+ var ens = encodeURIComponent(utf8Str)
58
+ var es = unescape(ens)
59
+ var esLen = es.length
60
+ // Convert
61
+ var words = []
62
+ for (var i = 0; i < esLen; i++) {
63
+ words[i] = es.charCodeAt(i)
64
+ }
65
+ return words
66
+ },
67
+ hexToUtf8Str: function (utf8Str) {
68
+ var utf8Byte = this.decode(utf8Str)
69
+ var latin1Chars = []
70
+ for (var i = 0; i < utf8Byte.length; i++) {
71
+ latin1Chars.push(String.fromCharCode(utf8Byte[i]))
72
+ }
73
+ return decodeURIComponent(escape(latin1Chars.join('')))
74
+ },
75
+ bytesToUtf8Str: function (bytesArray) {
76
+ var utf8Byte = bytesArray
77
+ var latin1Chars = []
78
+ for (var i = 0; i < utf8Byte.length; i++) {
79
+ latin1Chars.push(String.fromCharCode(utf8Byte[i]))
80
+ }
81
+ return decodeURIComponent(escape(latin1Chars.join('')))
82
+ },
83
+ toCharCodeArray: function (chs) {
84
+ var chArr = new Array(chs.length)
85
+ for (var i = 0; i < chs.length; i++) {
86
+ chArr[i] = chs.charCodeAt(i)
87
+ }
88
+ return chArr
89
+ }
90
+ }
91
+
92
+ export default hex
@@ -0,0 +1,13 @@
1
+ import hex from './hex'
2
+ import SM3Digest from './sm3'
3
+
4
+ function smSecret(inputtext) {
5
+ var dataBy = hex.utf8StrToBytes(inputtext)
6
+ var sm3 = new SM3Digest()
7
+ sm3.update(dataBy, 0, dataBy.length) // 数据很多的话,可以分多次update
8
+ var sm3Hash = sm3.doFinal() // 得到的数据是个byte数组
9
+ var sm3HashHex = hex.encode(sm3Hash, 0, sm3Hash.length) // 编码成16进制可见字符
10
+ return sm3HashHex
11
+ }
12
+
13
+ export default smSecret