Skip to content

Commit

Permalink
每天凌晨自动更新站点地图(sitemap)
Browse files Browse the repository at this point in the history
  • Loading branch information
TruthHun88 committed Sep 21, 2019
1 parent fa6c3c0 commit 5971fbd
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
- [x] 文档上传过程中临时文件命名出现重名的问题
- [x] 后台配置了备案号前台无法显示的问题(不知道是什么时候写死在模板里了)
- [x] 是否允许上传重复文档(管理后台 -> 系统设置 进行设置)
- [x] 每天凌晨 2:00 自动更新站点地图(sitemap)

## DocHub v2.3
- [ ] 用户注册和登录成功之后的跳转优化
Expand Down
1 change: 1 addition & 0 deletions models/Models.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func Init() {
}
//安装初始数据
install()
AutoSitemap()
}

//注册数据库
Expand Down
26 changes: 24 additions & 2 deletions models/SeoModel.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,28 @@ func GetTableSeo() string {
return getTable("seo")
}

// 自动更新站点地图
func AutoSitemap() {
go func() {
seo := NewSeo()
file := filepath.Join(helper.RootPath, "sitemap.xml")
for {
now := time.Now()
targetTime, _ := time.ParseInLocation("2006-01-02 15:04:05", now.Format("2006-01-02")+" 02:00:00", time.Local)
info, err := os.Stat(file)
if err != nil {
seo.BuildSitemap()
} else {
// 当前时间大于目标时间,并且文件更新时间小于目标时间
if now.Sub(targetTime) > 0 && info.ModTime().Sub(targetTime) < 0 && NewSys().GetByField("AutoSitemap").AutoSitemap {
seo.BuildSitemap()
}
}
time.Sleep(1 * time.Minute)
}
}()
}

//获取SEO
//@param page 页面
//@param defaultTitle 默认标题
Expand Down Expand Up @@ -101,7 +123,7 @@ func (this *Seo) BuildSitemap() {
fileNum = fileNum + 1
}
//创建文件夹
os.MkdirAll("sitemap", os.ModePerm)
os.MkdirAll(filepath.Join(helper.RootPath, "sitemap"), os.ModePerm)
for i := 0; i < fileNum; i++ {
var docs []DocumentInfo
o.QueryTable(GetTableDocumentInfo()).Filter("Status__gt", -1).Limit(limit).Offset(i*limit).All(&docs, "Id", "TimeCreate")
Expand All @@ -118,7 +140,7 @@ func (this *Seo) BuildSitemap() {
Priority: 0.9,
})
}
if err := Sitemap.CreateSitemapContent(su, file); err != nil {
if err := Sitemap.CreateSitemapContent(su, filepath.Join(helper.RootPath, file)); err != nil {
helper.Logger.Error("sitemap生成失败:" + err.Error())
}
}
Expand Down
1 change: 1 addition & 0 deletions models/SysModel.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Sys struct {
StoreType string `orm:"default(cs-oss);column(StoreType);size(15)"` //文档存储方式
CheckRegEmail bool `orm:"default(true);column(CheckRegEmail);"` //是否需要验证注册邮箱,如果需要验证注册邮箱,提要求发送注册验证码
AllowRepeatedDoc bool `orm:"default(false);column(AllowRepeatedDoc)"` //是否允许上传重复文档
AutoSitemap bool `orm:"default(true);column(AutoSitemap)"` //每天凌晨自动更新站点地图
}

func NewSys() *Sys {
Expand Down
13 changes: 11 additions & 2 deletions views/Admin/default/Sys/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,19 @@ <h1 class="page-header">系统设置</h1>
<input type="text" class="form-control" name="DomainPc" value="{{.DomainPc}}" />
</div>


<div class="form-group">
<label >网站备案号</label>
<input type="text" class="form-control" name="Icp" placeholder="请输入网站备案号,如 桂ICP备 xxxx" value="{{.Icp}}" />
</div>

<div class="col-xs-4" style="padding-left: 0px;">
<div class="form-group">
<label >网站备案号</label>
<input type="text" class="form-control" name="Icp" placeholder="请输入网站备案号,如 桂ICP备 xxxx" value="{{.Icp}}" />
<label >是否每天凌晨2:00自动更新站点地图</label>
<select name="AutoSitemap" class="form-control">
<option {{if eq .AutoSitemap true}}selected="selected"{{end}} value="true">启用</option>
<option {{if eq .AutoSitemap false}}selected="selected"{{end}} value="false">关闭</option>
</select>
</div>
</div>

Expand Down

0 comments on commit 5971fbd

Please sign in to comment.