Skip to content

Commit f18f149

Browse files
committed
修改配置方式
1 parent 0ee20b6 commit f18f149

File tree

9 files changed

+649
-440
lines changed

9 files changed

+649
-440
lines changed

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ node_modules
33
build
44
src
55
server
6+
static
67
.git
78
.babelrc
89
.editorconfig

README.md

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ Vue.use(http,{instanceName:"$ajax",mockInstanceName:"$mock",wsInstanceName:"$soc
2020
},errorFormat:function(d){
2121
return d.data;
2222
},defaultConfig:{}})
23-
#设置ajax全局请求过滤器,fn参数为当前data
24-
Vue.ajax.interceptors.setRequest(fn)
25-
#设置ajax全局响应过滤器,fn参数为全部响应对象
23+
#设置ajax全局请求过滤器,option参数为当前请求option
24+
Vue.ajax.interceptors.setRequest(function(option,request){return option;})
25+
#设置ajax全局响应过滤器,option参数为全部响应对象
2626
{
2727
data:data,//响应数据
2828
status:req.status,//响应状态码
@@ -31,13 +31,27 @@ Vue.ajax.interceptors.setRequest(fn)
3131
config:config,//配置option
3232
request:req//当前请求
3333
}
34-
Vue.ajax.interceptors.setResponse(fn)
34+
Vue.ajax.interceptors.setResponse(function(option,request){return option;})
3535
#设置ajax全局前缀路径
36-
Vue.ajax.setBaseUrl("http://localhost:8080")
36+
Vue.ajax.config.baseUrl="http://localhost:8080"
37+
#设置ajax全局是否启用mockserver,默认`false`
38+
Vue.ajax.config.mockMode=false
39+
#成功的status码
40+
Vue.ajax.config.successStatus=function(status){return status==200;}
41+
#修改默认配置
42+
Vue.ajax.config.default={type:"get",headers:{"Content-type":"application/json;charset=UTF-8"}}
43+
#设置mock请求方法或者指向的文件路径
44+
Vue.ajax.addMock(`String` mockUrl,function(param){return xxx;})
45+
Vue.ajax.addMock(
46+
{
47+
"url1":function(param){return xxx;},
48+
"url2":function(param){return xxx;}
49+
}
50+
)
3751
#全局配置发送socket未开启时数据延迟毫秒
38-
Vue.socket.setReconnectTimeout(30)
52+
Vue.socket.config.reconnectTimeout=30
3953
#全局配置发送socket的url前缀路径
40-
Vue.socket.setRootUrl("ws://47.104.xx.xx:8701")
54+
Vue.socket.config.baseUrl="ws://47.104.xx.xx:8701"
4155
```
4256
# 用法
4357
```javascript
@@ -89,7 +103,7 @@ this.$ajax.send(option)
89103
|------ |--------------- |:-----:|
90104
|type |类型 |`get` `post` `delete` `put`|
91105
|url |请求地址 | 必填 |
92-
|baseUrl |请求地址的前缀 | 设置为`false`时,不使用全局配置url:Vue.ajax.setBaseUrl(url);设置为字符串时,优先级高于全局配置 |
106+
|baseUrl |请求地址的前缀 | `boolean` `string` 设置为`false`时,不使用全局配置url:Vue.ajax.config.baseUrl;设置为字符串时,优先级高于全局配置 |
93107
|async |是否异步请求 | 默认`true` |
94108
|headers |请求headers对象 | 例如`{"Content-type":"application/json;charset=UTF-8"}` |
95109
|timeout |超时时间毫秒 | 毫秒数 |
@@ -98,6 +112,7 @@ this.$ajax.send(option)
98112
|dataType |表明要发送的数据格式 |默认`"json"` `"xml"` `formData`(使用formdata表单发送数据,通常用于文件上传)|
99113
|responseType|返回的数据类型|默认`""` `"json"` `"blob"` `"text"` `"arraybuffer"` `"document"`
100114
|transform |自定义格式化请求前数据的函数 | 参数为当前配置的data数据<br/> 例如`function(data){return JSON.stringify(data);}` |
115+
|mock|mock模拟数据请求|`true(需调用Vue.ajax.addMock(url,function)来拦截本次请求)` `function(data){//模拟请求,参数data为option的data}` |
101116
|success|请求成功的回调|`function(data,req){}` |
102117
|error|请求失败的回调|`function(err,req){}` |
103118
|complete|请求完成的回调|`function(){}` |
@@ -158,7 +173,7 @@ this.$socket.send("测试"+new Date(),{
158173
|onerror |错误事件 | |
159174
|onclose |关闭事件 | |
160175
|instanceId |全局id | String类型,设置此参数在页面跳转回来时,不会重复创建相同instanceId的socket连接,除非用户已经关闭对应的WebSocket |
161-
|root | 当前请求的url前缀 | 若不使用全局配置的url:Vue.socket.setRootUrl(url) ,可配置当前请求使用的前缀url,优先级高于全局配置 |
176+
|baseUrl | 当前请求的url前缀 | 若不使用全局配置的url:Vue.socket.config.baseUrl ,可配置当前请求使用的前缀url,优先级高于全局配置 |
162177

163178

164179
For detailed explanation on how things work, consult the [docs for vue-http-rexsheng](https://github.com/RexSheng/vue-http-rexsheng).

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vue-http-rexsheng",
33
"description": "A Vue.js plugin for providing http request",
4-
"version": "1.1.0",
4+
"version": "1.1.1",
55
"author": "Rex Sheng <shengxupeng@126.com>",
66
"license": "MIT",
77
"private": false,

src/App.vue

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@
1919
<li>
2020
<button @click="postFetchData(true)">Post用户列表</button>
2121
</li>
22+
<li>
23+
<button @click="form()">form请求</button>
24+
</li>
25+
<li>
26+
<button @click="mockGlobal()">全局mock</button>
27+
</li>
28+
<li>
29+
<button @click="mockStaticFile()">mock静态文件json</button>
30+
</li>
2231
</ul>
2332
<ul>
2433
<li>
@@ -53,7 +62,6 @@
5362

5463
<script>
5564
import Vue from "vue";
56-
import { setTimeout } from 'timers';
5765
export default {
5866
name: "app",
5967
data() {
@@ -233,7 +241,51 @@ export default {
233241
.catch(d => {
234242
console.log("error download file", d);
235243
});
236-
}
244+
},
245+
form:function(){
246+
Vue.ajax
247+
.send({
248+
url: "http://localhost:83/disease/section",
249+
type: "post",
250+
dataType: "form",
251+
data: { keyword: "管理",arr:["",'12f'] },
252+
// success:function(d){
253+
// console.log("success form", d, this.msg);
254+
// },
255+
// error:function(d,req){
256+
// console.log("error form", d,req);
257+
// }
258+
}).then(d => {
259+
console.log("success file", d, this.msg);
260+
}).catch(function(e){
261+
console.log("error2 form", e);
262+
})
263+
},
264+
mockGlobal:function(){
265+
Vue.ajax
266+
.send({
267+
url: "/test/mock001",
268+
data: { keyword: "管理",arr:["",'12f'] },
269+
success:function(d){
270+
console.log("success mockGlobal", d, this.msg);
271+
},
272+
error:function(d,req){
273+
console.log("error mockGlobal", d,req);
274+
}
275+
})
276+
},
277+
mockStaticFile:function(){
278+
Vue.ajax
279+
.send({
280+
url: "../static/mock/test.json",
281+
success:function(d){
282+
console.log("success mockGlobal", d, this.msg);
283+
},
284+
error:function(d,req){
285+
console.log("error mockGlobal", d,req);
286+
}
287+
})
288+
},
237289
}
238290
};
239291
</script>

src/config.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import Vue from 'vue'
22
import ajax from './lib/index'
3-
Vue.use(ajax,{
4-
// successFormat:function(d){
5-
// return d.data;
6-
// }
7-
})
3+
Vue.use(ajax)
84
// Vue.ajax.interceptors.setResponse(d=>{
95
// console.warn("response",d);
106
// return Promise.resolve(d.data);
@@ -14,6 +10,13 @@ Vue.use(ajax,{
1410
// console.warn("request",d,this);
1511
// return d;
1612
// })
17-
Vue.ajax.prefix="http://192.168.8.72:8700"
18-
Vue.socket.setReconnectTimeout(30)
19-
Vue.socket.setRootUrl("ws://47.104.154.110:8701")
13+
console.log(ajax.ajax.config)
14+
Vue.ajax.prefix="http://192.168.8.72:8700";
15+
Vue.ajax.config.baseUrl="";
16+
Vue.ajax.config.mockMode=true
17+
// Vue.ajax.addMock("/test/mock001",function(d){return d;})
18+
Vue.ajax.addMock({"/test/mock001":function(d){return d;}})
19+
// Vue.ajax.config.successStatus=function(d){return d>300};
20+
Vue.ajax.config.default={type:"get",headers:{"Content-type":"application/json;charset=UTF-8"}}
21+
Vue.socket.config.reconnectTimeout=30
22+
Vue.socket.config.baseUrl="ws://47.104.154.110:8701"

0 commit comments

Comments
 (0)