SettingController.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package controllers
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/beego/beego/v2/core/logs"
  10. "github.com/beego/i18n"
  11. "github.com/mindoc-org/mindoc/conf"
  12. "github.com/mindoc-org/mindoc/graphics"
  13. "github.com/mindoc-org/mindoc/models"
  14. "github.com/mindoc-org/mindoc/utils"
  15. )
  16. type SettingController struct {
  17. BaseController
  18. }
  19. func (c *SettingController) Index() {
  20. c.TplName = "setting/index.tpl"
  21. if c.Ctx.Input.IsPost() {
  22. email := strings.TrimSpace(c.GetString("email", ""))
  23. phone := strings.TrimSpace(c.GetString("phone"))
  24. description := strings.TrimSpace(c.GetString("description"))
  25. if email == "" {
  26. c.JsonResult(601, i18n.Tr(c.Lang, "message.email_empty"))
  27. }
  28. member := c.Member
  29. member.Email = email
  30. member.Phone = phone
  31. member.Description = description
  32. member.RealName = strings.TrimSpace(c.GetString("real_name", ""))
  33. if err := member.Update(); err != nil {
  34. c.JsonResult(602, err.Error())
  35. }
  36. c.SetMember(*member)
  37. c.JsonResult(0, "ok")
  38. }
  39. }
  40. func (c *SettingController) Password() {
  41. c.TplName = "setting/password.tpl"
  42. if c.Ctx.Input.IsPost() {
  43. if c.Member.AuthMethod == conf.AuthMethodLDAP {
  44. c.JsonResult(6009, i18n.Tr(c.Lang, "message.cur_user_cannot_change_pwd"))
  45. }
  46. password1 := c.GetString("password1")
  47. password2 := c.GetString("password2")
  48. password3 := c.GetString("password3")
  49. if password1 == "" {
  50. c.JsonResult(6003, i18n.Tr(c.Lang, "message.origin_pwd_empty"))
  51. }
  52. if password2 == "" {
  53. c.JsonResult(6004, i18n.Tr(c.Lang, "message.new_pwd_empty"))
  54. }
  55. if count := strings.Count(password2, ""); count < 6 || count > 18 {
  56. c.JsonResult(6009, i18n.Tr(c.Lang, "message.pwd_length"))
  57. }
  58. if password2 != password3 {
  59. c.JsonResult(6003, "确认密码不正确")
  60. }
  61. if ok, _ := utils.PasswordVerify(c.Member.Password, password1); !ok {
  62. c.JsonResult(6005, i18n.Tr(c.Lang, "message.wrong_origin_pwd"))
  63. }
  64. if password1 == password2 {
  65. c.JsonResult(6006, i18n.Tr(c.Lang, "message.same_pwd"))
  66. }
  67. pwd, err := utils.PasswordHash(password2)
  68. if err != nil {
  69. c.JsonResult(6007, i18n.Tr(c.Lang, "message.pwd_encrypt_failed"))
  70. }
  71. c.Member.Password = pwd
  72. if c.Member.AuthMethod == "" {
  73. c.Member.AuthMethod = "local"
  74. }
  75. if err := c.Member.Update(); err != nil {
  76. c.JsonResult(6008, err.Error())
  77. }
  78. c.JsonResult(0, "ok")
  79. }
  80. }
  81. // Upload 上传图片
  82. func (c *SettingController) Upload() {
  83. file, moreFile, err := c.GetFile("image-file")
  84. defer file.Close()
  85. if err != nil {
  86. logs.Error("", err.Error())
  87. c.JsonResult(500, "读取文件异常")
  88. }
  89. ext := filepath.Ext(moreFile.Filename)
  90. if !strings.EqualFold(ext, ".png") && !strings.EqualFold(ext, ".jpg") && !strings.EqualFold(ext, ".gif") && !strings.EqualFold(ext, ".jpeg") {
  91. c.JsonResult(500, "不支持的图片格式")
  92. }
  93. x1, _ := strconv.ParseFloat(c.GetString("x"), 10)
  94. y1, _ := strconv.ParseFloat(c.GetString("y"), 10)
  95. w1, _ := strconv.ParseFloat(c.GetString("width"), 10)
  96. h1, _ := strconv.ParseFloat(c.GetString("height"), 10)
  97. x := int(x1)
  98. y := int(y1)
  99. width := int(w1)
  100. height := int(h1)
  101. fmt.Println(x, x1, y, y1)
  102. fileName := "avatar_" + strconv.FormatInt(time.Now().UnixNano(), 16)
  103. filePath := filepath.Join(conf.WorkingDirectory, "uploads", time.Now().Format("200601"), fileName+ext)
  104. path := filepath.Dir(filePath)
  105. os.MkdirAll(path, os.ModePerm)
  106. err = c.SaveToFile("image-file", filePath)
  107. if err != nil {
  108. logs.Error("", err)
  109. c.JsonResult(500, "图片保存失败")
  110. }
  111. //剪切图片
  112. subImg, err := graphics.ImageCopyFromFile(filePath, x, y, width, height)
  113. if err != nil {
  114. logs.Error("ImageCopyFromFile => ", err)
  115. c.JsonResult(6001, "头像剪切失败")
  116. }
  117. os.Remove(filePath)
  118. filePath = filepath.Join(conf.WorkingDirectory, "uploads", time.Now().Format("200601"), fileName+"_small"+ext)
  119. err = graphics.ImageResizeSaveFile(subImg, 120, 120, filePath)
  120. //err = graphics.SaveImage(filePath,subImg)
  121. if err != nil {
  122. logs.Error("保存文件失败 => ", err.Error())
  123. c.JsonResult(500, "保存文件失败")
  124. }
  125. url := "/" + strings.Replace(strings.TrimPrefix(filePath, conf.WorkingDirectory), "\\", "/", -1)
  126. if strings.HasPrefix(url, "//") {
  127. url = string(url[1:])
  128. }
  129. if member, err := models.NewMember().Find(c.Member.MemberId); err == nil {
  130. avater := member.Avatar
  131. member.Avatar = url
  132. err := member.Update()
  133. if err == nil {
  134. if strings.HasPrefix(avater, "/uploads/") {
  135. os.Remove(filepath.Join(conf.WorkingDirectory, avater))
  136. }
  137. c.SetMember(*member)
  138. } else {
  139. c.JsonResult(60001, "保存头像失败")
  140. }
  141. }
  142. c.JsonResult(0, "ok", url)
  143. }