applet_customer.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package router
  2. import (
  3. "gas-cylinder-api/app/admin/controller"
  4. "gas-cylinder-api/app/admin/model"
  5. "github.com/dgrijalva/jwt-go"
  6. "github.com/gin-gonic/gin"
  7. "net/http"
  8. "strings"
  9. )
  10. func init() {
  11. routerNoCheckRole = append(routerNoCheckRole, registerAppletCustomerRouter)
  12. }
  13. func AppletCunJWTMiddleware() gin.HandlerFunc {
  14. return func(c *gin.Context) {
  15. tokenString := c.GetHeader("Authorization")
  16. if tokenString == "" {
  17. c.JSON(http.StatusUnauthorized, gin.H{
  18. "code": 200,
  19. "msg": "Unauthorized",
  20. })
  21. c.Abort()
  22. return
  23. }
  24. parts := strings.SplitN(tokenString, " ", 2)
  25. if !(len(parts) == 2 && parts[0] == "Bearer") {
  26. c.JSON(http.StatusOK, gin.H{
  27. "code": 2004,
  28. "msg": "请求头中auth格式有误",
  29. })
  30. c.Abort()
  31. return
  32. }
  33. // 解析token
  34. token, err := jwt.ParseWithClaims(parts[1], &model.CustomerClaims{}, func(token *jwt.Token) (i interface{}, err error) {
  35. return model.AppletCustomerSecret, nil
  36. })
  37. if err != nil || !token.Valid {
  38. c.JSON(http.StatusUnauthorized, gin.H{
  39. "code": 401,
  40. "msg": "Unauthorized",
  41. })
  42. c.Abort()
  43. return
  44. }
  45. claims, ok := token.Claims.(*model.CustomerClaims)
  46. if !ok {
  47. c.JSON(http.StatusUnauthorized, gin.H{
  48. "code": 401,
  49. "msg": "Unauthorized",
  50. })
  51. c.Abort()
  52. return
  53. }
  54. c.Set("customer_id", claims.CustomerId)
  55. c.Next()
  56. }
  57. }
  58. func registerAppletCustomerRouter(v1 *gin.RouterGroup) {
  59. //cont := controller.AppletCustomerController{}
  60. //r := v1.Group("/applet-customer").Use(AppletCunJWTMiddleware())
  61. //{
  62. // r.POST("login", cont.Login)
  63. // //r.GET("", cont.GetPage)
  64. // //r.GET("/:id", cont.Get)
  65. // //r.GET("/phone", cont.GetByPhone)
  66. // //r.POST("/insert-or-update", cont.InsertOrUpdate)
  67. // //r.PUT("", cont.Update)
  68. // //r.DELETE("", cont.Delete)
  69. //}
  70. }
  71. func AppletCustomerRouterInit(v1 *gin.RouterGroup) {
  72. cont := controller.AppletCustomerController{}
  73. r := v1.Group("/applet")
  74. {
  75. r.POST("/login", cont.Login)
  76. }
  77. }