diff --git a/api/Init.go b/api/Init.go index 8e7a61a..17fcf98 100644 --- a/api/Init.go +++ b/api/Init.go @@ -1,8 +1,11 @@ package api -import "net/http" +import ( + "net/http" +) func InitApi() { BilibiliInit() http.HandleFunc("/api/mdt", GetInfo) + http.HandleFunc("/api/memos", GetMemosJson) } diff --git a/api/memos.go b/api/memos.go new file mode 100644 index 0000000..ca46348 --- /dev/null +++ b/api/memos.go @@ -0,0 +1,61 @@ +package api + +import ( + "blog/config" + "encoding/json" + "fmt" + "net/http" + "regexp" +) + +type MemoInfo struct { + Id int `json:"id"` + Name string `json:"name"` + RowStatus string `json:"rowStatus"` + CreatorId int `json:"creatorId"` + CreatedTs int `json:"createdTs"` + UpdatedTs int `json:"updatedTs"` + DisplayTs int `json:"displayTs"` + Content string `json:"content"` + Visibility string `json:"visibility"` + Pinned bool `json:"pinned"` + CreatorName string `json:"creatorName"` + CreatorUsername string `json:"creatorUsername"` + ResourceList []interface{} `json:"resourceList"` + RelationList []interface{} `json:"relationList"` +} + +func GetMemosJson(w http.ResponseWriter, r *http.Request) { + if err := r.ParseForm(); err != nil { + http.Error(w, "参数解析错误", http.StatusInternalServerError) + } + id := r.Form.Get("id") + resp, err := http.Get(config.Cfg.MemosURL + "/api/v1/memo/" + id) + if err != nil { + http.Error(w, "无法获取文章数据", http.StatusInternalServerError) + return + } + defer resp.Body.Close() + var info MemoInfo + err = json.NewDecoder(resp.Body).Decode(&info) // 使用json.NewDecoder解码JSON数据 + if err != nil { + http.Error(w, "无法解析文章数据", http.StatusInternalServerError) + return + } + compileRegex := regexp.MustCompile("(?s)```json\n(.*?)```") + matchArr := compileRegex.FindStringSubmatch(info.Content) + if len(matchArr) > 0 { + source := make([]byte, 4096) + source = []byte(fmt.Sprintf("[%v]", matchArr[len(matchArr)-1])) + w.Header().Add("Content-Type", "application/octet-stream;charset=utf-8") + w.Header().Add("Content-Disposition", "attachment;filename="+info.Name+".json") + _, err = w.Write(source) + if err != nil { + http.Error(w, "无法解析源数据", http.StatusInternalServerError) + return + } + return + } + http.Error(w, "无法解析页面请求"+id, http.StatusInternalServerError) + return +} diff --git a/config/memos.go b/config/memos.go index 12f4346..f3b7e27 100644 --- a/config/memos.go +++ b/config/memos.go @@ -1,8 +1,9 @@ package config type memosConfig struct { - MemosUser string `json:"memos_user"` - MemosURL string `json:"memos_url"` - MemosAlbumTag string `json:"memos_album_tag"` - MemosTalkTag string `json:"memos_talk_tag"` + MemosUser string `json:"memos_user"` + MemosURL string `json:"memos_url"` + MemosAlbumTag string `json:"memos_album_tag"` + MemosTalkTag string `json:"memos_talk_tag"` + MemosReaderTag string `json:"memos_reader_tag"` } diff --git a/controller/memos.go b/controller/memos.go index c39af05..989c008 100644 --- a/controller/memos.go +++ b/controller/memos.go @@ -23,6 +23,16 @@ func Memos(w http.ResponseWriter, r *http.Request) { "author": config.Cfg.Author, "qq": config.Cfg.Qq, })) + case "reader": + Template := models.Template.Reader + //qq := "https://q1.qlogo.cn/g?b=qq&nk=" + config.Cfg.Qq + "&s=5" + Template.WriteData(w, models.BuildViewData("Reader", map[string]interface{}{ + "url": config.Cfg.MemosURL, + "user": config.Cfg.MemosUser, + "tag": config.Cfg.MemosReaderTag, + "author": config.Cfg.Author, + "qq": config.Cfg.Qq, + })) case "album": Template := models.Template.Album tag := strings.Split(config.Cfg.MemosAlbumTag, ",") diff --git a/models/html_template.go b/models/html_template.go index 5449bcf..7a067a6 100644 --- a/models/html_template.go +++ b/models/html_template.go @@ -12,6 +12,7 @@ type TemplatePointer struct { } type HtmlTemplate struct { + Reader TemplatePointer Talk TemplatePointer Album TemplatePointer Article TemplatePointer @@ -68,7 +69,7 @@ func BuildViewData(title string, data interface{}) map[string]interface{} { // error: 错误信息(如果有) func initHtmlTemplate(viewDir string) (HtmlTemplate, error) { tp, err := readHtmlTemplate( - []string{"index", "extraNav", "dashboard", "categories", "article", "album", "talk"}, + []string{"index", "extraNav", "dashboard", "categories", "article", "album", "talk", "reader"}, viewDir) if err != nil { return HtmlTemplate{}, err @@ -82,6 +83,7 @@ func initHtmlTemplate(viewDir string) (HtmlTemplate, error) { Article: tp[4], Album: tp[5], Talk: tp[6], + Reader: tp[7], }, nil } func SpreadDigit(n int) []int { diff --git a/themes/blog/layouts/nav.gohtml b/themes/blog/layouts/nav.gohtml index 73285b6..49f5d3c 100644 --- a/themes/blog/layouts/nav.gohtml +++ b/themes/blog/layouts/nav.gohtml @@ -24,6 +24,9 @@ {{if ne .Config.MemosTalkTag ""}}