setting.go 3.5 KB

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