@@ -11,8 +11,9 @@ import sort from '@/utils/sort'
1111import clone from '@/utils/clone'
1212import { Memory } from '@/utils/storage'
1313
14- // import { success, failed } from './interceptor' // [请求拦截]
15- // import { emit } from '../eventBus' // 通知取消请求 以便自定义取消策略使用
14+ import { success , failed } from './interceptor'
15+ import WS from './websocket'
16+ // import { emit } from '@/utils/eventBus' // 通知取消请求 以便自定义取消策略使用
1617
1718// 默认请求配置 https://github.com/axios/axios#config-defaults
1819clone ( AXIOS . defaults , {
@@ -54,6 +55,30 @@ const CancelToken = AXIOS.CancelToken
5455/** 是否被取消 */
5556const isCancel = AXIOS . isCancel
5657
58+ /** 全局请求头配置【只用于携带token等】 */
59+ let HEAD = AXIOS . defaults . headers || ( AXIOS . defaults . headers = { } )
60+ HEAD = HEAD . common || ( HEAD . common = { } )
61+
62+ /** 设置【全局】请求头
63+ * @param headOrKey
64+ * @param value
65+ * @param isToken
66+ */
67+ function setHEAD ( head : IObject ) : void
68+ function setHEAD ( name : string , value : string , isToken ?: boolean ) : void
69+ function setHEAD (
70+ headOrKey : IObject | string ,
71+ value ?: string ,
72+ isToken ?: boolean
73+ ) {
74+ if ( value ) {
75+ HEAD [ headOrKey as string ] = value
76+ isToken && ( WS . defaults . protocols = [ value ] ) // websocket 授权
77+ } else {
78+ Object . assign ( HEAD , headOrKey as IObject )
79+ }
80+ }
81+
5782/** 【debug】带上特定查询字段 */
5883let SEARCH : IObject | undefined
5984location . search
@@ -66,10 +91,6 @@ location.search
6691 }
6792 )
6893
69- /** 全局请求头配置【只用于携带token等】 */
70- let HEAD = AXIOS . defaults . headers || ( AXIOS . defaults . headers = { } )
71- HEAD = HEAD . common || ( HEAD . common = { } )
72-
7394/** 获取url (直接使用url的情况, 比如验证码、下载、上传等, 添加BaseUrl、调试参数等)
7495 * @param {string } url
7596 * @param {IObject } params 查询参数
@@ -102,6 +123,7 @@ function searchToObj(search: string) {
102123 }
103124 return Obj
104125}
126+
105127/** 获取请求标识
106128 * @param url 请求地址
107129 * @param params 查询参数
@@ -184,25 +206,20 @@ function request(
184206 requestQueue . remove ( config . key ) // 移除请求队列
185207 shouldCache && dataStore . set ( config . key , res , config . alive ) // 设置缓存
186208
187- return res // success(res) // [请求拦截]
209+ return success ( res )
188210 } )
189211 . catch ( ( res : any ) => {
190212 res . meta = config // 请求配置加到元数据
191213 requestQueue . remove ( config . key ) // 移除请求队列
192- // if (isCancel(res)) {
193- // throw res
194- // } else if (config.$_) {
195- // res = config.$_ // 自定义取消标记
196- // config.$_ = 0 // 只取消一次
197- // throw res
198- // } else {
199- // failed(res) // [请求拦截]
200- // }
201- if ( config . $_ ) {
214+ if ( isCancel ( res ) ) {
215+ throw res
216+ } else if ( config . $_ ) {
202217 res = config . $_ // 自定义取消标记
203218 config . $_ = 0 // 只取消一次
219+ throw res
220+ } else {
221+ failed ( res )
204222 }
205- throw res
206223 } )
207224
208225 // data && (cache.cancel = data) // [不划算]
@@ -309,6 +326,7 @@ export {
309326 CancelToken ,
310327 isCancel ,
311328 HEAD ,
329+ setHEAD ,
312330 getUri ,
313331 getKey ,
314332 get ,
@@ -317,4 +335,5 @@ export {
317335 post ,
318336 patch ,
319337 cancel ,
338+ WS ,
320339}
0 commit comments