login.js
7.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
var request = require('request');
var _ = require('lodash');
var fs = require('fs');
var Iaccount = global.Register.system;
var Utils = {
createrMenus: function (data) {
var menu = [];
for (var key in data) {
var v = data[key];
var item = {
title: v.menu_name,
}
if (v.parent_id === "0") {
item.parent = 'menu-template';
var itemSubs = [];
_.forEach(v.sub, function (val) {
var sub = {
title: val.menu_name,
href: val.menu_url,
icon: 'list-alt'
}
itemSubs.push(sub);
});
item.menu = itemSubs;
}
menu.push(item);
}
return menu;
}
}
module.exports = {
namespace: "common",
apis: {
Ilogin: {
title:'登录接口',
url: Iaccount.loginEx,
// form: '["{{account}}","{{password}}",{{platform}}]', 北京erp java登录接口,只接受application/json
params: {
account: { type: String },
password: { type: String },
platform: {type:Number,default: 2}
}
},
getResourceByPid: {
title:'菜单接口',
url: Iaccount.getResourceByPid,
form: '[{{pid}},{{role_id}},{{website}}]',
params: {
pid: { type: Number },
role_id: { type: Number },
website: { type: Number, default: Iaccount.WEBSITE }
}
},
allRight: {
title: '获取权限',
url: Iaccount.allRight,
form: '[false]'
},
sessionAsync: {
title:'与老系统session同步',
url: Iaccount.url + '/account/login/sessions',
outobj: "response",
form: {'account':'{{user}}','password':'{{password}}','refer': '{{refer}}'},
params: {
user: { type: String },
password: { type: String },
refer:{type:String,default:'%2Faccount%2Fprofile%2Fdisplay'}
}
},
login: function (req, callback, common) {
var userInfo = {},result = { code: 400, message: "登录失败" },isSuccess=false;
var user = req.body.user||req.query.user;
var password = req.body.password||req.query.password;
var errcallback = function (err) {
result.code = err.errorCode;
return callback(null, result);
}
console.log("begin enter common Ilogin --------------------------------------------------------------------------------------");
common.Ilogin(user, password).allRight().sessionAsync(user, password).done(function (IloginRes,allRes,sesRes) {
var userData = IloginRes.data, cookie = sesRes.caseless.dict["set-cookie"];
console.log("enter common Ilogin, code: " + IloginRes.code + ", pwdComplexRate: " + IloginRes.data.pwdComplexRate);
result.code = IloginRes.code;
result.pwdComplexRate = IloginRes.data.pwdComplexRate;
result.passwd_time = IloginRes.data.pwdExpirationDays;
userInfo = { auth: userData, uid: userData.pid, name: userData.truename, role: userData.roleName, right: {},shopList:[],brands:[]};
if(userData.data_authority&&userData.data_authority.brand&&userData.data_authority.brand.list){
userInfo.brands=userData.data_authority.brand.is_all?"0":Object.keys(userData.data_authority.brand.list);
}
allRes.data.forEach(function (data) {
if (data.platform_id == Iaccount.WEBSITE) {
userInfo.right[data.path] = true;
}
});
common.getResourceByPid(userData.pid, userData.role_id).done(function (getRRes) {
if (result.pwdComplexRate != undefined && !result.pwdComplexRate) { // 如果是弱密码,则需要强制修改密码,这里就不要展示任何菜单
userInfo.menu = [];
} else {
userInfo.menu = Utils.createrMenus(getRRes.data);
}
// 如果是要过期,则需要提示用户密码要过期
console.log("enter getResourceByPid, passwd_time: " + result.passwd_time);
if (result.passwd_time != undefined && result.passwd_time > 0) {
userInfo.passwordMsg = '密码' + result.passwd_time + '天后过期,请尽快修改';
} else if (result.passwd_time != undefined && result.passwd_time <= 0) {
userInfo.passwordMsg = '密码已过期,请尽快修改';
}
console.log("enter getResourceByPid, passwordMsg: " + userInfo.passwordMsg);
req.session.user = userInfo;
if (cookie && cookie.length > 0) {
req.session.gray = cookie[0];
result = { code: 200, message: "登录成功" ,data:req.session.user};
}
return callback(null, result);
},errcallback);
}, errcallback);
},
isUsedMenuAuth:{
title:'检查有没有权限',
url:Iaccount.isUsedMenuAuth,
form:'[{{pid}},{{role_id}},"{{path}}","","",' + Iaccount.WEBSITE + ']',
params:{
pid:{type:Number},
role_id:{type:Number},
path:{type:String}
}
},
gray: function (req, callback,common) {
var result = { code: 201, message: "没有权限" };
console.log("enter gray filter...");
if (req.session && req.session.user) {
var user = req.session.user;
var path = req.route?req.route.path[0]:req.originalUrl.replace(/\?.+/, '');
console.log("enter gray filter, user: " + user + ", path: " + path);
req._yoheaders = {
'x-user-id': user.auth.pid,
'x-user-name': user.auth.account,//防止header中包含中文
'x-site-type': Iaccount.WEBSITE,
'x-client-ip': req.ip,
'x-user-brands':typeof user.brands==="string"?user.brands: user.brands.join(',')
};
if (path && user.right[path]) {
common.isUsedMenuAuth(user.auth.pid,user.auth.role_id,path).done(function(isURes){
result = { code: 200, message: "具有权限" };
if(isURes.data){
result={code:isURes.data.code,message:isURes.data.errmsg};
}
console.log("return result: " + JSON.stringify(isURes));
console.log("code 200, result: " + JSON.stringify(result));
return callback(null, result);
},function(error){
console.log("error: " + error);
return callback(error, result);
});
} else {
result = { code: 200, message: "不受权限控制" };
console.log(result);
return callback(null, result);
}
} else {
console.log("no session..");
return callback(null, result);
}
}
}
}