applet_customer.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. "time"
  10. )
  11. var (
  12. appletRouterCheck = make([]func(v1 *gin.RouterGroup, authMiddleware gin.HandlerFunc), 0)
  13. )
  14. func init() {
  15. appletRouterCheck = append(appletRouterCheck, AppletCustomerRouter)
  16. }
  17. func AppletCunJWTMiddleware() gin.HandlerFunc {
  18. return func(c *gin.Context) {
  19. tokenString := c.GetHeader("Authorization")
  20. if tokenString == "" {
  21. c.JSON(http.StatusUnauthorized, gin.H{
  22. "code": 401,
  23. "msg": "Unauthorized",
  24. })
  25. c.Abort()
  26. return
  27. }
  28. parts := strings.SplitN(tokenString, " ", 2)
  29. if !(len(parts) == 2 && parts[0] == "Bearer") {
  30. c.JSON(http.StatusOK, gin.H{
  31. "code": 401,
  32. "msg": "请求头中auth格式有误",
  33. })
  34. c.Abort()
  35. return
  36. }
  37. // 解析token
  38. token, err := jwt.ParseWithClaims(parts[1], &model.CustomerClaims{}, func(token *jwt.Token) (i interface{}, err error) {
  39. return model.AppletCustomerSecret, nil
  40. })
  41. if err != nil || !token.Valid {
  42. if err.(*jwt.ValidationError).Errors != jwt.ValidationErrorExpired {
  43. c.JSON(http.StatusUnauthorized, gin.H{
  44. "code": 401,
  45. "msg": "Unauthorized",
  46. })
  47. c.Abort()
  48. return
  49. }
  50. }
  51. claims, ok := token.Claims.(*model.CustomerClaims)
  52. if !ok {
  53. c.JSON(http.StatusUnauthorized, gin.H{
  54. "code": 401,
  55. "msg": "Unauthorized",
  56. })
  57. c.Abort()
  58. return
  59. }
  60. if claims.ExpiresAt < time.Now().Unix() {
  61. if claims.ExpiresAt > time.Now().Add(-2*time.Hour).Unix() {
  62. c.JSON(http.StatusUnauthorized, gin.H{
  63. "code": 6401,
  64. "msg": "Token is expired",
  65. })
  66. c.Abort()
  67. return
  68. }
  69. c.JSON(http.StatusUnauthorized, gin.H{
  70. "code": 6401,
  71. "msg": "Token is expired",
  72. })
  73. c.Abort()
  74. return
  75. }
  76. c.Set("customer_id", claims.CustomerId)
  77. c.Next()
  78. }
  79. }
  80. func AppletCustomerRouterInit(v1 *gin.RouterGroup) {
  81. cont := controller.AppletCustomerController{}
  82. r := v1.Group("/api/applet")
  83. {
  84. r.POST("/login", cont.Login)
  85. r.POST("/register", cont.Register)
  86. r.GET("/refresh", cont.Refresh)
  87. }
  88. for _, f := range appletRouterCheck {
  89. f(r, AppletCunJWTMiddleware())
  90. }
  91. }
  92. func AppletCustomerRouter(v1 *gin.RouterGroup, authMiddleware gin.HandlerFunc) {
  93. cont := controller.AppletCustomerController{}
  94. r := v1.Group("").Use(authMiddleware)
  95. {
  96. r.GET("/profile", cont.GetProfile)
  97. r.PUT("/customer-info", cont.UpdateCustomerInfo)
  98. r.PUT("/pwd", cont.UpdatePwd)
  99. r.GET("/order", cont.GetOrderPage)
  100. r.POST("/order", cont.OrderInsert)
  101. r.PUT("/order", cont.OrderUpdate)
  102. r.POST("/order/cancel", cont.OrderCancel)
  103. r.GET("/store", cont.StoreList)
  104. r.GET("/goods", cont.GetGoodsPage)
  105. r.GET("/gas-cylinder-spec", cont.GetGasCylinderSpecPage)
  106. }
  107. }