setting.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package controllers
  2. import (
  3. "image"
  4. "fmt"
  5. "os"
  6. "strings"
  7. "image/jpeg"
  8. "image/png"
  9. "image/gif"
  10. "path/filepath"
  11. "strconv"
  12. "time"
  13. "io/ioutil"
  14. "bytes"
  15. "github.com/astaxie/beego/logs"
  16. "github.com/lifei6671/godoc/models"
  17. "github.com/lifei6671/godoc/utils"
  18. )
  19. type SettingController struct {
  20. BaseController
  21. }
  22. func (c *SettingController) Index() {
  23. c.TplName = "setting/index.tpl"
  24. if c.Ctx.Input.IsPost() {
  25. email := strings.TrimSpace(c.GetString("email", ""))
  26. phone := strings.TrimSpace(c.GetString("phone"))
  27. description := strings.TrimSpace(c.GetString("description"))
  28. if email == "" {
  29. c.JsonResult(601, "邮箱不能为空")
  30. }
  31. member := c.Member
  32. member.Email = email
  33. member.Phone = phone
  34. member.Description = description
  35. if err := member.Update(); err != nil {
  36. c.JsonResult(602, err.Error())
  37. }
  38. c.JsonResult(0, "ok")
  39. }
  40. }
  41. func (c *SettingController) Password() {
  42. c.TplName = "setting/password.tpl"
  43. if c.Ctx.Input.IsPost() {
  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(int64(time.Now().Nanosecond()), 16)
  98. filePath := "static/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. fileBytes,err := ioutil.ReadFile(filePath)
  107. if err != nil {
  108. logs.Error("",err)
  109. c.JsonResult(500,"图片保存失败")
  110. }
  111. buf := bytes.NewBuffer(fileBytes)
  112. m,_,err := image.Decode(buf)
  113. if err != nil{
  114. logs.Error("image.Decode => ",err)
  115. c.JsonResult(500,"图片解码失败")
  116. }
  117. var subImg image.Image
  118. if rgbImg,ok := m.(*image.YCbCr); ok {
  119. subImg = rgbImg.SubImage(image.Rect(x, y, x+width, y+height)).(*image.YCbCr) //图片裁剪x0 y0 x1 y1
  120. }else if rgbImg,ok := m.(*image.RGBA); ok {
  121. subImg = rgbImg.SubImage(image.Rect(x, y, x+width, y+height)).(*image.YCbCr) //图片裁剪x0 y0 x1 y1
  122. }else if rgbImg,ok := m.(*image.NRGBA); ok {
  123. subImg = rgbImg.SubImage(image.Rect(x, y, x+width, y+height)).(*image.YCbCr) //图片裁剪x0 y0 x1 y1
  124. } else {
  125. fmt.Println(m)
  126. c.JsonResult(500,"图片解码失败")
  127. }
  128. f, err := os.OpenFile("./" + filePath, os.O_SYNC | os.O_RDWR, 0666)
  129. if err != nil{
  130. c.JsonResult(500,"保存图片失败")
  131. }
  132. defer f.Close()
  133. if strings.EqualFold(ext,".jpg") || strings.EqualFold(ext,".jpeg"){
  134. err = jpeg.Encode(f,subImg,&jpeg.Options{ Quality : 100 })
  135. }else if strings.EqualFold(ext,".png") {
  136. err = png.Encode(f,subImg)
  137. }else if strings.EqualFold(ext,".gif") {
  138. err = gif.Encode(f,subImg,&gif.Options{ NumColors : 256})
  139. }
  140. if err != nil {
  141. logs.Error("图片剪切失败 => ",err.Error())
  142. c.JsonResult(500,"图片剪切失败")
  143. }
  144. if err != nil {
  145. logs.Error("保存文件失败 => ",err.Error())
  146. c.JsonResult(500,"保存文件失败")
  147. }
  148. url := "/" + filePath
  149. member := models.NewMember()
  150. if err := member.Find(c.Member.MemberId);err == nil {
  151. member.Avatar = url
  152. member.Update()
  153. c.SetMember(*member)
  154. }
  155. c.JsonResult(0,"ok",url)
  156. }