|
@@ -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
|
|
|
+}
|