setting.go 4.2 KB

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