Skip to content

Commit

Permalink
文章管理
Browse files Browse the repository at this point in the history
  • Loading branch information
guyan0319 committed Apr 13, 2020
1 parent 24051d1 commit f336bb3
Show file tree
Hide file tree
Showing 19 changed files with 197 additions and 43 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/.idea/workspace.xml
/vue-element-admin/.idea/workspace.xml
/.idea/inspectionProfiles/Project_Default.xml
/vue-element-admin/node_modules
2 changes: 2 additions & 0 deletions conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Config struct {
Token string
Super string
RedisPre string
Host string
Routes []string
}
var (
Expand All @@ -23,6 +24,7 @@ func Set(cfg Config) {
Cfg.Language=setDefault(cfg.Language,"","cn")
Cfg.Token=setDefault(cfg.Token,"","token")
Cfg.Super=setDefault(cfg.Super,"","admin")//超级账户
Cfg.Host=setDefault(cfg.Host,"","http://localhost:8090")//域名
Cfg.Routes=cfg.Routes
mutex.Unlock()
}
Expand Down
145 changes: 138 additions & 7 deletions ctrl/article/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package article
import (
"fmt"
"github.com/gin-gonic/gin"
"go-admin/conf"
"go-admin/models"
"go-admin/modules/request"
"go-admin/modules/response"
"go-admin/public/common"
"strconv"
"strings"
)

func Create(c *gin.Context) {
Expand Down Expand Up @@ -67,11 +69,12 @@ func Create(c *gin.Context) {
}
//fmt.Println(data)
model :=models.SystemArticle{}
model.ImageUrl,_=common.WriteFile("./upload",data["image_uri"].(string))
model.ImageUri,_=common.WriteFile("./upload",data["image_uri"].(string))
model.Title=data["title"].(string)
model.Content=data["content"].(string)
model.Content=common.Base64Content(conf.Cfg.Host+"/showimage?imgname=upload/","./upload",model.Content)
model.ContentShort=data["content_short"].(string)
model.SourceUrl=data["source_uri"].(string)
model.SourceUri=data["source_uri"].(string)

model.Importance=int(data["importance"].(float64))
status:=int(data["status"].(float64))
Expand All @@ -81,9 +84,9 @@ func Create(c *gin.Context) {
if data["comment_disabled"].(bool) {
model.CommentDisabled =1
}
model.Ptime = common.StrToTimes(data["display_time"].(string))
model.Ctime=int(model.Ptime.Unix())
model.Mtime=model.Ptime
model.Display_time = common.StrToTimes(data["display_time"].(string))
model.Ctime=int(model.Display_time.Unix())
model.Mtime=model.Display_time
user :=models.SystemUser{Name:data["author"].(string)}
has :=user.GetRow()
if !has {
Expand All @@ -93,7 +96,103 @@ func Create(c *gin.Context) {
model.Author=user.Id
_,err=model.Add()
if err!=nil {
fmt.Println(err)
common.ShowMsg("fail")
return
}
response.ShowData(c,model)
return
}

func Edit(c *gin.Context) {
data,err:=request.GetJson(c)
if err != nil {
response.ShowError(c, "fail")
return
}
if _, ok := data["id"]; !ok {
response.ShowError(c, "fail")
return
}
article:=models.SystemArticle{}
article.Id = int64(data["id"].(float64))
has:=article.GetRow()
if !has {
response.ShowError(c,"article_error")
return
}

if _, ok := data["title"]; !ok {
response.ShowError(c, "fail")
return
}
if _, ok := data["content"]; !ok {
response.ShowError(c, "fail")
return
}
if _, ok := data["content_short"]; !ok {
response.ShowError(c, "fail")
return
}
if _, ok := data["source_uri"]; !ok {
response.ShowError(c, "fail")
return
}
if _, ok := data["image_uri"]; !ok {
response.ShowError(c, "fail")
return
}
if _, ok := data["display_time"]; !ok {
response.ShowError(c, "fail")
return
}
if _, ok := data["comment_disabled"]; !ok {
response.ShowError(c, "fail")
return
}
if _, ok := data["importance"]; !ok {
response.ShowError(c, "fail")
return
}
if _, ok := data["author"]; !ok {
response.ShowError(c, "fail")
return
}
if data["title"].(string) ==""{
response.ShowErrorParams(c, "title")
return
}
if data["image_uri"].(string) ==""{
response.ShowErrorParams(c, "image_uri")
return
}
model :=models.SystemArticle{Id:article.Id}
if !strings.Contains(data["image_uri"].(string),"http://"){
model.ImageUri,_=common.WriteFile("./upload",data["image_uri"].(string))
}
model.Title=data["title"].(string)
model.Content=data["content"].(string)
model.ContentShort=data["content_short"].(string)
model.SourceUri=data["source_uri"].(string)
//
model.Importance=int(data["importance"].(float64))
status:=int(data["status"].(float64))
if status ==1{
model.Status=1
}
if data["comment_disabled"].(bool) {
model.CommentDisabled =1
}
model.Display_time = common.StrToTimes(data["display_time"].(string))

user :=models.SystemUser{Name:data["author"].(string)}
has =user.GetRow()
if !has {
common.ShowMsg("fail")
return
}
model.Author=user.Id
err=model.Update()
if err!=nil {
common.ShowMsg("fail")
return
}
Expand All @@ -117,7 +216,6 @@ func Index(c *gin.Context) {
if !has {
continue
}
fmt.Println("aa")
articlePageArr = append(articlePageArr,models.SystemArticlePage{SystemArticle:v,AuthorName:userModel.Name})
}
if err != nil {
Expand All @@ -129,4 +227,37 @@ func Index(c *gin.Context) {
data["total"]=paging.Total
response.ShowData(c, data)
return
}
func ShowImage(c *gin.Context){
imgName := c.Query("imgname")
fmt.Println(imgName)
c.File(imgName)
}
func Detail(c *gin.Context){
id,has:=c.GetQuery("id")
if !has{
response.ShowErrorParams(c, "id")
return
}
model:=models.SystemArticle{}
model.Id, _ = strconv.ParseInt(id, 10, 64)
has=model.GetRow()
if !has {
response.ShowError(c,"article_error")
return
}
articles:=models.SystemArticlePage{}
articles.SystemArticle=model
userModel := models.SystemUser{}
userModel.Id=model.Author
has=userModel.GetRow()
if has {
articles.AuthorName=userModel.Name
}
if articles.ImageUri!="" {

articles.ImageUri=conf.Cfg.Host+"/showimage?imgname=upload/"+articles.ImageUri
}
response.ShowData(c, articles)
return
}
18 changes: 10 additions & 8 deletions data/systemdb.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-- https://www.phpmyadmin.net/
--
-- 主机: localhost
-- 生成日期: 2020-04-08 09:34:31
-- 生成日期: 2020-04-13 23:20:08
-- 服务器版本: 8.0.12
-- PHP 版本: 7.3.4

Expand Down Expand Up @@ -36,21 +36,23 @@ CREATE TABLE `system_article` (
`title` varchar(200) NOT NULL DEFAULT '' COMMENT '标题',
`content` text NOT NULL COMMENT '内容',
`content_short` varchar(500) NOT NULL DEFAULT '' COMMENT '摘要',
`source_url` varchar(200) NOT NULL DEFAULT '' COMMENT '来源',
`source_uri` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '来源',
`ctime` int(11) NOT NULL DEFAULT '0' COMMENT '创建时间',
`image_url` varchar(200) NOT NULL DEFAULT '' COMMENT '图片',
`image_uri` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '图片',
`comment_disabled` tinyint(4) NOT NULL DEFAULT '0' COMMENT '是否展示评论',
`ptime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '发布时间',
`display_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '发布时间',
`mtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间'
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

--
-- 转存表中的数据 `system_article`
--

INSERT INTO `system_article` (`id`, `author`, `importance`, `status`, `title`, `content`, `content_short`, `source_url`, `ctime`, `image_url`, `comment_disabled`, `ptime`, `mtime`) VALUES
(1, 1, 2, 1, 'fasd', '<p>fasdffasdf</p>', 'a', '', 1585670400, '20200407/ftxqfEY9EjWGyHS7CFaDWkYYh2SLYUIY.jpeg', 0, '2020-04-01 00:00:00', '2020-03-31 16:00:00'),
(2, 1, 2, 1, 'fasd', '<p>fasdffasdf</p>', 'a', 'https://www.baidu.com/', 1585670400, '20200407/jbsW7XXW5nFcj6rNEydZhs9ma4BerLpB.jpeg', 0, '2020-04-01 00:00:00', '2020-03-31 16:00:00');
INSERT INTO `system_article` (`id`, `author`, `importance`, `status`, `title`, `content`, `content_short`, `source_uri`, `ctime`, `image_uri`, `comment_disabled`, `display_time`, `mtime`) VALUES
(1, 1, 2, 1, 'fasd', '<p>fasdffasdf</p>', 'a发生的发生', '', 1585670400, '20200407/ftxqfEY9EjWGyHS7CFaDWkYYh2SLYUIY.jpeg', 0, '2020-04-10 00:00:00', '2020-04-09 15:14:31'),
(2, 1, 2, 1, 'fasd', '<p>fasdffasdf</p>', 'a', 'https://www.baidu.com/', 1585670400, '20200407/jbsW7XXW5nFcj6rNEydZhs9ma4BerLpB.jpeg', 0, '2020-04-01 00:00:00', '2020-03-31 16:00:00'),
(3, 1, 2, 1, '测试', '<p><img class=\"wscnph\" src=\"http://localhost:8090/showimage?imgname=upload/20200413/8GMq0TEmC7rZcXLrDoSM0gNEYsUcdc6z.png\" />asdfasdf<img class=\"wscnph\" src=\"http://localhost:8090/showimage?imgname=upload/20200413/CBgqld3ZFuR4uBs3w9XJM9xPtQlhjflT.png\" /></p>', 'fasd', '', 1586707200, '20200413/yjJiBlZMs42Ag1YOWxW6PuuBU3Ut9XSe.png', 0, '2020-04-13 00:00:00', '2020-04-13 15:06:59'),
(4, 1, 2, 1, '测试', '<p><img class=\"wscnph\" src=\"http://localhost:8090/showimage?imgname=upload/20200413/y0hBc2CFylSBcxIFOFhFkloXewmrViVg.png\" />asdfasdf<img class=\"wscnph\" src=\"http://localhost:8090/showimage?imgname=upload/20200413/wXVgmVtr5WLdV6BMR6frCOkqAU5XElW9.png\" /></p>', 'fasd', '', 1586707200, '20200413/SRiwahWF5RqPZWMrRUeT3CmX5Bxveyko.png', 0, '2020-04-13 00:00:00', '2020-04-12 16:00:00');

-- --------------------------------------------------------

Expand Down Expand Up @@ -270,7 +272,7 @@ ALTER TABLE `system_user_role`
-- 使用表AUTO_INCREMENT `system_article`
--
ALTER TABLE `system_article`
MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键', AUTO_INCREMENT=3;
MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键', AUTO_INCREMENT=5;

--
-- 使用表AUTO_INCREMENT `system_log`
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ func main() {
r.POST("/login", user.Login)
r.POST("/reg", user.Reg)
r.POST("/article/create", article.Create)
r.POST("/article/edit", article.Edit)
r.GET("/article/list", article.Index)
r.GET("/article/detail", article.Detail)
r.GET("/showimage", article.ShowImage)

r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
Expand Down
13 changes: 10 additions & 3 deletions models/system_article.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ type SystemArticle struct {
Title string `json:"title" xorm:"not null default '' comment('标题') VARCHAR(200)"`
Content string `json:"content" xorm:"not null comment('内容') TEXT"`
ContentShort string `json:"content_short" xorm:"not null default '' comment('摘要') VARCHAR(500)"`
SourceUrl string `json:"source_url" xorm:"not null default '' comment('来源') VARCHAR(200)"`
SourceUri string `json:"source_uri" xorm:"not null default '' comment('来源') VARCHAR(200)"`
Ctime int `json:"ctime" xorm:"not null default 0 comment('创建时间') INT(11)"`
ImageUrl string `json:"image_url" xorm:"not null default '' comment('图片') VARCHAR(200)"`
ImageUri string `json:"image_uri" xorm:"not null default '' comment('图片') VARCHAR(200)"`
CommentDisabled int `json:"comment_disabled" xorm:"not null default 0 comment('是否展示评论') TINYINT(4)"`
Ptime time.Time `json:"ptime" xorm:"not null default 'CURRENT_TIMESTAMP' comment('发布时间') DATETIME"`
Display_time time.Time `json:"display_time" xorm:"not null default 'CURRENT_TIMESTAMP' comment('发布时间') DATETIME"`
Mtime time.Time `json:"mtime" xorm:"not null default 'CURRENT_TIMESTAMP' comment('修改时间') TIMESTAMP"`
}
type SystemArticlePage struct {
Expand Down Expand Up @@ -50,3 +50,10 @@ func (u *SystemArticle) GetAllPage(paging *common.Paging)([]SystemArticle,error)
err=mEngine.Where("status=?",1).Limit(int(paging.PageSize),int(paging.StartNums)).Find(&systemarticles)
return systemarticles,err
}
func (a *SystemArticle) Update() error {
if _, err := mEngine.Where("id = ?", a.Id).Update(a); err != nil {
return err
}
return nil
}

1 change: 1 addition & 0 deletions modules/lang/cn.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var cn = map[string]string{
"not_exists": "不存在",
"nologin": "未登录",
"user_error": "用户不存在",
"article_error": "文章不存在",
"name_exists": "用户名已存在",
"role_error": "角色不存在",
"unauthorized": "账户未授权",
Expand Down
13 changes: 13 additions & 0 deletions public/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@ func WriteFile(path string, content string) (string, bool) {
}
return relative, true
}
func Base64Content(url,path, content string) string {
reg := regexp.MustCompile(`data:\s*image\/(\w+);base64,[\w\d+/=]*[=|==]`)
imageArr:= reg.FindAllString(content,-1)

for _,v:=range imageArr{
imgPath,res:=WriteFile(path,v)
if!res{
continue
}
content = strings.Replace(content, v, url+imgPath, 1)
}
return content
}
func Contain(obj interface{}, target interface{}) (bool, error) {
targetValue := reflect.ValueOf(target)
switch reflect.TypeOf(target).Kind() {
Expand Down
21 changes: 9 additions & 12 deletions test1.go

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 6 additions & 7 deletions vue-element-admin/src/api/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ export function fetchList(query) {

export function fetchArticle(id) {
return request({
url: '/article/detail',
url: '',
method: 'get',
params: { id }
params: { id },
baseURL: httphost + '/article/detail'
})
}

Expand All @@ -27,9 +28,6 @@ export function fetchPv(pv) {

export function createArticle(data) {
return request({
// url: '/article/create',
// method: 'post',
// data
url: '',
method: 'post',
data,
Expand All @@ -39,8 +37,9 @@ export function createArticle(data) {

export function updateArticle(data) {
return request({
url: '/article/update',
url: '',
method: 'post',
data
data,
baseURL: httphost + '/article/edit'
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,8 @@ export default {
fetchData(id) {
fetchArticle(id).then(response => {
this.postForm = response.data
// just for test
this.postForm.title += ` Article Id:${this.postForm.id}`
this.postForm.content_short += ` Article Id:${this.postForm.id}`
this.postForm.comment_disabled = response.data.comment_disabled==0?false:true
this.postForm.author=response.data.authorname
// set tagsview title
this.setTagsViewTitle()
Expand Down
2 changes: 1 addition & 1 deletion vue-element-admin/src/views/article/list/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<el-table-column width="180px" align="center" label="Date">
<template slot-scope="scope">
<span>{{ scope.row.ptime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
<span>{{ scope.row.display_time | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
</template>
</el-table-column>

Expand Down

0 comments on commit f336bb3

Please sign in to comment.