mindoc/controllers/setting.go

160 lines
3.9 KiB
Go
Raw Normal View History

2017-04-20 18:19:32 +08:00
package controllers
2017-04-21 18:20:35 +08:00
import (
"fmt"
"os"
"strings"
"path/filepath"
"strconv"
"time"
"github.com/astaxie/beego/logs"
"github.com/lifei6671/godoc/models"
"github.com/lifei6671/godoc/utils"
2017-04-25 20:05:59 +08:00
"github.com/lifei6671/godoc/graphics"
2017-05-26 14:19:27 +08:00
"github.com/lifei6671/godoc/conf"
2017-06-05 13:38:06 +08:00
"github.com/lifei6671/godoc/commands"
2017-04-21 18:20:35 +08:00
)
2017-04-20 18:19:32 +08:00
type SettingController struct {
BaseController
}
2017-04-21 18:20:35 +08:00
func (c *SettingController) Index() {
c.TplName = "setting/index.tpl"
if c.Ctx.Input.IsPost() {
email := strings.TrimSpace(c.GetString("email", ""))
phone := strings.TrimSpace(c.GetString("phone"))
description := strings.TrimSpace(c.GetString("description"))
if email == "" {
c.JsonResult(601, "邮箱不能为空")
}
member := c.Member
member.Email = email
member.Phone = phone
member.Description = description
if err := member.Update(); err != nil {
c.JsonResult(602, err.Error())
}
c.SetMember(*member)
2017-04-21 18:20:35 +08:00
c.JsonResult(0, "ok")
}
}
func (c *SettingController) Password() {
c.TplName = "setting/password.tpl"
if c.Ctx.Input.IsPost() {
2017-05-26 14:19:27 +08:00
if c.Member.AuthMethod == conf.AuthMethodLDAP {
c.JsonResult(6009,"当前用户不支持修改密码")
}
2017-04-21 18:20:35 +08:00
password1 := c.GetString("password1")
password2 := c.GetString("password2")
password3 := c.GetString("password3")
if password1 == "" {
c.JsonResult(6003,"原密码不能为空")
}
if password2 == "" {
c.JsonResult(6004,"新密码不能为空")
}
if count := strings.Count(password2,""); count < 6 || count > 18 {
c.JsonResult(6009,"密码必须在6-18字之间")
}
if password2 != password3 {
c.JsonResult(6003,"确认密码不正确")
}
if ok,_ := utils.PasswordVerify(c.Member.Password,password1) ; !ok {
c.JsonResult(6005,"原始密码不正确")
}
if password1 == password2 {
c.JsonResult(6006,"新密码不能和原始密码相同")
}
pwd,err := utils.PasswordHash(password2)
if err != nil {
c.JsonResult(6007,"密码加密失败")
}
c.Member.Password = pwd
if err := c.Member.Update();err != nil {
c.JsonResult(6008,err.Error())
}
c.JsonResult(0,"ok")
}
2017-04-20 18:19:32 +08:00
}
2017-04-21 18:20:35 +08:00
// Upload 上传图片
func (c *SettingController) Upload() {
file,moreFile,err := c.GetFile("image-file")
defer file.Close()
if err != nil {
logs.Error("",err.Error())
c.JsonResult(500,"读取文件异常")
}
ext := filepath.Ext(moreFile.Filename)
if !strings.EqualFold(ext,".png") && !strings.EqualFold(ext,".jpg") && !strings.EqualFold(ext,".gif") && !strings.EqualFold(ext,".jpeg") {
c.JsonResult(500,"不支持的图片格式")
}
x1 ,_ := strconv.ParseFloat(c.GetString("x"),10)
y1 ,_ := strconv.ParseFloat(c.GetString("y"),10)
w1 ,_ := strconv.ParseFloat(c.GetString("width"),10)
h1 ,_ := strconv.ParseFloat(c.GetString("height"),10)
x := int(x1)
y := int(y1)
width := int(w1)
height := int(h1)
fmt.Println(x,x1,y,y1)
2017-04-25 20:05:59 +08:00
fileName := "avatar_" + strconv.FormatInt(time.Now().UnixNano(), 16)
2017-04-21 18:20:35 +08:00
2017-06-05 13:38:06 +08:00
filePath := filepath.Join(commands.WorkingDirectory,"uploads" , time.Now().Format("200601") , fileName + ext)
2017-04-21 18:20:35 +08:00
path := filepath.Dir(filePath)
os.MkdirAll(path, os.ModePerm)
err = c.SaveToFile("image-file",filePath)
if err != nil {
logs.Error("",err)
c.JsonResult(500,"图片保存失败")
}
2017-04-25 20:05:59 +08:00
//剪切图片
subImg,err := graphics.ImageCopyFromFile(filePath,x,y,width,height)
2017-04-21 18:20:35 +08:00
if err != nil {
2017-04-25 20:05:59 +08:00
logs.Error("ImageCopyFromFile => ",err)
c.JsonResult(6001,"头像剪切失败")
2017-04-21 18:20:35 +08:00
}
2017-05-13 14:43:03 +08:00
err = graphics.ImageResizeSaveFile(subImg,120,120,filePath)
//err = graphics.SaveImage(filePath,subImg)
2017-04-21 18:20:35 +08:00
if err != nil {
logs.Error("保存文件失败 => ",err.Error())
c.JsonResult(500,"保存文件失败")
}
2017-04-25 20:05:59 +08:00
2017-06-05 13:38:06 +08:00
url := "/" + strings.Replace(strings.TrimPrefix(filePath,commands.WorkingDirectory),"\\","/",-1)
if strings.HasPrefix(url,"//") {
url = string(url[1:])
}
2017-04-21 18:20:35 +08:00
2017-05-03 14:22:05 +08:00
if member,err := models.NewMember().Find(c.Member.MemberId);err == nil {
2017-04-21 18:20:35 +08:00
member.Avatar = url
member.Update()
c.SetMember(*member)
}
c.JsonResult(0,"ok",url)
2017-04-20 18:19:32 +08:00
}