Refactor WeChat integration to support multiple mini programs, removing hardcoded WeChat credentials and updating user model and authentication flow accordingly.
This commit is contained in:
@@ -21,11 +21,12 @@ func NewAuthHandler(authService *service.AuthService) *AuthHandler {
|
||||
}
|
||||
|
||||
type weChatLoginRequest struct {
|
||||
Code string `json:"code" binding:"required"`
|
||||
NickName string `json:"nickname"`
|
||||
AvatarURL string `json:"avatar_url"`
|
||||
Gender *int `json:"gender"`
|
||||
Phone string `json:"phone"`
|
||||
MiniProgramID uint `json:"mini_program_id" binding:"required"`
|
||||
Code string `json:"code" binding:"required"`
|
||||
NickName string `json:"nickname"`
|
||||
AvatarURL string `json:"avatar_url"`
|
||||
Gender *int `json:"gender"`
|
||||
Phone string `json:"phone"`
|
||||
}
|
||||
|
||||
func (h *AuthHandler) LoginWithWeChat(c *gin.Context) {
|
||||
@@ -36,16 +37,21 @@ func (h *AuthHandler) LoginWithWeChat(c *gin.Context) {
|
||||
}
|
||||
|
||||
result, err := h.authService.LoginWithCode(c.Request.Context(), service.LoginRequest{
|
||||
Code: req.Code,
|
||||
NickName: req.NickName,
|
||||
AvatarURL: req.AvatarURL,
|
||||
Gender: req.Gender,
|
||||
Phone: req.Phone,
|
||||
MiniProgramID: req.MiniProgramID,
|
||||
Code: req.Code,
|
||||
NickName: req.NickName,
|
||||
AvatarURL: req.AvatarURL,
|
||||
Gender: req.Gender,
|
||||
Phone: req.Phone,
|
||||
})
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, service.ErrCodeRequired):
|
||||
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "code is required"))
|
||||
case errors.Is(err, service.ErrMiniProgramRequired):
|
||||
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "mini_program_id is required"))
|
||||
case errors.Is(err, service.ErrMiniProgramNotFound):
|
||||
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "mini program not found"))
|
||||
default:
|
||||
var apiErr *service.WeChatError
|
||||
if errors.As(err, &apiErr) {
|
||||
@@ -58,19 +64,30 @@ func (h *AuthHandler) LoginWithWeChat(c *gin.Context) {
|
||||
}
|
||||
|
||||
userPayload := gin.H{
|
||||
"id": result.User.ID,
|
||||
"open_id": result.User.OpenID,
|
||||
"nickname": result.User.NickName,
|
||||
"avatar_url": result.User.AvatarURL,
|
||||
"gender": result.User.Gender,
|
||||
"phone": result.User.Phone,
|
||||
"id": result.User.ID,
|
||||
"mini_program_id": result.User.MiniProgramID,
|
||||
"open_id": result.User.OpenID,
|
||||
"nickname": result.User.NickName,
|
||||
"avatar_url": result.User.AvatarURL,
|
||||
"gender": result.User.Gender,
|
||||
"phone": result.User.Phone,
|
||||
}
|
||||
if result.User.UnionID != "" {
|
||||
userPayload["union_id"] = result.User.UnionID
|
||||
}
|
||||
|
||||
miniProgramPayload := gin.H{
|
||||
"id": result.MiniProgram.ID,
|
||||
"name": result.MiniProgram.Name,
|
||||
"app_id": result.MiniProgram.AppID,
|
||||
}
|
||||
if result.MiniProgram.Description != "" {
|
||||
miniProgramPayload["description"] = result.MiniProgram.Description
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, model.Success(gin.H{
|
||||
"user": userPayload,
|
||||
"session_key": result.SessionKey,
|
||||
"user": userPayload,
|
||||
"session_key": result.SessionKey,
|
||||
"mini_program": miniProgramPayload,
|
||||
}))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user