Skip to content

Commit

Permalink
♻️ 封装 doRequest 方法,方便进行请求处理 & checkRequestSuccess 方法,统一控制判断服务端的响应是否成功…
Browse files Browse the repository at this point in the history
…,方便应对服务端的响应结构切换
  • Loading branch information
Hccake committed Nov 20, 2021
1 parent 49d9d4a commit c7aca07
Show file tree
Hide file tree
Showing 20 changed files with 201 additions and 218 deletions.
18 changes: 8 additions & 10 deletions src/components/Table/ProTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import './listToolBar.less'
import './toolbar.less'
import './alert.less'
import './proTable.less'
import { doRequest } from '@/utils/request'

const SearchConfig = {
searchText: {
Expand Down Expand Up @@ -271,10 +272,11 @@ export default {
*/
loadData () {
const params = this.pageParams()

this.localLoading = true
this.request(params).then(res => {
if (res.code === 200) {

doRequest(this.request(params), {
successMessage: false,
onSuccess: (res) => {
const data = res.data

if(this.enablePagination){
Expand All @@ -292,14 +294,10 @@ export default {
this.localDataSource = this.responseDataProcess(data)
// 当分页加载成功时执行
this.onPageLoadSuccess(data)
} else {
this.$message.warning(res.message || 'error request')
},
onFinally: () => {
this.localLoading = false
}
}).catch((e) => {
// 未被 axios拦截器处理过,则在这里继续处理
!e.resolved && this.$message.error(e.message || 'error request')
}).finally(() => {
this.localLoading = false
})
},
/**
Expand Down
26 changes: 12 additions & 14 deletions src/mixins/formMixin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import pick from 'lodash.pick'
import { doRequest } from '@/utils/request'
import { delObj } from '@/api/system/role'

// 表单行为类型,标识当前表单是用来新建的还是更新的
const FORM_ACTION = {
Expand Down Expand Up @@ -127,21 +129,17 @@ export default {
this.form.validateFields((err, values) => {
if (!err) {
this.submitLoading = true
reqFunction(this.submitDataProcess(values))
.then(res => {
if (res.code === 200) {
this.$message.success(res.message)
this.submitSuccess(res)
} else {
this.submitError(res)
}
})
.catch(error => {
this.$message.error(error.response.data.message)
})
.finally(() => {
doRequest(reqFunction(this.submitDataProcess(values)), {
onSuccess: (res) => {
this.submitSuccess(res)
},
onFail: (res) => {
this.submitError(res)
},
onFinally: () => {
this.submitLoading = false
})
}
})
}
})
},
Expand Down
49 changes: 20 additions & 29 deletions src/mixins/tablePageMixin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { littleCamelToUnderline } from '@/utils/strUtil'
import { enableI18n } from '@/config/projectConfig'
import { doRequest } from '@/utils/request'
import { delObj } from '@/api/system/role'

export default {
data () {
Expand Down Expand Up @@ -106,29 +108,24 @@ export default {
*/
loadData () {
const params = this.pageParams()

this.loading = true
this.getPage(params)
.then(res => {
if (res.code === 200) {
const page = res.data
// 为防止删除数据后导致页面当前页面数据长度为 0 ,自动翻页到上一页
if (page.records.length === 0 && this.pagination.current > 1) {
this.pagination.current--
this.loadData()
return
}
this.dataSource = page.records
this.pagination.total = page.total
this.onPageLoadSuccess(page)
} else {
this.$message.warning(res.message || 'error request')
doRequest(this.getPage(params), {
successMessage: false,
onSuccess: (res) => {
const page = res.data
// 为防止删除数据后导致页面当前页面数据长度为 0 ,自动翻页到上一页
if (page.records.length === 0 && this.pagination.current > 1) {
this.pagination.current--
this.loadData()
return
}
}).catch((e) => {
// 未被 axios拦截器处理过,则在这里继续处理
!e.resolved && this.$message.error(e.message || 'error request')
}).finally(() => {
this.loading = false
this.dataSource = page.records
this.pagination.total = page.total
this.onPageLoadSuccess(page)
},
onFinally: () => {
this.loading = false
}
})
},
/**
Expand Down Expand Up @@ -178,16 +175,10 @@ export default {

// 删除
handleDel (record) {
this.delObj(record[this.rowKey]).then(res => {
if (res.code === 200) {
this.$message.success(res.message)
doRequest(delObj(record[this.rowKey]), {
onSuccess: () => {
this.reloadTable(false)
} else {
this.$message.error(res.message)
}
}).catch((e) => {
// 未被 axios拦截器处理过,则在这里继续处理
!e.resolved && this.$message.error(e.message || 'error request')
})
},

Expand Down
5 changes: 3 additions & 2 deletions src/store/modules/dict.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Vue from 'vue'
import { DICT } from '@/store/mutation-types'
import { getDictData, invalidDictHash } from '@/api/system/dict'
import { checkRequestSuccess } from '@/utils/request'

// 字典项hash列表的Key
const DICT_HASH_KEY = 'dict-hashes'
Expand Down Expand Up @@ -80,7 +81,7 @@ export default {
}
commit(DICT.SET_DICT_REQUEST_FLAG, requestFlagKey)
const res = await getDictData(noDataList)
if (res.code === 200 && res.data) {
if (checkRequestSuccess(res) && res.data) {
res.data.forEach(dictData => {
const dictCode = dictData.dictCode
const hashCode = dictData.hashCode
Expand Down Expand Up @@ -113,7 +114,7 @@ export default {
// 如果有属性,则去后台查询是否过期
if (map && Object.getOwnPropertyNames(map).length > 0) {
const res = await invalidDictHash(map)
if (res.code === 200) {
if (checkRequestSuccess(res)) {
// 删除对应过期数据
for (let dictCode of res.data) {
Vue.ls.remove(DICT_DATA_KEY_PREFIX + dictCode)
Expand Down
6 changes: 3 additions & 3 deletions src/store/modules/lov.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Vue from 'vue'
import { LOV } from '@/store/mutation-types'
import { check, getData } from '@/api/system/lov'
import { checkRequestSuccess } from '@/utils/request'

// 缓存过期时间
const TTL = 7 * 24 * 60 * 60 * 1000
Expand Down Expand Up @@ -60,7 +61,7 @@ const lov = {
const res = await getData(keyword)

// 解析
if (res.code === 200 && res.data) {
if (checkRequestSuccess(res) && res.data) {
Vue.ls.set(getKey(keyword), JSON.stringify(res.data), TTL)
commit(LOV.SET_CACHE, { keyword, lovInfo: res.data })
cacheUpdateTime(res.data)
Expand Down Expand Up @@ -90,11 +91,10 @@ const lov = {
const cache = getCache()
if (cache && Object.getOwnPropertyNames(cache).length > 0) {
const res = await check(cache)
if (res.code === 200 && res.data) {
if (checkRequestSuccess(res) && res.data) {
return res.data
}
}

return []
}
}
Expand Down
64 changes: 60 additions & 4 deletions src/utils/request.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import axios from 'axios'
import store from '@/store'
import notification from 'ant-design-vue/es/notification'
import { message } from 'ant-design-vue'
import { enableI18n } from '@/config/projectConfig'
import { debug } from 'script-ext-html-webpack-plugin/lib/common'

// 创建 axios 实例
const service = axios.create({
const axiosInstance = axios.create({
baseURL: process.env.VUE_APP_API_BASE_URL, // api base_url
timeout: 600000 // 请求超时时间
})
Expand Down Expand Up @@ -37,7 +39,7 @@ const onRejected = (error) => {
}

// request interceptor
service.interceptors.request.use(config => {
axiosInstance.interceptors.request.use(config => {
const headers = config.headers

// token
Expand All @@ -59,7 +61,7 @@ service.interceptors.request.use(config => {
}, onRejected)

// response interceptor
service.interceptors.response.use((response) => {
axiosInstance.interceptors.response.use((response) => {
const headers = response.headers
if (headers != null && headers['content-type'] && headers['content-type'].startsWith('application/json')) {
return response.data
Expand All @@ -68,4 +70,58 @@ service.interceptors.response.use((response) => {
}
}, onRejected)

export default service
// 判断是否请求成功
export function checkRequestSuccess (res) {
return res.code === 200
}

// 默认的响应处理器
const defaultResponseHandler = {
// 判断是否请求成功
checkRequestSuccess: checkRequestSuccess,
// 成功消息提示
successMessage: true,
// 成功时的处理函数
onSuccess (res) {
},
// 失败消息提示
failMessage: true,
// 失败时的处理函数
onFail (res) {
},
// 错误消息提示
errorMessage: true,
// 错误时的处理函数
onError (e) {
},
// 最终的执行函数
onFinally () {
}
}

/**
* 请求方法封装
* @param request promise
* @param userResponseHandler 用户设置的 userResponseHandler
*/
export function doRequest (request, userResponseHandler) {
const handler = Object.assign({}, defaultResponseHandler, userResponseHandler)
request.then(res => {
if (handler.checkRequestSuccess(res)) {
handler.successMessage && message.success(res.message)
handler.onSuccess(res)
} else {
handler.failMessage && message.error(res.message)
handler.onFail(res)
}
}).catch((e) => {
// 未被 axios拦截器处理过,则在这里继续处理
handler.errorMessage && !e.resolved && message.error(e.message || 'error request')
handler.onError(e)
}).finally(() => {
handler.onFinally()
}
)
}

export default axiosInstance
16 changes: 8 additions & 8 deletions src/views/i18n/I18nMessageModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
<script>
import { listByCode, putObj } from '@/api/i18n/i18n-data'
import EditableCell from '@/components/Table/EditableCell'
import { doRequest } from '@/utils/request'
import { delObj } from '@/api/system/role'
export default {
name: 'I18nMessageModal',
Expand Down Expand Up @@ -90,18 +92,16 @@ export default {
this.hasUpdate = true
// 值变更,则请求后台更新 i18nData 数据
this.dataLoading = true
putObj({
let obj = {
code: record.code,
languageTag: record.languageTag,
message: value
}).then(res => {
if (res.code === 200) {
this.$message.success(res.message)
} else {
this.$message.error(res.message)
}
doRequest(putObj(obj), {
onFinally: () => {
this.dataLoading = false
}
}).finally(() => {
this.dataLoading = false
})
}
}
Expand Down
19 changes: 6 additions & 13 deletions src/views/i18n/i18n-data/I18nDataPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ import I18nDataUpdateModal from '@/views/i18n/i18n-data/I18nDataUpdateModal'
import I18nDataImportModal from '@/views/i18n/i18n-data/I18nDataImportModal'
import { remoteFileDownload } from '@/utils/fileUtil'
import ProTable from '@/components/Table/ProTable'
import { doRequest } from '@/utils/request'
export default {
name: 'I18nDataPage',
Expand Down Expand Up @@ -171,19 +172,11 @@ export default {
},
// 删除
handleDel(record) {
delObj(record.code, record.languageTag)
.then(res => {
if (res.code === 200) {
this.$message.success(res.message)
this.reloadPageTable(false)
} else {
this.$message.error(res.message)
}
})
.catch(e => {
// 未被 axios拦截器处理过,则在这里继续处理
!e.resolved && this.$message.error(e.message || 'error request')
})
doRequest(delObj(record.code, record.languageTag), {
onSuccess: () => {
this.reloadPageTable(false)
}
})
},
/**
* 导出国际化信息
Expand Down
Loading

0 comments on commit c7aca07

Please sign in to comment.