byt-ui 0.1.3 → 0.1.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "byt-ui",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "private": false,
5
5
  "description": "byt组件库",
6
6
  "author": {
@@ -2,7 +2,7 @@
2
2
  * @Description:
3
3
  * @Author: 王国火
4
4
  * @Date: 2022-09-19 10:17:14
5
- * @LastEditTime: 2024-04-18 16:26:34
5
+ * @LastEditTime: 2024-04-22 12:56:46
6
6
  * @LastEditors: 王国火
7
7
  */
8
8
  // 动态引入
@@ -11,10 +11,13 @@ const requireContext = require.context('./modules', true, /.*\.js/)
11
11
  requireContext.keys().map(key => {
12
12
  const reg = /\w+/
13
13
  const k = key.match(reg)[0]
14
- if (requireContext(key).default) {
15
- conmmon[k] = requireContext(key).default
16
- } else {
17
- conmmon = Object.assign(conmmon, requireContext(key))
14
+ const cnt = requireContext(key)
15
+ if (!cnt) return
16
+ const conf = { ...cnt }
17
+ if (conf.default) {
18
+ conmmon[k] = conf.default
19
+ delete (conf.default)
18
20
  }
21
+ conmmon = Object.assign(conmmon, cnt)
19
22
  })
20
23
  export default conmmon
@@ -2,22 +2,30 @@
2
2
  * @Description:
3
3
  * @Author: 王国火
4
4
  * @Date: 2022-10-18 12:37:03
5
- * @LastEditTime: 2024-04-19 13:27:26
5
+ * @LastEditTime: 2024-04-22 14:40:35
6
6
  * @LastEditors: 王国火
7
7
  */
8
8
  import Cookie from 'js-cookie'
9
9
  import website from './website'
10
10
  export const getCookie = (key) => {
11
- const searchKey = `${website.key}-${key}`
12
- return Cookie.get(searchKey) || ''
11
+ const fullKey = `${website.key}-${key}`;
12
+ return Cookie.get(fullKey, {
13
+ domain: window.location.hostname
14
+ }) || ''
13
15
  }
14
16
 
15
17
  export const setCookie = (key, value, expires = 7, path = '/') => {
16
- return Cookie.set(`${website.key}-${key}`, value, { expires, path })
18
+ const fullKey = `${website.key}-${key}`;
19
+ return Cookie.set(fullKey, value, {
20
+ expires,
21
+ path,
22
+ domain: window.location.hostname
23
+ })
17
24
  }
18
25
 
19
26
  export const removeCookie = (key, path = '/') => {
20
- return Cookie.remove(`${website.key}-${key}`, {
27
+ const fullKey = `${website.key}-${key}`;
28
+ return Cookie.remove(fullKey, {
21
29
  path,
22
30
  domain: window.location.hostname
23
31
  })
@@ -0,0 +1,159 @@
1
+ import axios from 'axios'
2
+ import { Message } from 'element-ui'
3
+ import { getCookie } from './cookie'
4
+
5
+ class Request {
6
+ constructor(config = {}) {
7
+ const { options = {}, handler = {}} = config;
8
+ this.request = null;
9
+ this._errorCode = {
10
+ '000': '操作太频繁,请勿重复请求',
11
+ '401': '当前操作没有权限',
12
+ '403': '当前操作没有权限',
13
+ '404': '接口不存在',
14
+ '417': '未绑定登录账号,请使用密码登录后绑定',
15
+ '423': '演示环境不能操作,如需了解联系我们',
16
+ '426': '用户名不存在或密码错误',
17
+ '428': '验证码错误,请重新输入',
18
+ '429': '请求过频繁',
19
+ '479': '演示环境,没有权限操作',
20
+ 'default': '系统未知错误,请反馈给管理员'
21
+ }
22
+ this._methods = ['get', 'post', 'delete', 'put']
23
+ this.handler = Object.assign({}, handler);
24
+ this.options = Object.assign({}, {
25
+ baseURL: '/bytserver',
26
+ responseType: 'json',
27
+ timeout: 60000,
28
+ withCredentials: false
29
+ }, options)
30
+ // 注册fetch;
31
+ this.register()
32
+ }
33
+
34
+ interceptors() {
35
+ // HTTPrequest拦截
36
+ this.request.interceptors.request.use(config => {
37
+ const TENANT_ID = getCookie('tenantId')
38
+ const isToken = (config.headers || {}).isToken === false
39
+ const token = getCookie('access_token')
40
+ if (token && !isToken) {
41
+ config.headers['Authorization'] = `Bearer ${token}`// token
42
+ }
43
+ if (TENANT_ID) {
44
+ config.headers['TENANT-ID'] = TENANT_ID // 租户ID
45
+ }
46
+ return config
47
+ }, error => {
48
+ return Promise.reject(error)
49
+ })
50
+
51
+ // HTTPresponse拦截
52
+ this.request.interceptors.response.use(res => {
53
+ const status = Number(res.status) || 200
54
+ const message = res.data.msg || this._errorCode[status] || this._errorCode['default']
55
+ switch (status * 1) {
56
+ case 200:
57
+ if (res.data.code === 1) {
58
+ Message.error(message)
59
+ this.handler.error && this.handler.error(res.data);
60
+ return Promise.reject(res.data)
61
+ }
62
+ this.handler.success && this.handler.success(res.data)
63
+ return res.data
64
+ case 424:
65
+ case 428:
66
+ // 后台定义 424||428 针对令牌过期的特殊响应码
67
+ this.handler.expire && this.handler.expire(res.data);
68
+ break;
69
+ default:
70
+ Message.error(message);
71
+ this.handler.error && this.handler.error(res.data);
72
+ return Promise.reject(message)
73
+ }
74
+ }, error => {
75
+ if (error.response) {
76
+ const status = error.response.status
77
+ switch (status) {
78
+ case 404:
79
+ Message.error(this._errorCode[status])
80
+ break;
81
+ case 503:
82
+ Message.error(error.response.data.msg)
83
+ break;
84
+ }
85
+ this.error && this.error(error.response);
86
+ }
87
+ return Promise.reject(new Error(error))
88
+ })
89
+ }
90
+
91
+ setMethods() {
92
+ this._methods.forEach(v => {
93
+ this.request[v] = ({
94
+ url,
95
+ data = {},
96
+ params = {},
97
+ responseType = 'json',
98
+ headers = {},
99
+ retry = 0
100
+ }) => {
101
+ return new Promise((resolve, reject) => {
102
+ this.request({
103
+ url,
104
+ method: v.toUpperCase(),
105
+ data,
106
+ params,
107
+ responseType,
108
+ headers
109
+ }).then(res => {
110
+ if (
111
+ !res.code ||
112
+ res.code === 0 ||
113
+ responseType == 'arraybuffer' ||
114
+ responseType == 'blob'
115
+ ) {
116
+ resolve(res)
117
+ } else {
118
+ Message.error(res.msg)
119
+ reject(res)
120
+ }
121
+ }).catch(err => {
122
+ // 重试请求
123
+ reject(err)
124
+ if (retry > 0) {
125
+ this.request[v]({
126
+ url,
127
+ data,
128
+ params,
129
+ responseType,
130
+ headers,
131
+ retry: retry - 1
132
+ })
133
+ }
134
+ })
135
+ })
136
+ }
137
+ })
138
+ }
139
+
140
+ register() {
141
+ this.request = axios.create(this.options)
142
+
143
+ // 添加拦截器
144
+ this.interceptors();
145
+
146
+ // 覆盖自定义方法
147
+ this.setMethods();
148
+ }
149
+ }
150
+
151
+ export const { request } = new Request()
152
+
153
+ export default {
154
+ install(Vue, options = {}) {
155
+ const { request } = new Request(Object.assign({}, options))
156
+ Vue.prototype.$http = request
157
+ }
158
+ }
159
+
@@ -2,7 +2,7 @@
2
2
  * @Description:
3
3
  * @Author: 王国火
4
4
  * @Date: 2024-04-18 15:37:44
5
- * @LastEditTime: 2024-04-19 13:50:16
5
+ * @LastEditTime: 2024-04-22 08:56:36
6
6
  * @LastEditors: 王国火
7
7
  */
8
8
  import * as sentry from '@sentry/vue'
@@ -21,9 +21,34 @@ class Sentry {
21
21
  this.register();
22
22
  }
23
23
 
24
+ getUserInfo() {
25
+ // 处理要提交的用户信息
26
+ const { fullPath } = this.router.currentRoute;
27
+ const userInfo = getCookie('userInfo') || getStore('userInfo');
28
+ const tenantId = getCookie('tenantId') || ''
29
+ const params = {
30
+ path: fullPath,
31
+ title: document.title,
32
+ ip: window.location.host
33
+ }
34
+ let opts = {};
35
+ if (userInfo) {
36
+ const info = JSON.parse(userInfo);
37
+ const { username, email } = info;
38
+ opts = Object.assign(params, info, {
39
+ id: tenantId,
40
+ username,
41
+ email
42
+ })
43
+ } else {
44
+ opts = params
45
+ }
46
+ return opts
47
+ }
48
+
24
49
  captureEvent({
25
50
  message = '',
26
- level = 'error',
51
+ level = 'info',
27
52
  tags = {
28
53
  type: 'xhr-error',
29
54
  category: 'xhr-category'
@@ -31,6 +56,7 @@ class Sentry {
31
56
  extra = {},
32
57
  breadcrumbs = []
33
58
  }) {
59
+ // 错误上报
34
60
  sentry.captureEvent({
35
61
  message,
36
62
  level,
@@ -40,6 +66,7 @@ class Sentry {
40
66
  })
41
67
  }
42
68
  init(dsn) {
69
+ // init调用
43
70
  const params = {
44
71
  Vue: this.Vue,
45
72
  dsn,
@@ -60,32 +87,16 @@ class Sentry {
60
87
  },
61
88
  beforeSend: (e) => {
62
89
  // 请求发送前添加用户信息
63
- const { fullPath } = this.router.currentRoute;
64
- const userInfo = getCookie('userInfo') || getStore('userInfo');
65
- const tenantId = getCookie('tenantId') || ''
66
- const params = {
67
- path: fullPath,
68
- title: document.title,
69
- ip: window.location.host
70
- }
71
- if (userInfo) {
72
- const info = JSON.parse(userInfo);
73
- const { username, email } = info;
74
- e.user = Object.assign(params, info, {
75
- id: tenantId,
76
- username,
77
- email
78
- })
79
- } else {
80
- e.user = params
81
- }
90
+ e.user = this.getUserInfo();
82
91
  return e;
83
92
  },
84
93
  beforeSendTransaction: (e) => {
94
+ // 请求发送前添加用户信息,修改transaction为title
85
95
  e.transaction = document.title
96
+ e.user = this.getUserInfo();
86
97
  return e
87
98
  },
88
- // 任何请求都会调用的函数,处理请求status_code不是200时,自定义上报错误信息
99
+ // 任何请求都会调用的函数,处理请求status_code不是200时,或者code=1,自定义上报错误信息
89
100
  beforeBreadcrumb: (scope, hint) => {
90
101
  if (scope.category == 'xhr') {
91
102
  const statusCode = scope.data.status_code;
@@ -135,23 +146,29 @@ class Sentry {
135
146
  // 允许自定义配置覆盖、合并默认配置
136
147
  sentry.init(Object.assign({}, params, this.options))
137
148
  }
138
- register() {
139
- this.axios({
149
+ async register() {
150
+ const res = await this.axios({
140
151
  url: '/leo-tech-bridge/sysParam/getSentryDsn',
141
152
  method: 'GET'
142
- }).then((res) => {
143
- // 需要兼容商城和业务中台
144
- const val = res instanceof Object ? res.data : res
145
- if (val) {
146
- // 重新处理拼接dsn地址
147
- const localhost = window.location;
148
- const result = val.split('@');
149
- const dsn = `${localhost.protocol}//${result[0]}@${localhost.host}${result[1]}`;
150
- this.init(dsn);
151
- }
152
- })
153
+ });
154
+ // 需要兼容商城和业务中台
155
+ const val = res instanceof Object ? res.data : res;
156
+ if (val) {
157
+ // 重新处理拼接dsn地址
158
+ const localhost = window.location;
159
+ const result = val.split('@');
160
+ const dsn = `${localhost.protocol}//${result[0]}@${localhost.host}${result[1]}`;
161
+ this.init(dsn);
162
+ }
153
163
  }
154
164
  }
155
165
 
156
- export default Sentry
166
+ export default {
167
+ install(Vue, options) {
168
+ const sentry = new Sentry(Object.assign({
169
+ Vue
170
+ }, options))
171
+ Vue.prototype.$sentry = sentry
172
+ }
173
+ }
157
174