Authored by 陈峰

切换店铺调用shops接口

... ... @@ -5,7 +5,8 @@
{{userInfo.name}}
</Col>
<Col :span="12" class="shop-info">
<span class="name">{{userInfo.currentShop.shopName}}</span>
<span v-if="!showLoading" class="name">{{userInfo.currentShop.shopName}}</span>
<span v-if="showLoading" class="loading">切换中...</span>
<span>|</span>
<Dropdown @on-click="switchShop" trigger="click">
<a class="swtich-shop" href="javascript:void(0)">
... ... @@ -22,12 +23,14 @@
<script>
import Vue from 'vue';
import userService from 'user-service';
export default {
name: 'UserInfo',
data() {
return {
userInfo: this.$user
userInfo: this.$user,
showLoading: false
};
},
methods: {
... ... @@ -36,9 +39,18 @@ export default {
},
switchShop(id) {
if (this.userInfo.currentShop.shopsId !== id) {
this.userInfo.currentShop = this.userInfo.shops.find(shop => shop.shopsId === id);
Vue.switchShop(id);
this.$emit('shop-change', this.userInfo.currentShop);
this.showLoading = true;
userService.switchShop(id).then(res => {
this.showLoading = false;
if (res.code === 200) {
this.userInfo.currentShop = this.userInfo.shops.find(shop => shop.shopsId === id);
Vue.switchShop(id);
this.$emit('shop-change', this.userInfo.currentShop);
this.$Message.success(`当前店铺切换为:${this.userInfo.currentShop.shopName}`);
} else {
this.$Message.error(`切换失败:${res.message}`);
}
});
}
}
}
... ... @@ -85,6 +97,10 @@ export default {
margin-right: 5px;
}
.loading {
color: #ccc;
}
.swtich-shop {
margin-left: 5px;
font-size: 12px;
... ...
... ... @@ -3,8 +3,5 @@ const page = r => require.ensure([], () => r(require('./overview')), 'home.overv
export default {
path: '/overview.html',
name: 'overview',
component: page,
meta: {
authPass: true
}
component: page
};
... ...
... ... @@ -31,6 +31,13 @@ const userService = {
}
});
return purs;
},
switchShop(shopId) {
return axios.post('/switchShop', {
shopId
}).then(res => {
return res.data;
});
}
};
... ...
... ... @@ -55,7 +55,8 @@ let domainApis = {
getRemoteImageUrlBySku: '/product/getRemoteImageUrlBySku'
},
shop: {
login: '/loginInter'
login: '/loginInter',
switchShop: '/index/ajaxshop'
}
};
... ...
... ... @@ -83,7 +83,8 @@ class Api extends Context {
logger.error(`api call ${err.statusCode} [${err.options.method}]
${err.options.url} ${err.response.body || ''}`);
} else {
logger.error(`api call ${err}`);
logger.error(`api call [${options.method || 'get'}] ${options.url}
${JSON.stringify(options.qs || options.body)} ${err}`);
}
return Promise.reject(API_INTERNAL_ERROR);
});
... ...
... ... @@ -10,6 +10,8 @@ const Express = require('express');
const UserController = require('./user-controller');
const FileController = require('./file-controller');
const middleware = require('../framework/middleware');
const before = require('../middleware/before');
const auth = require('../middleware/auth');
const multipart = require('connect-multiparty');
const multipartMiddleware = multipart();
... ... @@ -17,7 +19,8 @@ let router = Express.Router(); // eslint-disable-line
router.post('/login', middleware(UserController, 'login'));
router.post('/logout', middleware(UserController, 'logout'));
router.post('/upload/image', multipartMiddleware, middleware(FileController, 'uploadImage'));
router.post('/upload/xlsx', multipartMiddleware, middleware(FileController, 'uploadXlsx'));
router.post('/switchShop', before, auth, middleware(UserController, 'switchShop'));
router.post('/upload/image', before, auth, multipartMiddleware, middleware(FileController, 'uploadImage'));
router.post('/upload/xlsx', before, auth, multipartMiddleware, middleware(FileController, 'uploadXlsx'));
module.exports = router;
... ...
... ... @@ -26,9 +26,8 @@ class UserController extends Context {
let currentShop = _.first(result.data);
this.syncSession({req, res}, Object.assign(user, {
shops: result.data,
currentShop: currentShop
}), sess);
shops: result.data
}), sess, currentShop);
return res.json({
code: 200,
... ... @@ -68,26 +67,62 @@ class UserController extends Context {
data: '登出成功'
});
}
switchShop(req, res) {
let shopId = req.body.shopId;
syncSession(context, user, sess) {
if (!shopId) {
return res.json({
code: 400,
message: '参数错误'
});
}
let shop = _.find(req.session.USER.shops, s => s.shopsId === shopId);
if (!shop) {
return res.json({
code: 400,
message: '不存在的店铺'
});
}
this.userService.switchShop({
shopId,
cookies: {
PHPSESSID: encodeURIComponent(req.cookies.PHPSESSID),
'connect.sid': encodeURIComponent(req.cookies['connect.sid'])
}
}).then(response => {
this.syncShopSession({
req,
res
}, response);
return res.json({
code: 200
});
});
}
syncSession(context, user, sess, currentShop) {
context.req.session.USER = user;
context.req.session.LOGIN_UID = user.pid; // pid 为用户名
this.syncShopSession(context, sess);
context.res.cookie('_isLogin', true, {
path: '/'
});
context.res.cookie('_sign', currentShop.shopsId, {
path: '/'
});
}
syncShopSession(context, sess) {
_.each(sess, (v, k) => {
context.res.cookie(k, v, {
path: '/',
domain: '.yohobuy.com',
httpOnly: true,
overwrite: false,
encode: val => val
});
});
context.res.cookie('_isLogin', true, {
path: '/'
});
context.res.cookie('_sign', user.currentShop.shopsId, {
path: '/'
});
}
}
... ...
... ... @@ -39,24 +39,28 @@ class UserService extends Context {
password: password
}
}).then(response => {
let sessId = '', sid = '';
return this._getShopsSession(response.rawHeaders);
});
}
_getShopsSession(rawHeaders) {
let sessId = '', sid = '';
_.each(response.rawHeaders, header => {
let sessIdMatched = header.match(new RegExp(regSession.replace('${0}', 'PHPSESSID')));
let sidMatched = header.match(new RegExp(regSession.replace('${0}', 'connect\.sid')));
_.each(rawHeaders, header => {
let sessIdMatched = header.match(new RegExp(regSession.replace('${0}', 'PHPSESSID')));
let sidMatched = header.match(new RegExp(regSession.replace('${0}', 'connect\.sid')));
if (sessIdMatched) {
sessId = sessIdMatched[1];
}
if (sidMatched) {
sid = sidMatched[1];
}
});
return {
PHPSESSID: sessId,
'connect.sid': sid
};
if (sessIdMatched) {
sessId = sessIdMatched[1];
}
if (sidMatched) {
sid = sidMatched[1];
}
});
return {
PHPSESSID: sessId,
'connect.sid': sid
};
}
getShops(pid) {
... ... @@ -68,6 +72,22 @@ class UserService extends Context {
});
}
switchShop(opts) {
let options = {
url: `${config.apiDomain.shop.switchShop}?shops_id=${opts.shopId}`,
resolveWithFullResponse: true,
};
let jar = rp.jar();
_.each(opts.cookies, (val, key) => {
jar.setCookie(rp.cookie(`${key}=${val}`), options.url);
});
options.jar = jar;
return rp.get(options).then(response => {
return this._getShopsSession(response.rawHeaders);
});
}
profile(pid) {
return this.instance(Api).get(config.apiDomain.shop.profile.url, {userId: pid});
}
... ...