Skip to content

Commit 2c0d91f

Browse files
committed
get,post区分
1 parent 534becb commit 2c0d91f

File tree

9 files changed

+165
-137
lines changed

9 files changed

+165
-137
lines changed

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,19 @@ Vue.ajax.config.successStatus=function(status){return status==200;}
4242
Vue.ajax.config.default={type:"get",headers:{"Content-type":"application/json;charset=UTF-8"}}
4343
#设置mock请求方法或者指向的文件路径
4444
Vue.ajax.addMock(`String` mockUrl,function(param){return xxx;})
45+
Vue.ajax.addMock(`String` mockUrl,"../static/file.json")
46+
#mock代理请求
47+
Vue.ajax.addMock(`String` mockUrl,{url:"/api/newurl",data:{},type:"get",success:function(d){},error:function(err){},complete:function(){}})
48+
#@get: @post: @delete: @put: 用于相同url但是不同请求类型的拦截
4549
Vue.ajax.addMock(
4650
{
47-
"url1":function(param){return xxx;},
48-
"url2":function(param){return xxx;}
51+
"/url1":function(param){return {code:0};},
52+
"/url2":"../static/file.json",
53+
"/url3":{url:"/api/newurl",data:{},type:"get",success:function(d){},error:function(err){},complete:function(){}},
54+
//{url:"/user/3",type:"get"}的请求会匹配到
55+
"@get:/user/3":"../static/filedetail.json",
56+
//{url:"/user/3",type:"delete"}的请求会匹配到
57+
"@delete:/user/3":"../static/filedetail.json",
4958
}
5059
)
5160
#全局配置发送socket未开启时数据延迟毫秒
@@ -112,7 +121,7 @@ this.$ajax.send(option)
112121
|dataType |表明要发送的数据格式 |默认`"json"` `"xml"` `"form"` `"formData"`(使用formdata表单发送数据,通常用于文件上传)|
113122
|responseType|返回的数据类型|默认`""` `"json"` `"blob"` `"text"` `"arraybuffer"` `"document"`
114123
|transform |自定义格式化请求前数据的函数 | 参数为当前配置的data数据<br/> 例如`function(data){return JSON.stringify(data);}` |
115-
|mock|mock模拟数据请求|`true(需调用Vue.ajax.addMock(url,function)来拦截本次请求)` `function(data){//模拟请求,参数data为option的data}` `String请求的json文件地址` |
124+
|mock|mock模拟数据请求|`true(需调用Vue.ajax.addMock(url,function)来拦截本次请求)` `function(data){//模拟请求,参数data为option的data}` `String请求的json文件地址` `{url:'/newurl',type:'get',data:{},complete:function(){},success:function(d){},error:function(err){}}`|
116125
|success|请求成功的回调|`function(data,req){}` |
117126
|error|请求失败的回调|`function(err,req){}` |
118127
|complete|请求完成的回调|`function(){}` |

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.4",
4+
"version": "1.1.5",
55
"author": "Rex Sheng <shengxupeng@126.com>",
66
"license": "MIT",
77
"private": false,

src/App.vue

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
<h2>Ajax请求测试</h2>
66
<ul>
7+
<li>
8+
<button @click="cTest">自定义测试</button>
9+
</li>
710
<li>
811
<button @click="getData">Get数据</button>
912
</li>
@@ -31,6 +34,9 @@
3134
<li>
3235
<button @click="mockStaticFileGlobal()">mock全局静态json</button>
3336
</li>
37+
<li>
38+
<button @click="mockProxy()">mock代理</button>
39+
</li>
3440
</ul>
3541
<ul>
3642
<li>
@@ -176,11 +182,26 @@ export default {
176182
]
177183
}))
178184
},
185+
cTest(){
186+
this.$ajax
187+
.send({
188+
url:"http://localhost:8762/test",
189+
type: "post",
190+
data:{"pageIndex":1},
191+
headers:{"Authorization":"12"}
192+
})
193+
.then(d => {
194+
console.log("success test", d, this.msg);
195+
})
196+
.catch(d => {
197+
console.log("error test", d);
198+
});
199+
},
179200
getData() {
180201
this.$ajax
181202
.send({
182203
url:
183-
"http://xconsole.rrslj.com/datacenter/userxw/getCenterData?dateFlag=2018-08-10",
204+
"datacenter/userxw/getCenterData?dateFlag=2018-08-10",
184205
type: "get"
185206
})
186207
.then(d => {
@@ -372,6 +393,41 @@ export default {
372393
console.log("error mockGlobal", d,req);
373394
}
374395
})
396+
},
397+
mockProxy:function(){
398+
Vue.ajax
399+
.send({
400+
url: "/test/mockfile",
401+
mock:{
402+
url:"/test/aaa",
403+
success:function(d){
404+
console.log("get proxy", d, this.msg);
405+
},
406+
},
407+
success:function(d){
408+
console.log("success mockGlobal", d, this.msg);
409+
},
410+
error:function(d,req){
411+
console.log("error mockGlobal", d,req);
412+
}
413+
})
414+
Vue.ajax
415+
.send({
416+
url: "/test/mockfile",
417+
type:"post",
418+
mock:{
419+
url:"/test/aaa",
420+
success:function(d){
421+
console.log("post proxy", d, this.msg);
422+
},
423+
},
424+
success:function(d){
425+
console.log("success mockGlobal", d, this.msg);
426+
},
427+
error:function(d,req){
428+
console.log("error mockGlobal", d,req);
429+
}
430+
})
375431
}
376432
}
377433
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default{
1+
{
22
"code":0,
33
"message":"消息"
44
}

src/config.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,21 @@ Vue.ajax.addMock({
1919
"/test/mock001":function(d){
2020
var a=Object.assign({},d,{
2121
"name|+1":["小明",'小花','小春'],
22-
"value|+1":[100,89,72,74,98],
22+
"value|+2":[100,89,72,74,98],
2323
"date":new Date(),
2424
"provider":function(){
2525
return d.arr;
2626
}
2727
});
28-
return a
28+
return Vue.ajax.mock(a);
2929
}
3030
})
31-
Vue.ajax.addMock({"/test/mockfile":"../static/mock/test.json"})
32-
33-
31+
// Vue.ajax.addMock({"/test/mockfile":"../static/mock/test.json"})
32+
Vue.ajax.addMock({
33+
"/test/mockfile":"../src/assets/data/01.json",
34+
"@get:/test/aaa":()=>{return {"dd":1};},
35+
"@post:/test/aaa":()=>{return {"dd":"post"};}
36+
});
3437
// Vue.ajax.config.successStatus=function(d){return d>300};
3538
Vue.ajax.config.default={type:"get",headers:{"Content-type":"application/json;charset=UTF-8"}}
3639
Vue.socket.config.reconnectTimeout=30

src/lib/ajax.js

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,21 @@ let ajax = {
5555
data: null,
5656
dataType: "json"
5757
};
58+
this.isNull=function(p){
59+
return p===undefined || p===null;
60+
};
5861
this.isMock=function(option){
5962
var useMock=AJAXCONF.mockMode;
60-
// console.log("mockCache",mockCache)
6163
this.mockInstance=AJAXCONF.mockCache[option.url];
6264
if(option.mock!==undefined && option.mock!==null){
6365
if(option.mock===true){
6466
//启用mock
6567
useMock=true;
66-
this.mockInstance=AJAXCONF.mockCache[option.url];
67-
if(this.mockInstance==undefined || this.mockInstance==null){
68-
useMock=false;
68+
if(this.isNull(this.mockInstance)){
69+
this.mockInstance=AJAXCONF.mockCache["@"+option.type.toLowerCase()+":"+option.url];
70+
if(this.isNull(this.mockInstance)){
71+
useMock=false;
72+
}
6973
}
7074
}
7175
if(Object.prototype.toString.call(option.mock) === '[object Function]'){
@@ -77,17 +81,46 @@ let ajax = {
7781
//指向文件地址,不启用mock
7882
useMock=false;
7983
option.url=option.mock;
84+
option.type="GET";
85+
}
86+
if(Object.prototype.toString.call(option.mock) === '[object Object]'){
87+
//启用mock
88+
useMock=false;
89+
this.mockInstance=option.mock;
8090
}
8191
}
8292
else{
8393
//全局使用mock,但是未配置实现方法,设置useMock=false,会直接默认请求option.url
84-
if(useMock && (this.mockInstance===undefined || this.mockInstance===null)){
85-
useMock=false;
94+
if(useMock){
95+
if(this.isNull(this.mockInstance)){
96+
this.mockInstance=AJAXCONF.mockCache["@"+option.type.toLowerCase()+":"+option.url];
97+
if(this.isNull(this.mockInstance)){
98+
useMock=false;
99+
}
100+
}
86101
}
87102
}
88-
if(useMock && (typeof this.mockInstance==="string")){
103+
if(useMock && (Object.prototype.toString.call(this.mockInstance) === '[object String]')){
89104
useMock=false;
90105
option.url=this.mockInstance;
106+
option.type="GET";
107+
}
108+
if(Object.prototype.toString.call(this.mockInstance) === '[object Object]'){
109+
option=Object.assign(option,this.mockInstance);
110+
option.mock=undefined;
111+
return this.isMock(option);
112+
}
113+
if(option.url.substr(0,5)==="@get:"){
114+
option.url=option.url.substr(5);
115+
}
116+
else if(option.url.substr(0,6)==="@post:"){
117+
option.url=option.url.substr(6);
118+
}
119+
else if(option.url.substr(0,8)==="@delete:"){
120+
option.url=option.url.substr(8);
121+
}
122+
else if(option.url.substr(0,5)==="@put:"){
123+
option.url=option.url.substr(5);
91124
}
92125
return useMock;
93126
};
@@ -155,8 +188,9 @@ let ajax = {
155188
return;
156189
}
157190
new Promise(function(res,rej){
158-
var dj=instance.call(scope, option.data,res, rej);
159-
res(new randomPlugin(dj));
191+
var dd=instance.call(scope, option.data,res, rej);
192+
// var dd=new randomPlugin().handle(dj);
193+
res(dd);
160194
}).then(function(dd){
161195
if(option.success){
162196
option.success.call(scope,dd);
@@ -205,11 +239,11 @@ let ajax = {
205239
var scope=this.scope;
206240
var formatResult=this.formatResult;
207241
var opt=Object.assign(this.defaultConfig,AJAXCONF.userDefaultConfig, config);
208-
var request = this.createInstance();
209-
opt.url = this.formatUrl(opt.url, opt.data);
210242
if(this.isMock(opt)){
211243
return this.mock(opt,scope);
212244
}
245+
var request = this.createInstance();
246+
opt.url = this.formatUrl(opt.url, opt.data);
213247
if(opt.baseUrl!==undefined && opt.baseUrl===false){
214248
request.open(opt.type, opt.url, opt.async);
215249
}
@@ -250,7 +284,7 @@ let ajax = {
250284
}
251285
else{
252286
if (opt.dataType.toLowerCase() === 'formdata') {
253-
if (opt.data != null) {
287+
if (opt.data != null && Object.prototype.toString.call(opt.data) !=='[object FormData]') {
254288
var formData = new FormData();
255289
Object.keys(opt.data).forEach(key => {
256290
if (opt.data[key].constructor === Array || opt.data[key].constructor === FileList) {

src/lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ var ajaxBody={
6161

6262
},
6363
mock:function(param){
64-
return new randomPlugin(param);
64+
return new randomPlugin().handle(param);
6565
}
6666
}
6767
var wsBody={

0 commit comments

Comments
 (0)