250 lines
5.7 KiB
Go
250 lines
5.7 KiB
Go
package router
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"hepa-calc-api/api/controller"
|
|
"hepa-calc-api/api/exception"
|
|
"hepa-calc-api/api/middlewares"
|
|
"hepa-calc-api/config"
|
|
"hepa-calc-api/consts"
|
|
"net/http"
|
|
)
|
|
|
|
// Init 初始化路由
|
|
func Init() *gin.Engine {
|
|
r := gin.New()
|
|
|
|
// 环境设置
|
|
if config.C.Env == "prod" {
|
|
gin.SetMode(gin.ReleaseMode)
|
|
}
|
|
|
|
// 获取请求参数中间件-json格式下会导致接口获取不到请求数据
|
|
r.Use(middlewares.RequestParamsMiddleware())
|
|
|
|
// 日志中间件
|
|
r.Use(middlewares.Logrus())
|
|
|
|
// 异常
|
|
r.Use(gin.Recovery())
|
|
|
|
// 404处理
|
|
r.NoRoute(func(c *gin.Context) {
|
|
path := c.Request.URL.Path
|
|
method := c.Request.Method
|
|
c.JSON(http.StatusNotFound, gin.H{
|
|
"msg": fmt.Sprintf("%s %s not found", method, path),
|
|
"code": consts.ClientHttpNotFound,
|
|
"data": "",
|
|
})
|
|
})
|
|
|
|
// 异常处理
|
|
r.Use(exception.Recover())
|
|
|
|
// 跨域处理
|
|
r.Use(middlewares.Cors())
|
|
|
|
// 加载基础路由
|
|
api := controller.Api{}
|
|
|
|
// 公开路由-不验证权限
|
|
publicRouter(r, api)
|
|
|
|
// 验证jwt
|
|
r.Use(middlewares.Jwt())
|
|
|
|
// 验证权限
|
|
r.Use(middlewares.Auth())
|
|
|
|
// 私有路由-验证权限
|
|
privateRouter(r, api)
|
|
|
|
// 公共路由-验证权限
|
|
adminRouter(r, api)
|
|
|
|
// 基础数据-验证权限
|
|
basicRouter(r, api)
|
|
|
|
return r
|
|
}
|
|
|
|
// publicRouter 公开路由-不验证权限
|
|
func publicRouter(r *gin.Engine, api controller.Api) {
|
|
// 登陆
|
|
loginGroup := r.Group("/login")
|
|
{
|
|
// 手机号登录
|
|
loginGroup.POST("/phone", api.Login.LoginPhone)
|
|
|
|
// 微信授权登录
|
|
loginGroup.POST("/wx", api.Login.LoginWx)
|
|
}
|
|
|
|
// 验证码
|
|
codeGroup := r.Group("/code")
|
|
{
|
|
// 获取手机号验证码
|
|
codeGroup.POST("/phone", api.Public.GetPhoneCode)
|
|
}
|
|
|
|
// 首页
|
|
r.GET("/index", api.Public.GetIndex)
|
|
|
|
// 回调
|
|
callbackGroup := r.Group("/callback")
|
|
{
|
|
// 微信支付回调
|
|
wxpayGroup := callbackGroup.Group("/wxpay")
|
|
{
|
|
// 单项
|
|
wxpayGroup.POST("/single", api.CallBack.WxPaySingle)
|
|
|
|
// 会员
|
|
wxpayGroup.POST("/member", api.CallBack.WxPayMember)
|
|
}
|
|
}
|
|
}
|
|
|
|
// adminRouter 公共路由-验证权限
|
|
func adminRouter(r *gin.Engine, api controller.Api) {
|
|
|
|
}
|
|
|
|
// basicRouter 基础数据-验证权限
|
|
func basicRouter(r *gin.Engine, api controller.Api) {
|
|
|
|
}
|
|
|
|
// privateRouter 私有路由-验证权限
|
|
func privateRouter(r *gin.Engine, api controller.Api) {
|
|
// 分类
|
|
classGroup := r.Group("/class")
|
|
{
|
|
// 获取分类列表
|
|
classGroup.GET("/list", api.Class.GetClassList)
|
|
|
|
// 获取分类详情
|
|
classGroup.GET("/:class_id", api.Class.GetClass)
|
|
}
|
|
|
|
// 问题
|
|
questionGroup := r.Group("/question")
|
|
{
|
|
// 获取问题列表-分页
|
|
questionGroup.POST("/page", api.Question.GetQuestionPage)
|
|
|
|
// 获取问题列表-热榜
|
|
questionGroup.GET("/hot", api.Question.GetQuestionHot)
|
|
|
|
// 获取问题详情
|
|
questionGroup.GET("/:question_id", api.Question.GetQuestion)
|
|
|
|
// 获取问题解锁状态
|
|
questionGroup.GET("/unlock/status/:question_id", api.Question.GetQuestionUnlockStatus)
|
|
|
|
// 点击次数
|
|
clickGroup := questionGroup.Group("/click")
|
|
{
|
|
// 增加问题点击次数(点击进入详情页的人次)
|
|
clickGroup.PUT("/:question_id", api.Question.PutQuestionClickCount)
|
|
|
|
// 增加问题提交次数(提交个人信息进行了算算的人次)
|
|
clickGroup.PUT("/submit/:question_id", api.Question.PutQuestionSubmitCount)
|
|
}
|
|
}
|
|
|
|
// 用户
|
|
centerGroup := r.Group("/user")
|
|
{
|
|
// 获取用户数据-基本信息
|
|
centerGroup.GET("", api.User.GetUser)
|
|
}
|
|
|
|
// 优惠卷
|
|
couponGroup := r.Group("/coupon")
|
|
{
|
|
// 获取优惠卷列表-分页
|
|
couponGroup.GET("", api.UserCoupon.GetUserCouponPage)
|
|
|
|
// 获取还未弹窗的优惠卷
|
|
couponGroup.GET("/unnotified", api.UserCoupon.GetUserCouponUnnotified)
|
|
|
|
usableGroup := couponGroup.Group("/usable")
|
|
{
|
|
// 获取患者可使用优惠卷-单项
|
|
usableGroup.GET("/question", api.UserCoupon.GetUserUsableQuestionCoupon)
|
|
|
|
// 获取患者可使用优惠卷-会员
|
|
usableGroup.GET("/member", api.UserCoupon.GetUserUsableMemberCoupon)
|
|
}
|
|
}
|
|
|
|
// 收藏
|
|
collectionGroup := r.Group("/collection")
|
|
{
|
|
// 问题
|
|
questionGroup := collectionGroup.Group("/question")
|
|
{
|
|
// 获取用户收藏题目列表-分页
|
|
questionGroup.GET("", api.UserCollection.GetUserCollectionPage)
|
|
|
|
// 收藏题目
|
|
questionGroup.POST("", api.UserCollection.PutUserCollection)
|
|
|
|
// 取消收藏题目
|
|
questionGroup.PUT("/cancel", api.UserCollection.PutUserCollectionCancel)
|
|
}
|
|
}
|
|
|
|
// 订单
|
|
orderGroup := r.Group("/order")
|
|
{
|
|
// 单项订单
|
|
singleGroup := orderGroup.Group("/single")
|
|
{
|
|
// 获取单项订单列表-分页
|
|
singleGroup.GET("/page", api.OrderSingle.GetOrderSinglePage)
|
|
|
|
// 创建单项订单
|
|
singleGroup.POST("", api.OrderSingle.AddOrderSingle)
|
|
|
|
// 取消单项订单
|
|
singleGroup.PUT("/cancel/:order_id", api.OrderSingle.PutCancelOrderSingle)
|
|
|
|
// 获取单项订单支付数据
|
|
singleGroup.GET("/pay/:order_id", api.OrderSingle.GetOrderSinglePay)
|
|
|
|
// 删除单项订单
|
|
singleGroup.DELETE("/:order_id", api.OrderSingle.DeleteOrderSingle)
|
|
|
|
// 查询订单支付状态-单项
|
|
singleGroup.GET("/pay/status/:order_id", api.OrderSingle.GetOrderSinglePayStatus)
|
|
}
|
|
|
|
// 会员订单
|
|
memberGroup := orderGroup.Group("/member")
|
|
{
|
|
// 获取会员订单列表-分页
|
|
memberGroup.GET("/page", api.OrderMember.GetOrderMemberPage)
|
|
|
|
// 创建会员订单
|
|
memberGroup.POST("", api.OrderMember.AddOrderMember)
|
|
|
|
// 获取会员订单支付数据
|
|
memberGroup.GET("/pay/:order_id", api.OrderMember.GetOrderMemberPay)
|
|
|
|
// 查询订单支付状态-会员
|
|
memberGroup.GET("/pay/status/:order_id", api.OrderMember.GetOrderMemberPayStatus)
|
|
}
|
|
}
|
|
|
|
// 会员配置
|
|
memberGroup := r.Group("/member")
|
|
{
|
|
// 获取会员配置数据
|
|
memberGroup.GET("", api.SystemMember.GetSystemMember)
|
|
}
|
|
}
|