Code Monkey home page Code Monkey logo

Comments (9)

xiongwilee avatar xiongwilee commented on May 19, 2024
  1. 根据经验所有的controller一般会有都需要执行的方法,或者需要某个公用的方法挂载在上下文中,所以提供这个方法;也可以选择不用;
  2. defaultCtrl是一个generator吗?如果defaultCtrl返回的是一个generator的话,用await语法肯定不起作用的;

from gracejs.

stone120 avatar stone120 commented on May 19, 2024
  1. 请教一下 怎么实现 filter功能(可能多个: 比如用户登陆校验, 错误统一处理),实现一个新的middleware?
    当前 在 每个 controller方法中显示的调用 bindDefault 感觉不好用

from gracejs.

xiongwilee avatar xiongwilee commented on May 19, 2024

你是拿grace当MVC框架吗?
如果是要做权限校验功能,一般就是显示调用defaultCtrl 的;也可以自定义中间件,但不推荐,因为grace本身是一个多应用的前后端分离框架~

from gracejs.

stone120 avatar stone120 commented on May 19, 2024

grace作为前端框架 主要功能 就是路由+proxy; 典型的filter功能 就是在路由层控制的把?
请指导一下, 前后端分离框架中, filter一般在哪部分实现, 谢谢

from gracejs.

xiongwilee avatar xiongwilee commented on May 19, 2024

没明白你说的“典型的filter”的功能具体是什么功能呢?是指类似laravel的路由机制,某个路由才注入某个中间件?

from gracejs.

stone120 avatar stone120 commented on May 19, 2024

比如:spring中通过filter 限制未登录用户不能访问, 实现异常处理 等功能。
进入路由前的过滤功能 / 后台处理后 ,反馈给用户前的 过滤功能

from gracejs.

xiongwilee avatar xiongwilee commented on May 19, 2024

明白了。
目前的做法确实就是你上面提到的:在控制器中显式调用 this.bindDefault(),然后在defaultCtrl中调用用户是否已登录的接口(前后端分离的框架下),然后根据业务做处理;
说实话,我也在想有没有更优雅的方案,但是要考虑几个使用场景:

  1. 如何保证控制器的调用这个filter的灵活性,比如:提供可供控制器配置是否选择执行这个"filter";
  2. 如何保证控制调用这个filter的时机,比如:如果需求是每次控制器执行完就调用,还是在执行之前调用;
  3. 如果实现了以上两种场景的配置的话,这个功能跟“如何在逻辑上区分这个'filter'和可自由配置的中间件的区别?”

如果有好的建议的话,可以提出来

from gracejs.

evanzlj avatar evanzlj commented on May 19, 2024

目前我在使用过程中,做路由限制的方式如下 不知道是否有更合适的想法,现在的登陆是做到cookie中的(跳到第三方登陆界面,存放cookie)

// defaultCtrl.js
module.exports = async function(auth){
  // vip权限
   if(auth=='vip'){
      // 验证cookies中的vip账号
      let vipid = this.cookies.get('baiduId')
      // 发送验证接口到内网
     this.proxy(`接口?id=${vipid}`)
     // 验证返回数据是否满足需要  不是则重定向到登陆
     if( not )  this.redirect('/login')
   }
   // 别的权限同理 如admin权限等
}
// 具体路由如个人中心 center.js
exports.home = async function(){
      await this.bindDefault('user')
      // 余下则是业务代码...
}

from gracejs.

xiongwilee avatar xiongwilee commented on May 19, 2024

@evanzlj 没怎么看明白问题,是说,是要实现登录用户的权限系统吗?这样的情况,可以让后端提供一个获取用户信息的接口;然后在defaultCtrl里直接调用接口,根据用户的情况进行判断。

另外,关于proxy看你上面写的有两个问题:

  1. proxy没有必要先拿到baiduId的cookie,然后通过get参数发出去,你可以直接proxy这个接口,cookie信息会自动带过去;
  2. this.proxy方法记得通过await语法执行,await this.proxy(...)

from gracejs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.