diff --git a/db_init.sql b/db_init.sql index c8e2951..39c9f32 100644 --- a/db_init.sql +++ b/db_init.sql @@ -108,30 +108,30 @@ CREATE INDEX IDX_task_model_id ON task (model_id); CREATE INDEX IDX_task_node_id ON task (node_id); -- 插入病害数据库 -INSERT INTO diagnostic_platform.disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (1, '横向拼接缝', 1, 1, 1673504353, 1673504353); -INSERT INTO diagnostic_platform.disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (2, '块裂', 1, 1, 1673504376, 1673504376); -INSERT INTO diagnostic_platform.disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (3, '开挖修补缝', 1, 1, 1673504480, 1673504480); -INSERT INTO diagnostic_platform.disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (4, '横向裂缝', 1, 1, 1673504487, 1673504487); -INSERT INTO diagnostic_platform.disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (5, '纵向裂缝', 1, 1, 1673504496, 1673504496); -INSERT INTO diagnostic_platform.disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (6, '龟裂', 1, 1, 1673504503, 1673504503); -INSERT INTO diagnostic_platform.disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (7, '管道修补缝', 1, 1, 1673504511, 1673504511); -INSERT INTO diagnostic_platform.disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (8, '结构裂缝', 2, 1, 1673504520, 1673504520); -INSERT INTO diagnostic_platform.disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (9, '蜂窝麻面', 2, 1, 1673504528, 1673504528); -INSERT INTO diagnostic_platform.disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (10, '剥落掉角', 2, 1, 1673504537, 1673504537); -INSERT INTO diagnostic_platform.disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (11, '空洞及孔洞', 2, 1, 1673504544, 1673504544); -INSERT INTO diagnostic_platform.disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (12, '破损露筋', 2, 1, 1673504553, 1673504553); -INSERT INTO diagnostic_platform.disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (13, '钢筋锈蚀', 2, 1, 1673504562, 1673504562); -INSERT INTO diagnostic_platform.disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (14, '衬砌漏水', 3, 1, 1673504570, 1673504570); -INSERT INTO diagnostic_platform.disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (15, '衬砌裂缝', 3, 1, 1673504578, 1673504578); -INSERT INTO diagnostic_platform.disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (16, '衬砌腐蚀', 3, 1, 1673504585, 1673504585); -INSERT INTO diagnostic_platform.disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (17, '衬砌开裂及剥落', 3, 1, 1673504594, 1673504594); -INSERT INTO diagnostic_platform.disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (18, '衬砌变形及位移', 3, 1, 1673504601, 1673504601); -INSERT INTO diagnostic_platform.disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (19, '隧道洞门裂损及洞口病害', 3, 1, 1673504609, 1673504609); -INSERT INTO diagnostic_platform.disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (20, '隧道运营通风不畅及照明不良', 3, 1, 1673504619, 1673504619); -INSERT INTO diagnostic_platform.disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (21, '崩塌', 4, 1, 1673504627, 1673504627); -INSERT INTO diagnostic_platform.disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (22, '滑坡', 4, 1, 1673504635, 1673504635); -INSERT INTO diagnostic_platform.disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (23, '错落', 4, 1, 1673504642, 1673504642); -INSERT INTO diagnostic_platform.disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (24, '坍塌', 4, 1, 1673504649, 1673504649); +INSERT INTO disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (1, '横向拼接缝', 1, 1, 1673504353, 1673504353); +INSERT INTO disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (2, '块裂', 1, 1, 1673504376, 1673504376); +INSERT INTO disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (3, '开挖修补缝', 1, 1, 1673504480, 1673504480); +INSERT INTO disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (4, '横向裂缝', 1, 1, 1673504487, 1673504487); +INSERT INTO disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (5, '纵向裂缝', 1, 1, 1673504496, 1673504496); +INSERT INTO disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (6, '龟裂', 1, 1, 1673504503, 1673504503); +INSERT INTO disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (7, '管道修补缝', 1, 1, 1673504511, 1673504511); +INSERT INTO disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (8, '结构裂缝', 2, 1, 1673504520, 1673504520); +INSERT INTO disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (9, '蜂窝麻面', 2, 1, 1673504528, 1673504528); +INSERT INTO disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (10, '剥落掉角', 2, 1, 1673504537, 1673504537); +INSERT INTO disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (11, '空洞及孔洞', 2, 1, 1673504544, 1673504544); +INSERT INTO disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (12, '破损露筋', 2, 1, 1673504553, 1673504553); +INSERT INTO disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (13, '钢筋锈蚀', 2, 1, 1673504562, 1673504562); +INSERT INTO disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (14, '衬砌漏水', 3, 1, 1673504570, 1673504570); +INSERT INTO disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (15, '衬砌裂缝', 3, 1, 1673504578, 1673504578); +INSERT INTO disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (16, '衬砌腐蚀', 3, 1, 1673504585, 1673504585); +INSERT INTO disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (17, '衬砌开裂及剥落', 3, 1, 1673504594, 1673504594); +INSERT INTO disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (18, '衬砌变形及位移', 3, 1, 1673504601, 1673504601); +INSERT INTO disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (19, '隧道洞门裂损及洞口病害', 3, 1, 1673504609, 1673504609); +INSERT INTO disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (20, '隧道运营通风不畅及照明不良', 3, 1, 1673504619, 1673504619); +INSERT INTO disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (21, '崩塌', 4, 1, 1673504627, 1673504627); +INSERT INTO disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (22, '滑坡', 4, 1, 1673504635, 1673504635); +INSERT INTO disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (23, '错落', 4, 1, 1673504642, 1673504642); +INSERT INTO disease_type (type_id, type_name, category_id, status, create_at, update_at) VALUES (24, '坍塌', 4, 1, 1673504649, 1673504649); -- 插入初始用户 -INSERT INTO diagnostic_platform.system_user (user_id, phone, avatar, `desc`, home_path, pass, salt, real_name, create_at, update_at) VALUES (1, '18906517788', null, null, null, 'NMIYXdSWsGXoz/+uRs5hdKGqVRc=', '69wBi9uU8FFa8OTW', '王剑', null, null); +INSERT INTO system_user (user_id, phone, avatar, `desc`, home_path, pass, salt, real_name, create_at, update_at) VALUES (1, '18906517788', null, null, null, 'NMIYXdSWsGXoz/+uRs5hdKGqVRc=', '69wBi9uU8FFa8OTW', '王剑', null, null); diff --git a/go.mod b/go.mod index e74c47e..60dc648 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.18 require ( git.hpds.cc/Component/gin_valid v0.0.0-20230104142509-f956bce255b6 git.hpds.cc/Component/logging v0.0.0-20230106105738-e378e873921b - git.hpds.cc/pavement/hpds_node v0.0.0-20230326152949-a1c0ad2f7052 + git.hpds.cc/pavement/hpds_node v0.0.0-20230402152619-41414aafa930 github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/gin-contrib/zap v0.1.0 github.com/gin-gonic/gin v1.8.2 diff --git a/internal/handler/user.go b/internal/handler/user.go index c6a6553..1e15add 100644 --- a/internal/handler/user.go +++ b/internal/handler/user.go @@ -20,6 +20,37 @@ func (s HandlerService) Login(c *gin.Context) (data interface{}, err error) { data, err = repo.Login(c, req.UserName, req.UserPass) return } +func (s HandlerService) AddUser(c *gin.Context) (data interface{}, err error) { + repo := service.NewUserService(s.Engine, s.Logger) + us, _ := c.Get("operatorUser") + userInfo := us.(*model.SystemUser) + var req proto.UserRequestItem + err = c.ShouldBindJSON(&req) + if err != nil { + go s.SaveLog("AddUser", "System", "", "", req.ToString(), "", c.Request.RemoteAddr, "") + return nil, e.NewValidErr(err) + } + data, err = repo.AddUser(c, req) + go s.SaveLog("新增用户", "Manage", "", "", "", fmt.Sprintf("%d", userInfo.UserId), c.Request.RemoteAddr, "") + return + return +} + +func (s HandlerService) EditUser(c *gin.Context) (data interface{}, err error) { + repo := service.NewUserService(s.Engine, s.Logger) + us, _ := c.Get("operatorUser") + userInfo := us.(*model.SystemUser) + var req proto.UserRequestItem + err = c.ShouldBindJSON(&req) + if err != nil { + go s.SaveLog("EditUser", "System", "", "", req.ToString(), "", c.Request.RemoteAddr, "") + return nil, e.NewValidErr(err) + } + data, err = repo.EditUser(c, req) + go s.SaveLog("修改用户信息", "Manage", "", "", "", fmt.Sprintf("%d", userInfo.UserId), c.Request.RemoteAddr, "") + return + return +} func (s HandlerService) GetUserInfo(c *gin.Context) (data interface{}, err error) { repo := service.NewUserService(s.Engine, s.Logger) diff --git a/internal/proto/request.go b/internal/proto/request.go index 76fd6da..c9952f6 100644 --- a/internal/proto/request.go +++ b/internal/proto/request.go @@ -23,6 +23,22 @@ func (us UserLogin) ToString() string { return string(data) } +type UserRequestItem struct { + RealName string `json:"realName"` + Phone string `json:"phone"` + Password string `json:"password"` + Desc string `json:"desc"` + Status int `json:"status"` +} + +func (us UserRequestItem) ToString() string { + data, err := json.Marshal(us) + if err != nil { + return "" + } + return string(data) +} + type UserRequest struct { RealName string `json:"realName"` Phone string `json:"phone"` diff --git a/internal/router/router.go b/internal/router/router.go index ed150a5..b8eb7a4 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -24,6 +24,8 @@ func InitRouter(cfg *config.WebConfig, logger *logging.Logger, engine *xorm.Engi { user.Use(middleware.JwtAuthMiddleware(logger.Logger)) user.POST("/login", e.ErrorWrapper(hs.Login)) + user.POST("/add", e.ErrorWrapper(hs.AddUser)) + user.POST("/edit", e.ErrorWrapper(hs.EditUser)) user.POST("/list", e.ErrorWrapper(hs.GetUserList)) user.GET("/getUserInfo", e.ErrorWrapper(hs.GetUserInfo)) diff --git a/internal/service/index.go b/internal/service/index.go index c4a0e7a..57e51ae 100644 --- a/internal/service/index.go +++ b/internal/service/index.go @@ -52,6 +52,8 @@ type UserService interface { GetUserInfo(ctx context.Context, userId int64) (rsp *proto.BaseResponse, err error) MenuList(ctx context.Context, userId int64) (rsp *proto.BaseResponse, err error) GetUserList(ctx context.Context, req proto.UserRequest) (rsp *proto.BaseResponse, err error) + AddUser(ctx context.Context, req proto.UserRequestItem) (rsp *proto.BaseResponse, err error) + EditUser(ctx context.Context, req proto.UserRequestItem) (rsp *proto.BaseResponse, err error) } func NewUserService(engine *xorm.Engine, logger *logging.Logger) UserService { @@ -283,3 +285,111 @@ ReturnPoint: } return rsp, err } +func (rp *repo) AddUser(ctx context.Context, req proto.UserRequestItem) (rsp *proto.BaseResponse, err error) { + rsp = new(proto.BaseResponse) + select { + case <-ctx.Done(): + err = fmt.Errorf("超时/取消") + rsp.Code = http.StatusInternalServerError + rsp.Status = http.StatusText(http.StatusInternalServerError) + rsp.Message = "超时/取消" + rsp.Err = ctx.Err() + return rsp, ctx.Err() + default: + item := new(model.SystemUser) + b, err := rp.engine.Where("phone = ?)", req.Phone). + Get(item) + if err != nil { + goto ReturnPoint + } + if !b { + item.Status = req.Status + item.Phone = req.Phone + item.RealName = req.RealName + item.Desc = req.Desc + item.CreateAt = time.Now().Unix() + item.UpdateAt = time.Now().Unix() + item.Salt = utils.RandomString(16, 3) + item.Pass = utils.GetUserSha1Pass(req.Password, item.Salt) + _, err = rp.engine.Insert(item) + if err != nil { + goto ReturnPoint + } + } else { + err = fmt.Errorf("该手机号已经被注册") + goto ReturnPoint + } + rsp.Code = http.StatusOK + rsp.Status = http.StatusText(http.StatusOK) + rsp.Message = "成功" + rsp.Err = ctx.Err() + rsp.Data = item + return rsp, err + } +ReturnPoint: + if err != nil { + rsp.Code = http.StatusInternalServerError + rsp.Status = http.StatusText(http.StatusInternalServerError) + rsp.Err = err + rsp.Message = "失败" + } + return rsp, err +} + +func (rp *repo) EditUser(ctx context.Context, req proto.UserRequestItem) (rsp *proto.BaseResponse, err error) { + rsp = new(proto.BaseResponse) + select { + case <-ctx.Done(): + err = fmt.Errorf("超时/取消") + rsp.Code = http.StatusInternalServerError + rsp.Status = http.StatusText(http.StatusInternalServerError) + rsp.Message = "超时/取消" + rsp.Err = ctx.Err() + return rsp, ctx.Err() + default: + item := new(model.SystemUser) + b, err := rp.engine.Where("phone = ?)", req.Phone). + Get(item) + if err != nil { + goto ReturnPoint + } + if !b { + err = fmt.Errorf("未能找到对应的用户") + goto ReturnPoint + } else { + item.Status = req.Status + if len(item.Phone) > 0 { + item.Phone = req.Phone + } + if len(req.RealName) > 0 { + item.RealName = req.RealName + } + if len(req.Desc) > 0 { + item.Desc = req.Desc + } + if len(req.Password) > 0 { + item.Salt = utils.RandomString(16, 3) + item.Pass = utils.GetUserSha1Pass(req.Password, item.Salt) + } + item.UpdateAt = time.Now().Unix() + _, err = rp.engine.AllCols().Update(item) + if err != nil { + goto ReturnPoint + } + } + rsp.Code = http.StatusOK + rsp.Status = http.StatusText(http.StatusOK) + rsp.Message = "成功" + rsp.Err = ctx.Err() + rsp.Data = item + return rsp, err + } +ReturnPoint: + if err != nil { + rsp.Code = http.StatusInternalServerError + rsp.Status = http.StatusText(http.StatusInternalServerError) + rsp.Err = err + rsp.Message = "失败" + } + return rsp, err +}