SettingController.go 4.3 KB

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