Просмотр исходного кода

feat 配置可公开访问的controller和action

tumobi 7 лет назад
Родитель
Сommit
4696be945d
4 измененных файлов с 75 добавлено и 94 удалено
  1. 36 77
      nideshop.sql
  2. 26 0
      src/api/config/config.js
  3. 13 11
      src/api/controller/base.js
  4. 0 6
      src/api/controller/collect.js

Разница между файлами не показана из-за своего большого размера
+ 36 - 77
nideshop.sql


+ 26 - 0
src/api/config/config.js

@@ -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',
+  ]
 };

+ 13 - 11
src/api/controller/base.js

@@ -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;
     }
+
   }
 }

+ 0 - 6
src/api/controller/collect.js

@@ -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} []

Некоторые файлы не были показаны из-за большого количества измененных файлов