diff --git a/controllers/BlogController.go b/controllers/BlogController.go index 5e478e4..58d5f94 100644 --- a/controllers/BlogController.go +++ b/controllers/BlogController.go @@ -111,7 +111,10 @@ func (this *BlogController) Get() { this.Data["Top"] = blogs } this.Data["Blog"] = blog + this.Data["UserId"] = this.GetSession("userid") + this.Data["HeadImg"] = this.GetSession("headimg") this.Data["NickName"] = this.GetSession("nickname") + this.Data["UserId"] = this.GetSession("userid") this.Data["IsLogin"] = this.GetSession("nickname") != nil this.TplName = "blog.html" service.CountBrows(blog.UserId) @@ -188,7 +191,8 @@ func (this *BlogController) BlogsPage() { this.Redirect("/500", 302) return } - + this.Data["UserId"] = this.GetSession("userid") + this.Data["HeadImg"] = this.GetSession("headimg") this.Data["NickName"] = this.GetSession("nickname") this.Data["IsLogin"] = this.GetSession("nickname") != nil this.Data["Page"] = pages diff --git a/controllers/FileController.go b/controllers/FileController.go index a24c182..bb9099e 100644 --- a/controllers/FileController.go +++ b/controllers/FileController.go @@ -9,6 +9,7 @@ import ( "crypto/md5" "math/rand" "beeblog/models" + "beeblog/service" ) type FileController struct { @@ -17,7 +18,8 @@ type FileController struct { func (this *FileController) Upload() { filename := "" - for filename, _ = range this.Ctx.Request.MultipartForm.File {} + for filename, _ = range this.Ctx.Request.MultipartForm.File { + } f, h, filerr := this.GetFile(filename) //获取上传的文件 if filerr != nil { fmt.Println("err:", filerr) @@ -59,7 +61,75 @@ func (this *FileController) Upload() { this.ServeJSON() return } - this.Data["json"] = models.ReurnData("",fpath) + this.Data["json"] = models.ReurnData("", fpath) + this.ServeJSON() + return +} + +func (this *FileController) HeadImgUpload() { + uid := this.GetSession("userid") + if uid == nil { + this.Data["json"] = models.ReurnError(401, "") + this.ServeJSON() + return + } + filename := "imgfile" + f, h, filerr := this.GetFile(filename) //获取上传的文件 + if filerr != nil { + fmt.Println("err:", filerr) + this.Data["json"] = models.ReurnError(500, "后缀名不符合上传要求") + this.ServeJSON() + return + } + ext := path.Ext(h.Filename) + //验证后缀名是否符合要求 + var AllowExtMap map[string]bool = map[string]bool{ + ".jpg": true, + ".jpeg": true, + ".gif": true, + ".png": true, + ".GIF": true, + ".JPG": true, + ".PNG": true, + } + if _, ok := AllowExtMap[ext]; !ok { + this.Data["json"] = models.ReurnError(500, "后缀名不符合上传要求") + this.ServeJSON() + return + } + //创建目录 + urlDir := time.Now().Format("2006/01/02/") + uploadDir := "/opt/filetom/webapps/itstack/" + urlDir + err := os.MkdirAll(uploadDir, 777) + if err != nil { + this.Data["json"] = models.ReurnError(500, "") + this.ServeJSON() + return + } + //构造文件名称 + rand.Seed(time.Now().UnixNano()) + randNum := fmt.Sprintf("%d", rand.Intn(9999)+1000) + hashName := md5.Sum([]byte( time.Now().Format("2006_01_02_15_04_05_") + randNum )) + fileName := fmt.Sprintf("%x", hashName) + ext + fpath := uploadDir + fileName + urlDir += fileName + defer f.Close() //关闭上传的文件,不然的话会出现临时文件不能清除的情况 + err = this.SaveToFile(filename, fpath) + if err != nil { + fmt.Println(err) + this.Data["json"] = models.ReurnError(500, "") + this.ServeJSON() + return + } + urlDir = "https://aiprose.com/foss/" + urlDir + user := &models.User{Id: uid.(int64), Headimg: urlDir} + if _, err = service.EditHeadImg(user); err != nil { + this.Data["json"] = models.ReurnError(500, "") + this.ServeJSON() + return + } + this.SetSession("headimg", urlDir) + this.Data["json"] = models.ReurnData("", urlDir) this.ServeJSON() return } diff --git a/controllers/MapController.go b/controllers/MapController.go index 4e15142..e6280b0 100644 --- a/controllers/MapController.go +++ b/controllers/MapController.go @@ -7,6 +7,8 @@ type MapController struct { } func (this *MapController) Get() { + this.Data["UserId"] = this.GetSession("userid") + this.Data["HeadImg"] = this.GetSession("headimg") this.Data["NickName"] = this.GetSession("nickname") this.Data["IsLogin"] = this.GetSession("nickname") != nil this.Data["IsMap"] = true diff --git a/controllers/UserController.go b/controllers/UserController.go index 0bb55e5..1545830 100644 --- a/controllers/UserController.go +++ b/controllers/UserController.go @@ -40,6 +40,10 @@ func (this *UserController) UserInfo() { this.Data["Page"] = page } this.Data["User"] = user + this.Data["UserId"] = this.GetSession("userid") + this.Data["HeadImg"] = this.GetSession("headimg") + this.Data["NickName"] = this.GetSession("nickname") + this.Data["IsLogin"] = this.GetSession("nickname") != nil this.TplName = "user.html" return } @@ -66,6 +70,8 @@ func (this *UserController) PersonBlog() { this.Redirect("/500", 302) return } + this.Data["UserId"] = this.GetSession("userid") + this.Data["HeadImg"] = this.GetSession("headimg") this.Data["NickName"] = this.GetSession("nickname") this.Data["IsLogin"] = this.GetSession("nickname") != nil this.Data["Page"] = page @@ -103,6 +109,8 @@ func (this *UserController) PersonNote() { return } } + this.Data["UserId"] = this.GetSession("userid") + this.Data["HeadImg"] = this.GetSession("headimg") this.Data["NickName"] = this.GetSession("nickname") this.Data["IsLogin"] = this.GetSession("nickname") != nil this.Data["Note"] = notColl @@ -134,6 +142,8 @@ func (this *UserController) PersonLike() { return } } + this.Data["UserId"] = this.GetSession("userid") + this.Data["HeadImg"] = this.GetSession("headimg") this.Data["NickName"] = this.GetSession("nickname") this.Data["IsLogin"] = this.GetSession("nickname") != nil this.Data["Page"] = page @@ -153,8 +163,10 @@ func (this *UserController) PersonInfo() { this.Redirect("/500", 302) return } - this.Data["NickName"] = this.GetSession("nickname") this.Data["IsLogin"] = this.GetSession("nickname") != nil + this.Data["UserId"] = this.GetSession("userid") + this.Data["HeadImg"] = this.GetSession("headimg") + this.Data["NickName"] = this.GetSession("nickname") this.Data["IsMeInfo"] = true this.Data["User"] = user this.TplName = "uinfo.html" @@ -227,6 +239,7 @@ func (this *UserController) Login() { this.Data["json"] = models.ReurnSuccess("") this.SetSession("userid", user.Id) this.SetSession("nickname", user.NickName) + this.SetSession("headimg", user.Headimg) } else { this.Data["json"] = models.ReurnError(1, "用户名或密码错误") } @@ -275,6 +288,7 @@ func (this *UserController) Regist() { h.Write([]byte(userpwd + salt)) userpwd = hex.EncodeToString(h.Sum(nil)) user = &models.User{UserName: username, NickName: username, UserPwd: userpwd, Salt: salt} + user.Headimg = "https://www.aiprose.com/foss/timg.jpg" err := service.SaveUser(user) if err == nil { this.Data["json"] = models.ReurnSuccess("") diff --git a/models/Like.go b/models/Like.go index 714fb11..46b4610 100644 --- a/models/Like.go +++ b/models/Like.go @@ -10,3 +10,7 @@ type Like struct { Blog *Blog `orm:"-"` } + +func (u *Like) TableName() string { + return "tb_like" +} \ No newline at end of file diff --git a/routers/router.go b/routers/router.go index 8229895..f990467 100644 --- a/routers/router.go +++ b/routers/router.go @@ -9,5 +9,6 @@ func init() { beego.Router("/", &controllers.IndexController{}) beego.Router("/map", &controllers.MapController{}) beego.Router("/file/upload", &controllers.FileController{}, "post:Upload") + beego.Router("/himg/upload", &controllers.FileController{}, "post:HeadImgUpload") beego.Include(&controllers.PageController{}) } diff --git a/service/BlogService.go b/service/BlogService.go index 385065d..bd91519 100644 --- a/service/BlogService.go +++ b/service/BlogService.go @@ -43,7 +43,7 @@ func EditBlogBrows(id int64) { func TopBlogByUser(uid int64) ([]*models.Blog, error) { o := orm.NewOrm() var blogs []*models.Blog - o.QueryTable(models.Blog{}).Filter("UserId", uid).Limit(12, 0).OrderBy("-Browses").All(&blogs) + o.QueryTable(models.Blog{}).Filter("Delflag", 0).Filter("UserId", uid).Limit(12, 0).OrderBy("-Browses").All(&blogs) return blogs, nil } diff --git a/service/UserService.go b/service/UserService.go index 62bffa2..c70f1b5 100644 --- a/service/UserService.go +++ b/service/UserService.go @@ -44,7 +44,7 @@ func SaveUser(user *models.User) error { func CountBlog(uid int64) { o := orm.NewOrm() browses := 0 - o.Raw("UPDATE `auth_user` SET `blog_count` = (SELECT count(id) FROM blog where delflag = 0 and user_id =? ) WHERE `id` = ? ", uid, uid).QueryRow(&browses) + o.Raw("UPDATE `auth_user` SET `blog_count` = (SELECT count(id) num FROM blog where delflag = 0 and user_id =? ) WHERE `id` = ? ", uid, uid).QueryRow(&browses) return } func CountBrows(uid int64) { @@ -56,16 +56,20 @@ func CountBrows(uid int64) { func CountComments(uid int64) { o := orm.NewOrm() browses := 0 - o.Raw("UPDATE `auth_user` SET `blog_comment` = (select count(id) from comment where cuser_id = ?) WHERE `id` = ? ", uid, uid).QueryRow(&browses) //获取总条数 + o.Raw("UPDATE `auth_user` SET `blog_comment` = (select count(id) num from comment where cuser_id = ?) WHERE `id` = ? ", uid, uid).QueryRow(&browses) //获取总条数 return } func CountLike(uid int64) { o := orm.NewOrm() browses := 0 - o.Raw("UPDATE `auth_user` SET `blog_like` = (select count(id) from like where user_id = ?) WHERE `id` = ?", uid, uid).QueryRow(&browses) + o.Raw("UPDATE `auth_user` SET `blog_like` = (select count(id) num from tb_like where user_id = ?) WHERE `id` = ?", uid, uid).QueryRow(&browses) return } func EditUser(user *models.User) (int64, error) { return orm.NewOrm().Update(user) } + +func EditHeadImg(user *models.User) (int64, error) { + return orm.NewOrm().Update(user,"Headimg") +} diff --git a/static/css/common.css b/static/css/common.css index 3baf200..27216d1 100644 --- a/static/css/common.css +++ b/static/css/common.css @@ -1,5 +1,4 @@ body { - /*background: #f5f7f9 url("/static/img/bg.jpg") repeat;*/ background: #f5f7f9; } diff --git a/static/img/bg.jpg b/static/img/bg.jpg deleted file mode 100644 index 3e14d6e..0000000 Binary files a/static/img/bg.jpg and /dev/null differ diff --git a/static/img/like.png b/static/img/like.png new file mode 100644 index 0000000..4bfa8bb Binary files /dev/null and b/static/img/like.png differ diff --git a/static/img/upload.png b/static/img/upload.png new file mode 100644 index 0000000..0db1dd7 Binary files /dev/null and b/static/img/upload.png differ diff --git a/static/js/upload.js b/static/js/upload.js new file mode 100644 index 0000000..df0f62f --- /dev/null +++ b/static/js/upload.js @@ -0,0 +1,119 @@ +function DragImgUpload(id,options) { + this.me = $(id); + var defaultOpt = { + boxWidth:'120px', + boxHeight:'auto' + } + this.preview = $('
'); + this.opts=$.extend(true, defaultOpt,{ + }, options); + this.init(); + this.callback = this.opts.callback; +} + +//定义原型方法 +DragImgUpload.prototype = { + init:function () { + this.me.append(this.preview); + this.me.append(this.fileupload); + this.cssInit(); + this.eventClickInit(); + }, + cssInit:function () { + this.me.css({ + 'width':this.opts.boxWidth, + 'height':this.opts.boxHeight, + 'border':'1px solid #cccccc', + 'padding':'10px', + 'cursor':'pointer' + }) + this.preview.css({ + 'height':'100%', + 'overflow':'hidden' + }) + + }, + onDragover:function (e) { + e.stopPropagation(); + e.preventDefault(); + e.dataTransfer.dropEffect = 'copy'; + }, + onDrop:function (e) { + var self = this; + e.stopPropagation(); + e.preventDefault(); + var fileList = e.dataTransfer.files; //获取文件对象 + // do something upload + if(fileList.length == 0){ + return false; + } + //检测文件是不是图片 + if(fileList[0].type.indexOf('image') === -1){ + alert("您拖的不是图片!"); + return false; + } + + //拖拉图片到浏览器,可以实现预览功能 + var img = window.URL.createObjectURL(fileList[0]); + var filename = fileList[0].name; //图片名称 + var filesize = Math.floor((fileList[0].size)/1024); + if(filesize>500){ + alert("上传大小不能超过500K."); + return false; + } + + self.me.find("img").attr("src",img); + self.me.find("img").attr("title",filename); + if(this.callback){ + this.callback(fileList); + } + }, + eventClickInit:function () { + var self = this; + this.me.unbind().click(function () { + self.createImageUploadDialog(); + }) + var dp = this.me[0]; + dp.addEventListener('dragover', function(e) { + self.onDragover(e); + }); + dp.addEventListener("drop", function(e) { + self.onDrop(e); + }); + + + }, + onChangeUploadFile:function () { + var fileInput = this.fileInput; + var files = fileInput.files; + var file = files[0]; + var img = window.URL.createObjectURL(file); + var filename = file.name; + this.me.find("img").attr("src",img); + this.me.find("img").attr("title",filename); + if(this.callback){ + this.callback(files); + } + }, + createImageUploadDialog:function () { + var fileInput = this.fileInput; + if (!fileInput) { + //创建临时input元素 + fileInput = document.createElement('input'); + //设置input type为文件类型 + fileInput.type = 'file'; + //设置文件name + fileInput.name = 'ime-images'; + //允许上传多个文件 + fileInput.multiple = true; + fileInput.onchange = this.onChangeUploadFile.bind(this); + this.fileInput = fileInput; + } + //触发点击input点击事件,弹出选择文件对话框 + fileInput.click(); + } + + + + +} \ No newline at end of file diff --git a/views/T.me.tpl b/views/T.me.tpl index 05bb1c3..77d9a11 100644 --- a/views/T.me.tpl +++ b/views/T.me.tpl @@ -1,7 +1,7 @@ {{define "memenu"}}
- 头像 + 头像 {{.User.NickName}}

diff --git a/views/T.nav.tpl b/views/T.nav.tpl index 8f046bf..7efb49c 100644 --- a/views/T.nav.tpl +++ b/views/T.nav.tpl @@ -20,15 +20,16 @@ 地图 - {{/**/}} + {{/**/}}