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