From 4f1f5e977829cfcb721c5ecc45ee51d0b3b8f435 Mon Sep 17 00:00:00 2001 From: Jimmy Kuu Date: Wed, 13 Feb 2019 23:17:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E9=80=89=E6=8B=A9=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E5=A4=B4=E5=83=8F=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- actions/user_center.go | 1 + apis/user_center.go | 57 +++++++++++++++++- routes.go | 4 +- static/js/application.js | 19 ++++++ templates/base.html | 4 +- templates/user_center/avatar.html | 96 +++++++++++++++++++++++++++---- 6 files changed, 167 insertions(+), 14 deletions(-) diff --git a/actions/user_center.go b/actions/user_center.go index 21712bf..8df0366 100644 --- a/actions/user_center.go +++ b/actions/user_center.go @@ -35,6 +35,7 @@ type ChangeAvatar struct { // Get /user_center/avatar func (a *ChangeAvatar) Get() error { return a.Render("user_center/avatar.html", renders.T{ + "title": "修改头像", "sub_active": "change_avatar", }) } diff --git a/apis/user_center.go b/apis/user_center.go index 14b09ae..d9082cc 100644 --- a/apis/user_center.go +++ b/apis/user_center.go @@ -31,6 +31,7 @@ func (a *UserInfo) Get() interface{} { "status": 1, "username": a.User.Username, "email": a.User.Email, + "avatar": a.User.Avatar, "avatars": []string{ a.User.AvatarImgSrc(128), a.User.AvatarImgSrc(64), @@ -164,7 +165,7 @@ type UploadAvatarImage struct { binding.Binder } -// Get /api/user_center/upload_avatar +// Post /api/user_center/upload_avatar func (a *UploadAvatarImage) Post() interface{} { filename, err := saveImage(a.Req(), []string{"avatar"}, 500*1024) if err != nil { @@ -190,3 +191,57 @@ func (a *UploadAvatarImage) Post() interface{} { }, } } + +// DefaultAvatars 默认头像 +type DefaultAvatars struct { + Base + binding.Binder +} + +// Get /api/user_center/default_avatars +func (a *DefaultAvatars) Get() interface{} { + var defaultAvatars = []string{ + "gopher_aqua.jpg", + "gopher_boy.jpg", + "gopher_brown.jpg", + "gopher_gentlemen.jpg", + "gopher_girl.jpg", + "gopher_strawberry_bg.jpg", + "gopher_strawberry.jpg", + "gopher_teal.jpg", + } + + return map[string]interface{}{ + "status": 1, + "default_avatars": defaultAvatars, + } +} + +// SetAvatar 设置头像 +type SetAvatar struct { + Base + binding.Binder +} + +// Put /api/user_center/set_avatar +func (a *SetAvatar) Put() interface{} { + var form struct { + Avatar string `json:"avatar"` + } + + a.ReadJSON(&form) + + c := a.DB.C(models.USERS) + c.Update(bson.M{"_id": a.User.Id_}, bson.M{"$set": bson.M{"avatar": form.Avatar}}) + + a.User.Avatar = form.Avatar + + return map[string]interface{}{ + "status": 1, + "avatars": []string{ + a.User.AvatarImgSrc(128), + a.User.AvatarImgSrc(64), + a.User.AvatarImgSrc(32), + }, + } +} diff --git a/routes.go b/routes.go index a886ae4..c9091ad 100644 --- a/routes.go +++ b/routes.go @@ -55,9 +55,11 @@ func setRoutes(t *tango.Tango) { g.Get("/comments/:commentID", new(apis.Comment)) g.Put("/comments/:commentID", new(apis.Comment)) + g.Get("/user_center/default_avatars", new(apis.DefaultAvatars)) g.Get("/user_center/user_info", new(apis.UserInfo)) g.Put("/user_center/profile", new(apis.UserProfile)) g.Put("/user_center/change_password", new(apis.UserChangePassword)) - g.Post("/user_center/set_avatar", new(apis.UploadAvatarImage)) + g.Post("/user_center/upload_avatar", new(apis.UploadAvatarImage)) + g.Put("/user_center/set_avatar", new(apis.SetAvatar)) }) } diff --git a/static/js/application.js b/static/js/application.js index 48dbefd..cf5386a 100644 --- a/static/js/application.js +++ b/static/js/application.js @@ -54,3 +54,22 @@ function put(url, body) { return fetch(url, opts).then(getJson); } + +function getAll(selector) { + return Array.prototype.slice.call(document.querySelectorAll(selector), 0); +} + +$(document).ready(function() { + const $burgers = getAll('.burger'); + + if ($burgers.length > 0) { + $burgers.forEach($el => { + $el.addEventListener('click', () => { + const target = $el.dataset.target; + const $target = document.getElementById(target); + $el.classList.toggle('is-active'); + $target.classList.toggle('is-active'); + }); + }); + } +}); \ No newline at end of file diff --git a/templates/base.html b/templates/base.html index 23056b2..9219f2f 100644 --- a/templates/base.html +++ b/templates/base.html @@ -46,14 +46,14 @@ - -