1
0
mirror of https://github.com/fumiama/paper-manager.git synced 2026-06-12 04:00:23 +08:00

初步完成step1前端设计

This commit is contained in:
源文雨
2023-04-24 00:59:06 +08:00
parent ff831f2f7a
commit aabd25ea19
10 changed files with 326 additions and 77 deletions

32
backend/generate.go Normal file
View File

@@ -0,0 +1,32 @@
package backend
import (
"encoding/json"
"io"
"net/http"
"github.com/fumiama/paper-manager/backend/global"
)
func init() {
apimap["/api/genFile"] = &apihandler{"POST", func(w http.ResponseWriter, r *http.Request) {
user := usertokens.Get(r.Header.Get("Authorization"))
if user == nil {
writeresult(w, codeError, nil, errInvalidToken.Error(), typeError)
return
}
conf := global.GenerateConfig{}
defer r.Body.Close()
err := json.NewDecoder(r.Body).Decode(&conf)
if err != nil {
writeresult(w, codeError, nil, err.Error(), typeError)
return
}
docf, err := global.FileDB.GenerateFile(&conf)
if err != nil {
writeresult(w, codeError, nil, err.Error(), typeError)
return
}
_, _ = io.Copy(w, docf)
}}
}

View File

@@ -235,7 +235,7 @@ func (f *FileDatabase) AddFile(lstid int, reg *Regex, istemp bool, progress func
m := md5.Sum(sb.Bytes())
que := &Question{
ID: int64(binary.LittleEndian.Uint64(m[:8])),
FileID: file.ID,
ListID: *lst.ID,
Major: majorq.Name,
Plain: base14.BytesToString(sb.Bytes()),
Images: func() []byte {

View File

@@ -2,6 +2,7 @@ package global
import (
"errors"
"os"
"strconv"
sql "github.com/FloatTech/sqlite"
@@ -77,7 +78,26 @@ func (f *FileDatabase) GenerateFile(config *GenerateConfig) (*docx.Docx, error)
if rate > config.RateLimit {
return nil, ErrRateLimitExceeded
}
// TODO: 写入question到docf
for i, q := range ques {
lst, err := sql.Find[List](&f.db, FileTableFile, "WHERE ID="+strconv.Itoa(q.ListID))
if err != nil {
return nil, err
}
quesfile, err := os.Open(q.Path)
if err != nil {
return nil, err
}
stat, err := quesfile.Stat()
if err != nil {
return nil, err
}
docq, err := docx.Parse(quesfile, stat.Size())
if err != nil {
return nil, err
}
docf.AddParagraph().AddText(strconv.Itoa(i+1) + ". (" + lst.Desc + ")")
docf.AppendFile(docq)
}
}
return nil, nil
return docf, nil
}

View File

@@ -80,7 +80,7 @@ func (f *FileDatabase) DelQuestion(id int64, istemp bool) error {
type Question struct {
ID int64 // ID is the first 8 bytes of the Plain's md5
FileID int64 // FileID is fk to File(ID)
ListID int // ListID is fk to List(ID)
Major string // Major is sub's major name
Path string // Path is the question's docx position
Plain string // Plain is the plain text of the question (like markdown format)

View File

@@ -4,6 +4,7 @@ import (
"container/heap"
"encoding/json"
"math"
"net/http"
"strconv"
"github.com/fumiama/paper-manager/backend/global"
@@ -95,3 +96,22 @@ func parseFileQuestions(qb []byte, istemp bool) ([]question, []duplication, floa
return qs, ds, sum / float64(cnt), nil
}
func init() {
apimap["/api/getMajors"] = &apihandler{"GET", func(w http.ResponseWriter, r *http.Request) {
user := usertokens.Get(r.Header.Get("Authorization"))
if user == nil {
writeresult(w, codeError, nil, errInvalidToken.Error(), typeError)
return
}
majs := global.FileDB.GetMajors()
type majret struct {
Name string
}
majrets := make([]majret, len(majs))
for i, s := range majs {
majrets[i].Name = s
}
writeresult(w, codeSuccess, &majrets, messageOk, typeSuccess)
}}
}