mirror of
https://github.com/mindoc-org/mindoc.git
synced 2025-12-17 18:31:21 +08:00
搭建框架
This commit is contained in:
79
controllers/account.go
Normal file
79
controllers/account.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/lifei6671/godoc/conf"
|
||||
"github.com/lifei6671/godoc/models"
|
||||
"github.com/lifei6671/godoc/utils"
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
|
||||
// AccountController 用户登录与注册.
|
||||
type AccountController struct {
|
||||
BaseController
|
||||
}
|
||||
|
||||
// Login 用户登录.
|
||||
func (c *AccountController) Login() {
|
||||
c.Prepare()
|
||||
|
||||
var remember struct { MemberId int ; Account string; Time time.Time}
|
||||
|
||||
//如果Cookie中存在登录信息
|
||||
if cookie,ok := c.GetSecureCookie(conf.GetAppKey(),"login");ok{
|
||||
|
||||
if err := utils.Decode(cookie,&remember); err == nil {
|
||||
member := models.NewMember()
|
||||
member.MemberId = remember.MemberId
|
||||
|
||||
if err := models.NewMember().Find(remember.MemberId); err == nil {
|
||||
c.SetMember(*member)
|
||||
|
||||
c.Redirect(beego.URLFor("HomeController.Index"), 302)
|
||||
c.StopRun()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if c.Ctx.Input.IsPost() {
|
||||
account := c.GetString("inputAccount")
|
||||
password := c.GetString("inputPassword")
|
||||
|
||||
member,err := models.NewMember().Login(account,password)
|
||||
|
||||
//如果没有数据
|
||||
if err == nil {
|
||||
c.SetMember(*member)
|
||||
c.JsonResult(0,"ok")
|
||||
c.StopRun()
|
||||
}else{
|
||||
fmt.Println(err)
|
||||
c.JsonResult(500,"账号或密码错误",nil)
|
||||
}
|
||||
|
||||
return
|
||||
}else{
|
||||
|
||||
c.Layout = ""
|
||||
c.TplName = "account/login.tpl"
|
||||
}
|
||||
}
|
||||
|
||||
func (p *AccountController) Register() {
|
||||
p.TplName = "account/register.tpl"
|
||||
|
||||
|
||||
}
|
||||
|
||||
func (p *AccountController) FindPassword() {
|
||||
p.TplName = "account/find_password.tpl"
|
||||
}
|
||||
// Logout 退出登录.
|
||||
func (c *AccountController) Logout(){
|
||||
c.SetMember(models.Member{});
|
||||
|
||||
c.Redirect(beego.URLFor("AccountController.Login"),302)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,85 @@
|
||||
package controllers
|
||||
|
||||
import "github.com/astaxie/beego"
|
||||
import (
|
||||
|
||||
"bytes"
|
||||
|
||||
"github.com/lifei6671/godoc/models"
|
||||
"github.com/lifei6671/godoc/conf"
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
|
||||
|
||||
type BaseController struct {
|
||||
beego.Controller
|
||||
Member *models.Member
|
||||
}
|
||||
|
||||
// Prepare 预处理.
|
||||
func (c *BaseController) Prepare (){
|
||||
c.Data["SiteName"] = "MinDoc"
|
||||
c.Data["Member"] = models.Member{}
|
||||
|
||||
if member,ok := c.GetSession(conf.LoginSessionName).(models.Member); ok && member.MemberId > 0{
|
||||
c.Member = &member
|
||||
c.Data["Member"] = c.Member
|
||||
}else{
|
||||
c.Member = models.NewMember()
|
||||
c.Member.Find(1)
|
||||
c.Data["Member"] = *c.Member
|
||||
}
|
||||
c.Data["BaseUrl"] = c.Ctx.Input.Scheme() + "://" + c.Ctx.Request.Host
|
||||
|
||||
if options,err := models.NewOption().All();err == nil {
|
||||
for _,item := range options {
|
||||
c.Data[item.OptionName] = item.OptionValue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SetMember 获取或设置当前登录用户信息,如果 MemberId 小于 0 则标识删除 Session
|
||||
func (c *BaseController) SetMember(member models.Member) {
|
||||
|
||||
if member.MemberId <= 0 {
|
||||
c.DelSession(conf.LoginSessionName)
|
||||
c.DelSession("uid")
|
||||
c.DestroySession()
|
||||
} else {
|
||||
c.SetSession(conf.LoginSessionName, member)
|
||||
c.SetSession("uid", member.MemberId)
|
||||
}
|
||||
}
|
||||
|
||||
// JsonResult 响应 json 结果
|
||||
func (c *BaseController) JsonResult(errCode int,errMsg string,data ...interface{}){
|
||||
json := make(map[string]interface{},3)
|
||||
|
||||
json["errcode"] = errCode
|
||||
json["message"] = errMsg
|
||||
|
||||
if len(data) > 0 && data[0] != nil{
|
||||
json["data"] = data[0]
|
||||
}
|
||||
|
||||
c.Data["json"] = json
|
||||
c.ServeJSON(true)
|
||||
c.StopRun()
|
||||
}
|
||||
|
||||
// ExecuteViewPathTemplate 执行指定的模板并返回执行结果.
|
||||
func (c *BaseController) ExecuteViewPathTemplate(tplName string,data interface{}) (string,error){
|
||||
var buf bytes.Buffer
|
||||
|
||||
viewPath := c.ViewPath
|
||||
|
||||
if c.ViewPath == "" {
|
||||
viewPath = beego.BConfig.WebConfig.ViewsPath
|
||||
|
||||
}
|
||||
|
||||
if err := beego.ExecuteViewPathTemplate(&buf,tplName,viewPath,data); err != nil {
|
||||
return "",err
|
||||
}
|
||||
return buf.String(),nil
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package controllers
|
||||
|
||||
import "strings"
|
||||
|
||||
type BookController struct {
|
||||
BaseController
|
||||
}
|
||||
@@ -22,8 +24,17 @@ func (p *BookController) Users() {
|
||||
p.TplName = "book/users.tpl"
|
||||
}
|
||||
|
||||
func (p *BookController) Create() {
|
||||
p.TplName = "book/create.tpl"
|
||||
func (c *BookController) Create() {
|
||||
|
||||
if c.Ctx.Input.IsPost() {
|
||||
book_name := strings.TrimSpace(c.GetString("book_name",""))
|
||||
identify := strings.TrimSpace(c.GetString("identify",""))
|
||||
description := strings.TrimSpace(c.GetString("description",""))
|
||||
privately_owned := c.GetString("privately_owned")
|
||||
comment_status := c.GetString("comment_status")
|
||||
|
||||
}
|
||||
c.JsonResult(6001,"error")
|
||||
}
|
||||
|
||||
// Edit 编辑项目.
|
||||
|
||||
@@ -7,3 +7,7 @@ type ManagerController struct {
|
||||
func (p *ManagerController) Index() {
|
||||
p.TplName = "manager/index.tpl"
|
||||
}
|
||||
|
||||
func (p *ManagerController) Users() {
|
||||
p.TplName = "manager/users.tpl"
|
||||
}
|
||||
@@ -1,13 +1,196 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"image"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"image/gif"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
"io/ioutil"
|
||||
"bytes"
|
||||
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/lifei6671/godoc/models"
|
||||
"github.com/lifei6671/godoc/utils"
|
||||
)
|
||||
|
||||
type SettingController struct {
|
||||
BaseController
|
||||
}
|
||||
|
||||
func (p *SettingController) Index() {
|
||||
p.TplName = "setting/index.tpl"
|
||||
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.JsonResult(0, "ok")
|
||||
}
|
||||
}
|
||||
|
||||
func (p *SettingController) Password() {
|
||||
p.TplName = "setting/password.tpl"
|
||||
func (c *SettingController) Password() {
|
||||
c.TplName = "setting/password.tpl"
|
||||
|
||||
if c.Ctx.Input.IsPost() {
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
||||
fileName := "avatar_" + strconv.FormatInt(int64(time.Now().Nanosecond()), 16)
|
||||
|
||||
filePath := "static/uploads/" + time.Now().Format("200601") + "/" + fileName + ext
|
||||
|
||||
path := filepath.Dir(filePath)
|
||||
|
||||
os.MkdirAll(path, os.ModePerm)
|
||||
|
||||
err = c.SaveToFile("image-file",filePath)
|
||||
|
||||
if err != nil {
|
||||
logs.Error("",err)
|
||||
c.JsonResult(500,"图片保存失败")
|
||||
}
|
||||
|
||||
fileBytes,err := ioutil.ReadFile(filePath)
|
||||
|
||||
if err != nil {
|
||||
logs.Error("",err)
|
||||
c.JsonResult(500,"图片保存失败")
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(fileBytes)
|
||||
|
||||
m,_,err := image.Decode(buf)
|
||||
|
||||
if err != nil{
|
||||
logs.Error("image.Decode => ",err)
|
||||
c.JsonResult(500,"图片解码失败")
|
||||
}
|
||||
|
||||
|
||||
var subImg image.Image
|
||||
|
||||
if rgbImg,ok := m.(*image.YCbCr); ok {
|
||||
subImg = rgbImg.SubImage(image.Rect(x, y, x+width, y+height)).(*image.YCbCr) //图片裁剪x0 y0 x1 y1
|
||||
}else if rgbImg,ok := m.(*image.RGBA); ok {
|
||||
subImg = rgbImg.SubImage(image.Rect(x, y, x+width, y+height)).(*image.YCbCr) //图片裁剪x0 y0 x1 y1
|
||||
}else if rgbImg,ok := m.(*image.NRGBA); ok {
|
||||
subImg = rgbImg.SubImage(image.Rect(x, y, x+width, y+height)).(*image.YCbCr) //图片裁剪x0 y0 x1 y1
|
||||
} else {
|
||||
fmt.Println(m)
|
||||
c.JsonResult(500,"图片解码失败")
|
||||
}
|
||||
|
||||
f, err := os.OpenFile("./" + filePath, os.O_SYNC | os.O_RDWR, 0666)
|
||||
|
||||
if err != nil{
|
||||
c.JsonResult(500,"保存图片失败")
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
if strings.EqualFold(ext,".jpg") || strings.EqualFold(ext,".jpeg"){
|
||||
|
||||
err = jpeg.Encode(f,subImg,&jpeg.Options{ Quality : 100 })
|
||||
}else if strings.EqualFold(ext,".png") {
|
||||
err = png.Encode(f,subImg)
|
||||
}else if strings.EqualFold(ext,".gif") {
|
||||
err = gif.Encode(f,subImg,&gif.Options{ NumColors : 256})
|
||||
}
|
||||
if err != nil {
|
||||
logs.Error("图片剪切失败 => ",err.Error())
|
||||
c.JsonResult(500,"图片剪切失败")
|
||||
}
|
||||
|
||||
|
||||
if err != nil {
|
||||
logs.Error("保存文件失败 => ",err.Error())
|
||||
c.JsonResult(500,"保存文件失败")
|
||||
}
|
||||
url := "/" + filePath
|
||||
|
||||
member := models.NewMember()
|
||||
if err := member.Find(c.Member.MemberId);err == nil {
|
||||
member.Avatar = url
|
||||
member.Update()
|
||||
c.SetMember(*member)
|
||||
}
|
||||
|
||||
c.JsonResult(0,"ok",url)
|
||||
}
|
||||
Reference in New Issue
Block a user