Giter Site home page Giter Site logo

权拦拦截 about gf HOT 2 CLOSED

gogf avatar gogf commented on May 3, 2024
权拦拦截

from gf.

Comments (2)

gqcn avatar gqcn commented on May 3, 2024

@zhaopengme

fmt.Println("Init")
Json(r, 0, "")
r.ExitAll()

这里的Json已经退出了当前逻辑执行,所以后面的r.ExitAll()没有起作用。

按照你的需求,可以这样来实现:

package main

import (
    "fmt"
    "github.com/gogf/gf/g"
    "github.com/gogf/gf/g/net/ghttp"
    "github.com/gogf/gf/g/os/glog"
)

type UserController struct {
}

func (obj *UserController) Init(r *ghttp.Request) {
    fmt.Println("Init")
    Json(r, 0, "")
    r.ExitAll()
}

func (c *UserController) Find(r *ghttp.Request) {
    fmt.Println("Find")
    Json(r, 0, "")
    // 或者
    // JsonExit(r, 0, "")
}

func Json(r *ghttp.Request, err int, msg string, data ...interface{}) {
    responseData := interface{}(nil)
    if len(data) > 0 {
        responseData = data[0]
    }
    r.Response.WriteJson(g.Map{
        "err":  err,
        "msg":  msg,
        "data": responseData,
    })
}

func JsonExit(r *ghttp.Request, err int, msg string, data ...interface{}) {
    Json(r, err, msg, data...)
    r.Exit()
}

func main() {
    s := g.Server()
    s.BindHookHandlerByMap("/user/*", map[string]ghttp.HandlerFunc{
        ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) {
            glog.Println(ghttp.HOOK_BEFORE_SERVE)
            Json(r, 0, "")
            r.ExitHook()
        },
        ghttp.HOOK_AFTER_SERVE:   func(r *ghttp.Request) { glog.Println(ghttp.HOOK_AFTER_SERVE) },
        ghttp.HOOK_BEFORE_OUTPUT: func(r *ghttp.Request) { glog.Println(ghttp.HOOK_BEFORE_OUTPUT) },
        ghttp.HOOK_AFTER_OUTPUT:  func(r *ghttp.Request) { glog.Println(ghttp.HOOK_AFTER_OUTPUT) },
        ghttp.HOOK_BEFORE_CLOSE:  func(r *ghttp.Request) { glog.Println(ghttp.HOOK_BEFORE_CLOSE) },
        ghttp.HOOK_AFTER_CLOSE:   func(r *ghttp.Request) { glog.Println(ghttp.HOOK_AFTER_CLOSE) },
    })
    s.BindObject("/user", new(UserController))
    s.SetPort(8888)
    s.Run()
}

from gf.

zhaopengme avatar zhaopengme commented on May 3, 2024

还是r.ExitAll()这句起的作用.去掉就不行了.

from gf.

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.