Преглед на файлове

add template && user/profile

animeic преди 2 години
родител
ревизия
390f5bdf3d

+ 1 - 0
oilseal-train/Dockerfile

@@ -15,6 +15,7 @@ RUN echo -e https://mirrors.ustc.edu.cn/alpine/v3.15/main > /etc/apk/repositorie
 WORKDIR /root/oilseal-train
 
 ADD ./app.yaml ./app.yaml
+ADD ./file ./file
 ADD ./oilseal-train-service ./oilseal-train-service
 
 EXPOSE 7105

+ 11 - 1
oilseal-train/api/bank.go

@@ -6,6 +6,7 @@ import (
 	"oilseal-train/db/model"
 	"oilseal-train/db/repo"
 	"oilseal-train/log"
+	"oilseal-train/utils"
 	"strings"
 	"time"
 
@@ -22,6 +23,7 @@ func Bank(r *GinRouter) {
 	r.POSTJWT("/bank/delete/:id", BankDelete)
 	r.GETJWT("/bank/exportXls/:bankId", BankExportXls)
 	r.POSTJWT("/bank/importXls", BankImportXls)
+	r.GETJWT("/bank/template", TestTemplate)
 }
 
 func BankAdd(c *gin.Context, apictx *ApiSession) (interface{}, error) {
@@ -344,7 +346,7 @@ func BankExportXls(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	}
 	// 另存为
 	// _ = os.MkdirAll("excel", os.ModePerm)
-	// filename1 := time.Now().Format("20060102150405") + ".xlsx"
+	// // filename1 := time.Now().Format("20060102150405") + ".xlsx"
 	// filename1 := bank.Name + ".xlsx"
 	// err = f.SaveAs(filename1)
 	// if err != nil {
@@ -450,3 +452,11 @@ func BankImportXls(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 
 	return errors, nil
 }
+
+// 下载模板文件
+func TestTemplate(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	c.Writer.Header().Add("Content-Disposition", "attachment; filename=试题导入模板.zip")
+	c.Writer.Header().Set("Content-Type", "application/zip")
+	c.File(utils.TestTemplatePath)
+	return nil, nil
+}

+ 0 - 121
oilseal-train/api/tmp.json

@@ -1,121 +0,0 @@
-{
-  "errorNo": 200,
-  "result": {
-    "list1": [
-      {
-        "_id": "639939b5333d64a81fbdd3e6",
-        "bankId": "6397f71e94ab390229bb866e",
-        "question": "下列哪个不属于CRF模型对于HMM和MEMM模型的优势",
-        "answerItem": {
-          "A": "特征灵活",
-          "B": "速度快",
-          "C": "可容纳较多上下文信息",
-          "D": "全局最优"
-        },
-        "answer": [
-          "B"
-        ],
-        "analyze": "CRF没有HMM那样严格的独立性假设条件,因而可以容纳任意的上下文信息。特征设计灵活与ME一样",
-        "type": 1,
-        "state": 1,
-        "createTime": "2022-12-14T02:49:25.885Z",
-        "updateTime": "2022-12-14T02:49:25.885Z"
-      },
-      {
-        "_id": "63a2b0c240546830a2017fb2",
-        "bankId": "6397f71e94ab390229bb866e",
-        "question": "下列哪个不属于CRF模型对于HMM和MEMM模型的优势",
-        "answerItem": {
-          "A": "特征灵活",
-          "B": "速度快",
-          "C": "可容纳较多上下文信息",
-          "D": "全局最优"
-        },
-        "answer": [
-          "B"
-        ],
-        "analyze": "CRF没有HMM那样严格的独立性假设条件,因而可以容纳任意的上下文信息。特征设计灵活与ME一样",
-        "type": 1,
-        "state": 1,
-        "createTime": "2022-12-14T02:49:25.885Z",
-        "updateTime": "2022-12-14T02:49:25.885Z"
-      },
-      {
-        "_id": "63a2b0c540546830a2017fb3",
-        "bankId": "6397f71e94ab390229bb866e",
-        "question": "下列哪个不属于CRF模型对于HMM和MEMM模型的优势",
-        "answerItem": {
-          "A": "特征灵活",
-          "B": "速度快",
-          "C": "可容纳较多上下文信息",
-          "D": "全局最优"
-        },
-        "answer": [
-          "B"
-        ],
-        "analyze": "CRF没有HMM那样严格的独立性假设条件,因而可以容纳任意的上下文信息。特征设计灵活与ME一样",
-        "type": 1,
-        "state": 1,
-        "createTime": "2022-12-14T02:49:25.885Z",
-        "updateTime": "2022-12-14T02:49:25.885Z"
-      },
-      {
-        "_id": "63a2b0c640546830a2017fb4",
-        "bankId": "6397f71e94ab390229bb866e",
-        "question": "下列哪个不属于CRF模型对于HMM和MEMM模型的优势",
-        "answerItem": {
-          "A": "特征灵活",
-          "B": "速度快",
-          "C": "可容纳较多上下文信息",
-          "D": "全局最优"
-        },
-        "answer": [
-          "B"
-        ],
-        "analyze": "CRF没有HMM那样严格的独立性假设条件,因而可以容纳任意的上下文信息。特征设计灵活与ME一样",
-        "type": 1,
-        "state": 1,
-        "createTime": "2022-12-14T02:49:25.885Z",
-        "updateTime": "2022-12-14T02:49:25.885Z"
-      },
-      {
-        "_id": "63a2b0c740546830a2017fb5",
-        "bankId": "6397f71e94ab390229bb866e",
-        "question": "下列哪个不属于CRF模型对于HMM和MEMM模型的优势",
-        "answerItem": {
-          "A": "特征灵活",
-          "B": "速度快",
-          "C": "可容纳较多上下文信息",
-          "D": "全局最优"
-        },
-        "answer": [
-          "B"
-        ],
-        "analyze": "CRF没有HMM那样严格的独立性假设条件,因而可以容纳任意的上下文信息。特征设计灵活与ME一样",
-        "type": 1,
-        "state": 1,
-        "createTime": "2022-12-14T02:49:25.885Z",
-        "updateTime": "2022-12-14T02:49:25.885Z"
-      }
-    ],
-    "list2": [
-      {
-        "_id": "639939b5333d64a81fbdd3e5",
-        "bankId": "6397f71e94ab390229bb866e",
-        "question": "辅助存储器用于存储当前不参与运行或需要长久保存的程序和数据。其特点是存储容量大、价格低,但与主存储器相比,其存取速度较慢。",
-        "answerItem": {
-          "true": "正确",
-          "false": "错误"
-        },
-        "answer": [
-          "true"
-        ],
-        "type": 2,
-        "state": 1,
-        "createTime": "2022-12-14T02:49:25.845Z",
-        "updateTime": "2022-12-14T02:49:25.845Z"
-      }
-    ]
-  },
-  "errorDesc": ""
-}

+ 48 - 9
oilseal-train/api/user.go

@@ -6,7 +6,7 @@ import (
 	"oilseal-train/db/model"
 	"oilseal-train/db/repo"
 	"oilseal-train/log"
-	"os"
+	"oilseal-train/utils"
 	"strings"
 	"time"
 
@@ -25,6 +25,8 @@ func User(r *GinRouter) {
 	r.POSTJWT("/user/delete/:id", UserDelete)
 	r.GETJWT("/user/exportXls", UserExportXls)
 	r.POSTJWT("/user/importXls", UserImportXls)
+	r.GETJWT("/user/template", UserTemplate)
+	r.GETJWT("/user/profile", UserProfile)
 }
 
 // 用户登录 student teacher admin
@@ -74,6 +76,27 @@ func Login(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	return out, nil
 }
 
+func UserProfile(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	userId, err := primitive.ObjectIDFromHex(apictx.User.ID)
+	if err != nil {
+		return nil, errors.New("当前用户非法")
+	}
+	err = IsAdmin(apictx)
+	if err != nil {
+		return nil, err
+	}
+	user := model.User{}
+	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
+		CollectName: repo.CollectionUser,
+		Query:       repo.Map{"_id": userId, "state": 1},
+	}, &user)
+	if !found || err != nil {
+		return nil, errors.New("未找到用户信息")
+	}
+	user.Password = ""
+	return user, nil
+}
+
 // 创建账号 主要用于admin注册
 func Register(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	var form model.User
@@ -165,6 +188,10 @@ func UserEdit(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	if len(form.Id) != 12 {
 		return nil, errors.New("id不正确")
 	}
+	if len(form.Password) > 0 {
+		form.Password = UtilMd5(form.Password)
+	}
+	form.UpdateTime = time.Now()
 
 	return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionUser, form.Id.Hex(), &form)
 }
@@ -297,13 +324,13 @@ func UserExportXls(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 
 	}
 	// 另存为
-	_ = os.MkdirAll("excel", os.ModePerm)
-	// filename1 := time.Now().Format("20060102150405") + ".xlsx"
-	filename1 := "用户列表.xlsx"
-	err = f.SaveAs(filename1)
-	if err != nil {
-		return nil, err
-	}
+	// _ = os.MkdirAll("excel", os.ModePerm)
+	// // filename1 := time.Now().Format("20060102150405") + ".xlsx"
+	// filename1 := "用户列表.xlsx"
+	// err = f.SaveAs(filename1)
+	// if err != nil {
+	// 	return nil, err
+	// }
 
 	// filename := time.Now().Format("20060102150405") + ".xlsx"
 	filename := "用户列表.xlsx"
@@ -364,11 +391,15 @@ func UserImportXls(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 
 func formatUser(u []string, rowNum int) (*model.User, error) {
 	fmt.Println(u)
+	password := u[3]
+	if len(u[3]) != 32 {
+		password = UtilMd5(u[3])
+	}
 	user := &model.User{
 		LoginName: u[0],
 		Name:      u[1],
 		Role:      u[2],
-		Password:  u[3],
+		Password:  password,
 	}
 
 	if user.LoginName == "" {
@@ -396,3 +427,11 @@ func IsAdmin(apictx *ApiSession) error {
 		return nil
 	}
 }
+
+// 下载模板文件
+func UserTemplate(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	c.Writer.Header().Add("Content-Disposition", "attachment; filename=用户导入模板.zip")
+	c.Writer.Header().Set("Content-Type", "application/zip")
+	c.File(utils.UserTemplatePath)
+	return nil, nil
+}

BIN
oilseal-train/file/用户导入模板.zip


BIN
oilseal-train/file/试题导入模板.zip


+ 5 - 0
oilseal-train/utils/uitls.go

@@ -2,6 +2,11 @@ package utils
 
 import "reflect"
 
+const (
+	UserTemplatePath = "./file/用户导入模板.zip"
+	TestTemplatePath = "./file/试题导入模板.zip"
+)
+
 // 把value的值按字段分配给 binding
 func StructAssign(binding interface{}, value interface{}) {
 	bVal := reflect.ValueOf(binding).Elem()