first release
This commit is contained in:
1
build/weRequest.js
Normal file
1
build/weRequest.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
module.exports=function(t){function e(n){if(o[n])return o[n].exports;var c=o[n]={i:n,l:!1,exports:{}};return t[n].call(c.exports,c,c.exports,e),c.l=!0,c.exports}var o={};return e.m=t,e.c=o,e.i=function(t){return t},e.d=function(t,o,n){e.o(t,o)||Object.defineProperty(t,o,{configurable:!1,enumerable:!0,get:n})},e.n=function(t){var o=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(o,"a",o),o},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=1)}([function(t,e){function o(){wx.showToast({title:"加载中",icon:"loading",mask:!0,duration:6e4})}function n(){wx.hideToast()}t.exports={show:o,hide:n}},function(t,e,o){function n(t,e){C?c(t,e):(e.count++,wx.checkSession({success:function(){C=!0},fail:function(){S=""},complete:function(){e.count--,c(t,e)}}))}function c(t,e){if(!S)try{S=wx.getStorageSync(p)||""}catch(t){}S||e.isLogin?"function"==typeof t&&t():(e.count++,wx.login({complete:function(){e.count--,"function"==typeof e.complete&&0==e.count&&e.complete()},success:function(o){if(o.code){var n;n="function"==typeof h.data?h.data():h.data||{},n[h.codeName]=o.code,e.count++,f({url:h.url,data:n,method:h.method,isLogin:!0,success:function(e){S=e,C=!0,"function"==typeof t&&t(),wx.setStorage({key:p,data:S})},complete:function(){e.count--,"function"==typeof e.complete&&0==e.count&&e.complete()}})}else wx.showModal({title:"登录失败",content:"请稍后重试",showCancel:!1}),console.error(o)},fail:function(t){wx.showModal({title:"登录失败",content:t.errMsg||"请稍后重试",showCancel:!1}),console.error(t)}}))}function i(t){return"function"==typeof t.beforeSend&&t.beforeSend(),void 0===t.reLoginLimit?t.reLoginLimit=0:t.reLoginLimit++,void 0===t.count&&(t.count=0),t.showLoading&&(d.show(),t.complete=function(t){return function(){d.hide(),"function"==typeof t&&t.apply(this,arguments)}}(t.complete)),t}function u(t){t.count++,t.data||(t.data={}),t.url!=h.url&&(t.data[p]=S),t.method=t.method||"GET";var e=t.url.startsWith("http")?t.url:g+t.url;"GET"!=t.method&&(e.indexOf("?")>=0?e+="&"+p+"="+S:e+="?"+p+"="+S),wx.request({url:e,data:t.data,method:t.method,header:t.header||{},dataType:t.dataType||"json",success:function(e){if(200==e.statusCode)if(t.isLogin){var o="";try{o=h.success(e.data)}catch(t){}o?t.success(o):a(t,e)}else m(e.data)&&t.reLoginLimit<T?(S="",wx.removeStorage({key:p,complete:function(){c(function(){f(t)},t)}})):y(e.data)&&"function"==typeof t.success?t.success(w(e.data)):a(t,e);else a(t,e)},fail:function(e){wx.showModal({title:"请求失败",content:e.errMsg,showCancel:!1}),a(t,e)},complete:function(){t.count--,"function"==typeof t.complete&&0==t.count&&t.complete()}})}function r(t){t.count++,t.formData||(t.formData={}),t.formData[p]=S,t.dataType=t.dataType||"json",wx.uploadFile({url:g+t.url,filePath:t.filePath||"",name:t.name||"",formData:t.formData,success:function(e){if(200==e.statusCode&&"uploadFile:ok"==e.errMsg){if("json"==t.dataType)try{e.data=JSON.parse(e.data)}catch(o){return a(t,e),!1}m(e.data)&&t.reLoginLimit<T?(S="",wx.removeStorage({key:p,complete:function(){c(function(){l(t)},t)}})):y(e.data)&&"function"==typeof t.success?t.success(w(e.data)):a(t,e)}else a(t,e)},fail:function(e){wx.showModal({title:"请求失败",content:e.errMsg,showCancel:!1}),a(t,e)},complete:function(){t.count--,"function"==typeof t.complete&&0==t.count&&t.complete()}})}function a(t,e){if("function"==typeof t.fail)t.fail(e);else{var o="";if("function"==typeof x)try{o=x(e.data)}catch(t){}else"string"==typeof x&&(o=x);var n="";if("function"==typeof L)try{n=L(e.data)}catch(t){}else"string"==typeof L&&(n=L);wx.showModal({title:o||"操作失败",content:n||"服务器异常,请稍后重试",showCancel:!1})}"function"==typeof v&&v(t,e),console.error(e)}function s(t){p=t.sessionName||"session",m=t.loginTrigger||function(){return!1},h=Object.assign({},{method:"GET",codeName:"code"},t.codeToSession),y=t.successTrigger||function(){return!0},g=t.urlPerfix||"",w=t.successData||function(t){return t},x=t.errorTitle||"操作失败",L=t.errorContent||!1,T=t.reLoginLimit||3,v=t.errorCallback||null,C=t.doNotCheckSession||!1;try{S=wx.getStorageSync(p)||""}catch(t){}}function f(t){t=i(t),n(function(){u(t)},t)}function l(t){t=i(t),n(function(){r(t)},t)}const d=o(0);var p="session",m=function(){return!1},h={},y=function(){return!0},g="",w=function(t){return t},x="操作失败",L=function(t){return t},T=3,v=null,S="",C=!1;t.exports={init:s,request:f,uploadFile:l}}]);
|
||||||
33
example/index.js
Normal file
33
example/index.js
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
const weRequest = require('./request');
|
||||||
|
|
||||||
|
Page({
|
||||||
|
data: {},
|
||||||
|
onLoad: function (option) {
|
||||||
|
this.getData(option.orderid);
|
||||||
|
},
|
||||||
|
getData: function (id) {
|
||||||
|
weRequest.request({
|
||||||
|
url: 'order/detail',
|
||||||
|
data: {
|
||||||
|
id: id
|
||||||
|
},
|
||||||
|
showLoading: true,
|
||||||
|
success: function (data) {
|
||||||
|
console.log(data);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
upload: function() {
|
||||||
|
weRequest.uploadFile({
|
||||||
|
url: 'user/setapplyinfo',
|
||||||
|
filePath: 'xxxxx.png',
|
||||||
|
name: 'pic',
|
||||||
|
formData: {
|
||||||
|
'other': 'params'
|
||||||
|
},
|
||||||
|
success: function (data) {
|
||||||
|
console.log(data);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
59
example/request.js
Normal file
59
example/request.js
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
var weRequest = require('../src/weRequest');
|
||||||
|
|
||||||
|
weRequest.init({
|
||||||
|
// 存在localStorage的session名称,且CGI请求的data中会自动带上以此为名称的session值;可不传,默认为session
|
||||||
|
sessionName: "session",
|
||||||
|
// 请求URL的固定前缀;可不传,默认为空
|
||||||
|
urlPerfix: "https://payapp.weixin.qq.com/wxmaterial/",
|
||||||
|
// 触发重新登录的条件,res为CGI返回的数据
|
||||||
|
loginTrigger: function (res) {
|
||||||
|
// 此处例子:当返回数据中的字段errcode等于0x10040009,会自动触发重新登录
|
||||||
|
return res.errcode == 0x10040009;
|
||||||
|
},
|
||||||
|
// 用code换取session的CGI配置
|
||||||
|
codeToSession: {
|
||||||
|
// CGI的URL
|
||||||
|
url: 'user/login',
|
||||||
|
// 调用改CGI的方法;可不传,默认为GET
|
||||||
|
method: 'GET',
|
||||||
|
// CGI中传参时,存放code的名称,此处例子名称就是code;可不传,默认值为code
|
||||||
|
codeName: 'code',
|
||||||
|
// 登录接口需要的其他参数;可不传,默认为{}
|
||||||
|
data: {},
|
||||||
|
// CGI中返回的session值
|
||||||
|
success: function (res) {
|
||||||
|
// 此处例子:CGI返回数据中的字段session即为session值
|
||||||
|
return res.session;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
// 登录重试次数,当连续请求登录接口返回失败次数超过这个次数,将不再重试登录
|
||||||
|
reLoginLimit: 2,
|
||||||
|
// 触发请求成功的条件
|
||||||
|
successTrigger: function (res) {
|
||||||
|
// 此处例子:当返回数据中的字段errcode等于0时,代表请求成功,其他情况都认为业务逻辑失败
|
||||||
|
return res.errcode == 0;
|
||||||
|
},
|
||||||
|
// 成功之后返回数据;可不传
|
||||||
|
successData: function (res) {
|
||||||
|
// 此处例子:返回数据中的字段data为业务接受到的数据
|
||||||
|
return res.data;
|
||||||
|
},
|
||||||
|
// 当CGI返回错误时,弹框提示的标题文字
|
||||||
|
errorTitle: function(res) {
|
||||||
|
// 此处例子:当返回数据中的字段errcode等于0x10040730时,错误弹框的标题是“温馨提示”,其他情况下则是“操作失败”
|
||||||
|
return res.errcode == 0x10040730 ? '温馨提示' : '操作失败'
|
||||||
|
},
|
||||||
|
// 当CGI返回错误时,弹框提示的内容文字
|
||||||
|
errorContent: function(res) {
|
||||||
|
// 此处例子:返回数据中的字段msg为错误弹框的提示内容文字
|
||||||
|
return res.msg
|
||||||
|
},
|
||||||
|
// 当出现CGI错误时,统一的回调函数,这里可以做统一的错误上报等处理
|
||||||
|
errorCallback: function(obj, res) {
|
||||||
|
// do some report
|
||||||
|
},
|
||||||
|
// 是否需要调用checkSession,验证小程序的登录态过期,可不传,默认为false
|
||||||
|
doNotCheckSession: true
|
||||||
|
})
|
||||||
|
|
||||||
|
module.exports = weRequest;
|
||||||
9
package.json
Normal file
9
package.json
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"name": "weRequest",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "本工具通过拓展小程序的wx.request,让开发者通过简单的配置,实现自动管理登录态等功能",
|
||||||
|
"main": "./src/weRequest.js",
|
||||||
|
"dependencies": {
|
||||||
|
"webpack": "latest"
|
||||||
|
}
|
||||||
|
}
|
||||||
17
src/loading.js
Normal file
17
src/loading.js
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
function show() {
|
||||||
|
wx.showToast({
|
||||||
|
title: '加载中',
|
||||||
|
icon: 'loading',
|
||||||
|
mask: true,
|
||||||
|
duration: 60000
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function hide() {
|
||||||
|
wx.hideToast();
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
show: show,
|
||||||
|
hide: hide
|
||||||
|
}
|
||||||
360
src/weRequest.js
Normal file
360
src/weRequest.js
Normal file
@@ -0,0 +1,360 @@
|
|||||||
|
const loading = require('./loading');
|
||||||
|
|
||||||
|
//params
|
||||||
|
var sessionName = "session";
|
||||||
|
var loginTrigger = function () { return false };
|
||||||
|
var codeToSession = {};
|
||||||
|
var successTrigger = function () { return true };
|
||||||
|
var urlPerfix = "";
|
||||||
|
var successData = function (res) { return res };
|
||||||
|
var errorTitle = "操作失败";
|
||||||
|
var errorContent = function(res) {return res};
|
||||||
|
var reLoginLimit = 3;
|
||||||
|
var errorCallback = null;
|
||||||
|
|
||||||
|
//global data
|
||||||
|
var session = '';
|
||||||
|
var sessionIsFresh = false;
|
||||||
|
|
||||||
|
function checkSession(callback, obj) {
|
||||||
|
if (!sessionIsFresh) {
|
||||||
|
obj.count ++;
|
||||||
|
// 如果还没检验过session是否有效,则需要检验一次
|
||||||
|
wx.checkSession({
|
||||||
|
success: function () {
|
||||||
|
// 登录态有效,且在本生命周期内无须再检验了
|
||||||
|
sessionIsFresh = true;
|
||||||
|
},
|
||||||
|
fail: function () {
|
||||||
|
// 登录态过期
|
||||||
|
session = '';
|
||||||
|
},
|
||||||
|
complete: function () {
|
||||||
|
obj.count --;
|
||||||
|
doLogin(callback, obj);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
// 已经检验过了
|
||||||
|
doLogin(callback, obj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function doLogin(callback, obj) {
|
||||||
|
// 如果运行时Storage中通过其他业务种下了session,但是本控件不知道的,这时候尝试读取Storage
|
||||||
|
if(!session) {
|
||||||
|
try {
|
||||||
|
session = wx.getStorageSync(sessionName) || '';
|
||||||
|
} catch (e) {}
|
||||||
|
}
|
||||||
|
//TODO: 不能这样,否则session失效时无法清掉
|
||||||
|
|
||||||
|
if (session || obj.isLogin) {
|
||||||
|
// 缓存中有session,或者是登录接口
|
||||||
|
typeof callback == "function" && callback();
|
||||||
|
} else {
|
||||||
|
// 缓存中无session
|
||||||
|
obj.count ++;
|
||||||
|
wx.login({
|
||||||
|
complete: function () {
|
||||||
|
obj.count --;
|
||||||
|
typeof obj.complete == "function" && obj.count == 0 && obj.complete();
|
||||||
|
},
|
||||||
|
success: function (res) {
|
||||||
|
if (res.code) {
|
||||||
|
var data;
|
||||||
|
// codeToSession.data支持函数
|
||||||
|
if(typeof codeToSession.data == "function") {
|
||||||
|
data = codeToSession.data();
|
||||||
|
} else {
|
||||||
|
data = codeToSession.data || {};
|
||||||
|
}
|
||||||
|
data[codeToSession.codeName] = res.code;
|
||||||
|
|
||||||
|
obj.count ++;
|
||||||
|
requestWrapper({
|
||||||
|
url: codeToSession.url,
|
||||||
|
data: data,
|
||||||
|
method: codeToSession.method,
|
||||||
|
isLogin: true,
|
||||||
|
success: function (s) {
|
||||||
|
session = s;
|
||||||
|
sessionIsFresh = true;
|
||||||
|
typeof callback == "function" && callback();
|
||||||
|
wx.setStorage({
|
||||||
|
key: sessionName,
|
||||||
|
data: session
|
||||||
|
})
|
||||||
|
},
|
||||||
|
complete: function() {
|
||||||
|
obj.count --;
|
||||||
|
typeof obj.complete == "function" && obj.count == 0 && obj.complete();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
wx.showModal({
|
||||||
|
title: '登录失败',
|
||||||
|
content: '请稍后重试',
|
||||||
|
showCancel: false
|
||||||
|
})
|
||||||
|
console.error(res);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
fail: function (res) {
|
||||||
|
wx.showModal({
|
||||||
|
title: '登录失败',
|
||||||
|
content: res.errMsg || '请稍后重试',
|
||||||
|
showCancel: false
|
||||||
|
})
|
||||||
|
console.error(res);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function preDo(obj) {
|
||||||
|
typeof obj.beforeSend == "function" && obj.beforeSend();
|
||||||
|
|
||||||
|
// 登录态失效,重复登录计数
|
||||||
|
if (typeof obj.reLoginLimit == "undefined") {
|
||||||
|
obj.reLoginLimit = 0;
|
||||||
|
} else {
|
||||||
|
obj.reLoginLimit ++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(typeof obj.count == "undefined") {
|
||||||
|
obj.count = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(obj.showLoading) {
|
||||||
|
loading.show();
|
||||||
|
obj.complete = (function(fn) {
|
||||||
|
return function() {
|
||||||
|
loading.hide();
|
||||||
|
typeof fn == "function" && fn.apply(this, arguments);
|
||||||
|
}
|
||||||
|
})(obj.complete)
|
||||||
|
}
|
||||||
|
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
function request(obj) {
|
||||||
|
obj.count ++;
|
||||||
|
|
||||||
|
if (!obj.data) {
|
||||||
|
obj.data = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (obj.url != codeToSession.url) {
|
||||||
|
obj.data[sessionName] = session;
|
||||||
|
}
|
||||||
|
|
||||||
|
obj.method = obj.method || 'GET';
|
||||||
|
|
||||||
|
// 如果请求的URL中不是http开头的,则自动添加配置中的前缀
|
||||||
|
var url = obj.url.startsWith('http') ? obj.url : (urlPerfix + obj.url);
|
||||||
|
// 如果请求不是GET,则在URL中自动加上登录态
|
||||||
|
if(obj.method != "GET") {
|
||||||
|
if(url.indexOf('?') >= 0) {
|
||||||
|
url += '&' + sessionName + '=' + session;
|
||||||
|
} else {
|
||||||
|
url += '?' + sessionName + '=' + session;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
wx.request({
|
||||||
|
url: url,
|
||||||
|
data: obj.data,
|
||||||
|
method: obj.method,
|
||||||
|
header: obj.header || {},
|
||||||
|
dataType: obj.dataType || 'json',
|
||||||
|
success: function (res) {
|
||||||
|
if (res.statusCode == 200) {
|
||||||
|
if (obj.isLogin) {
|
||||||
|
// 登录请求
|
||||||
|
var s = "";
|
||||||
|
try {
|
||||||
|
s = codeToSession.success(res.data);
|
||||||
|
} catch (e) {}
|
||||||
|
if(s) {
|
||||||
|
obj.success(s);
|
||||||
|
} else {
|
||||||
|
fail(obj, res);
|
||||||
|
}
|
||||||
|
} else if (loginTrigger(res.data) && obj.reLoginLimit < reLoginLimit) {
|
||||||
|
// 登录态失效,且重试次数不超过配置
|
||||||
|
session = '';
|
||||||
|
wx.removeStorage({
|
||||||
|
key: sessionName,
|
||||||
|
complete: function() {
|
||||||
|
doLogin(function () {
|
||||||
|
requestWrapper(obj);
|
||||||
|
}, obj)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else if (successTrigger(res.data) && typeof obj.success == "function") {
|
||||||
|
// 接口返回成功码
|
||||||
|
obj.success(successData(res.data));
|
||||||
|
} else {
|
||||||
|
// 接口返回失败码
|
||||||
|
fail(obj, res);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fail(obj, res);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
fail: function (res) {
|
||||||
|
wx.showModal({
|
||||||
|
title: "请求失败",
|
||||||
|
content: res.errMsg,
|
||||||
|
showCancel: false
|
||||||
|
})
|
||||||
|
fail(obj, res);
|
||||||
|
},
|
||||||
|
complete: function () {
|
||||||
|
obj.count --;
|
||||||
|
typeof obj.complete == "function" && obj.count == 0 && obj.complete();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function uploadFile(obj) {
|
||||||
|
obj.count ++;
|
||||||
|
|
||||||
|
if (!obj.formData) {
|
||||||
|
obj.formData = {};
|
||||||
|
}
|
||||||
|
obj.formData[sessionName] = session;
|
||||||
|
|
||||||
|
obj.dataType = obj.dataType || 'json';
|
||||||
|
|
||||||
|
wx.uploadFile({
|
||||||
|
url: urlPerfix + obj.url,
|
||||||
|
filePath: obj.filePath || '',
|
||||||
|
name: obj.name || '',
|
||||||
|
formData: obj.formData,
|
||||||
|
success: function (res) {
|
||||||
|
if (res.statusCode == 200 && res.errMsg == 'uploadFile:ok') {
|
||||||
|
if(obj.dataType == 'json') {
|
||||||
|
try {
|
||||||
|
res.data = JSON.parse(res.data);
|
||||||
|
} catch (e) {
|
||||||
|
fail(obj, res);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (loginTrigger(res.data) && obj.reLoginLimit < reLoginLimit) {
|
||||||
|
// 登录态失效,且重试次数不超过配置
|
||||||
|
session = '';
|
||||||
|
wx.removeStorage({
|
||||||
|
key: sessionName,
|
||||||
|
complete: function() {
|
||||||
|
doLogin(function () {
|
||||||
|
uploadFileWrapper(obj);
|
||||||
|
}, obj)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else if (successTrigger(res.data) && typeof obj.success == "function") {
|
||||||
|
// 接口返回成功码
|
||||||
|
obj.success(successData(res.data));
|
||||||
|
} else {
|
||||||
|
// 接口返回失败码
|
||||||
|
fail(obj, res);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fail(obj, res);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
fail: function (res) {
|
||||||
|
wx.showModal({
|
||||||
|
title: "请求失败",
|
||||||
|
content: res.errMsg,
|
||||||
|
showCancel: false
|
||||||
|
})
|
||||||
|
fail(obj, res);
|
||||||
|
},
|
||||||
|
complete: function () {
|
||||||
|
obj.count --;
|
||||||
|
typeof obj.complete == "function" && obj.count == 0 && obj.complete();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function fail(obj, res) {
|
||||||
|
if(typeof obj.fail == "function") {
|
||||||
|
obj.fail(res);
|
||||||
|
} else {
|
||||||
|
var title = "";
|
||||||
|
if(typeof errorTitle == "function") {
|
||||||
|
try {
|
||||||
|
title = errorTitle(res.data)
|
||||||
|
} catch (e) {}
|
||||||
|
} else if (typeof errorTitle == "string") {
|
||||||
|
title = errorTitle;
|
||||||
|
}
|
||||||
|
|
||||||
|
var content = "";
|
||||||
|
if(typeof errorContent == "function") {
|
||||||
|
try {
|
||||||
|
content = errorContent(res.data)
|
||||||
|
} catch (e) {}
|
||||||
|
} else if(typeof errorContent == "string") {
|
||||||
|
content = errorContent;
|
||||||
|
}
|
||||||
|
|
||||||
|
wx.showModal({
|
||||||
|
title: title || "操作失败",
|
||||||
|
content: content || "服务器异常,请稍后重试",
|
||||||
|
showCancel: false
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果有配置统一错误回调函数,则执行它
|
||||||
|
if(typeof errorCallback == "function") {
|
||||||
|
errorCallback(obj, res);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.error(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
function init(params) {
|
||||||
|
sessionName = params.sessionName || 'session';
|
||||||
|
loginTrigger = params.loginTrigger || function () { return false };
|
||||||
|
codeToSession = Object.assign({}, {
|
||||||
|
method: 'GET',
|
||||||
|
codeName: 'code'
|
||||||
|
}, params.codeToSession);
|
||||||
|
successTrigger = params.successTrigger || function () { return true };
|
||||||
|
urlPerfix = params.urlPerfix || "";
|
||||||
|
successData = params.successData || function (res) { return res };
|
||||||
|
errorTitle = params.errorTitle || "操作失败";
|
||||||
|
errorContent = params.errorContent || false;
|
||||||
|
reLoginLimit = params.reLoginLimit || 3;
|
||||||
|
errorCallback = params.errorCallback || null;
|
||||||
|
sessionIsFresh = params.doNotCheckSession || false;
|
||||||
|
|
||||||
|
try {
|
||||||
|
session = wx.getStorageSync(sessionName) || '';
|
||||||
|
} catch (e) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
function requestWrapper(obj) {
|
||||||
|
obj = preDo(obj);
|
||||||
|
checkSession(function () {
|
||||||
|
request(obj);
|
||||||
|
}, obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
function uploadFileWrapper(obj) {
|
||||||
|
obj = preDo(obj);
|
||||||
|
checkSession(function () {
|
||||||
|
uploadFile(obj);
|
||||||
|
}, obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
init: init,
|
||||||
|
request: requestWrapper,
|
||||||
|
uploadFile: uploadFileWrapper
|
||||||
|
};
|
||||||
21
webpack.config.js
Normal file
21
webpack.config.js
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
var path = require('path');
|
||||||
|
var webpack = require('webpack');
|
||||||
|
var UglifyJsPlugin = webpack.optimize.UglifyJsPlugin;
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
entry: './src/weRequest.js',
|
||||||
|
output: {
|
||||||
|
path: path.join(__dirname, 'build'),
|
||||||
|
filename: 'weRequest.js',
|
||||||
|
library: "weRequest",
|
||||||
|
libraryTarget: "commonjs-module"
|
||||||
|
},
|
||||||
|
plugins: [
|
||||||
|
new UglifyJsPlugin({
|
||||||
|
compress: {
|
||||||
|
warnings: false
|
||||||
|
},
|
||||||
|
except: []
|
||||||
|
})
|
||||||
|
]
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user