PageSourceSearch

https://takungpao.com/static/tkwwtemplate/js/api/http.js

js takungpao.com collected 2026-10-02 03:06:29 UTC 3,354 bytes, 118 lines download raw bytes

1//注意:后置错误拦截中不使用promise.reject
2//1、后置错误请求会走.then()
3//2、后置错误await会继续走
4import Storage from "../utils/storage.js"
5import Util from "../utils/util.js"
6class Request {
7  constructor(config){
8    // 使用axios.create创建axios实例
9    this.instance = axios.create(config)
10    this.instance.interceptors.request.use(
11      (config) => {
12        // 获取截取到请求参数,做一些处理
13        return this.initConfig({config})
14      },
15      (err) => {
16        console.log("fail,request err")
17        return
18      }
19    )
20    this.instance.interceptors.response.use(
21      (res) => {
22        const {status,data,config: {common: {
23          showMessage,
24          showSuccessMessage
25        }}} = res
26        if (status === 200) {
27          if(showMessage){
28            if(data.message != "success"){
29              showErrorMessage(data.message)
30              return
31            }
32          }
33          if(showSuccessMessage){
34            if(data.message == "success"){
35              showInfoMessage(data.data.resultMessage)
36            }
37          }
38          // 请求成功
39          return data
40        }
41          // 错误提醒
42        console.log("success,status=",status)
43        return
44      },
45      (err) => {
46        if(err.response&&err.response.status){
47          let {status} = err.response
48          if(status===401){
49            return loginout()
50          }
51        }
52        const {alertText,showFailMessage} = err.config.common
53        if(alertText){
54          alert(alertText)
55        }
56        let data = err.response&&err.response.data?err.response.data:""
57        if(data !== "" &&showFailMessage){
58          showErrorMessage(data.message)
59        }
60        // 请求错误
61        return
62      }
63    )
64  }
65  //初始化请求配置
66  initConfig({config}){
67    const {method,common: {
68      useToken,
69    }} = config
70    const contentType = config.headers["Content-Type"]
71    const access_token = Storage.getCookie({key:"access_token"})
72    // 请求拦截里面加token
73    if(Util.isNull(config.headers.Authorization)&&!Util.isNull(access_token)&&useToken){
74      config.headers.Authorization = `Bearer ${access_token}`
75    }
76    
77    //測試用 start
78    /*if(testToken!==""){
79      config.headers.Authorization = testToken
80    }
81    else if(!Util.isNull(access_token)&&useToken){
82      config.headers.Authorization = `Bearer ${access_token}`
83    }*/
84    //測試用 end
85    
86    //不同请求类型的配置
87    switch(method){
88      case "post":{
89        config.headers["Content-Type"] = Util.isNull(contentType)?"application/x-www-form-urlencoded":contentType
90        /*Util.isNull(contentType)?config.headers["Content-Type"] = "application/x-www-form-urlencoded" : ""*/
91        break
92      }
93    }
94    return config
95  }
96  // 定义请求方法
97  request({config}) {
98    return this.instance.request(config)
99  }
100
101  get({url,config}){
102    return this.instance.get(url, config)
103  }
104
105  post({url,data,config}){
106    return this.instance.post(url, data, config)
107  }
108
109  put({url,data,config}){
110    return this.instance.put(url, data, config)
111  }
112
113  delete({url,config}){
114    return this.instance.delete(url, config)
115  }
116}
117
118export default Request

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.