2017-04-20 18:19:32 +08:00
|
|
|
package controllers
|
|
|
|
|
2017-04-21 18:20:35 +08:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2018-01-26 17:17:38 +08:00
|
|
|
"strings"
|
2017-04-21 18:20:35 +08:00
|
|
|
"time"
|
|
|
|
|
2021-03-26 14:26:19 +08:00
|
|
|
"github.com/beego/beego/v2/core/logs"
|
2021-04-19 14:32:55 +08:00
|
|
|
"github.com/beego/i18n"
|
2021-03-23 15:09:17 +08:00
|
|
|
"github.com/mindoc-org/mindoc/conf"
|
|
|
|
"github.com/mindoc-org/mindoc/graphics"
|
|
|
|
"github.com/mindoc-org/mindoc/models"
|
|
|
|
"github.com/mindoc-org/mindoc/utils"
|
2017-04-21 18:20:35 +08:00
|
|
|
)
|
|
|
|
|
2017-04-20 18:19:32 +08:00
|
|
|
type SettingController struct {
|
|
|
|
BaseController
|
|
|
|
}
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
func (c *SettingController) Index() {
|
2017-04-21 18:20:35 +08:00
|
|
|
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 == "" {
|
2021-04-19 14:32:55 +08:00
|
|
|
c.JsonResult(601, i18n.Tr(c.Lang, "message.email_empty"))
|
2017-04-21 18:20:35 +08:00
|
|
|
}
|
|
|
|
member := c.Member
|
|
|
|
member.Email = email
|
|
|
|
member.Phone = phone
|
|
|
|
member.Description = description
|
2021-03-23 15:09:17 +08:00
|
|
|
member.RealName = strings.TrimSpace(c.GetString("real_name", ""))
|
2017-04-21 18:20:35 +08:00
|
|
|
if err := member.Update(); err != nil {
|
|
|
|
c.JsonResult(602, err.Error())
|
|
|
|
}
|
2017-05-11 13:39:34 +08:00
|
|
|
c.SetMember(*member)
|
2017-04-21 18:20:35 +08:00
|
|
|
c.JsonResult(0, "ok")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
func (c *SettingController) Password() {
|
2017-04-21 18:20:35 +08:00
|
|
|
c.TplName = "setting/password.tpl"
|
|
|
|
|
|
|
|
if c.Ctx.Input.IsPost() {
|
2017-05-26 14:19:27 +08:00
|
|
|
if c.Member.AuthMethod == conf.AuthMethodLDAP {
|
2021-04-19 14:32:55 +08:00
|
|
|
c.JsonResult(6009, i18n.Tr(c.Lang, "message.cur_user_cannot_change_pwd"))
|
2017-05-26 14:19:27 +08:00
|
|
|
}
|
2017-04-21 18:20:35 +08:00
|
|
|
password1 := c.GetString("password1")
|
|
|
|
password2 := c.GetString("password2")
|
|
|
|
password3 := c.GetString("password3")
|
|
|
|
|
|
|
|
if password1 == "" {
|
2021-04-19 14:32:55 +08:00
|
|
|
c.JsonResult(6003, i18n.Tr(c.Lang, "message.origin_pwd_empty"))
|
2017-04-21 18:20:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if password2 == "" {
|
2021-04-19 14:32:55 +08:00
|
|
|
c.JsonResult(6004, i18n.Tr(c.Lang, "message.new_pwd_empty"))
|
2017-04-21 18:20:35 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
if count := strings.Count(password2, ""); count < 6 || count > 18 {
|
2021-04-19 14:32:55 +08:00
|
|
|
c.JsonResult(6009, i18n.Tr(c.Lang, "message.pwd_length"))
|
2017-04-21 18:20:35 +08:00
|
|
|
}
|
|
|
|
if password2 != password3 {
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(6003, "确认密码不正确")
|
2017-04-21 18:20:35 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
if ok, _ := utils.PasswordVerify(c.Member.Password, password1); !ok {
|
2021-04-19 14:32:55 +08:00
|
|
|
c.JsonResult(6005, i18n.Tr(c.Lang, "message.wrong_origin_pwd"))
|
2017-04-21 18:20:35 +08:00
|
|
|
}
|
|
|
|
if password1 == password2 {
|
2021-04-19 14:32:55 +08:00
|
|
|
c.JsonResult(6006, i18n.Tr(c.Lang, "message.same_pwd"))
|
2017-04-21 18:20:35 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
pwd, err := utils.PasswordHash(password2)
|
2017-04-21 18:20:35 +08:00
|
|
|
if err != nil {
|
2021-04-19 14:32:55 +08:00
|
|
|
c.JsonResult(6007, i18n.Tr(c.Lang, "message.pwd_encrypt_failed"))
|
2017-04-21 18:20:35 +08:00
|
|
|
}
|
|
|
|
c.Member.Password = pwd
|
2023-04-20 13:24:28 +08:00
|
|
|
if c.Member.AuthMethod == "" {
|
|
|
|
c.Member.AuthMethod = "local"
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
if err := c.Member.Update(); err != nil {
|
|
|
|
c.JsonResult(6008, err.Error())
|
2017-04-21 18:20:35 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(0, "ok")
|
2017-04-21 18:20:35 +08:00
|
|
|
}
|
2017-04-20 18:19:32 +08:00
|
|
|
}
|
|
|
|
|
2017-04-21 18:20:35 +08:00
|
|
|
// Upload 上传图片
|
|
|
|
func (c *SettingController) Upload() {
|
2018-01-26 17:17:38 +08:00
|
|
|
file, moreFile, err := c.GetFile("image-file")
|
2017-04-21 18:20:35 +08:00
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
if err != nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
logs.Error("", err.Error())
|
|
|
|
c.JsonResult(500, "读取文件异常")
|
2017-04-21 18:20:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ext := filepath.Ext(moreFile.Filename)
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
if !strings.EqualFold(ext, ".png") && !strings.EqualFold(ext, ".jpg") && !strings.EqualFold(ext, ".gif") && !strings.EqualFold(ext, ".jpeg") {
|
|
|
|
c.JsonResult(500, "不支持的图片格式")
|
2017-04-21 18:20:35 +08:00
|
|
|
}
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
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)
|
2017-04-21 18:20:35 +08:00
|
|
|
|
|
|
|
x := int(x1)
|
|
|
|
y := int(y1)
|
|
|
|
width := int(w1)
|
|
|
|
height := int(h1)
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
fmt.Println(x, x1, y, y1)
|
2017-04-21 18:20:35 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
fileName := "avatar_" + strconv.FormatInt(time.Now().UnixNano(), 16)
|
2017-04-21 18:20:35 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
filePath := filepath.Join(conf.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)
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
err = c.SaveToFile("image-file", filePath)
|
2017-04-21 18:20:35 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
logs.Error("", err)
|
|
|
|
c.JsonResult(500, "图片保存失败")
|
2017-04-21 18:20:35 +08:00
|
|
|
}
|
|
|
|
|
2017-04-25 20:05:59 +08:00
|
|
|
//剪切图片
|
2018-01-26 17:17:38 +08:00
|
|
|
subImg, err := graphics.ImageCopyFromFile(filePath, x, y, width, height)
|
2017-04-21 18:20:35 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
logs.Error("ImageCopyFromFile => ", err)
|
|
|
|
c.JsonResult(6001, "头像剪切失败")
|
2017-04-21 18:20:35 +08:00
|
|
|
}
|
2017-06-05 14:00:33 +08:00
|
|
|
os.Remove(filePath)
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
filePath = filepath.Join(conf.WorkingDirectory, "uploads", time.Now().Format("200601"), fileName+"_small"+ext)
|
2017-06-05 14:00:33 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
err = graphics.ImageResizeSaveFile(subImg, 120, 120, filePath)
|
2017-05-13 14:43:03 +08:00
|
|
|
//err = graphics.SaveImage(filePath,subImg)
|
2017-04-21 18:20:35 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
logs.Error("保存文件失败 => ", err.Error())
|
|
|
|
c.JsonResult(500, "保存文件失败")
|
2017-04-21 18:20:35 +08:00
|
|
|
}
|
2017-04-25 20:05:59 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
url := "/" + strings.Replace(strings.TrimPrefix(filePath, conf.WorkingDirectory), "\\", "/", -1)
|
|
|
|
if strings.HasPrefix(url, "//") {
|
2017-06-05 13:38:06 +08:00
|
|
|
url = string(url[1:])
|
|
|
|
}
|
2017-04-21 18:20:35 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
if member, err := models.NewMember().Find(c.Member.MemberId); err == nil {
|
2017-06-05 14:00:33 +08:00
|
|
|
avater := member.Avatar
|
|
|
|
|
2017-04-21 18:20:35 +08:00
|
|
|
member.Avatar = url
|
2018-01-26 17:17:38 +08:00
|
|
|
err := member.Update()
|
2017-06-05 14:00:33 +08:00
|
|
|
if err == nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
if strings.HasPrefix(avater, "/uploads/") {
|
|
|
|
os.Remove(filepath.Join(conf.WorkingDirectory, avater))
|
2017-06-05 14:00:33 +08:00
|
|
|
}
|
|
|
|
c.SetMember(*member)
|
2018-01-26 17:17:38 +08:00
|
|
|
} else {
|
|
|
|
c.JsonResult(60001, "保存头像失败")
|
2017-06-05 14:00:33 +08:00
|
|
|
}
|
2017-04-21 18:20:35 +08:00
|
|
|
}
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(0, "ok", url)
|
|
|
|
}
|