简单跑起来了
This commit is contained in:
1016
build/weRequest.js
1016
build/weRequest.js
File diff suppressed because one or more lines are too long
2
build/weRequest.min.js
vendored
2
build/weRequest.min.js
vendored
File diff suppressed because one or more lines are too long
2745
package-lock.json
generated
2745
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -23,16 +23,15 @@
|
||||
"scripts": {
|
||||
"build": "webpack"
|
||||
},
|
||||
"dependencies": {
|
||||
"webpack": "latest"
|
||||
},
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"prettier": "^1.15.3",
|
||||
"pretty-quick": "^1.8.0",
|
||||
"ts-loader": "^5.3.1",
|
||||
"tslint-config-prettier": "^1.17.0",
|
||||
"typescript": "^3.2.2",
|
||||
"webpack-cli": "^2.1.3"
|
||||
"webpack": "^4.28.0",
|
||||
"webpack-cli": "^3.1.1"
|
||||
},
|
||||
"husky": {
|
||||
"hooks": {
|
||||
|
||||
@@ -2,10 +2,11 @@ import config from '../store/config'
|
||||
import status from '../store/status'
|
||||
|
||||
export default () => {
|
||||
return {
|
||||
const configResult :IGetConfigResult = {
|
||||
urlPerfix: config.urlPerfix,
|
||||
sessionExpireTime: status.sessionExpireTime,
|
||||
sessionExpireKey: config.sessionExpireKey,
|
||||
sessionExpire: status.sessionExpire
|
||||
}
|
||||
return configResult;
|
||||
};
|
||||
|
||||
@@ -2,11 +2,11 @@ import defaultConfig from '../store/config'
|
||||
import status from '../store/status'
|
||||
|
||||
export default (params: IInitOption) => {
|
||||
const config: IInitOption = {...params, ...defaultConfig)}
|
||||
const config: IInitOption = {...params, ...defaultConfig}
|
||||
// 如果配置更改了session的存储名字,则重新获取一次session
|
||||
if (params.sessionName) {
|
||||
try {
|
||||
status.session = wx.getStorageSync(config.sessionName) || '';
|
||||
status.session = wx.getStorageSync(config.sessionName!) || '';
|
||||
} catch (e) {
|
||||
console.error('wx.getStorageSync:fail, can not get session.')
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import checkSession from '../module/sessionManager'
|
||||
|
||||
export default (callback) => {
|
||||
export default (callback: Function) => {
|
||||
return checkSession(callback, {})
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import requestHandler from '../module/requestHandler'
|
||||
|
||||
export default (obj: IRequestOption) => {
|
||||
return requestHandler.request(obj)
|
||||
requestHandler.request(obj)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import status from '../store/status'
|
||||
|
||||
export default (session) => {
|
||||
export default (session: TODO) => {
|
||||
status.session = session;
|
||||
status.sessionIsFresh = true;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import requestHandler from '../module/requestHandler'
|
||||
|
||||
export default (obj) => {
|
||||
export default (obj: TODO) => {
|
||||
requestHandler.uploadFile(obj)
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
function get(obj) {
|
||||
function get(obj: TODO) {
|
||||
wx.getStorage({
|
||||
key: obj.originUrl,
|
||||
success: function (res) {
|
||||
@@ -16,7 +16,7 @@ function get(obj) {
|
||||
})
|
||||
}
|
||||
|
||||
function set(obj, realData) {
|
||||
function set(obj: TODO , realData: TODO) {
|
||||
if (obj.cache === true || (typeof obj.cache === "function" && obj.cache(realData))) {
|
||||
wx.setStorage({
|
||||
key: obj.originUrl,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import config from '../store/config'
|
||||
|
||||
function start(obj, name) {
|
||||
function start(obj: TODO, name?: string) {
|
||||
switch (name) {
|
||||
case 'checkSession':
|
||||
obj._checkSessionStartTime = new Date().getTime();
|
||||
@@ -15,7 +15,7 @@ function start(obj, name) {
|
||||
}
|
||||
}
|
||||
|
||||
function end(obj, name) {
|
||||
function end(obj: TODO, name?: string) {
|
||||
switch (name) {
|
||||
case 'checkSession':
|
||||
// wx.checkSession 耗时上报
|
||||
|
||||
@@ -1,23 +1,23 @@
|
||||
import config from '../store/config'
|
||||
|
||||
export default (obj, res) => {
|
||||
export default (obj: TODO, res: TODO) => {
|
||||
if (typeof obj.fail === "function") {
|
||||
obj.fail(res);
|
||||
} else {
|
||||
let title = "";
|
||||
if (typeof config.errorTitle === "function") {
|
||||
try {
|
||||
title = config.errorTitle(res.data)
|
||||
title = config.errorTitle(res.data || res.errMsg)
|
||||
} catch (e) {
|
||||
}
|
||||
} else if (typeof errorTitle === "string") {
|
||||
} else if (typeof config.errorTitle === "string") {
|
||||
title = config.errorTitle;
|
||||
}
|
||||
|
||||
let content = "";
|
||||
if (typeof config.errorContent === "function") {
|
||||
try {
|
||||
content = config.errorContent(res.data)
|
||||
content = config.errorContent(res.data || res.errMsg)
|
||||
} catch (e) {
|
||||
}
|
||||
} else if (typeof config.errorContent === "string") {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import config from '../store/config'
|
||||
import responseHandler from './responseHandler'
|
||||
|
||||
function get(obj, method) {
|
||||
function get(obj: TODO, method: "request" | "uploadFile"): any {
|
||||
|
||||
if(!config.mockJson[obj.url] && !config.mockJson[obj.originUrl]) {
|
||||
// mock 没有对应接口的数据
|
||||
|
||||
@@ -9,7 +9,7 @@ import responseHandler from './responseHandler'
|
||||
import durationReporter from "./durationReporter";
|
||||
|
||||
// 格式化url
|
||||
function format(url) {
|
||||
function format(url: string) {
|
||||
if (url.startsWith('http')) {
|
||||
return url
|
||||
} else {
|
||||
@@ -22,7 +22,7 @@ function format(url) {
|
||||
}
|
||||
|
||||
// 所有请求发出前需要做的事情
|
||||
function preDo(obj) {
|
||||
function preDo(obj: TODO) {
|
||||
if(typeof obj.beforeSend === "function") {
|
||||
obj.beforeSend();
|
||||
}
|
||||
@@ -39,10 +39,12 @@ function preDo(obj) {
|
||||
|
||||
if (obj.showLoading) {
|
||||
loading.show(obj.showLoading);
|
||||
obj.complete = ((fn) => {
|
||||
obj.complete = ((fn: Function, ...args) => {
|
||||
return ()=> {
|
||||
// TODO 使用Promise方式后,可能不需要这些了
|
||||
loading.hide();
|
||||
typeof fn === "function" && fn.apply(this, arguments);
|
||||
// @ts-ignore
|
||||
typeof fn === "function" && fn.apply(this, ...args);
|
||||
}
|
||||
})(obj.complete)
|
||||
}
|
||||
@@ -54,17 +56,17 @@ function preDo(obj) {
|
||||
}
|
||||
|
||||
// 格式化处理请求的obj内容
|
||||
function initialize(obj, container) {
|
||||
function initialize(obj: TODO, container: TODO) {
|
||||
if (!obj[container]) {
|
||||
obj[container] = {};
|
||||
}
|
||||
|
||||
if (obj.originUrl !== config.codeToSession.url && status.session) {
|
||||
obj[container][config.sessionName] = status.session;
|
||||
obj[container][config.sessionName!] = status.session;
|
||||
}
|
||||
|
||||
// 如果有全局参数,则添加
|
||||
let gd = {};
|
||||
let gd: any = {};
|
||||
if (typeof config.globalData === "function") {
|
||||
gd = config.globalData();
|
||||
} else if (typeof config.globalData === "object") {
|
||||
@@ -101,7 +103,7 @@ function initialize(obj, container) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
function doRequest(obj) {
|
||||
function doRequest(obj: TODO) {
|
||||
obj = initialize(obj, 'data');
|
||||
obj.count++;
|
||||
wx.request({
|
||||
@@ -110,10 +112,10 @@ function doRequest(obj) {
|
||||
method: obj.method,
|
||||
header: obj.header || {},
|
||||
dataType: obj.dataType || 'json',
|
||||
success: function (res) {
|
||||
success: function (res: wx.RequestSuccessCallbackResult) {
|
||||
responseHandler(res, obj, 'request')
|
||||
},
|
||||
fail: function (res) {
|
||||
fail: function (res: wx.GeneralCallbackResult) {
|
||||
errorHandler(obj, res);
|
||||
console.error(res);
|
||||
},
|
||||
@@ -124,18 +126,17 @@ function doRequest(obj) {
|
||||
})
|
||||
}
|
||||
|
||||
function doUploadFile(obj) {
|
||||
function doUploadFile(obj: TODO) {
|
||||
obj.count++;
|
||||
wx.uploadFile({
|
||||
url: obj.url,
|
||||
filePath: obj.filePath || '',
|
||||
name: obj.name || '',
|
||||
method: 'POST',
|
||||
formData: obj.formData,
|
||||
success: function (res) {
|
||||
success: function (res: wx.UploadFileSuccessCallbackResult) {
|
||||
responseHandler(res, obj, 'uploadFile')
|
||||
},
|
||||
fail: function (res) {
|
||||
fail: function (res: wx.GeneralCallbackResult) {
|
||||
errorHandler(obj, res);
|
||||
console.error(res);
|
||||
},
|
||||
@@ -146,7 +147,7 @@ function doUploadFile(obj) {
|
||||
})
|
||||
}
|
||||
|
||||
function request(obj) {
|
||||
function request(obj: TODO): TODO {
|
||||
obj = preDo(obj);
|
||||
if(config.mockJson) {
|
||||
mockManager.get(obj, 'request');
|
||||
@@ -161,7 +162,7 @@ function request(obj) {
|
||||
}, obj)
|
||||
}
|
||||
|
||||
function uploadFile(obj) {
|
||||
function uploadFile(obj: TODO): TODO {
|
||||
obj = preDo(obj);
|
||||
if(config.mockJson) {
|
||||
mockManager.get(obj, 'uploadFile');
|
||||
|
||||
@@ -5,7 +5,7 @@ import errorHandler from './errorHandler'
|
||||
import cacheManager from './cacheManager'
|
||||
import durationReporter from './durationReporter'
|
||||
|
||||
function response(res, obj, method) {
|
||||
function response(res: wx.RequestSuccessCallbackResult | wx.UploadFileSuccessCallbackResult, obj: TODO, method: "request" | "uploadFile"): any {
|
||||
if (res.statusCode === 200) {
|
||||
|
||||
// 兼容uploadFile返回的res.data可能是字符串
|
||||
@@ -24,7 +24,7 @@ function response(res, obj, method) {
|
||||
// 登录请求
|
||||
let s = "";
|
||||
try {
|
||||
s = config.codeToSession.success(res.data);
|
||||
s = config.codeToSession.success!(res.data);
|
||||
} catch (e) {
|
||||
}
|
||||
if (s) {
|
||||
@@ -32,21 +32,21 @@ function response(res, obj, method) {
|
||||
} else {
|
||||
errorHandler(obj, res);
|
||||
}
|
||||
} else if (config.loginTrigger(res.data) && obj.reLoginLimit < config.reLoginLimit) {
|
||||
} else if (config.loginTrigger!(res.data) && obj.reLoginLimit < config.reLoginLimit!) {
|
||||
// 登录态失效,且重试次数不超过配置
|
||||
status.session = '';
|
||||
status.sessionIsFresh = true;
|
||||
wx.removeStorage({
|
||||
key: config.sessionName,
|
||||
key: config.sessionName!,
|
||||
complete: function () {
|
||||
requestHandler[method](obj)
|
||||
}
|
||||
})
|
||||
} else if (config.successTrigger(res.data) && typeof obj.success === "function") {
|
||||
} else if (config.successTrigger!(res.data) && typeof obj.success === "function") {
|
||||
// 接口返回成功码
|
||||
let realData = null;
|
||||
try {
|
||||
realData = config.successData(res.data);
|
||||
realData = config.successData!(res.data);
|
||||
} catch (e) {
|
||||
console.error("Function successData occur error: " + e);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import requestHandler from './requestHandler'
|
||||
import errorHandler from './errorHandler'
|
||||
import durationReporter from './durationReporter'
|
||||
|
||||
function checkSession(callback, obj) {
|
||||
function checkSession(callback: Function, obj: TODO) {
|
||||
if (status.isCheckingSession) {
|
||||
flow.wait('checkSessionFinished', () => {
|
||||
checkSession(callback, obj)
|
||||
@@ -38,7 +38,7 @@ function checkSession(callback, obj) {
|
||||
}
|
||||
}
|
||||
|
||||
function doLogin(callback, obj) {
|
||||
function doLogin(callback: Function, obj: TODO) {
|
||||
if (obj.isLogin) {
|
||||
// 登录接口,直接放过
|
||||
typeof callback === "function" && callback();
|
||||
@@ -90,7 +90,7 @@ function doLogin(callback, obj) {
|
||||
}
|
||||
}
|
||||
|
||||
function code2Session(obj, code, callback) {
|
||||
function code2Session(obj: TODO, code: TODO, callback: Function) {
|
||||
let data;
|
||||
// codeToSession.data支持函数
|
||||
if (typeof config.codeToSession.data === "function") {
|
||||
@@ -98,7 +98,7 @@ function code2Session(obj, code, callback) {
|
||||
} else {
|
||||
data = config.codeToSession.data || {};
|
||||
}
|
||||
data[config.codeToSession.codeName] = code;
|
||||
data[config.codeToSession.codeName!] = code;
|
||||
|
||||
obj.count++;
|
||||
requestHandler.request({
|
||||
@@ -107,7 +107,7 @@ function code2Session(obj, code, callback) {
|
||||
method: config.codeToSession.method || 'GET',
|
||||
isLogin: true,
|
||||
report: config.codeToSession.report || config.codeToSession.url,
|
||||
success: function (s) {
|
||||
success: function (s: TODO) {
|
||||
status.session = s;
|
||||
status.sessionIsFresh = true;
|
||||
// 如果有设置本地session过期时间
|
||||
@@ -115,12 +115,12 @@ function code2Session(obj, code, callback) {
|
||||
status.sessionExpire = new Date().getTime() + status.sessionExpireTime;
|
||||
wx.setStorage({
|
||||
key: config.sessionExpireKey,
|
||||
data: status.sessionExpire
|
||||
data: String(status.sessionExpire)
|
||||
})
|
||||
}
|
||||
typeof callback === "function" && callback();
|
||||
wx.setStorage({
|
||||
key: config.sessionName,
|
||||
key: config.sessionName!,
|
||||
data: status.session
|
||||
})
|
||||
},
|
||||
|
||||
@@ -9,4 +9,4 @@ export default {
|
||||
logining: false as boolean,
|
||||
// 正在查询session有效期中,避免重复调用接口
|
||||
isCheckingSession: false as boolean
|
||||
}
|
||||
} as any
|
||||
|
||||
45
src/typings/weRequest/index.d.ts
vendored
45
src/typings/weRequest/index.d.ts
vendored
@@ -2,15 +2,15 @@ interface weRequest {
|
||||
/** 小程序账号信息 */
|
||||
init?: (obj: IInitOption) => void;
|
||||
/** 插件账号信息(仅在插件中调用时包含这一项) */
|
||||
request?: (option: IRequestOption) => Promise<any>;
|
||||
request?: (option: IRequestOption) => void;
|
||||
/** 插件账号信息(仅在插件中调用时包含这一项) */
|
||||
uploadFile?: (option: IUploadFileOption) => Promise<any>;
|
||||
uploadFile?: (option: IUploadFileOption) => void;
|
||||
/* 获取本地缓存中用户票据的值 */
|
||||
getSession?: () => string;
|
||||
/* 获取weRequest的配置 */
|
||||
getConfig?: () => IGetConfigResult;
|
||||
/* [不建议使用] 在不发起业务请求的情况下,单独执行登录逻辑 */
|
||||
login?: (callback: ) => void;
|
||||
login?: (callback: Function) => void;
|
||||
/* [不建议使用] 设置用户票据的值 */
|
||||
setSession?: (x: string) => void;
|
||||
}
|
||||
@@ -26,17 +26,26 @@ interface IInitOption {
|
||||
/* 登录重试次数,当连续请求登录接口返回失败次数超过这个次数,将不再重试登录 */
|
||||
reLoginLimit?: number;
|
||||
/* 当出现接口逻辑错误时,会执行统一的回调函数,这里可以做统一的错误上报等处理 */
|
||||
errorCallback?: null | (() => void);
|
||||
errorCallback?: null | Function;
|
||||
/* 接口返回成功之后,会执行统一的回调函数,这里可以做统一的耗时上报等处理 */
|
||||
reportCGI?: boolean | ((...args: IReportCGIParam[]) => void);
|
||||
reportCGI?: boolean | ((
|
||||
/* 调用的接口名字,可在request接口的report字段配置 */
|
||||
name: string,
|
||||
/* 发起请求时的时间戳 */
|
||||
startTime: number,
|
||||
/* 请求返回时的时间戳 */
|
||||
endTime: number,
|
||||
/* 请求方法,可用于上报 */
|
||||
request?: () => void
|
||||
) => void);
|
||||
/* 可为接口提供mock数据 */
|
||||
mockJson?: boolean | (() => object);
|
||||
mockJson?: TODO;
|
||||
/** 所有请求都会自动带上这里的参数 */
|
||||
globalData?: boolean | object | (() => object);
|
||||
globalData?: boolean | object | Function;
|
||||
/** session在本地缓存的key */
|
||||
sessionExpireKey: string;
|
||||
/* 触发重新登录的条件;参数为CGI返回的数据,返回需要重新登录的条件 */
|
||||
loginTrigger?: () => boolean;
|
||||
loginTrigger?: (res: string | IAnyObject | ArrayBuffer) => boolean;
|
||||
/* 触发请求成功的条件;参数为CGI返回的数据,返回接口逻辑成功的条件 */
|
||||
successTrigger?: (res: string | IAnyObject | ArrayBuffer) => boolean;
|
||||
/* 成功之后返回数据;参数为CGI返回的数据,返回逻辑需要使用的数据 */
|
||||
@@ -64,9 +73,11 @@ interface ICodeToSessionOptions{
|
||||
/* 登录接口需要的其他参数 */
|
||||
data?: string | IAnyObject | ArrayBuffer;
|
||||
/* 接口返回成功的函数;需要返回session的值 */
|
||||
success?: () => void;
|
||||
success?: Function;
|
||||
/* code换取session的接口逻辑出错时,执行的函数,若配置了此函数,则不再默认弹窗报错 */
|
||||
fail?: () => void;
|
||||
fail?: Function;
|
||||
/* codeToSession的上报字段名 */
|
||||
report?: string;
|
||||
}
|
||||
|
||||
interface IReportCGIParam{
|
||||
@@ -77,12 +88,12 @@ interface IReportCGIParam{
|
||||
/* 请求返回时的时间戳 */
|
||||
endTime: number;
|
||||
/* 请求方法,可用于上报 */
|
||||
request: () => void;
|
||||
request: Function;
|
||||
}
|
||||
|
||||
interface IRequestOption extends wx.RequestOption {
|
||||
/* 发起请求前执行的函数 */
|
||||
beforeSend?: () => void;
|
||||
beforeSend?: Function;
|
||||
/* 请求过程页面是否展示全屏的loading */
|
||||
showLoading?: boolean | string;
|
||||
/* 接口请求成功后将自动执行init()中配置的reportCGI函数,其中的name字段值为这里配置的值 */
|
||||
@@ -91,7 +102,7 @@ interface IRequestOption extends wx.RequestOption {
|
||||
|
||||
interface IUploadFileOption extends wx.UploadFileOption {
|
||||
/* 发起请求前执行的函数 */
|
||||
beforeSend?: () => void;
|
||||
beforeSend?: Function;
|
||||
/* 请求过程页面是否展示全屏的loading */
|
||||
showLoading?: boolean | string;
|
||||
/* 接口请求成功后将自动执行init()中配置的reportCGI函数,其中的name字段值为这里配置的值 */
|
||||
@@ -100,13 +111,13 @@ interface IUploadFileOption extends wx.UploadFileOption {
|
||||
|
||||
interface IGetConfigResult{
|
||||
/* 在组件初始化时传入的请求URL的固定前缀 */
|
||||
urlPerfix: string | (() => string);
|
||||
urlPerfix?: string | (() => string);
|
||||
/* 在组件初始化时传入的用户登陆态设置本地缓存时间 */
|
||||
sessionExpireTime: number;
|
||||
sessionExpireTime?: number;
|
||||
/* 在组件初始化时传入的用户登陆态本地缓存时间Storage的key */
|
||||
sessionExpireKey: string;
|
||||
sessionExpireKey?: string;
|
||||
/* 用户登陆态本地缓存过期的时间戳 */
|
||||
sessionExpire: number;
|
||||
sessionExpire?: number;
|
||||
}
|
||||
|
||||
declare const weRequest: weRequest
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
let store = {};
|
||||
let store: TODO = {};
|
||||
|
||||
function emit(key) {
|
||||
function emit(key: TODO) {
|
||||
let flow = getFlow(key);
|
||||
let currentLength = flow.waitingList.length;
|
||||
for (let i = 0; i < currentLength; i++) {
|
||||
@@ -9,12 +9,12 @@ function emit(key) {
|
||||
}
|
||||
}
|
||||
|
||||
function wait(key, callback) {
|
||||
function wait(key: TODO, callback: Function) {
|
||||
var flow = getFlow(key);
|
||||
flow.waitingList.push(callback)
|
||||
}
|
||||
|
||||
function getFlow(key) {
|
||||
function getFlow(key: TODO) {
|
||||
if (!store[key]) {
|
||||
store[key] = {
|
||||
waitingList: []
|
||||
|
||||
Reference in New Issue
Block a user