Authored by htoooth

Merge remote-tracking branch 'origin/master'

1 <template> 1 <template>
2 <Row class="layout-header"> 2 <Row class="layout-header">
3 - <Col :span="12" class="brand-title">{{userInfo.currentShop.shopName}}</Col> 3 + <Col :span="12" class="brand-title">{{userInfo.name}}</Col>
4 <Col :span="12" class="shop-info"> 4 <Col :span="12" class="shop-info">
5 - <span class="name">{{userInfo.name}}</span>  
6 - <a href="javascript:;" @click="logout">[退出]</a> 5 + <span class="name">{{userInfo.currentShop.shopName}}</span>
  6 + <span>|</span>
  7 + <Dropdown @on-click="switchShop">
  8 + <a class="swtich-shop" href="javascript:void(0)">
  9 + [切换店铺]
  10 + </a>
  11 + <Dropdown-menu slot="list">
  12 + <Dropdown-item v-for="shop in userInfo.shops" :key="shop.id" :name="shop.id">{{shop.shopName}}</Dropdown-item>
  13 + </Dropdown-menu>
  14 + </Dropdown>
  15 + <a class="logout" href="javascript:;" @click="logout">[退出]</a>
7 </Col> 16 </Col>
8 </Row> 17 </Row>
9 </template> 18 </template>
@@ -23,11 +32,42 @@ export default { @@ -23,11 +32,42 @@ export default {
23 methods: { 32 methods: {
24 logout() { 33 logout() {
25 Vue.logout(); 34 Vue.logout();
  35 + },
  36 + switchShop(shop) {
  37 + console.log(shop)
26 } 38 }
27 } 39 }
28 }; 40 };
29 </script> 41 </script>
30 42
31 <style lang="scss"> 43 <style lang="scss">
  44 +.layout-header {
  45 + height: 60px !important;
  46 + background: #fff;
  47 + box-shadow: 0 1px 1px rgba(0, 0, 0, .1);
  48 + font-size: 14px;
  49 + line-height: 20px;
  50 + padding: 20px;
  51 +
  52 + .shop-info {
  53 + text-align: right;
  54 + padding-right: 20px;
  55 +
  56 + .name {
  57 + margin-right: 5px;
  58 + }
32 59
  60 + .swtich-shop {
  61 + margin-left: 5px;
  62 + font-size: 12px;
  63 + color: #444;
  64 + }
  65 +
  66 + .logout {
  67 + color: #F44545;
  68 + font-size: 12px;
  69 + margin-left: 5px;
  70 + }
  71 + }
  72 +}
33 </style> 73 </style>
@@ -82,25 +82,7 @@ export default { @@ -82,25 +82,7 @@ export default {
82 color: #9ea7b4; 82 color: #9ea7b4;
83 } 83 }
84 84
85 -.layout-header {  
86 - height: 60px !important;  
87 - background: #fff;  
88 - box-shadow: 0 1px 1px rgba(0, 0, 0, .1);  
89 - font-size: 14px;  
90 - line-height: 20px;  
91 - padding: 20px;  
92 -  
93 - .shop-info {  
94 - text-align: right;  
95 - padding-right: 20px;  
96 -  
97 - a {  
98 - color: #F44545;  
99 - font-size: 12px;  
100 - margin-left: 10px;  
101 - }  
102 - }  
103 -} 85 +
104 86
105 .layout-logo-left { 87 .layout-logo-left {
106 width: 90%; 88 width: 90%;
@@ -8,7 +8,8 @@ @@ -8,7 +8,8 @@
8 const bodyParser = require('body-parser'); 8 const bodyParser = require('body-parser');
9 const cookieParser = require('cookie-parser'); 9 const cookieParser = require('cookie-parser');
10 const Express = require('express'); 10 const Express = require('express');
11 -const session = require('express-session'); 11 +// const session = require('express-session');
  12 +const cookieSession = require('cookie-session');
12 const favicon = require('serve-favicon'); 13 const favicon = require('serve-favicon');
13 const path = require('path'); 14 const path = require('path');
14 15
@@ -28,9 +29,12 @@ global.yoho = { @@ -28,9 +29,12 @@ global.yoho = {
28 co: global.Promise.coroutine 29 co: global.Promise.coroutine
29 }; 30 };
30 31
31 -app.use(session({ 32 +app.use(cookieSession({
32 name: 'yoho-shop', 33 name: 'yoho-shop',
33 - secret: 'yoho!shop@manage' 34 + secret: 'yoho!shop@manage',
  35 + cookie: {
  36 + maxAge: 24 * 60 * 60 * 1000
  37 + }
34 })); 38 }));
35 39
36 app.use(favicon(path.join(__dirname, '/favicon.ico'))); 40 app.use(favicon(path.join(__dirname, '/favicon.ico')));
@@ -14,6 +14,7 @@ let router = Express.Router(); // eslint-disable-line @@ -14,6 +14,7 @@ let router = Express.Router(); // eslint-disable-line
14 14
15 router.post('/login', middleware(UserController, 'login')); 15 router.post('/login', middleware(UserController, 'login'));
16 router.post('/logout', middleware(UserController, 'logout')); 16 router.post('/logout', middleware(UserController, 'logout'));
  17 +router.post('/switchShop', middleware(UserController, 'switchShop'));
17 18
18 19
19 module.exports = router; 20 module.exports = router;
@@ -29,6 +29,12 @@ class UserController extends Context { @@ -29,6 +29,12 @@ class UserController extends Context {
29 name: user.account, 29 name: user.account,
30 email: user.email, 30 email: user.email,
31 createDate: user.create_date, 31 createDate: user.create_date,
  32 + shops: _.map(result.data, shop => {
  33 + return {
  34 + id: shop.id,
  35 + shopName: shop.shopName
  36 + };
  37 + }),
32 currentShop: { 38 currentShop: {
33 shopName: currentShop.shopName, 39 shopName: currentShop.shopName,
34 id: currentShop.id 40 id: currentShop.id
@@ -45,12 +51,39 @@ class UserController extends Context { @@ -45,12 +51,39 @@ class UserController extends Context {
45 } 51 }
46 52
47 logout(req, res) { 53 logout(req, res) {
  54 + delete req.session.USER;
  55 + delete req.session.LOGIN_UID;
  56 + delete req.session.CURRENT_SHOP;
48 return res.json({ 57 return res.json({
49 code: 200, 58 code: 200,
50 data: '登出成功' 59 data: '登出成功'
51 }); 60 });
52 } 61 }
53 62
  63 + switchShop(req, res) {
  64 + let id = req.body.id;
  65 +
  66 + if (!id) {
  67 + return res.json({
  68 + code: 400,
  69 + message: '参数错误'
  70 + });
  71 + }
  72 + let shop = _.get(req.user, 'shops', []).find(s => s.id === id);
  73 +
  74 + if (shop.id === id) {
  75 + return res.json({
  76 + code: 200,
  77 + message: '已设置店铺'
  78 + });
  79 + }
  80 + req.session.CURRENT_SHOP = shop;
  81 + return res.json({
  82 + code: 200,
  83 + message: '切换成功'
  84 + });
  85 + }
  86 +
54 syncSession(req, user) { 87 syncSession(req, user) {
55 req.session.USER = user; 88 req.session.USER = user;
56 req.session.LOGIN_UID = user.pid; // pid 为用户名 89 req.session.LOGIN_UID = user.pid; // pid 为用户名