Skip to content

Commit

Permalink
feat 配置可公开访问的controller和action
Browse files Browse the repository at this point in the history
  • Loading branch information
tumobi committed Jul 25, 2017
1 parent 6e49c65 commit 4696be9
Show file tree
Hide file tree
Showing 4 changed files with 301 additions and 559 deletions.
804 changes: 262 additions & 542 deletions nideshop.sql

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions src/api/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,30 @@
*/
export default {
//key: value

//可以公开访问的Controller
publicController: [
//格式为controller
'index',
'catalog',
'topic',
'auth',
'goods',
'brand',
'search',
'region',
],

//可以公开访问的Action
publicAction: [
//格式为: controller+action
'comment/list',
'comment/count',
'cart/index',
'cart/add',
'cart/checked',
'cart/update',
'cart/delete',
'cart/goodscount',
]
};
24 changes: 13 additions & 11 deletions src/api/controller/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@ export default class extends think.controller.base {
*/
async __before() {

//根据token值获取用户id
think.token = this.header('X-Nideshop-Token') || '';
if (!think.isEmpty(think.token)) {
let TokenSerivce = this.service('token');
let tokenObj = new TokenSerivce();
let sessionInfo = await tokenObj.parse();
let TokenSerivce = this.service('token');
let tokenObj = new TokenSerivce();
think.userId = await tokenObj.getUserId();

console.log(sessionInfo)
if (!think.isEmpty(sessionInfo) && sessionInfo.user_id > 0) {
think.userId = sessionInfo.user_id;
} else {
think.userId = 0;
const publicController = this.http.config('publicController');
const publicAction = this.http.config('publicAction');

//如果为非公开,则验证用户是否登录
console.log(this.http.controller + '/' + this.http.action)
if (!publicController.includes(this.http.controller) && !publicAction.includes(this.http.controller + '/' + this.http.action)) {
if (think.userId <= 0) {
return this.fail(401, '请先登录');
}
} else {
think.userId = 0;
}

}
}
6 changes: 0 additions & 6 deletions src/api/controller/collect.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ import Base from './base.js';

export default class extends Base {

async __before() {
if (think.userId <= 0) {
return this.fail(401, '请先登录');
}
}

/**
* index action
* @return {Promise} []
Expand Down

0 comments on commit 4696be9

Please sign in to comment.