king
2020-01-10 1b0fd0a20d54068f0f4716177780e00a75b860ef
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import axios from 'axios'
import qs from 'qs'
import {notification } from 'antd'
import options from '@/store/options.js'
 
// axios.defaults.baseURL = 'http://qingqiumarket.cn'
axios.defaults.crossDomain = true
// axios.defaults.headers.common['token'] = 'token'
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
axios.defaults.withCredentials = true
 
axios.interceptors.request.use((config) => {
  config.method = 'post'
  if (config.url.includes('LoginAndRedirect') || config.url.includes('getjsonresult')) {
    config.data = qs.stringify(config.data)
  } else if (config.url.includes('Upload')) {
    config.headers = { 'Content-Type': 'multipart/form-data' }
  } else {
    config.headers.token = sessionStorage.getItem('TOKEN') || ''
    config.data = JSON.stringify(config.data)
  }
 
  return config
}, (error) => {
  return Promise.reject(error)
})
 
const setCurrentUrl = () => {
  if (!!(window.history && window.history.pushState)) {
    window.history.replaceState(null, null, window.location.href.split('#')[0] + '#/login')
    window.location.reload()
  }
}
 
let GlobMap = new Map()
 
axios.interceptors.response.use((response) => {
  if (response.data.ErrCode === 'LoginError') {
    setCurrentUrl()
  } else {
    return Promise.resolve(response.data)
  }
}, (error) => {
  notification.error({
    className: 'notification-custom-error',
    bottom: 0,
    message: '状态码-' + error.response.status + ',请联系管理员',
    placement: 'bottomRight',
    duration: 15
  })
  return Promise.reject(error)
})
 
class Api {
  constructor() {
    let service = window.GLOB.service ? (/\/$/.test(window.GLOB.service) ? window.GLOB.service : window.GLOB.service + '/') : ''
    if (process.env.NODE_ENV === 'production') {
      axios.defaults.baseURL = document.location.origin + '/' + service
    } else {
      // axios.defaults.baseURL = 'http://127.0.0.1:8888'
    }
  }
 
  /**
   * @description 系统授权
   */
  systemauth (appId, timestamp) {
    return axios({
      url: '/webapi/dostar',
      data: {
        rduri: 'http://minkesoft.com/mksepc/webapi/dostar',
        func: 'sEmpowerCloud_Get_LinkUrl',
        userid: '',
        AppID: appId,
        TimeStamp: timestamp
      }
    })
  }
  
  /**
   * @description 登录系统
   */
  loginsystem (username, password) {
    return axios({
      url: '/zh-CN/Home/LoginAndRedirect',
      data: {
        Username: username,
        Password: password
      }
    })
  }
 
  /**
   * @description 登录系统, 获取用户信息
   */
  getusermsg (username, password) {
    return axios({
      url: 'webapi/getjsonresult',
      data: {
        DBT: 'proc',
        DBS: 'webapi_login',
        DBP: JSON.stringify({ 'UserName': username, 'Password': password })
      }
    })
  }
 
  /**
   * @description 获取或修改系统配置,增加appkey
   */
  getSystemConfig (param) {
    param.userid = sessionStorage.getItem('UserID')
    param.lang = localStorage.getItem('lang') || ''
    param.SessionUid = sessionStorage.getItem('SessionUid') || ''
    param.LoginUID = sessionStorage.getItem('LoginUID') || ''
    param.appkey = window.GLOB.appkey || ''
    
    if (sessionStorage.getItem('isEditState') && options.cloudServiceApi) { // 编辑状态,且存在云端地址
      param.rduri = options.cloudServiceApi
      param.userid = sessionStorage.getItem('CloudUserID')
      param.SessionUid = sessionStorage.getItem('CloudSessionUid') || ''
      param.LoginUID = sessionStorage.getItem('CloudLoginUID') || ''
    } else if (window.GLOB.mainSystemApi) {
      param.rduri = window.GLOB.mainSystemApi
    }
 
    return axios({
      url: '/webapi/dostar',
      data: param
    })
  }
 
  /**
   * @description 获取或修改本地配置,增加appkey
   */
  getLocalConfig (param) {
    param.userid = sessionStorage.getItem('UserID')
    param.lang = localStorage.getItem('lang') || ''
    param.SessionUid = sessionStorage.getItem('SessionUid') || ''
    param.LoginUID = sessionStorage.getItem('LoginUID') || ''
    param.appkey = window.GLOB.appkey || ''
 
    return axios({
      url: '/webapi/dostar',
      data: param
    })
  }
 
  /**
   * @description 获取系统配置,优先从缓存中取值,增加appkey
   */
  getSystemCacheConfig (param) {
    param.userid = sessionStorage.getItem('UserID')
    param.lang = localStorage.getItem('lang') || ''
    param.SessionUid = sessionStorage.getItem('SessionUid') || ''
    param.LoginUID = sessionStorage.getItem('LoginUID') || ''
    param.appkey = window.GLOB.appkey || ''
    if (window.GLOB.mainSystemApi) {
      param.rduri = window.GLOB.mainSystemApi
    }
 
    let _param = JSON.parse(JSON.stringify(param)) // 缓存校验,去除时间和加密字符
    delete _param.timestamp
    delete _param.secretkey
    _param = JSON.stringify(_param)
 
    if (GlobMap.has(_param)) {
      return Promise.resolve(GlobMap.get(_param))
    } else {
      return new Promise(resolve => {
        axios({
          url: '/webapi/dostar',
          data: param
        }).then(res => {
 
          GlobMap.set(_param, res)
          resolve(res)
        })
      })
    }
  }
 
  /**
   * @description 获取业务通用接口
   */
  genericInterface (param) {
    param.userid = sessionStorage.getItem('UserID')
    param.lang = localStorage.getItem('lang') || ''
    param.SessionUid = sessionStorage.getItem('SessionUid') || ''
    param.LoginUID = sessionStorage.getItem('LoginUID') || ''
 
    if (param.func === 'RolesAdd') { // 角色添加时,传appkey
      param.appkey = window.GLOB.appkey || ''
    }
 
    return axios({
      url: '/webapi/dostar',
      data: param
    })
  }
 
  /**
   * @description 导出Excel
   */
  getExcelOut (param, name) {
    param.userid = sessionStorage.getItem('UserID')
    param.lang = localStorage.getItem('lang') || ''
    param.SessionUid = sessionStorage.getItem('SessionUid') || ''
    param.LoginUID = sessionStorage.getItem('LoginUID') || ''
    
    return new Promise(resolve => {
      axios({
        url: '/webapi/doexcel',
        responseType: 'blob',
        data: param
      }).then(res => {
 
        try {
          const blob = new Blob([res])
          
          if (res.type === 'application/json') {
            const reader = new FileReader()
            reader.onload = e => resolve(JSON.parse(e.target.result))
            reader.readAsText(blob)
          } else {
            if ('download' in document.createElement('a')) { // 非IE下载
              const elink = document.createElement('a')
              elink.download = name
              elink.style.display = 'none'
              elink.href = URL.createObjectURL(blob)
              document.body.appendChild(elink)
              elink.click()
              URL.revokeObjectURL(elink.href) // 释放URL 对象
              document.body.removeChild(elink)
            } else { // IE10+下载
              navigator.msSaveBlob(blob, name)
            }
            resolve()
          }
        } catch {
          resolve({
            ErrCode: 'E',
            ErrMesg: '文件解析错误',
            message: '',
            status: false
          })
        }
      })
    })
  }
 
  /**
   * @description 文件上传
   */
  getFileUpload (param) {
    return axios({
      url: '/zh-CN/Home/Upload',
      data: param
    })
  }
 
  /**
   * @description 通用接口(数据管理)
   * @param {Object} param 查询及提交参数
   */
  commonInterface (param) {
    param.userid = sessionStorage.getItem('UserID')
    param.lang = localStorage.getItem('lang') || ''
    param.SessionUid = sessionStorage.getItem('SessionUid') || ''
    param.LoginUID = sessionStorage.getItem('LoginUID') || ''
    param.BID = param.BID || ''
    param.debug = param.debug || ''
    
    return axios({
      url: '/webapi/dostar',
      data: param
    })
  }
 
  /**
   * @description 通用接口(提交)(数据管理)
   * @param {Object} param 查询及提交参数
   */
  submitInterface (param) {
    param.userid = sessionStorage.getItem('UserID')
    param.lang = localStorage.getItem('lang') || ''
    param.SessionUid = sessionStorage.getItem('SessionUid') || ''
    param.LoginUID = sessionStorage.getItem('LoginUID') || ''
 
    return axios({
      url: '/webapi/dostar',
      data: param
    })
  }
}
 
export default new Api()