Toggle navigation
Toggle navigation
This project
Loading...
Sign in
fe
/
yohobuywap-node
·
Commits
Go to a project
GitLab
Go to group
Project
Activity
Files
Commits
Pipelines
0
Builds
0
Graphs
Milestones
Issues
0
Merge Requests
0
Members
Labels
Wiki
Forks
Network
Create a new issue
Download as
Email Patches
Plain Diff
Browse Files
Authored by
陈峰
8 years ago
Commit
060199101afbb472ce171da9e815df899dc37ae8
1 parent
1b2537c2
用户接口携带sessionid参数
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
50 additions
and
6 deletions
doraemon/middleware/error-handler.js
doraemon/middleware/user.js
public/js/global.js
public/webpack.config.js
doraemon/middleware/error-handler.js
View file @
0601991
...
...
@@ -4,6 +4,7 @@
*/
const
headerModel
=
require
(
'../models/header'
);
const
logger
=
global
.
yoho
.
logger
;
const
helpers
=
global
.
yoho
.
helpers
;
const
forceNoCache
=
(
res
)
=>
{
if
(
res
&&
!
res
.
finished
)
{
...
...
@@ -47,10 +48,18 @@ exports.notFound = () => {
*/
exports
.
serverError
=
()
=>
{
return
(
err
,
req
,
res
,
next
)
=>
{
console
.
log
(
err
);
forceNoCache
(
res
);
if
(
err
&&
err
.
code
===
401
)
{
if
(
req
.
xhr
)
{
return
res
.
json
(
err
);
}
else
{
return
res
.
redirect
(
helpers
.
urlFormat
(
'/signin.html'
,
{
refer
:
req
.
originalUrl
}));
}
}
logger
.
error
(
`
error
at
path
:
$
{
req
.
url
}
`
);
logger
.
error
(
err
);
...
...
doraemon/middleware/user.js
View file @
0601991
...
...
@@ -7,8 +7,13 @@ module.exports = () => {
return
(
req
,
res
,
next
)
=>
{
// 从 SESSION 中获取到当前登录用户的 UID
if
(
req
.
session
&&
_
.
isNumber
(
req
.
session
.
LOGIN_UID
))
{
req
.
user
.
uid
=
req
.
session
.
LOGIN_UID
;
// 不要使用 === 判断uid的值,如果需要判断使用 ==
req
.
user
.
uid
=
{
toString
:
()
=>
{
return
req
.
session
.
LOGIN_UID
;
},
sessionId
:
req
.
cookies
.
_session
};
let
userData
=
_
.
get
(
req
.
session
,
'USER'
,
{});
_
.
merge
(
req
.
user
,
userData
);
...
...
@@ -16,7 +21,13 @@ module.exports = () => {
// session 没有读取到的时候,从 cookie 读取 UID
if
(
!
req
.
user
.
uid
&&
req
.
cookies
.
_UID
)
{
req
.
user
.
uid
=
cookie
.
getUid
(
req
);
// 不要使用 === 判断uid的值,如果需要判断使用 ==
req
.
user
.
uid
=
{
toString
:
()
=>
{
return
cookie
.
getUid
(
req
);
},
sessionId
:
req
.
cookies
.
_session
};
}
next
();
...
...
public/js/global.js
0 → 100644
View file @
0601991
/**
* 全局引用js
* @author: feng.chen<feng.chen@yoho.cn>
* @date: 2017/03/15
*/
const
$
=
require
(
'yoho-jquery'
);
const
cookie
=
require
(
'yoho-cookie'
);
var
yoho
=
require
(
'./yoho-app'
);
// 初始化
// 注册ajaxError处理服务端401状态
$
(
document
).
ajaxError
((
event
,
xhr
)
=>
{
if
(
xhr
.
status
===
401
)
{
cookie
.
remove
(
'_UID'
);
cookie
.
remove
(
'_TOKEN'
);
if
(
yoho
.
isApp
)
{
yoho
.
goLogin
(
window
.
location
.
href
);
}
else
{
window
.
location
.
href
=
`
/
signin
.
html
?
refer
=
$
{
encodeURIComponent
(
window
.
location
.
href
)}
`
;
}
}
});
...
...
public/webpack.config.js
View file @
0601991
...
...
@@ -22,7 +22,8 @@ shelljs.ls(path.join(__dirname, 'js/**/*.page.js')).forEach((f) => {
// 生成规则:module.page: './js/module/xx.page.js'
entries
[
`
$
{
dir
[
0
]}.
$
{
dir
[
1
].
match
(
/
(
.*
)
.page.js/
)[
1
]}
`
]
=
`
.
/
js
/
$
{
dir
.
join
(
'/'
)}
`
;
entries
.
libs
=
[
'yoho-jquery'
'yoho-jquery'
,
'./js/global.js'
// 全局引用js
];
});
...
...
Please
register
or
login
to post a comment