|
| 1 | +package user |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/coderi421/gframework/gmicro/server/restserver/middlewares" |
| 5 | + "github.com/coderi421/gframework/pkg/common/core" |
| 6 | + jtime "github.com/coderi421/gframework/pkg/common/time" |
| 7 | + "github.com/coderi421/goshop/app/pkg/translator/gin" |
| 8 | + "github.com/gin-gonic/gin" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +type UpdateUserForm struct { |
| 13 | + Name string `form:"name" json:"name" binding:"required,min=3,max=10"` |
| 14 | + Gender string `form:"gender" json:"gender" binding:"required,oneof=female male"` |
| 15 | + Birthday string `form:"birthday" json:"birthday" binding:"required,datetime=2006-01-02"` |
| 16 | +} |
| 17 | + |
| 18 | +func (us *userServer) UpdateUser(ctx *gin.Context) { |
| 19 | + updateForm := UpdateUserForm{} |
| 20 | + if err := ctx.ShouldBind(&updateForm); err != nil { |
| 21 | + translator.GinHandleValidatorError(ctx, err, us.trans) |
| 22 | + return |
| 23 | + } |
| 24 | + userID, _ := ctx.Get(middlewares.KeyUserID) |
| 25 | + userIDInt := int32(userID.(float64)) |
| 26 | + userDTO, err := us.srv.User().Get(ctx, userIDInt) |
| 27 | + if err != nil { |
| 28 | + core.WriteResponse(ctx, err, nil) |
| 29 | + return |
| 30 | + } |
| 31 | + //将前端传递过来的日期格式转换成int |
| 32 | + loc, _ := time.LoadLocation("Local") //local的L必须大写 |
| 33 | + birthDay, _ := time.ParseInLocation("2006-01-02", updateForm.Birthday, loc) |
| 34 | + userDTO.NickName = updateForm.Name |
| 35 | + userDTO.Birthday = jtime.Time{Time: birthDay}.Time |
| 36 | + userDTO.Gender = updateForm.Gender |
| 37 | + err = us.srv.User().Update(ctx, userDTO) |
| 38 | + if err != nil { |
| 39 | + core.WriteResponse(ctx, err, nil) |
| 40 | + return |
| 41 | + } |
| 42 | + core.WriteResponse(ctx, nil, nil) |
| 43 | +} |
0 commit comments