Files
leanote/app/controllers/api/ApiUserController.go

162 lines
3.2 KiB
Go
Raw Normal View History

2015-03-31 14:27:26 +08:00
package api
import (
"github.com/revel/revel"
// "encoding/json"
"github.com/leanote/leanote/app/info"
. "github.com/leanote/leanote/app/lea"
2015-11-13 17:58:41 +08:00
"gopkg.in/mgo.v2/bson"
2015-03-31 14:27:26 +08:00
"time"
// "github.com/leanote/leanote/app/types"
2015-11-13 17:58:41 +08:00
"io/ioutil"
2015-03-31 14:27:26 +08:00
// "fmt"
// "math"
2015-11-13 17:58:41 +08:00
"os"
2015-03-31 14:27:26 +08:00
// "path"
// "strconv"
)
type ApiUser struct {
ApiBaseContrller
}
// 获取用户信息
// [OK]
func (c ApiUser) Info() revel.Result {
re := info.NewApiRe()
userInfo := c.getUserInfo()
if userInfo.UserId == "" {
return c.RenderJSON(re)
2015-03-31 14:27:26 +08:00
}
apiUser := info.ApiUser{
2015-11-13 17:58:41 +08:00
UserId: userInfo.UserId.Hex(),
2015-03-31 14:27:26 +08:00
Username: userInfo.Username,
2015-11-13 17:58:41 +08:00
Email: userInfo.Email,
Logo: userInfo.Logo,
2015-03-31 14:27:26 +08:00
Verified: userInfo.Verified,
}
return c.RenderJSON(apiUser)
2015-03-31 14:27:26 +08:00
}
// 修改用户名
// [OK]
func (c ApiUser) UpdateUsername(username string) revel.Result {
re := info.NewApiRe()
if c.GetUsername() == "demo" {
re.Msg = "cannotUpdateDemo"
return c.RenderJSON(re)
2015-03-31 14:27:26 +08:00
}
if re.Ok, re.Msg = Vd("username", username); !re.Ok {
return c.RenderJSON(re)
2015-03-31 14:27:26 +08:00
}
re.Ok, re.Msg = userService.UpdateUsername(c.getUserId(), username)
return c.RenderJSON(re)
2015-03-31 14:27:26 +08:00
}
// 修改密码
// [OK]
func (c ApiUser) UpdatePwd(oldPwd, pwd string) revel.Result {
re := info.NewApiRe()
if c.GetUsername() == "demo" {
re.Msg = "cannotUpdateDemo"
return c.RenderJSON(re)
2015-03-31 14:27:26 +08:00
}
if re.Ok, re.Msg = Vd("password", oldPwd); !re.Ok {
return c.RenderJSON(re)
2015-03-31 14:27:26 +08:00
}
if re.Ok, re.Msg = Vd("password", pwd); !re.Ok {
return c.RenderJSON(re)
2015-03-31 14:27:26 +08:00
}
re.Ok, re.Msg = userService.UpdatePwd(c.getUserId(), oldPwd, pwd)
return c.RenderJSON(re)
2015-03-31 14:27:26 +08:00
}
// 获得同步状态
// [OK]
func (c ApiUser) GetSyncState() revel.Result {
ret := bson.M{"LastSyncUsn": userService.GetUsn(c.getUserId()), "LastSyncTime": time.Now().Unix()}
return c.RenderJSON(ret)
2015-03-31 14:27:26 +08:00
}
// 头像设置
// 参数file=文件
// 成功返回{Logo: url} 头像新url
// [OK]
func (c ApiUser) UpdateLogo() revel.Result {
ok, msg, url := c.uploadImage()
if ok {
ok = userService.UpdateAvatar(c.getUserId(), url)
return c.RenderJSON(map[string]string{"Logo": url})
2015-03-31 14:27:26 +08:00
} else {
re := info.NewApiRe()
re.Msg = msg
return c.RenderJSON(re)
2015-03-31 14:27:26 +08:00
}
}
// 上传图片
func (c ApiUser) uploadImage() (ok bool, msg, url string) {
var fileUrlPath = ""
ok = false
2017-11-30 18:10:59 +08:00
var data []byte
c.Params.Bind(&data, "file")
handel := c.Params.Files["file"][0]
if data == nil || len(data) == 0 {
2015-03-31 14:27:26 +08:00
return
}
2017-11-30 18:10:59 +08:00
// file, handel, err := c.Request.FormFile("file")
// if err != nil {
// return
// }
// defer file.Close()
2015-03-31 14:27:26 +08:00
// 生成上传路径
fileUrlPath = "public/upload/" + c.getUserId() + "/images/logo"
2015-11-13 17:58:41 +08:00
2015-03-31 14:27:26 +08:00
dir := revel.BasePath + "/" + fileUrlPath
2017-11-30 18:10:59 +08:00
err := os.MkdirAll(dir, 0755)
2015-03-31 14:27:26 +08:00
if err != nil {
return
}
// 生成新的文件名
filename := handel.Filename
var ext string
2015-11-13 17:58:41 +08:00
2015-03-31 14:27:26 +08:00
_, ext = SplitFilename(filename)
if ext != ".gif" && ext != ".jpg" && ext != ".png" && ext != ".bmp" && ext != ".jpeg" {
msg = "notImage"
2015-11-13 17:58:41 +08:00
return
2015-03-31 14:27:26 +08:00
}
filename = NewGuid() + ext
2017-11-30 18:10:59 +08:00
// data, err := ioutil.ReadAll(file)
// if err != nil {
// LogJ(err)
// return
// }
2015-03-31 14:27:26 +08:00
// > 5M?
if len(data) > 5*1024*1024 {
msg = "fileIsTooLarge"
return
}
toPath := dir + "/" + filename
err = ioutil.WriteFile(toPath, data, 0777)
if err != nil {
LogJ(err)
return
}
2015-11-13 17:58:41 +08:00
2015-03-31 14:27:26 +08:00
ok = true
url = configService.GetSiteUrl() + "/" + fileUrlPath + "/" + filename
2015-11-13 17:58:41 +08:00
return
2015-03-31 14:27:26 +08:00
}