settings.go 977 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package controllers
  2. import (
  3. "encoding/json"
  4. log "github.com/sirupsen/logrus"
  5. "io"
  6. "net/http"
  7. "pmail/dto"
  8. "pmail/dto/response"
  9. "pmail/i18n"
  10. "pmail/mysql"
  11. )
  12. type modifyPasswordRequest struct {
  13. Password string `json:"password"`
  14. }
  15. func ModifyPassword(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
  16. reqBytes, err := io.ReadAll(req.Body)
  17. if err != nil {
  18. log.Errorf("%+v", err)
  19. }
  20. var retData modifyPasswordRequest
  21. err = json.Unmarshal(reqBytes, &retData)
  22. if err != nil {
  23. log.Errorf("%+v", err)
  24. }
  25. if retData.Password != "" {
  26. encodePwd := md5Encode(md5Encode(retData.Password+"pmail") + "pmail2023")
  27. _, err := mysql.Instance.Exec(mysql.WithContext(ctx, "update user set password = ? where id =?"), encodePwd, ctx.UserInfo.ID)
  28. if err != nil {
  29. response.NewErrorResponse(response.ServerError, i18n.GetText(ctx.Lang, "unknowError"), "").FPrint(w)
  30. return
  31. }
  32. }
  33. response.NewSuccessResponse(i18n.GetText(ctx.Lang, "succ")).FPrint(w)
  34. }