go+beego框架项目中实现发布文章信息功能流程步骤
接上一篇博文,在我的项目中开发发布文章功能
需求:
发布文章功能分两个插入步骤:
1.插入文章信息
2.拆分标签,循环插入标签信息
步骤:
一.路由
//创建文章
beego.Router("/addart", &controllers.ArtController{}, "get:Addart;post:Addart")
二.模型
1.文章模型
package models
import (
"github.com/astaxie/beego/orm"
)
//art表需显示的字段
type Art struct {
Art_id int `orm:"column(art_id);pk"` // 设置主键
Title string
Content string
Cat_id int
Author string
User_id int
Tags string
Pubtime string
Is_state int
Lastup int
Comm int
View int
}
//实例
func init() {
orm.RegisterModel(new(Art))
}
//方法 编辑
func EditArt(art *Art) (int64, error) {
o := orm.NewOrm()
id, err := o.Update(art)
return id, err
}
//方法 添加
func AddArt(art *Art) (int64, error) {
o := orm.NewOrm()
//art := new(Art)
//art.Profile = profile
//art.Name = "slene"
id, err := o.Insert(art)
return id, err
}
2.标签模型
package models
import (
"github.com/astaxie/beego/orm"
)
//tag表需显示的字段
type Tag struct {
Tag_id int `orm:"column(tag_id);pk"` // 设置主键
Art_id int
Tagname string
Addtime string
}
//实例
func init() {
orm.RegisterModel(new(Tag))
}
//方法 编辑
func EditTag(tag *Tag) (int64, error) {
o := orm.NewOrm()
id, err := o.Update(tag)
return id, err
}
//方法 添加
func AddTag(tag *Tag) (int64, error) {
o := orm.NewOrm()
id, err := o.Insert(tag)
return id, err
}
三.控制器
package controllers
import (
"fmt"
"gblog/models"
"strconv"
"strings"
"time"
"github.com/astaxie/beego/orm"
)
type ArtController struct {
BaseController
}
...
//添加数据
func (c *ArtController) Addart() {
//1.拿到数据
title := c.GetString("title")
content := c.GetString("content")
tags := c.GetString("tags")
cat_id, _ := strconv.Atoi(c.GetString("cat_id"))
//session user信息
username := c.GetSession("username").(string)
//接口类型interface int64转int
userid := int(c.GetSession("userid").(int64))
if c.Ctx.Request.Method == "POST" {
//2.对数据进行校验
if title == "" && content == "" {
c.ErrorJson(500, "标题/内容不能为空", nil)
}
//获取时间戳
s := time.Now().Unix()
timestamp := strconv.FormatInt(int64(s), 10)
art := models.Art{Title: title, Content: content, Cat_id: cat_id, Author: username, User_id: userid, Tags: tags, Pubtime: timestamp, Is_state: 0}
id, err := models.AddArt(&art)
if err != nil {
c.ErrorJson(500, "发布失败", nil)
}
//插入标签
ss := strings.Split(tags, ",")
for i := 0; i < len(ss); i++ {
tag := models.Tag{Art_id: int(id), Tagname: ss[i], Addtime: timestamp}
models.AddTag(&tag)
}
//改成事务方式
fmt.Println(art, id, err)
c.SuccessJson("发布成功")
}
fmt.Println("6666", title, cat_id, content, tags)
c.TplName = "addart.html"
}
四.视图
web编辑器我直接用的wangEditor很方便
{{template "public/header.html" .}}
<div class="row clearfix">
<div class="col-md-8 column">
<ul class="breadcrumb">
<li><a href="/">首页</a></li><li><a href="/user">个人中心</a></li><li class="active">发布文章</li>
</ul>
<div>
<p class="h-block"><strong>发布文章:</strong>
<form class="h-block" id="myForm" method="post" action="/addart" role="form">
<div class="form-group">
<span id="check_art" style="font-size: 8px;color: red;display: none;padding-left: 40%;"></span>
<label>标题:</label><input type="text" name="title" value="" class="form-control">
<label>栏目:</label>
<select name="cat_id" class="form-control">
{{range $ind, $elem := .cats}}
<option value="{{$elem.cat_id}}" >{{$elem.catname}}</option>
{{end}}
</select>
<label >内容:</label>
<div id="div-demo"></div>
<textarea name="content" id="text1" style="display: none"></textarea>
<label>标签: ( 用英文逗号分隔 比如: 标签a , 标签b )</label>
<input type="text" name="tags" value="" class="form-control">
</div>
<button type="submit" class="btn btn-default">提交</button>
</form>
</p>
</div>
</div>
<div class="col-md-4 column">
</div>
</div>
{{template "public/footer.html" .}}
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/w[email protected]/dist/wangEditor.min.js"></script>
<script type="text/javascript">
$(function (){
var E = window.wangEditor
if (document.getElementById('div-demo')) {
var editor = new E('#div-demo')
// 配置菜单栏,设置不需要的菜单
editor.config.excludeMenus = [
'italic',
'underline',
'strikeThrough',
'indent',
'emoticon',
'video'
]
editor.config.uploadImgShowBase64 = true
editor.config.height = 200
const $text1 = $('#text1')
editor.config.onchange = function (html) {
// 第二步,监控变化,同步更新到 textarea
$text1.val(html)
}
editor.create()
// 第一步,初始化 textarea 的值
$text1.val(editor.txt.html())
}
});
</script>
<script type="text/javascript">
$(document).ready(function() {
// bind form using ajaxForm
$('#myForm').ajaxForm({
dataType: 'json',
// success: processJson
success: function(data){
if (data.Code == 500) {console.log(data);
alert(data.Msg);
}else{
$("#check_art").html(data.Data);
$("#check_art").css("display","block");
};
}
});
});
</script>
效果图:
相关文章