###client\vendor\wafer2-client-sdk\lib\request
module.exports = {
request: request,
RequestError: RequestError,
};
/***
###与LoginError差不多
* @class
* 表示请求过程中发生的异常
*/
var RequestError = (function () {
function RequestError(type, message) {
Error.call(this, message);
this.type = type;
this.message = message;
}
RequestError.prototype = new Error();
RequestError.prototype.constructor = RequestError;
return RequestError;
})();
###request
(传入)options = {
login: Boolean,//是否需要带有登录状态--可能是字符串'http://www....'
success: function(){},
fail: function(){},
complete: function(){},
header: {},
}
function request(options) {
if (typeof options !== 'object') {
var message = '请求传参应为 object 类型,但实际传了 ' + (typeof options) + ' 类型';
throw new RequestError(constants.ERR_INVALID_PARAMS, message);
}
var requireLogin = options.login;
var success = options.success || noop;
var fail = options.fail || noop;
var complete = options.complete || noop;
var originHeader = options.header || {};
// 成功回调
var callSuccess = function () {
success.apply(null, arguments);
complete.apply(null, arguments);
};
// 失败回调
var callFail = function (error) {
fail.call(null, error);
complete.call(null, error);
};
// 是否已经进行过重试
var hasRetried = false;
//是否需要带有登录状态-请求,应该是字符串-即请求地址
if (requireLogin) {
doRequestWithLogin();
} else {
doRequest();
}
// 登录后再请求
// loginLib = require('./login')接前一章
function doRequestWithLogin() {
//登录成功1、wx.setStorageSync(SESSION_KEY, session);
//登录成功执行回调2、doRequest(userInfo)
//失败callFail(error)
//SESSION_KEY值也可能会失效,服务器会判断
loginLib.login({ success: doRequest, fail: callFail });
}
// 实际进行请求的方法
function doRequest() {
//buildAuthHeader函数返回一个对象
/*{header:
{constants.WX_HEADER_SKEY: Session.get()}//登录时候放进去的
}*/
var authHeader = buildAuthHeader(Session.get());
wx.request(utils.extend({}, options, {
header: utils.extend({}, originHeader, authHeader),
success: function (response) {
var data = response.data;
var error, message;
if (data && data.code === -1) {
Session.clear();
// 如果是登录态无效,并且还没重试过,会尝试登录后刷新凭据重新请求
if (!hasRetried) {//是否已经进行过重试
hasRetried = true;
doRequestWithLogin();
return;
}
message = '登录态已过期';
error = new RequestError(data.error, message);
callFail(error);
return;
} else {
callSuccess.apply(null, arguments);
}
},
fail: callFail,
complete: noop,
}));
};
}
###其它
//buildAuthHeader函数返回一个对象
/*{header:
{constants.WX_HEADER_SKEY: session}
}*/
var buildAuthHeader = function buildAuthHeader(session) {
var header = {};
if (session) {
header[constants.WX_HEADER_SKEY] = session;
}
return header;
};