Skip to content

Commit ac1f67e

Browse files
committed
form时header修改
1 parent 2c0d91f commit ac1f67e

File tree

7 files changed

+110
-42
lines changed

7 files changed

+110
-42
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Vue.ajax.config.mockMode=false
4040
Vue.ajax.config.successStatus=function(status){return status==200;}
4141
#修改默认配置
4242
Vue.ajax.config.default={type:"get",headers:{"Content-type":"application/json;charset=UTF-8"}}
43+
Vue.ajax.config.default=()=>{return {type:"get",headers:{"Content-type":"application/json;charset=UTF-8","Lang":localStorage.getItem("language")}}}
4344
#设置mock请求方法或者指向的文件路径
4445
Vue.ajax.addMock(`String` mockUrl,function(param){return xxx;})
4546
Vue.ajax.addMock(`String` mockUrl,"../static/file.json")

package.json

Lines changed: 2 additions & 2 deletions
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.5",
4+
"version": "1.1.9",
55
"author": "Rex Sheng <shengxupeng@126.com>",
66
"license": "MIT",
77
"private": false,
@@ -27,7 +27,6 @@
2727
"url": "https://github.com/RexSheng/vue-http-rexsheng"
2828
},
2929
"dependencies": {
30-
"vue": "^2.5.11"
3130
},
3231
"browserslist": [
3332
"> 1%",
@@ -45,6 +44,7 @@
4544
"css-loader": "^0.28.7",
4645
"extract-text-webpack-plugin": "^3.0.2",
4746
"file-loader": "^1.1.4",
47+
"vue": "^2.5.11",
4848
"vue-loader": "^13.0.5",
4949
"vue-template-compiler": "^2.4.4",
5050
"webpack": "^3.6.0",

src/App.vue

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export default {
139139
"340000": "安徽省"
140140
}}))
141141
console.log("'array|1':['Mock.js']",this.$ajax.mock({"array|1": ["Mock.js"]}))
142-
console.log("'array|+1':['Mock.js','!']",this.$ajax.mock({"array|+1": ["Mock.js",'!']}))
142+
console.log("'array|+1':['Mock.js','!']",this.$ajax.mock({"array|+3": ["Mock.js",'!']}))
143143
console.log("'array|2':['Mock.js']",this.$ajax.mock({"array|2": ["Mock.js"]}))
144144
console.log("'array|1-3':['Mock.js']",this.$ajax.mock({"array|1-3": ["Mock.js"]}))
145145
console.log("'array|1-3':['Mock.js','!']",this.$ajax.mock({"array|1-3": ["Mock.js",'!']}))
@@ -201,8 +201,15 @@ export default {
201201
this.$ajax
202202
.send({
203203
url:
204-
"datacenter/userxw/getCenterData?dateFlag=2018-08-10",
205-
type: "get"
204+
"datacenter/userxw/getCenterData?dateFlag={dateFlag}&size={size}",
205+
// type: "get" ,
206+
data:{dateFlag:"2018-08-11",size:null},
207+
cancel:function(cb){
208+
console.log(new Date())
209+
setTimeout(()=>{
210+
cb()
211+
},4000)
212+
}
206213
})
207214
.then(d => {
208215
console.log("success1", d, this.msg);
@@ -340,10 +347,11 @@ export default {
340347
form:function(){
341348
Vue.ajax
342349
.send({
343-
url: "http://localhost:83/disease/section",
344-
type: "post",
345-
dataType: "form",
346-
data: { keyword: "管理",arr:["",'12f'] },
350+
url: "http://localhost:9030/web/auth/users",
351+
type: "get",
352+
// dataType: "form",
353+
// data: { keyword: "管理",arr:["生",'12f'] },
354+
data:{userName:'',company:'',index:1,size:10},
347355
// success:function(d){
348356
// console.log("success form", d, this.msg);
349357
// },
@@ -361,6 +369,7 @@ export default {
361369
.send({
362370
url: "/test/mock001",
363371
data: { keyword: "管理",arr:["",'12f'] },
372+
type:"post",
364373
success:function(d){
365374
console.log("success mockGlobal", d, this.msg);
366375
},

src/config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ Vue.ajax.addMock({
3232
Vue.ajax.addMock({
3333
"/test/mockfile":"../src/assets/data/01.json",
3434
"@get:/test/aaa":()=>{return {"dd":1};},
35-
"@post:/test/aaa":()=>{return {"dd":"post"};}
35+
"@post:/test/aaa":()=>{return {"dd":"post"};},
36+
"@post:/test/mock001":()=>{return {"001":"post"};}
3637
});
3738
// Vue.ajax.config.successStatus=function(d){return d>300};
38-
Vue.ajax.config.default={type:"get",headers:{"Content-type":"application/json;charset=UTF-8"}}
39+
Vue.ajax.config.default=()=>{return{type:"get",headers:{"Content-type":"application/json;charset=UTF-8","Authorization":new Date().getTime()}}}
3940
Vue.socket.config.reconnectTimeout=30
4041
Vue.socket.config.baseUrl="ws://47.104.154.110:8701"

src/lib/ajax.js

Lines changed: 70 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ import randomPlugin from './random'
55
*/
66
let defaultFormatCallback=function(d){return d.data;};
77
let STRATEGY={
8-
LOCAL_GLOBAL:0,//局部配置优先
9-
GLOBAL_LOCAL:0,//局部配置优先
10-
8+
LOCAL_GLOBAL:0,//局部配置优先,全局次之
9+
GLOBAL_LOCAL:1,//全局配置优先,局部次之
1110
}
1211
let AJAXCONF={
1312
instanceName:"$ajax",
@@ -17,7 +16,7 @@ let AJAXCONF={
1716
WSGlobalInstanceName:"$socket".replace(/\$/g,''),
1817
successFormatCallback:defaultFormatCallback,
1918
errorFormatCallback:defaultFormatCallback,
20-
userDefaultConfig:{},
19+
userDefaultConfig:function(){return {}},
2120
requestInterceptor:function(opt,req){return opt;},
2221
responseInterceptor:function(res,req){return res;},
2322
successStatus:function(status){return status===200;},
@@ -60,13 +59,13 @@ let ajax = {
6059
};
6160
this.isMock=function(option){
6261
var useMock=AJAXCONF.mockMode;
63-
this.mockInstance=AJAXCONF.mockCache[option.url];
62+
this.mockInstance=AJAXCONF.mockCache["@"+option.type.toLowerCase()+":"+option.url];
6463
if(option.mock!==undefined && option.mock!==null){
6564
if(option.mock===true){
6665
//启用mock
6766
useMock=true;
6867
if(this.isNull(this.mockInstance)){
69-
this.mockInstance=AJAXCONF.mockCache["@"+option.type.toLowerCase()+":"+option.url];
68+
this.mockInstance=AJAXCONF.mockCache[option.url];
7069
if(this.isNull(this.mockInstance)){
7170
useMock=false;
7271
}
@@ -93,7 +92,7 @@ let ajax = {
9392
//全局使用mock,但是未配置实现方法,设置useMock=false,会直接默认请求option.url
9493
if(useMock){
9594
if(this.isNull(this.mockInstance)){
96-
this.mockInstance=AJAXCONF.mockCache["@"+option.type.toLowerCase()+":"+option.url];
95+
this.mockInstance=AJAXCONF.mockCache[option.url];
9796
if(this.isNull(this.mockInstance)){
9897
useMock=false;
9998
}
@@ -238,7 +237,7 @@ let ajax = {
238237
this.send = function(config) {
239238
var scope=this.scope;
240239
var formatResult=this.formatResult;
241-
var opt=Object.assign(this.defaultConfig,AJAXCONF.userDefaultConfig, config);
240+
var opt=Object.assign(this.defaultConfig,AJAXCONF.userDefaultConfig(), config);
242241
if(this.isMock(opt)){
243242
return this.mock(opt,scope);
244243
}
@@ -253,18 +252,37 @@ let ajax = {
253252
else{
254253
request.open(opt.type, AJAXCONF.baseUrl+opt.url, opt.async);
255254
}
256-
if (opt.dataType.toLowerCase() === 'json') {
257-
request.setRequestHeader("Content-type", "application/json;charset=UTF-8");
258-
}
259-
else if (opt.dataType.toLowerCase() === 'form') {
260-
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=UTF-8");
261-
}else {}
255+
262256
//加入header
263257
if (opt.headers) {
264258
Object.keys(opt.headers).forEach(item => {
265-
request.setRequestHeader(item, opt.headers[item]);
259+
if(item.toLowerCase()==="content-type"){
260+
//用户无设置自定义header
261+
if (opt.dataType.toLowerCase() === 'json') {
262+
request.setRequestHeader("Content-type", "application/json;charset=UTF-8");
263+
}
264+
else if (opt.dataType.toLowerCase() === 'form') {
265+
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=UTF-8");
266+
}
267+
else{
268+
request.setRequestHeader(item, opt.headers[item]);
269+
}
270+
}
271+
else{
272+
request.setRequestHeader(item, opt.headers[item]);
273+
}
274+
266275
});
267276
}
277+
else{
278+
//用户无设置自定义header
279+
if (opt.dataType.toLowerCase() === 'json') {
280+
request.setRequestHeader("Content-type", "application/json;charset=UTF-8");
281+
}
282+
else if (opt.dataType.toLowerCase() === 'form') {
283+
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=UTF-8");
284+
}else {}
285+
}
268286

269287
if(opt.responseType!=null){
270288
request.responseType=opt.responseType;
@@ -316,7 +334,7 @@ let ajax = {
316334
}
317335
}
318336
}
319-
if (opt.type.toLowerCase() === "post" && opt.dataType.toLowerCase() === 'json' && opt.data) {
337+
if (opt.dataType.toLowerCase() === 'json' && opt.data) {
320338
opt.data = JSON.stringify(opt.data);
321339
}
322340
}
@@ -359,17 +377,32 @@ let ajax = {
359377
}
360378
}
361379
request.send(newOpt.data);
380+
if (newOpt.cancel) {
381+
newOpt.cancel.call(scope, function() {
382+
var oldState=request.readyState;
383+
request.abort();
384+
if(oldState<4){
385+
if(newOpt.complete){
386+
newOpt.complete.call(scope);
387+
}
388+
if(newOpt.error){
389+
newOpt.error.apply(scope,[request]);
390+
}
391+
reject(request);
392+
}
393+
});
394+
}
395+
// var abortIntervalHandler=null;
396+
// if (newOpt.cancel) {
397+
// abortIntervalHandler = setInterval(function() {
398+
// newOpt.cancel.call(scope, function() {
399+
// request.abort();
400+
// clearInterval(abortIntervalHandler);
401+
// abortIntervalHandler = null;
402+
// });
403+
// }, 0);
404+
// }
362405
request.onreadystatechange = function() {
363-
364-
if (newOpt.cancel) {
365-
abortIntervalHandler = setInterval(function() {
366-
newOpt.cancel.call(scope, function() {
367-
request.abort();
368-
clearInterval(abortIntervalHandler);
369-
abortIntervalHandler = null;
370-
});
371-
}, 0);
372-
}
373406
if (request.readyState === 4) { // 4代表执行完成
374407
request._complete = true;
375408
if(newOpt.complete){
@@ -462,7 +495,17 @@ let ajax = {
462495
};
463496
})
464497
.catch(function(e){
465-
formatResult(scope,request.responseText, request,opt,false).then(dd=>reject(dd[0])).catch(ee=>reject(ee[0]))
498+
formatResult(scope,request.responseText, request,opt,false).then(dd=>{
499+
if(opt.success){
500+
opt.success.apply(scope,dd);
501+
}
502+
reject(dd[0]);
503+
}).catch(ee=>{
504+
if(opt.error){
505+
opt.error.apply(scope,dd);
506+
}
507+
reject(ee[0])
508+
})
466509
})
467510

468511
} catch (err) {

src/lib/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,16 @@ var ajaxBody={
3434
AJAXCONF.mockMode=value;
3535
},
3636
get default(){
37-
return AJAXCONF.userDefaultConfig;
37+
return AJAXCONF.userDefaultConfig();
3838
},
3939
set default(value){
40-
AJAXCONF.userDefaultConfig=value;
40+
if(Object.prototype.toString.call(value) === '[object Function]'){
41+
AJAXCONF.userDefaultConfig=value;
42+
}
43+
else{
44+
AJAXCONF.userDefaultConfig=function(){return value};
45+
}
46+
4147
},
4248
set successStatus(fn){
4349
AJAXCONF.successStatus=fn;
@@ -134,7 +140,7 @@ rexShengPlugin.install = function(Vue, options={}) {
134140
AJAXCONF.WSGlobalInstanceName=AJAXCONF.wsInstanceName.replace(/\$/g,'');//全局替换
135141
AJAXCONF.successFormatCallback=options.successFormat || options.resultFormat || AJAXCONF.successFormatCallback;//返回数据的格式化
136142
AJAXCONF.errorFormatCallback=options.errorFormat || options.resultFormat || AJAXCONF.errorFormatCallback;
137-
AJAXCONF.userDefaultConfig=Object.assign({},options.defaultConfig || {},AJAXCONF.userDefaultConfig);
143+
AJAXCONF.userDefaultConfig=function(){return Object.assign({},options.defaultConfig || {},AJAXCONF.userDefaultConfig());}
138144

139145
Vue[AJAXCONF.VueGlobalInstanceName]=Vue.prototype[AJAXCONF.instanceName]=ajaxBody
140146

src/lib/random.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ let getRandomByCount=function(count,minNum,maxNum){
2727
var arr=[];
2828
while(arr.length<count){
2929
var digit=createRandomNumber(minNum,maxNum);
30+
console.log(digit)
3031
if(arr.indexOf(digit)<0){
3132
arr.push(digit);
3233
}
@@ -168,7 +169,14 @@ let rd={
168169
resultValue=this.handle(value[count]);
169170
}
170171
else{
171-
var count=getRandomByCount(parseInt((min+"").substr(1)),0,value.length-1);
172+
console.warn(parseInt((min+"").substr(1)),value.length)
173+
var count=0;
174+
if(parseInt((min+"").substr(1))>value.length){
175+
count=getRandomByCount(value.length,0,value.length-1);
176+
}
177+
else{
178+
count=getRandomByCount(parseInt((min+"").substr(1)),0,value.length-1);
179+
}
172180
resultValue=[];
173181
var self=this;
174182
count.forEach(i=>{

0 commit comments

Comments
 (0)