setting.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package controllers
  2. import (
  3. "fmt"
  4. "os"
  5. "strings"
  6. "path/filepath"
  7. "strconv"
  8. "time"
  9. "github.com/astaxie/beego/logs"
  10. "github.com/lifei6671/godoc/models"
  11. "github.com/lifei6671/godoc/utils"
  12. "github.com/lifei6671/godoc/graphics"
  13. "github.com/lifei6671/godoc/conf"
  14. "github.com/lifei6671/godoc/commands"
  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, "邮箱不能为空")
  27. }
  28. member := c.Member
  29. member.Email = email
  30. member.Phone = phone
  31. member.Description = description
  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(commands.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(commands.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,commands.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(commands.WorkingDirectory,avater))
  132. }
  133. c.SetMember(*member)
  134. }else{
  135. c.JsonResult(60001,"保存头像失败")
  136. }
  137. }
  138. c.JsonResult(0,"ok",url)
  139. }