Skip to content

Commit ea9a0d5

Browse files
committed
修复退出状态未被清除的问题
1 parent 3650721 commit ea9a0d5

File tree

3 files changed

+56
-10
lines changed

3 files changed

+56
-10
lines changed

index.js

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,28 +64,49 @@ var Base = /** @class */ (function () {
6464
var Server = /** @class */ (function (_super) {
6565
__extends(Server, _super);
6666
function Server() {
67-
return _super !== null && _super.apply(this, arguments) || this;
67+
var _this = _super !== null && _super.apply(this, arguments) || this;
68+
_this.data = {
69+
login: false
70+
};
71+
return _this;
6872
}
6973
return Server;
7074
}(Base));
7175
var User = /** @class */ (function (_super) {
7276
__extends(User, _super);
7377
function User() {
74-
return _super !== null && _super.apply(this, arguments) || this;
78+
var _this = _super !== null && _super.apply(this, arguments) || this;
79+
_this.data = {};
80+
return _this;
7581
}
7682
return User;
7783
}(Base));
84+
var Config = /** @class */ (function (_super) {
85+
__extends(Config, _super);
86+
function Config() {
87+
var _this = _super !== null && _super.apply(this, arguments) || this;
88+
_this.data = {
89+
username: 'admin',
90+
password: '123456',
91+
retryUrl: 'http://localhost:3030/info'
92+
};
93+
return _this;
94+
}
95+
return Config;
96+
}(Base));
7897
var server = new Server;
7998
var user = new User;
99+
var config = new Config;
80100
var crossDomain = {
81101
'Access-Control-Allow-Origin': '*',
82102
'Access-Control-Allow-Methods': '*',
83103
'Access-Control-Allow-Headers': '*',
84104
'Access-Control-Allow-Credentials': true
85105
};
86-
var config = {
106+
var config$1 = {
87107
user: user,
88108
server: server,
109+
config: config,
89110
crossDomain: crossDomain
90111
};
91112

@@ -117,6 +138,7 @@ var Auth = /** @class */ (function () {
117138
* 退出登录
118139
*/
119140
Auth.prototype.logout = function () {
141+
console.log(server);
120142
user.clear();
121143
server.set('login', false);
122144
};
@@ -226,14 +248,21 @@ var Server$1 = function (option, callback) {
226248
server$1.use(jsonServer.rewriter(option.rules instanceof Object ? option.rules : rules));
227249
server$1.use(function (req, res, next) {
228250
var http = new Http(res);
229-
var _a = getInfo(req, option, config.crossDomain), url = _a.url, data = _a.data, method = _a.method, urlKey = _a.urlKey, params = _a.params, headConfig = _a.headConfig;
251+
var _a = getInfo(req, option, config$1.crossDomain), url = _a.url, data = _a.data, method = _a.method, urlKey = _a.urlKey, params = _a.params, headConfig = _a.headConfig;
230252
var result = {};
231253
// 是否需要将接口的处理逻辑交由json-server
232254
var transfer = method === 'post' && router.db.__wrapped__.hasOwnProperty(urlKey);
233255
// 1. 验证用户请求的api地址是否有数据
234256
if (data || transfer) {
235257
data = data || {};
236-
result = JSON.parse(JSON.stringify(data[method] || {}));
258+
try {
259+
result = JSON.parse(JSON.stringify(typeof data[method] == 'function'
260+
? data[method](method, params, result)
261+
: data[method] instanceof Object
262+
? data[method]
263+
: {}));
264+
}
265+
catch (e) { }
237266
if (!(result instanceof Object)) {
238267
result = {};
239268
}
@@ -325,6 +354,9 @@ var Server$1 = function (option, callback) {
325354
if (urlKey === option.loginUrl) {
326355
auth.login(params);
327356
}
357+
if (urlKey === option.logoutUrl) {
358+
auth.logout();
359+
}
328360
// 如果存在当前的请求方法, 先根据配置进行处理, 再判断是否需要转交给 json-server
329361
var formatResult = data[method] && data.format ? data.format(method, params, result, { url: url }) : undefined;
330362
if (formatResult) {
@@ -355,10 +387,10 @@ var Server$1 = function (option, callback) {
355387
http.end(result);
356388
});
357389
router.render = function (req, res) {
358-
var _a = getInfo(req, option, config.crossDomain), url = _a.url, method = _a.method, urlKey = _a.urlKey, params = _a.params;
390+
var _a = getInfo(req, option, config$1.crossDomain), url = _a.url, method = _a.method, urlKey = _a.urlKey, params = _a.params;
359391
mockData = mockData || {};
360392
var body = {
361-
code: 200,
393+
code: 201,
362394
message: 'ok',
363395
data: res.locals.data
364396
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ks-mock",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"description": "mock server 模拟后端API接口",
55
"main": "index.js",
66
"scripts": {

src/server.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,17 @@ const Server = (option: anyObject, callback?: Function) => {
8282
// 1. 验证用户请求的api地址是否有数据
8383
if (data || transfer) {
8484
data = data || {}
85-
result = JSON.parse(JSON.stringify(data[method] || {}))
85+
try {
86+
result = JSON.parse(
87+
JSON.stringify(
88+
typeof data[method] == 'function'
89+
? data[method](method, params, result)
90+
: data[method] instanceof Object
91+
? data[method]
92+
: {}
93+
)
94+
)
95+
} catch (e) {}
8696
if (!(result instanceof Object)) {
8797
result = {}
8898
}
@@ -168,6 +178,9 @@ const Server = (option: anyObject, callback?: Function) => {
168178
if (urlKey === option.loginUrl) {
169179
auth.login(params)
170180
}
181+
if (urlKey === option.logoutUrl) {
182+
auth.logout()
183+
}
171184
// 如果存在当前的请求方法, 先根据配置进行处理, 再判断是否需要转交给 json-server
172185
let formatResult = data[method] && data.format ? data.format(method, params, result, { url }) : undefined;
173186
if (formatResult) {
@@ -206,11 +219,12 @@ const Server = (option: anyObject, callback?: Function) => {
206219

207220
mockData = mockData || {}
208221
let body = {
209-
code: 200,
222+
code: 201,
210223
message: 'ok',
211224
data: res.locals.data
212225
}
213226
let current = mockData[urlKey]
227+
214228
if (urlKey === option.loginUrl) {
215229
auth.login(params)
216230
} else if (urlKey === option.logoutUrl) {

0 commit comments

Comments
 (0)