@deppon/deppon-request 1.15.8 → 1.15.11
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.md +46 -0
- package/es/composable.js +2 -2
- package/es/index.js +2 -2
- package/es/request/cookieDomain.js +42 -0
- package/es/request/index.js +2 -2
- package/es/request/instance.js +8 -0
- package/es/request/reqInterceptor.js +1 -37
- package/es/request/request.js +2 -2
- package/es/vue.js +7 -3
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -220,3 +220,49 @@ request({
|
|
|
220
220
|
```
|
|
221
221
|
|
|
222
222
|
更多 API 请参考源码。
|
|
223
|
+
|
|
224
|
+
## Cookie 跨域处理
|
|
225
|
+
|
|
226
|
+
本包自动处理 Cookie 跨域设置,确保在跨域请求时能够正确携带 Cookie。
|
|
227
|
+
|
|
228
|
+
### 自动设置
|
|
229
|
+
|
|
230
|
+
1. **`withCredentials`**:默认设置为 `true`,允许跨域请求携带 Cookie
|
|
231
|
+
2. **`document.domain`**:自动调用 `@deppon/deppon-utils` 的 `setCookieDomain()` 方法设置:
|
|
232
|
+
|
|
233
|
+
- 如果 URL 包含 `deppontest.com`,自动设置 `document.domain = 'deppontest.com'`
|
|
234
|
+
- 如果 URL 包含 `deppon.com`,自动设置 `document.domain = 'deppon.com'`
|
|
235
|
+
|
|
236
|
+
注意:`setCookieDomain` 方法已从 `@deppon/deppon-utils` 导入,你也可以手动调用:
|
|
237
|
+
|
|
238
|
+
```javascript
|
|
239
|
+
import { setCookieDomain } from '@deppon/deppon-utils';
|
|
240
|
+
setCookieDomain();
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
### 使用场景
|
|
244
|
+
|
|
245
|
+
- 在 UAP 环境中,需要跨域请求时携带 Cookie(如登录凭证)
|
|
246
|
+
- 在子域名之间共享 Cookie
|
|
247
|
+
- 在 iframe 环境中进行跨域请求
|
|
248
|
+
|
|
249
|
+
### 注意事项
|
|
250
|
+
|
|
251
|
+
1. `document.domain` 只能在当前域或其父域上设置
|
|
252
|
+
- 例如:如果当前域是 `a.deppon.com`,可以设置为 `deppon.com`,但不能设置为其他域
|
|
253
|
+
2. 设置 `document.domain` 后,端口号会被忽略
|
|
254
|
+
3. 如果设置失败(例如跨域限制),会静默失败,不影响其他功能
|
|
255
|
+
4. `withCredentials` 需要服务端配合设置 `Access-Control-Allow-Credentials: true` 响应头
|
|
256
|
+
|
|
257
|
+
### 示例
|
|
258
|
+
|
|
259
|
+
```javascript
|
|
260
|
+
import request from '@deppon/deppon-request';
|
|
261
|
+
|
|
262
|
+
// 跨域请求会自动携带 Cookie
|
|
263
|
+
request({
|
|
264
|
+
url: 'https://api.deppon.com/user/info',
|
|
265
|
+
method: 'get',
|
|
266
|
+
// withCredentials: true 已自动设置
|
|
267
|
+
});
|
|
268
|
+
```
|
package/es/composable.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import './_virtual/_rollup-plugin-inject-process-env.js';
|
|
2
2
|
import { inject, getCurrentInstance } from 'vue';
|
|
3
|
-
import
|
|
3
|
+
import DepponRquest from './request/request.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Composition API: useRequest
|
|
@@ -43,7 +43,7 @@ function useRequest() {
|
|
|
43
43
|
|
|
44
44
|
// 如果都没有,返回默认实例并提示
|
|
45
45
|
if (!requestFromProvide) {
|
|
46
|
-
return
|
|
46
|
+
return DepponRquest;
|
|
47
47
|
}
|
|
48
48
|
return requestFromProvide;
|
|
49
49
|
}
|
package/es/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import './_virtual/_rollup-plugin-inject-process-env.js';
|
|
2
|
-
import
|
|
2
|
+
import DepponRquest from './request/request.js';
|
|
3
3
|
export { default as VuePlugin } from './vue.js';
|
|
4
4
|
export { useRequest } from './composable.js';
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
export {
|
|
8
|
+
export { DepponRquest as default };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import '../_virtual/_rollup-plugin-inject-process-env.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 设置 document.domain 以支持 cookie 跨域
|
|
5
|
+
*
|
|
6
|
+
* 参考 cmc-prefercenter-vue 项目中的实现:
|
|
7
|
+
* - 如果 URL 包含 deppontest.com,设置 document.domain = 'deppontest.com'
|
|
8
|
+
* - 如果 URL 包含 deppon.com,设置 document.domain = 'deppon.com'
|
|
9
|
+
*
|
|
10
|
+
* 注意:document.domain 只能在当前域或其父域上设置
|
|
11
|
+
* 例如:如果当前域是 a.deppon.com,可以设置为 deppon.com,但不能设置为其他域
|
|
12
|
+
*/
|
|
13
|
+
function setCookieDomain() {
|
|
14
|
+
if (typeof window === 'undefined' || typeof document === 'undefined') {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
try {
|
|
18
|
+
var href = window.location.href;
|
|
19
|
+
|
|
20
|
+
// 检查是否包含 deppontest.com
|
|
21
|
+
if (href.indexOf('deppontest.com') !== -1) {
|
|
22
|
+
// 检查当前域是否已经是 deppontest.com 或其子域
|
|
23
|
+
var currentHost = window.location.hostname;
|
|
24
|
+
if (currentHost === 'deppontest.com' || currentHost.endsWith('.deppontest.com')) {
|
|
25
|
+
document.domain = 'deppontest.com';
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// 检查是否包含 deppon.com
|
|
30
|
+
if (href.indexOf('deppon.com') !== -1) {
|
|
31
|
+
// 检查当前域是否已经是 deppon.com 或其子域
|
|
32
|
+
var _currentHost = window.location.hostname;
|
|
33
|
+
if (_currentHost === 'deppon.com' || _currentHost.endsWith('.deppon.com')) {
|
|
34
|
+
document.domain = 'deppon.com';
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
} catch (error) {
|
|
38
|
+
// 如果设置失败(例如跨域限制),静默失败
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export { setCookieDomain };
|
package/es/request/index.js
CHANGED
package/es/request/instance.js
CHANGED
|
@@ -2,10 +2,18 @@ import '../_virtual/_rollup-plugin-inject-process-env.js';
|
|
|
2
2
|
import axios from 'axios';
|
|
3
3
|
import { res, resErr } from './resInterceptor.js';
|
|
4
4
|
import { req, reqErr } from './reqInterceptor.js';
|
|
5
|
+
import { setCookieDomain } from '@deppon/deppon-utils';
|
|
5
6
|
|
|
7
|
+
// 初始化时设置 document.domain(仅执行一次)
|
|
8
|
+
var domainInitialized = false;
|
|
9
|
+
if (!domainInitialized) {
|
|
10
|
+
setCookieDomain();
|
|
11
|
+
domainInitialized = true;
|
|
12
|
+
}
|
|
6
13
|
function appInstance(options) {
|
|
7
14
|
var App = axios.create();
|
|
8
15
|
App.defaults.timeout = process.env.NODE_ENV === 'production' ? 10000 : 300000;
|
|
16
|
+
// 允许携带 cookie(跨域请求时需要)
|
|
9
17
|
App.defaults.withCredentials = true;
|
|
10
18
|
App.interceptors.request.use(function (_req) {
|
|
11
19
|
return req(_req, options);
|
|
@@ -49,43 +49,7 @@ var replaceTrace = function replaceTrace(targetUrl, value) {
|
|
|
49
49
|
|
|
50
50
|
// check列表中是否包含pathname
|
|
51
51
|
var checkPathName = function checkPathName(pathname) {
|
|
52
|
-
var PATHS = ['ts/hotel/item'
|
|
53
|
-
// 酒店
|
|
54
|
-
'ts/groupon/item',
|
|
55
|
-
// 拼团
|
|
56
|
-
'ts/agentpurchasing/item',
|
|
57
|
-
// 盖亚
|
|
58
|
-
'ts/pn/item',
|
|
59
|
-
// 票牛
|
|
60
|
-
'ts/performance/item',
|
|
61
|
-
// 演出
|
|
62
|
-
'ts/platform/item',
|
|
63
|
-
// 平台
|
|
64
|
-
'ts/ticket/item',
|
|
65
|
-
// 门票
|
|
66
|
-
'ts/shopcard/item',
|
|
67
|
-
// 老商户卡
|
|
68
|
-
'ts/hotelcard/index',
|
|
69
|
-
// 酒店卡
|
|
70
|
-
'kt/bargain/item',
|
|
71
|
-
// 砍价一元拿
|
|
72
|
-
'ts/groupon/detail',
|
|
73
|
-
// 参团详情
|
|
74
|
-
'm/club/index',
|
|
75
|
-
// club首页
|
|
76
|
-
'm/club/detail',
|
|
77
|
-
// club详情
|
|
78
|
-
'm/bekka/coin',
|
|
79
|
-
// club充值
|
|
80
|
-
'h5/product/detail',
|
|
81
|
-
// 新商户卡
|
|
82
|
-
'h5/item/detail',
|
|
83
|
-
// 新酒店
|
|
84
|
-
'h5/product/package/detail', 'h5/product/groupon/package/detail', 'h5/product/groupon/detail',
|
|
85
|
-
// 新拼团
|
|
86
|
-
'cps/partner/groupon/item',
|
|
87
|
-
// 分销拼团
|
|
88
|
-
'cps/partner/hotel/item' // 分销酒店
|
|
52
|
+
var PATHS = ['ts/hotel/item' // 酒店
|
|
89
53
|
];
|
|
90
54
|
var isIncludes = false;
|
|
91
55
|
for (var i in PATHS) {
|
package/es/request/request.js
CHANGED
|
@@ -22,7 +22,7 @@ if (!window.onceList) {
|
|
|
22
22
|
}
|
|
23
23
|
});
|
|
24
24
|
}
|
|
25
|
-
function
|
|
25
|
+
function DepponRquest(options) {
|
|
26
26
|
// 适配当前ajax已有的功能
|
|
27
27
|
// resove cancel token
|
|
28
28
|
if (options.once) {
|
|
@@ -92,4 +92,4 @@ function autoParseParams(config) {
|
|
|
92
92
|
}
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
-
export {
|
|
95
|
+
export { DepponRquest as default };
|
package/es/vue.js
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
import './_virtual/_rollup-plugin-inject-process-env.js';
|
|
2
|
-
import
|
|
2
|
+
import DepponRquest from './request/request.js';
|
|
3
|
+
import { setCookieDomain } from '@deppon/deppon-utils';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Vue 插件安装函数
|
|
6
7
|
* @param {Object} app - Vue 应用实例
|
|
7
8
|
*/
|
|
8
9
|
function install(app) {
|
|
10
|
+
// 设置 document.domain 以支持 cookie 跨域
|
|
11
|
+
setCookieDomain();
|
|
12
|
+
|
|
9
13
|
// 注册全局属性
|
|
10
|
-
app.config.globalProperties.$request =
|
|
14
|
+
app.config.globalProperties.$request = DepponRquest;
|
|
11
15
|
|
|
12
16
|
// 提供实例,供 Composition API 使用
|
|
13
|
-
app.provide('depponRequest',
|
|
17
|
+
app.provide('depponRequest', DepponRquest);
|
|
14
18
|
}
|
|
15
19
|
var vue = {
|
|
16
20
|
install: install
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@deppon/deppon-request",
|
|
3
|
-
"version": "1.15.
|
|
3
|
+
"version": "1.15.11",
|
|
4
4
|
"description": "德邦前端 http 请求包",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "",
|
|
@@ -35,9 +35,9 @@
|
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"@babel/runtime": "^7.17.7",
|
|
38
|
-
"@deppon/deppon-bridge": "1.15.
|
|
39
|
-
"@deppon/deppon-ui": "1.15.
|
|
40
|
-
"@deppon/deppon-utils": "1.15.
|
|
38
|
+
"@deppon/deppon-bridge": "1.15.11",
|
|
39
|
+
"@deppon/deppon-ui": "1.15.11",
|
|
40
|
+
"@deppon/deppon-utils": "1.15.11",
|
|
41
41
|
"axios": "^0.26.0",
|
|
42
42
|
"md5": "^2.3.0",
|
|
43
43
|
"qs": "^6.10.3"
|
|
@@ -45,5 +45,5 @@
|
|
|
45
45
|
"peerDependencies": {
|
|
46
46
|
"vue": "^3.0.0"
|
|
47
47
|
},
|
|
48
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "77b874714bab56f83e6336762f87e9b13f234cc9"
|
|
49
49
|
}
|