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)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 修改用户名
|
|
|
|
// [OK]
|
|
|
|
func (c ApiUser) UpdateUsername(username string) revel.Result {
|
|
|
|
re := info.NewApiRe()
|
|
|
|
if c.GetUsername() == "demo" {
|
|
|
|
re.Msg = "cannotUpdateDemo"
|
|
|
|
return c.RenderJson(re)
|
|
|
|
}
|
|
|
|
|
|
|
|
if re.Ok, re.Msg = Vd("username", username); !re.Ok {
|
|
|
|
return c.RenderJson(re)
|
|
|
|
}
|
|
|
|
|
|
|
|
re.Ok, re.Msg = userService.UpdateUsername(c.getUserId(), username)
|
|
|
|
return c.RenderJson(re)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 修改密码
|
|
|
|
// [OK]
|
|
|
|
func (c ApiUser) UpdatePwd(oldPwd, pwd string) revel.Result {
|
|
|
|
re := info.NewApiRe()
|
|
|
|
if c.GetUsername() == "demo" {
|
|
|
|
re.Msg = "cannotUpdateDemo"
|
|
|
|
return c.RenderJson(re)
|
|
|
|
}
|
|
|
|
if re.Ok, re.Msg = Vd("password", oldPwd); !re.Ok {
|
|
|
|
return c.RenderJson(re)
|
|
|
|
}
|
|
|
|
if re.Ok, re.Msg = Vd("password", pwd); !re.Ok {
|
|
|
|
return c.RenderJson(re)
|
|
|
|
}
|
|
|
|
re.Ok, re.Msg = userService.UpdatePwd(c.getUserId(), oldPwd, pwd)
|
|
|
|
return c.RenderJson(re)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获得同步状态
|
|
|
|
// [OK]
|
|
|
|
func (c ApiUser) GetSyncState() revel.Result {
|
|
|
|
ret := bson.M{"LastSyncUsn": userService.GetUsn(c.getUserId()), "LastSyncTime": time.Now().Unix()}
|
|
|
|
return c.RenderJson(ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 头像设置
|
|
|
|
// 参数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})
|
|
|
|
} else {
|
|
|
|
re := info.NewApiRe()
|
|
|
|
re.Msg = msg
|
|
|
|
return c.RenderJson(re)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 上传图片
|
|
|
|
func (c ApiUser) uploadImage() (ok bool, msg, url string) {
|
|
|
|
var fileUrlPath = ""
|
|
|
|
ok = false
|
|
|
|
|
|
|
|
file, handel, err := c.Request.FormFile("file")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
// 生成上传路径
|
|
|
|
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
|
|
|
|
err = os.MkdirAll(dir, 0755)
|
|
|
|
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
|
|
|
|
data, err := ioutil.ReadAll(file)
|
|
|
|
if err != nil {
|
|
|
|
LogJ(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// > 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
|
|
|
}
|