SettingController.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package controllers
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/astaxie/beego/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 err := c.Member.Update(); err != nil {
  73. c.JsonResult(6008, err.Error())
  74. }
  75. c.JsonResult(0, "ok")
  76. }
  77. }
  78. // Upload 上传图片
  79. func (c *SettingController) Upload() {
  80. file, moreFile, err := c.GetFile("image-file")
  81. defer file.Close()
  82. if err != nil {
  83. logs.Error("", err.Error())
  84. c.JsonResult(500, "读取文件异常")
  85. }
  86. ext := filepath.Ext(moreFile.Filename)
  87. if !strings.EqualFold(ext, ".png") && !strings.EqualFold(ext, ".jpg") && !strings.EqualFold(ext, ".gif") && !strings.EqualFold(ext, ".jpeg") {
  88. c.JsonResult(500, "不支持的图片格式")
  89. }
  90. x1, _ := strconv.ParseFloat(c.GetString("x"), 10)
  91. y1, _ := strconv.ParseFloat(c.GetString("y"), 10)
  92. w1, _ := strconv.ParseFloat(c.GetString("width"), 10)
  93. h1, _ := strconv.ParseFloat(c.GetString("height"), 10)
  94. x := int(x1)
  95. y := int(y1)
  96. width := int(w1)
  97. height := int(h1)
  98. fmt.Println(x, x1, y, y1)
  99. fileName := "avatar_" + strconv.FormatInt(time.Now().UnixNano(), 16)
  100. filePath := filepath.Join(conf.WorkingDirectory, "uploads", time.Now().Format("200601"), fileName+ext)
  101. path := filepath.Dir(filePath)
  102. os.MkdirAll(path, os.ModePerm)
  103. err = c.SaveToFile("image-file", filePath)
  104. if err != nil {
  105. logs.Error("", err)
  106. c.JsonResult(500, "图片保存失败")
  107. }
  108. //剪切图片
  109. subImg, err := graphics.ImageCopyFromFile(filePath, x, y, width, height)
  110. if err != nil {
  111. logs.Error("ImageCopyFromFile => ", err)
  112. c.JsonResult(6001, "头像剪切失败")
  113. }
  114. os.Remove(filePath)
  115. filePath = filepath.Join(conf.WorkingDirectory, "uploads", time.Now().Format("200601"), fileName+"_small"+ext)
  116. err = graphics.ImageResizeSaveFile(subImg, 120, 120, filePath)
  117. //err = graphics.SaveImage(filePath,subImg)
  118. if err != nil {
  119. logs.Error("保存文件失败 => ", err.Error())
  120. c.JsonResult(500, "保存文件失败")
  121. }
  122. url := "/" + strings.Replace(strings.TrimPrefix(filePath, conf.WorkingDirectory), "\\", "/", -1)
  123. if strings.HasPrefix(url, "//") {
  124. url = string(url[1:])
  125. }
  126. if member, err := models.NewMember().Find(c.Member.MemberId); err == nil {
  127. avater := member.Avatar
  128. member.Avatar = url
  129. err := member.Update()
  130. if err == nil {
  131. if strings.HasPrefix(avater, "/uploads/") {
  132. os.Remove(filepath.Join(conf.WorkingDirectory, avater))
  133. }
  134. c.SetMember(*member)
  135. } else {
  136. c.JsonResult(60001, "保存头像失败")
  137. }
  138. }
  139. c.JsonResult(0, "ok", url)
  140. }