提交 c5c5cc55 authored 作者: 王鹏飞's avatar 王鹏飞

bug fixes

上级 79ad72c7
......@@ -4,7 +4,7 @@ import _ from 'lodash'
import { MessageBox, Message } from 'element-ui'
export default class API {
constructor (config) {
constructor(config) {
/* 创建一个 自定义配置axios实例 */
// 让ajax携带cookie
axios.defaults.withCredentials = true
......@@ -24,14 +24,16 @@ export default class API {
}
/* 获取当前Vue创建实例 */
getVueInstance () {
getVueInstance() {
return window.G.$instance_vue
}
/* 重新封装 请求时的执行函数 */
_request (_config = {}) {
_request(_config = {}) {
/* 具体执行请求成功后业务逻辑前,先执行该方法 */
const beforeSuccess = _config.beforeSuccess ? _config.beforeSuccess : this._reqSuccess
const beforeSuccess = _config.beforeSuccess
? _config.beforeSuccess
: this._reqSuccess
/* 具体执行请求失败后业务逻辑前,先执行该方法 */
const beforeFail = _config.beforeFail ? _config.beforeFail : this._reqFail
const headers = {
......@@ -40,7 +42,9 @@ export default class API {
}
_config.headers = _.assignIn(_config.headers, headers)
/* 判别 传输方式 */
if (_config.headers['Content-Type'] === 'application/x-www-form-urlencoded') {
if (
_config.headers['Content-Type'] === 'application/x-www-form-urlencoded'
) {
let str = ''
const _obj = _config.data || _config.params
for (const key in _obj) {
......@@ -62,11 +66,13 @@ export default class API {
_config.data = fr
}
/* 创建并根据参数发起请求 */
return this._axios(_config)
.then(beforeSuccess.bind(this), beforeFail.bind(this))
return this._axios(_config).then(
beforeSuccess.bind(this),
beforeFail.bind(this)
)
}
setConfirm (titleStr, btnStr, msgStr) {
setConfirm(titleStr, btnStr, msgStr) {
return MessageBox.confirm(msgStr, titleStr, {
confirmButtonText: btnStr,
type: 'warning',
......@@ -77,12 +83,16 @@ export default class API {
})
}
goLoginIndex (_vIn) {
goLoginIndex(_vIn) {
const href = window.location.href
if (/\/login\/index/gi.test(href)) {
_vIn.$router.go(0)
} else {
_vIn.$router.push({ path: '/login/index?rd=' + encodeURIComponent(href.replace(/.*?\/\/.*?\//gi, '/')) })
_vIn.$router.push({
path:
'/login/index?rd=' +
encodeURIComponent(href.replace(/.*?\/\/.*?\//gi, '/'))
})
}
}
......@@ -91,35 +101,15 @@ export default class API {
* 注意:如果不能满足需求,可在接口定义处重新实现
* @param {[object]} res 返回数据
*/
_reqSuccess (res) {
const _vIn = this.getVueInstance()
const { status, data } = res
if (status === 200) {
if (/util\/kaosx/gi.test(res.config.url)) {
return data
}
/* 针对 不同的 接口做一下 统一处理 */
/* 带 code 参数,新接口模型 */
if (data && data.code !== undefined) {
if (data.code !== 0) {
if (!/account\/get-user-info/gi.test(res.config.url)) {
data.msg && Message({ type: 'error', message: data.msg })
}
}
_reqSuccess(res) {
const { data } = res
/* 带 code 参数,新接口模型 */
if (data.code !== undefined) {
if (data.code !== 0 && !/get-user-info$/gi.test(res.config.url)) {
data.msg && Message({ type: 'error', message: data.msg })
}
return data
} else if (status === 403 && !/\/getinfo$/gi.test(res.config.url)) {
/* 不带 code 参数,老接口模型 */
this.setConfirm('提示', '确定', '登录状态已过期, 请重新登录。').then(() => {
this.goLoginIndex(_vIn)
}).catch(() => {
Message({ type: 'info', message: '操作已取消,将不再记录任何数据操作,除非重新登录' })
})
} else if (status !== 200 && data.message) {
throw new Error(data.message)
} else {
throw new Error(JSON.stringify(res.data))
}
return data
}
/**
......@@ -127,28 +117,32 @@ export default class API {
* 注意:如果不能满足需求,可在接口定义处重新实现
* @param {[object]} res 如果未到达 response 阶段,则无res.response
*/
_reqFail (res) {
_reqFail(res) {
const _vIn = this.getVueInstance()
let err = null
if (res.code === 'ECONNABORTED') {
err = new Error('网络超时,请稍后重试')
} else if (res.response) {
const { status, data } = res.response
/* 不带 code 参数,老接口模型 */
if (res.response.data) {
if (!/\/getinfo$/gi.test(res.config.url) && res.response.status === 403) {
this.setConfirm('提示', '确定', '登录状态已过期, 请重新登录。').then(() => {
this.goLoginIndex(_vIn)
}).catch(() => {
Message({ type: 'info', message: '已取消,将不再记录任何数据操作,除非重新登录' })
})
if (data) {
if (status === 403 && !/check-access$/gi.test(res.config.url)) {
this.setConfirm('提示', '确定', '登录状态已过期, 请重新登录。')
.then(() => {
this.goLoginIndex(_vIn)
})
.catch(() => {
Message({
type: 'info',
message: '已取消,将不再记录任何数据操作,除非重新登录'
})
})
} else if (status === 401) {
this.setConfirm('提示', '关闭', data.message).then(() => {})
}
} else if (res.response.status === 401) {
this.setConfirm('提示', '关闭', res.response.data.message).then(() => {
_vIn.$router.go(0)
})
}
err = new Error(res.response.data.message || JSON.stringify(res.response.data))
err.code = res.response.data.code
err = new Error(data.message || JSON.stringify(data))
err.code = data.code
} else {
err = new Error('msg:' + res.message + 'stack:' + res.stack)
err.code = 500
......@@ -158,11 +152,28 @@ export default class API {
}
/* 重新实现 get请求 */
get (url, data, config) { return this._request(_.assignIn({ url, method: 'GET', params: data }, config)) }
get(url, data, config) {
return this._request(
_.assignIn({ url, method: 'GET', params: data }, config)
)
}
/* 重新实现 post请求 */
post (url, data, config) { return this._request(_.assignIn({ url, method: 'POST', data: data }, config)) }
post(url, data, config) {
return this._request(
_.assignIn({ url, method: 'POST', data: data }, config)
)
}
/* 重新实现 put请求 */
put (url, data, config) { return this._request(_.assignIn({ url, method: 'PUT', data: data }, config)) }
put(url, data, config) {
return this._request(_.assignIn({ url, method: 'PUT', data: data }, config))
}
/* 重新实现 delete请求 */
delete (url, data, config) { return this._request(_.assignIn({ url, method: 'DELETE', params: data }, config)) }
delete(url, data, config) {
return this._request(
_.assignIn({ url, method: 'DELETE', params: data }, config)
)
}
}
import cAction from '@action'
import { Message } from 'element-ui'
export default class Before {
constructor(opt) {
const UA = navigator.userAgent
......@@ -53,10 +51,7 @@ export default class Before {
return false
}
})
.catch((res) => {
if (res.code !== 80201) {
Message({ type: 'error', message: res.message })
}
.catch(() => {
return false
})
}
......
......@@ -120,7 +120,7 @@ export default {
if (json.status === 200) {
this.newLiveMsg = json.data
}
}).catch(e => { this.$message.error(e.message) }).finally(() => { })
})
}, 3000)
// const loading = this.$loading({ lock: true, text: '', spinner: '', background: 'rgba(255, 255, 255, 0.9)' })
......@@ -201,7 +201,7 @@ export default {
},
/* 直接进直播 */
goLive () {
this.$router.push({ path: `/player/${this.newLiveMsg.semester_id}/${this.newLiveMsg.course_id}/live/${this.newLiveMsg.live.id}` })
this.$router.push({ path: `/player/${this.newLiveMsg.course_id}/live/${this.newLiveMsg.live.id}` })
}
}
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论