Browse Source

部署

layui
yirenyishi 6 years ago
parent
commit
94580c3d07
  1. 6
      controllers/BlogController.go
  2. 74
      controllers/FileController.go
  3. 2
      controllers/MapController.go
  4. 16
      controllers/UserController.go
  5. 4
      models/Like.go
  6. 1
      routers/router.go
  7. 2
      service/BlogService.go
  8. 10
      service/UserService.go
  9. 1
      static/css/common.css
  10. BIN
      static/img/bg.jpg
  11. BIN
      static/img/like.png
  12. BIN
      static/img/upload.png
  13. 119
      static/js/upload.js
  14. 2
      views/T.me.tpl
  15. 19
      views/T.nav.tpl
  16. 2
      views/blog.html
  17. 2
      views/note.html
  18. 33
      views/uinfo.html
  19. 29
      views/user.html

6
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

74
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
}

2
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

16
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("")

4
models/Like.go

@ -10,3 +10,7 @@ type Like struct {
Blog *Blog `orm:"-"`
}
func (u *Like) TableName() string {
return "tb_like"
}

1
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{})
}

2
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
}

10
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")
}

1
static/css/common.css

@ -1,5 +1,4 @@
body {
/*background: #f5f7f9 url("/static/img/bg.jpg") repeat;*/
background: #f5f7f9;
}

BIN
static/img/bg.jpg

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

BIN
static/img/like.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 556 B

BIN
static/img/upload.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

119
static/js/upload.js

@ -0,0 +1,119 @@
function DragImgUpload(id,options) {
this.me = $(id);
var defaultOpt = {
boxWidth:'120px',
boxHeight:'auto'
}
this.preview = $('<div id="preview"><img src="/static/img/upload.png" class="img-responsive" style="width: 100%;height: auto;" alt="" title=""> </div>');
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();
}
}

2
views/T.me.tpl

@ -1,7 +1,7 @@
{{define "memenu"}}
<div class="me-menu">
<div style="height: 50px;line-height: 50px">
<a href="/u/{{.User.Id}}" target="_blank"><img src="/static/img/2.png" alt="头像" class="img-circle"></a>
<a href="/u/{{.User.Id}}" target="_blank"><img src="{{.HeadImg}}" alt="头像" class="img-circle"></a>
<a href="/u/{{.User.Id}}" target="_blank" style="margin-left: 15px;font-size: 18px;text-decoration: none">{{.User.NickName}}</a>
</div>
<hr style="height:1px;border:none;border-top:1px solid #EEE;margin: 6px;"/>

19
views/T.nav.tpl

@ -20,15 +20,16 @@
<a href="/map" target="_blank">地图</a>
</li>
</ul>
{{/*<form class="navbar-form navbar-left" role="search">*/}}
{{/*<div class="form-group">*/}}
{{/*<input type="text" class="form-control" placeholder="Search">*/}}
{{/*</div>*/}}
{{/*<button type="submit" class="btn btn-default">Submit</button>*/}}
{{/*</form>*/}}
{{/*<form class="navbar-form navbar-left" role="search">*/}}
{{/*<div class="form-group">*/}}
{{/*<input type="text" class="form-control" placeholder="Search">*/}}
{{/*</div>*/}}
{{/*<button type="submit" class="btn btn-default">Submit</button>*/}}
{{/*</form>*/}}
<ul class="nav navbar-nav navbar-right">
{{if .IsLogin }}
<li><a href="/me/blog" style="padding: 0;"><img src="/static/img/2.png" alt="头像" class="img-circle"></a></li>
<li><a href="/me/blog" style="padding: 0;"><img src="{{.HeadImg}}" alt="头像" class="img-circle"></a>
</li>
{{else}}
<li><a href="/login">登录</a></li>
<li><a href="/regist">注册</a></li>
@ -38,9 +39,9 @@
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
{{.NickName}}<span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li><a href="#">个人中心</a></li>
<li><a href="/me/blog">个人中心</a></li>
<li><a href="/note" target="_blank">我的笔记</a></li>
<li><a href="/note" target="_blank">我的首页</a></li>
<li><a href="/u/{{.UserId}}" target="_blank">我的首页</a></li>
<li class="divider"></li>
<li><a href="/logout">安全退出</a></li>
</ul>

2
views/blog.html

@ -7,7 +7,7 @@
{{template "nav" .}}
<div class="blog-user">
<div style="height: 50px;line-height: 50px">
<a href="/u/{{.Blog.User.Id}}"><img src="/static/img/2.png" alt="头像" class="img-circle"></a>
<a href="/u/{{.Blog.User.Id}}"><img src="{{.Blog.User.Headimg}}" alt="头像" class="img-circle"></a>
<a href="/u/{{.Blog.User.Id}}"
style="margin-left: 15px;font-size: 18px;text-decoration: none">{{.Blog.User.NickName}}</a>
</div>

2
views/note.html

@ -50,7 +50,7 @@
<a href="javascript:void(0)" id="newNote">新增笔记</a>
<a href="/me/note" id="newNoteColl">管理文件夹</a>
<a href="javascript:void(0)" id="delNote">删除笔记</a>
<a href="javascript:void(0)">个人中心</a>
<a href="/me/blog" target="_blank">个人中心</a>
</nav>
</nav>
</div> <!-- cd-main-content -->

33
views/uinfo.html

@ -1,6 +1,7 @@
{{template "header"}}
<title>博客 - 个人随笔</title>
<link type="text/css" rel="styleSheet" href="/static/css/me.css"/>
<script type="text/javascript" src="/static/js/upload.js"></script>
</head>
<body>
<div class="root-container">
@ -15,7 +16,8 @@
</ol>
<div style="display: flex">
<div style="width: 128px">
<img src="/static/img/2.png" alt="头像" class="img-circle img-circle-large">
<img src="{{.HeadImg}}" alt="头像" class="img-circle img-circle-large">
<div id="drop_area" style="margin-top: 15px"></div>
</div>
<div class="info-header">
<div style="display: flex">
@ -56,6 +58,35 @@
</div>
</body>
<script>
var dragImgUpload = new DragImgUpload("#drop_area",{
callback:function (files) {
//回调函数,可以传递给后台等等
var file = files[0];
console.log(file.name);
debugger
var formData = new FormData();
formData.append("imgfile",file );
if(!/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(file.name))
{
return false;
}else{
$.ajax({
url: '/himg/upload',
type:'POST',
contentType: false,
processData: false,
data: formData,
success:function(data,textstatus){
if (data.Status == 0){
layer.msg("上传成功", {icon: 6});
}else{
layer.msg("上传失败", {icon: 5});
}
}
});
}
}
})
function refresh() {
window.location.href = window.location.href
}

29
views/user.html

@ -7,7 +7,7 @@
{{template "nav" .}}
<div style="height: 100%;margin: 50px 10% 0 10%;">
<div style="display: flex" class="uinfo">
<img src="/static/img/2.png" alt="头像" class="img-circle" style="width: 120px;height: 120px">
<img src="{{.User.Headimg}}" alt="头像" class="img-circle" style="width: 120px;height: 120px">
<div style="flex: 1;margin-left: 20px;">
{{/*<h1>{{.User.NickName}}</h1>*/}}
<h1 style="margin-top: 20px;margin-bottom: 0">码农笔录</h1>
@ -26,16 +26,37 @@
<hr class="fhr" color=#ffeeeeee SIZE=0.1 style="margin: 0">
<div style="color: #515a6e;height: 30px;line-height: 30px;">
<span>排序: </span>
<span style="margin-left: 15px"><a href="" style="color: #515a6e;text-decoration: none">发布时间</a></span>
<span style="margin-left: 8px"><a href="" style="color: #515a6e;text-decoration: none">访问量</a></span>
<span style="margin-left: 15px"><a href="/u/{{.User.Id}}?num=1&flag=0" style="color: #515a6e;text-decoration: none">发布时间</a></span>
<span style="margin-left: 8px"><a href="/u/{{.User.Id}}?num=1&flag=1" style="color: #515a6e;text-decoration: none">访问量</a></span>
</div>
{{range .Page.List}}
<div class="blog-list">
<p style="margin: 0;font-size: 16px"><a href="">{{.Title}}</a></p>
<p style="margin: 0;font-size: 16px"><a href="/blog/{{.Id}}" target="_blank">{{.Title}}</a></p>
</div>
<hr style="height:1px;border:none;border-top:1px solid #EEE;margin: 0"/>
{{end}}
</div>
<div style="text-align: center">
<nav>
<div class="pagination pagination-lg">
<ul>
{{if .Page.FirstPage}}
{{else}}
<li class="prev">
<a href="/u/{{.User.Id}}?num={{ .Page.PageNo | NAdd -1}}&flag={{.Flag}}">&laquo;</a>
</li>
{{end}}
<li class="active"><a href="javascript:void(0)">{{.Page.PageNo}}</a></li>
{{if .Page.LastPage}}
{{else}}
<li class="next">
<a href="/u/{{.User.Id}}?num={{ .Page.PageNo | NAdd +1}}&flag={{.Flag}}">&raquo;</a>
</li>
{{end}}
</ul>
</div>
</nav>
</div>
</div>
</body>
</html>
Loading…
Cancel
Save