12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- package router
- import (
- "gas-cylinder-api/app/admin/controller"
- "gas-cylinder-api/app/admin/model"
- "github.com/dgrijalva/jwt-go"
- "github.com/gin-gonic/gin"
- "net/http"
- "strings"
- )
- func init() {
- routerNoCheckRole = append(routerNoCheckRole, registerAppletCustomerRouter)
- }
- func AppletCunJWTMiddleware() gin.HandlerFunc {
- return func(c *gin.Context) {
- tokenString := c.GetHeader("Authorization")
- if tokenString == "" {
- c.JSON(http.StatusUnauthorized, gin.H{
- "code": 200,
- "msg": "Unauthorized",
- })
- c.Abort()
- return
- }
- parts := strings.SplitN(tokenString, " ", 2)
- if !(len(parts) == 2 && parts[0] == "Bearer") {
- c.JSON(http.StatusOK, gin.H{
- "code": 2004,
- "msg": "请求头中auth格式有误",
- })
- c.Abort()
- return
- }
- // 解析token
- token, err := jwt.ParseWithClaims(parts[1], &model.CustomerClaims{}, func(token *jwt.Token) (i interface{}, err error) {
- return model.AppletCustomerSecret, nil
- })
- if err != nil || !token.Valid {
- c.JSON(http.StatusUnauthorized, gin.H{
- "code": 401,
- "msg": "Unauthorized",
- })
- c.Abort()
- return
- }
- claims, ok := token.Claims.(*model.CustomerClaims)
- if !ok {
- c.JSON(http.StatusUnauthorized, gin.H{
- "code": 401,
- "msg": "Unauthorized",
- })
- c.Abort()
- return
- }
- c.Set("customer_id", claims.CustomerId)
- c.Next()
- }
- }
- func registerAppletCustomerRouter(v1 *gin.RouterGroup) {
- //cont := controller.AppletCustomerController{}
- //r := v1.Group("/applet-customer").Use(AppletCunJWTMiddleware())
- //{
- // r.POST("login", cont.Login)
- // //r.GET("", cont.GetPage)
- // //r.GET("/:id", cont.Get)
- // //r.GET("/phone", cont.GetByPhone)
- // //r.POST("/insert-or-update", cont.InsertOrUpdate)
- // //r.PUT("", cont.Update)
- // //r.DELETE("", cont.Delete)
- //}
- }
- func AppletCustomerRouterInit(v1 *gin.RouterGroup) {
- cont := controller.AppletCustomerController{}
- r := v1.Group("/applet")
- {
- r.POST("/login", cont.Login)
- }
- }
|