package service import ( "context" "encoding/json" "fmt" "git.hpds.cc/Component/logging" "hpds-iot-web/config" "hpds-iot-web/internal/middleware" "hpds-iot-web/internal/proto" "hpds-iot-web/model" "hpds-iot-web/pkg/utils" "net/http" "time" "xorm.io/xorm" ) type PagingStruct struct { List interface{} `json:"list"` Total int64 `json:"total"` } // FillPaging 填充分页数据 func FillPaging(count, pageNum, pageSize int64, list interface{}, data *proto.BaseResponse) *proto.BaseResponse { //var tp int64 //if count%pageSize > 0 { // tp = count/pageSize + 1 //} else { // tp = count / pageSize //} _ = fmt.Sprintf("%d, %d", pageNum, pageSize) ps := new(PagingStruct) ps.List = list ps.Total = count data.Data = ps //data.PageSize = pageSize //data.Data = list //data.Page = pageNum //data.PageCount = tp //data.TotalSize = count return data } type repo struct { AppConfig *config.WebConfig engine *xorm.Engine logger *logging.Logger } type UserService interface { Login(ctx context.Context, userName, pass string) (rsp *proto.BaseResponse, err error) 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) GetRoleList(ctx context.Context, req proto.RoleRequest) (rsp *proto.BaseResponse, err error) AddRole(ctx context.Context, req proto.RoleItemRequest) (rsp *proto.BaseResponse, err error) EditRole(ctx context.Context, req proto.RoleItemRequest) (rsp *proto.BaseResponse, err error) } func NewUserService(engine *xorm.Engine, logger *logging.Logger) UserService { return &repo{ engine: engine, logger: logger, } } func (rp *repo) Login(ctx context.Context, userName, pass string) (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: //获取用户 us := new(model.SystemUser) h, err := rp.engine.Where("phone=?", userName).Get(us) if err != nil { goto ReturnPoint } if !h { err = fmt.Errorf("未能找到对应的用户") goto ReturnPoint } vPass := utils.GetUserSha1Pass(pass, us.Salt) if vPass != us.Pass { err = fmt.Errorf("用户名或者密码不正确") goto ReturnPoint } data := new(proto.UserLoginResponse) data.UserName = us.Phone data.RealName = us.RealName data.UserId = us.UserId data.Avatar = us.Avatar data.Desc = us.Desc data.HomePath = us.HomePath //生成JWT token data.Token, err = middleware.Sign(us, 86400) if err != nil { goto ReturnPoint } body, _ := json.Marshal(us) err = model.Redis.Set(data.Token, string(body), time.Duration(24)*time.Hour).Err() if err != nil { goto ReturnPoint } roleList := model.GetRolesByUserId(us.UserId) data.Roles = make([]proto.RoleItem, len(roleList)) for k, v := range roleList { data.Roles[k] = proto.RoleItem{ RoleId: v.RoleId, RoleName: v.RoleName, Value: v.RoleValue, } } rsp.Code = http.StatusOK rsp.Status = http.StatusText(http.StatusOK) rsp.Message = "成功" rsp.Data = data rsp.Err = err 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) GetUserInfo(ctx context.Context, userId int64) (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: //获取用户 us := new(model.SystemUser) h, err := rp.engine.ID(userId).Get(us) if err != nil { goto ReturnPoint } if !h { err = fmt.Errorf("未能找到对应的用户") goto ReturnPoint } data := new(proto.UserLoginResponse) data.UserName = us.Phone data.RealName = us.RealName data.UserId = us.UserId data.Avatar = us.Avatar data.Desc = us.Desc data.HomePath = us.HomePath //生成JWT token data.Token, err = middleware.Sign(us, 86400) if err != nil { goto ReturnPoint } body, _ := json.Marshal(us) err = model.Redis.Set(data.Token, string(body), time.Duration(24)*time.Hour).Err() if err != nil { goto ReturnPoint } roleList := model.GetRolesByUserId(us.UserId) data.Roles = make([]proto.RoleItem, len(roleList)) for k, v := range roleList { data.Roles[k] = proto.RoleItem{ RoleId: v.RoleId, RoleName: v.RoleName, Value: v.RoleValue, } } rsp.Code = http.StatusOK rsp.Status = http.StatusText(http.StatusOK) rsp.Message = "成功" rsp.Data = data rsp.Err = err 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) MenuList(ctx context.Context, userId int64) (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: //获取用户 roleList := make([]model.SystemUserRole, 0) err = rp.engine.Where("user_id = ? and status = 1", userId).Find(&roleList) if err != nil { goto ReturnPoint } roleIdList := make([]int64, len(roleList)) for k, v := range roleList { roleIdList[k] = v.RoleId } menuList := make([]model.SystemRoleMenu, 0) err = rp.engine.In("role_id", roleIdList).And("status = 1").Find(&menuList) if err != nil { goto ReturnPoint } menuIdList := make([]int64, len(menuList)) for k, v := range menuList { menuIdList[k] = v.RoleId } list := make([]model.SystemMenu, 0) err = rp.engine.In("menu_id", menuIdList).And("status = 1").Find(&list) if err != nil { goto ReturnPoint } rsp.Code = http.StatusOK rsp.Status = http.StatusText(http.StatusOK) rsp.Message = "成功" rsp.Data = list rsp.Err = err 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) GetUserList(ctx context.Context, req proto.UserRequest) (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: userList := make([]model.SystemUser, 0) count, err := rp.engine.Where("(? = '' or phone like ?)", req.Phone, "%"+req.Phone+"%"). And("(? = '' or real_name like ?)", req.RealName, req.RealName). And("status = 1").Limit(int(req.Size), int(((req.Page)-1)*req.Size)). FindAndCount(&userList) if err != nil { goto ReturnPoint } data := make([]proto.UserLoginResponse, len(userList)) for k, v := range userList { roleList := model.GetRolesByUserId(v.UserId) roles := make([]proto.RoleItem, len(roleList)) for key, val := range roleList { roles[key] = proto.RoleItem{ RoleId: val.RoleId, RoleName: val.RoleName, Value: val.RoleValue, } } data[k] = proto.UserLoginResponse{ UserId: v.UserId, RealName: v.RealName, UserName: v.Phone, Roles: roles, Avatar: v.Avatar, Desc: v.Desc, HomePath: v.HomePath, } } rsp.Code = http.StatusOK rsp.Status = http.StatusText(http.StatusOK) rsp.Message = "成功" rsp = FillPaging(count, req.Page, req.Size, data, rsp) rsp.Err = err 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) 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 { sess := rp.engine.NewSession() item.Status = req.Status item.Phone = req.Phone item.RealName = req.RealName //item.RoleId = req.RoleId 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 = sess.Insert(item) if err != nil { _ = sess.Rollback() goto ReturnPoint } if len(req.RoleId) > 0 { for _, v := range req.RoleId { role := new(model.SystemUserRole) role.RoleId = v role.UserId = item.UserId role.Status = 1 _, err = sess.Insert(role) if err != nil { _ = sess.Rollback() goto ReturnPoint } } } err = sess.Commit() if err != nil { _ = sess.Rollback() 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 { sess := rp.engine.NewSession() 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 = sess.AllCols().Update(item) if err != nil { _ = sess.Rollback() goto ReturnPoint } role := new(model.SystemUserRole) role.UserId = item.UserId role.Status = 0 _, err = sess.Where("user_id = ?", item.UserId).AllCols().Update(role) if err != nil { _ = sess.Rollback() goto ReturnPoint } if len(req.RoleId) > 0 { for _, v := range req.RoleId { role := new(model.SystemUserRole) b, err = sess.Where("user_id = ?", item.UserId).And("role_id=?", v).Get(role) if err != nil { _ = sess.Rollback() goto ReturnPoint } if b { role.Status = 1 _, err = sess.ID(role.UserRoleId).Update(role) } else { role.RoleId = v role.UserId = item.UserId role.Status = 1 _, err = sess.Insert(role) } if err != nil { _ = sess.Rollback() goto ReturnPoint } } } err = sess.Commit() if err != nil { _ = sess.Rollback() 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) GetRoleList(ctx context.Context, req proto.RoleRequest) (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: data := make([]model.SystemRole, 0) count, err := rp.engine.Where("(? = '' or role_name like ?)", req.RoleName, "%"+req.RoleName+"%"). And("(? = 0 or status = ?)", req.Status, req.Status).Limit(int(req.Size), int(((req.Page)-1)*req.Size)). FindAndCount(&data) if err != nil { goto ReturnPoint } rsp.Code = http.StatusOK rsp.Status = http.StatusText(http.StatusOK) rsp.Message = "成功" rsp = FillPaging(count, req.Page, req.Size, data, rsp) rsp.Err = err 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) AddRole(ctx context.Context, req proto.RoleItemRequest) (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.SystemRole) b, err := rp.engine.Where("role_value = ?", req.RoleValue). Get(item) if err != nil { goto ReturnPoint } if b { err = fmt.Errorf("已经存在相同值的角色") goto ReturnPoint } else { item.AliasName = req.AliasName item.Description = req.Description item.RoleName = req.RoleName item.RoleValue = req.RoleValue item.Status = req.Status item.CreateAt = time.Now().Unix() item.UpdateAt = time.Now().Unix() _, err = rp.engine.Insert(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 } func (rp *repo) EditRole(ctx context.Context, req proto.RoleItemRequest) (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.SystemRole) b, err := rp.engine.Where("role_id = ?", req.RoleId). Get(item) if err != nil { goto ReturnPoint } if !b { err = fmt.Errorf("未能找到对应的用户") goto ReturnPoint } else { if len(req.AliasName) > 0 && item.AliasName != req.AliasName { item.AliasName = req.AliasName } if len(req.Description) > 0 && item.Description != req.Description { item.Description = req.Description } if len(req.RoleName) > 0 && item.RoleName != req.RoleName { item.RoleName = req.RoleName } if len(req.RoleValue) > 0 && item.RoleValue != req.RoleValue { item.RoleValue = req.RoleValue } item.Status = req.Status 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 }