Skip to content

Commit 129cda0

Browse files
committed
feat: 改造axios
1 parent e2332df commit 129cda0

8 files changed

Lines changed: 51 additions & 85 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
"lint-staged": {
9090
"*.{js,vue}": [
9191
"prettier --write",
92-
"vue-cli-service list",
92+
"vue-cli-service lint",
9393
"git add"
9494
]
9595
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
diff a/src/component/base/table/lin-table.vue b/src/component/base/table/lin-table.vue (rejected hunks)
2+
@@ -217 +217,4 @@ export default {
3+
- this.toggleSelection(this.currentData.filter(item => item.key === row.key), false)
4+
+ this.toggleSelection(
5+
+ this.currentData.filter(item => item.key === row.key),
6+
+ false,
7+
+ )

src/component/base/tinymce/index.vue

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
// eslint-disable-next-line
88
import tinymce from 'tinymce/tinymce'
99
import Editor from '@tinymce/tinymce-vue'
10+
import { post } from '@/lin/plugin/axios'
11+
1012
import 'tinymce/themes/silver'
1113
import './import-all'
1214
@@ -59,7 +61,6 @@ export default {
5961
}
6062
},
6163
created() {
62-
const _this = this
6364
this.tinymceInit = {
6465
language_url: `${this.baseUrl}/tinymce/langs/zh_CN.js`,
6566
skin_url: `${this.baseUrl}/tinymce/skins/ui/oxide`,
@@ -81,14 +82,9 @@ export default {
8182
const file = new File([blobInfo.blob()], blobInfo.filename(), {
8283
type: 'image/*',
8384
})
84-
_this
85-
.$axios({
86-
method: 'post',
87-
url: '/cms/file',
88-
data: {
89-
file,
90-
},
91-
})
85+
post('cms/file', {
86+
file,
87+
})
9288
.then(res => {
9389
if (res[0] && res[0].url) {
9490
success(res[0].url)

src/component/base/upload-image/index.vue

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ todo: 文件判断使用 serveWorker 优化性能
7878

7979
<script>
8080
import { getFileType, checkIsAnimated, isEmptyObj, createId } from './utils'
81+
import { post } from '@/lin/plugin/axios'
8182
8283
/**
8384
* 本地图像通过验证后构造的信息对象
@@ -458,11 +459,7 @@ export default {
458459
uploadList.forEach((item, index) => {
459460
data[`file_${index}`] = item.img.file
460461
})
461-
return this.$axios({
462-
method: 'post',
463-
url: '/cms/file',
464-
data,
465-
})
462+
return post('cms/file', data)
466463
.then(res => {
467464
if (!Array.isArray(res) || res.length === 0) {
468465
throw new Error('图像上传失败')

src/component/layout/user.vue

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
import { mapActions, mapGetters } from 'vuex'
114114
import Vue from 'vue'
115115
import Croppa from 'vue-croppa'
116+
import axios, { post } from '@/lin/plugin/axios'
116117
import User from '@/lin/model/user'
117118
import 'vue-croppa/dist/vue-croppa.css'
118119
import defaultAvatar from '@/assets/image/user/user.png'
@@ -256,12 +257,8 @@ export default {
256257
type: 'image/jpeg',
257258
})
258259
259-
return this.$axios({
260-
method: 'post',
261-
url: '/cms/file',
262-
data: {
263-
file,
264-
},
260+
return post('/cms/file', {
261+
file,
265262
}).then(res => {
266263
// 清空输入框
267264
this.clearFileInput(this.$refs.avatarInput)
@@ -273,12 +270,8 @@ export default {
273270
// if (res.code === 10110) {
274271
// throw new Error('文件体积过大')
275272
// }
276-
return this.$axios({
277-
method: 'put',
278-
url: '/cms/user',
279-
data: {
280-
avatar: res[0].path,
281-
},
273+
return post('/cms/user', {
274+
avatar: res[0].path,
282275
})
283276
.then(putRes => {
284277
// eslint-disable-line
@@ -311,7 +304,7 @@ export default {
311304
if (this.nickname) {
312305
const { user } = this.$store.state
313306
if (this.nickname !== user.nickname && this.nickname !== '佚名') {
314-
this.$axios({
307+
axios({
315308
method: 'put',
316309
url: '/cms/user',
317310
data: {

src/config/stage/plugin.js.rej

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
diff a/src/config/stage/plugin.js b/src/config/stage/plugin.js (rejected hunks)
2+
@@ -6,5 +6 @@ import linCmsUi from '@/plugin/lin-cms-ui/stage-config'
3+
-const pluginsConfig = [
4+
- chart,
5+
- custom,
6+
- linCmsUi,
7+
-]
8+
+const pluginsConfig = [chart, custom, linCmsUi]

src/lin/plugin/axios.js

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
// ajax 封装插件, 使用 axios
2-
import Vue from 'vue'
1+
/**
2+
* 封装 axios
3+
*/
34
import axios from 'axios'
5+
import { Message } from 'element-ui'
6+
47
import store from '@/store'
58
import Config from '@/config'
69
import autoJump from '@/lin/util/auto-jump'
@@ -151,57 +154,24 @@ _axios.interceptors.response.use(
151154
}
152155
}
153156

154-
Vue.prototype.$message({
155-
message,
156-
type: 'error',
157-
})
157+
Message.error(message)
158158
reject()
159159
})
160160
},
161161
error => {
162162
if (!error.response) {
163-
Vue.prototype.$notify({
164-
title: 'Network Error',
165-
dangerouslyUseHTMLString: true,
166-
message: '<strong class="my-notify">请检查 API 是否异常</strong>',
167-
})
163+
Message.error('请检查 API 是否异常')
168164
console.log('error', error)
169165
}
170166

171167
// 判断请求超时
172168
if (error.code === 'ECONNABORTED' && error.message.indexOf('timeout') !== -1) {
173-
Vue.prototype.$message({
174-
type: 'warning',
175-
message: '请求超时',
176-
})
169+
Message.warning('请求超时')
177170
}
178171
return Promise.reject(error)
179172
},
180173
)
181174

182-
// eslint-disable-next-line
183-
Plugin.install = function(Vue, options) {
184-
// eslint-disable-next-line
185-
Vue.axios = _axios
186-
window.axios = _axios
187-
Object.defineProperties(Vue.prototype, {
188-
axios: {
189-
get() {
190-
return _axios
191-
},
192-
},
193-
$axios: {
194-
get() {
195-
return _axios
196-
},
197-
},
198-
})
199-
}
200-
201-
if (!Vue.axios) {
202-
Vue.use(Plugin)
203-
}
204-
205175
// 导出常用函数
206176

207177
/**

src/view/center/center.vue

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
label-position="left"
3939
ref="form"
4040
label-width="90px"
41-
@submit.native.prevent
41+
@submit.prevent
4242
>
4343
<el-form-item label="原始密码" prop="old_password">
4444
<el-input type="password" v-model="form.old_password" autocomplete="off"></el-input>
@@ -61,7 +61,7 @@
6161
<!-- 修改头像 -->
6262
<el-dialog
6363
title="裁剪"
64-
:visible.sync="cropVisible"
64+
v-model:visible="cropVisible"
6565
width="300px"
6666
:append-to-body="true"
6767
:close-on-click-modal="false"
@@ -88,10 +88,12 @@
8888
</div>
8989
<div style="margin-top: 1em;">通过鼠标滚轮调节头像大小</div>
9090
</div>
91-
<div slot="footer" class="dialog-footer">
92-
<el-button @click="cropVisible = false" size="small">取 消</el-button>
93-
<el-button type="primary" @click="handleCrop" size="small">确 定</el-button>
94-
</div>
91+
<template v-solt:footer>
92+
<div class="dialog-footer">
93+
<el-button @click="cropVisible = false" size="small">取 消</el-button>
94+
<el-button type="primary" @click="handleCrop" size="small">确 定</el-button>
95+
</div>
96+
</template>
9597
</el-dialog>
9698
</div>
9799
</template>
@@ -101,6 +103,7 @@ import { mapActions, mapGetters } from 'vuex'
101103
import Vue from 'vue'
102104
import Croppa from 'vue-croppa'
103105
import User from '@/lin/model/user'
106+
import axios, { post, put } from '@/lin/plugin/axios'
104107
import 'vue-croppa/dist/vue-croppa.css'
105108
import defaultAvatar from '@/assets/image/user/user.png'
106109
@@ -240,12 +243,8 @@ export default {
240243
type: 'image/jpeg',
241244
})
242245
243-
return this.$axios({
244-
method: 'post',
245-
url: '/cms/file',
246-
data: {
247-
file,
248-
},
246+
return post('cms/file', {
247+
file,
249248
}).then(res => {
250249
// 清空输入框
251250
this.clearFileInput(this.$refs.avatarInput)
@@ -257,12 +256,8 @@ export default {
257256
// if (res.code === 10110) {
258257
// throw new Error('文件体积过大')
259258
// }
260-
return this.$axios({
261-
method: 'put',
262-
url: '/cms/user',
263-
data: {
264-
avatar: res[0].path,
265-
},
259+
return put('/cms/user', {
260+
avatar: res[0].path,
266261
})
267262
.then(putRes => {
268263
// eslint-disable-line
@@ -289,7 +284,7 @@ export default {
289284
if (this.nickname) {
290285
const { user } = this.$store.state
291286
if (this.nickname !== user.nickname && this.nickname !== '佚名') {
292-
this.$axios({
287+
axios({
293288
method: 'put',
294289
url: '/cms/user',
295290
data: {

0 commit comments

Comments
 (0)