123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- package router
- import (
- "exam_system/dao"
- "exam_system/entity"
- "exam_system/result"
- "exam_system/service"
- "exam_system/utils"
- "exam_system/vo"
- "github.com/gin-gonic/gin"
- "github.com/xuri/excelize/v2"
- "net/http"
- "strconv"
- "strings"
- )
- func User(router *RouterPlus) {
-
- router.GET("/user", UserInfo)
- router.PUT("/user", UpdateUser)
- r := router.Group("/admin")
- {
-
- r.GET("/user/:sid", AdminUserDetail)
-
- r.GET("/user/list", AdminUserList)
-
- r.POST("/user", AdminAddUser)
-
- r.PUT("/user", AdminUpdateUser)
-
- r.DELETE("/user/:ids", AdminDeleteUser)
-
- r.POST("/user/upload", AdminUserImport)
-
- r.routerGroup.GET("/user/download", AdminDownload)
-
- r.routerGroup.GET("/user/template", AdminUserTemplate)
- }
- }
- func UserInfo(c *gin.Context) *result.Result {
- id, exists := c.Get("id")
- if !exists {
- return result.USER_IS_NOT_EXISTED
- }
- return dao.FindUserbyId(id.(int))
- }
- func UpdateUser(c *gin.Context) *result.Result {
- var body struct {
- entity.User
- UserType string `json:"user_type,omitempty"`
- }
- if err := c.ShouldBindJSON(&body); err != nil {
- return result.PASSWORD_ERROR
- }
- status := entity.UNDER_REVIEW
- body.Status = &status
- roleNames := c.GetStringSlice("role")
- for _, roleName := range roleNames {
- if roleName == "admin" {
- status = entity.NORMAL
- body.Status = &status
- break
- }
- }
- id := c.GetInt("id")
- body.ID = id
- return dao.UpdateUser(&body.User, body.UserType)
- }
- func AdminUserDetail(c *gin.Context) *result.Result {
- sid := c.Param("sid")
- if sid == "" {
- return result.PARAM_ERROR
- }
- res := dao.FindUserbySid(sid)
- if res.Code != result.SUCCESS.Code {
- return res
- }
- userVo := res.Data.(vo.UserVo)
- userVo.Password = ""
- return res.SetData(userVo)
- }
- func AdminUserList(c *gin.Context) *result.Result {
- page, size, sort, query, err := utils.Page(c)
- if err != nil {
- return result.PARAM_ERROR
- }
- return dao.FindUserList(page, size, sort, query)
- }
- func AdminAddUser(c *gin.Context) *result.Result {
- var body struct {
- entity.User
- UserType string `json:"user_type,omitempty"`
- }
- if err := c.ShouldBindJSON(&body); err != nil {
- return result.PARAM_ERROR
- }
- if body.Username == "" || body.Password == "" || body.Sid == "" {
- return result.PARAM_ERROR
- }
- status := entity.NORMAL
- body.Status = &status
- return service.AddUser(&body.User, body.UserType)
- }
- func AdminUpdateUser(c *gin.Context) *result.Result {
- var body struct {
- entity.User
- UserType string `json:"user_type,omitempty"`
- }
- if err := c.ShouldBindJSON(&body); err != nil {
- return result.PARAM_ERROR
- }
- if body.ID == 0 {
- return result.PARAM_ERROR
- }
- status := entity.NORMAL
- body.Status = &status
- return dao.UpdateUser(&body.User, body.UserType)
- }
- func AdminDeleteUser(c *gin.Context) *result.Result {
- idStr := c.Param("ids")
- if idStr == "" {
- return result.PARAM_ERROR
- }
- ids := strings.Split(idStr, ",")
- return dao.DeleteUsers(ids)
- }
- func AdminUserImport(c *gin.Context) *result.Result {
- fh, _ := c.FormFile("file")
- termIdStr := c.PostForm("term_id")
- classIdStr := c.PostForm("class_id")
- file, err := fh.Open()
- if termIdStr != "" {
- var termId int
- termId, err = strconv.Atoi(termIdStr)
- if err != nil {
- return result.UNKNOW_ERROR.SetMsg(err.Error())
- }
- var classId int
- if classIdStr != "" {
- classId, err = strconv.Atoi(classIdStr)
- if err != nil {
- return result.UNKNOW_ERROR.SetMsg(err.Error())
- }
- }
- return dao.InsertBatchUserByTermId(file, termId, classId)
- }
- if classIdStr != "" {
- var classId int
- classId, err = strconv.Atoi(classIdStr)
- if err != nil {
- return result.UNKNOW_ERROR.SetMsg(err.Error())
- }
- return dao.InsertBatchUserByClassId(file, classId)
- }
- return dao.InsertBatchUser(file)
- }
- func AdminDownload(c *gin.Context) {
- file, err := excelize.OpenFile(utils.StudentPath)
- if err != nil {
- c.JSON(http.StatusOK, result.UNKNOW_ERROR.SetMsg(err.Error()))
- }
- res := dao.DownloadUserInfo(file)
- if res.Code != result.SUCCESS.Code {
- c.JSON(http.StatusOK, res)
- }
- c.Header("Content-Type", "application/octet-stream")
- c.Header("Content-Disposition", "attachment; filename=考生信息导出.xlsx")
- c.Header("Content-Transfer-Encoding", "binary")
- _ = file.Write(c.Writer)
- }
- func AdminUserTemplate(c *gin.Context) {
- c.Writer.Header().Add("Content-Disposition", "attachment; filename=试题模板.zip")
- c.Writer.Header().Set("Content-Type", "application/zip")
- c.File(utils.StuTemplatePath)
- }
|