自己的客服系统做好了,官网页面也有了,但是没有介绍性的内容文章。网站被收录的太少,这样会导致网站的权重不高,搜索排名比较低。
因此要简单的加上一个小型的内容管理功能。
设计数据库
很简单的两张表,分类表和内容表
DROP TABLE IF EXISTS `cms_cate`; CREATE TABLE `cms_cate` ( `id` int(11) NOT NULL AUTO_INCREMENT, `cat_name` varchar(50) NOT NULL DEFAULT '' COMMENT '分类名称', `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', PRIMARY KEY (`id`) COMMENT '自增主键索引' ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT 'CMS分类表'; DROP TABLE IF EXISTS `cms_news`; CREATE TABLE `cms_news` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(500) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '标题', `content` text COLLATE utf8mb4_general_ci COMMENT '内容', `cat_id` int(11) NOT NULL DEFAULT '0' COMMENT '分类ID', `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', PRIMARY KEY (`id`) COMMENT '自增主键索引' ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT 'CMS内容表';
编写数据库操作gorm Model部分
设计两个结构体
type CmsCate struct { Id uint `json:"id"` CatName string `json:"cat_name"` CreatedAt types.Time `json:"created_at"` } type CmsNews struct { Id uint `json:"id"` Title string `json:"title"` Content string `json:"content"` CreatedAt types.Time `json:"created_at"` }
types.Time类型是我对time.Time类型的包装,用于在序列化为json的时候,可以格式化时间
package types import ( "database/sql/driver" "fmt" "time" ) type Time struct { time.Time } func (t Time) MarshalJSON() ([]byte, error) { localTime := t.Format("2006-01-02 15:04:05") return []byte(fmt.Sprintf(`"%s"`, localTime)), nil } func (t Time) Value() (driver.Value, error) { var zeroTime time.Time if t.Time.UnixNano() == zeroTime.UnixNano() { return nil, nil } return t.Time, nil } func (t *Time) Scan(v interface{}) error { value, ok := v.(time.Time) if ok { *t = Time{Time: value} return nil } return fmt.Errorf("can not convert %v to timestamp", v) }
分类表和内容表的增删查改
DB就是*gorm.DB类型,这是在链接数据库的时候,已经赋值好的全局变量
/*内容表*/ //根据条件查询条数 func CountCmsNews(query interface{}, args ...interface{}) uint { var v uint DB.Table("cms_news").Where(query, args...).Count(&v) return v } //根据条件更新 func (this *CmsNews) SaveCmsNews(query interface{}, args ...interface{}) error { db := DB.Table("cms_news").Where(query, args...).Update(this) return db.Error } //增加数据 func (this *CmsNews) AddCmsNews() error { return DB.Create(this).Error } //根据条件查询分页列表 func FindCmsNews(page, pagesize int, query interface{}, args ...interface{}) []CmsNews { offset := (page - 1) * pagesize var res []CmsNews DB.Table("cms_news").Where(query, args...).Order("id desc").Offset(offset).Limit(pagesize).Find(&res) return res } //根据条件删除 func DelCmsNews(query interface{}, args ...interface{}) error { return DB.Where(query, args...).Delete(&CmsNews{}).Error } /*分类表*/ //根据条件分类 func (this *CmsCate) SaveCmsCate(query interface{}, args ...interface{}) error { db := DB.Table("cms_cate").Where(query, args...).Update(this) return db.Error } //增加分类 func (this *CmsCate) AddCmsCate() error { return DB.Create(this).Error } //根据条件查询分类列表 func FindCmsCate(page, pagesize int, query interface{}, args ...interface{}) []CmsCate { offset := (page - 1) * pag