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) } 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: data := 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(&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) 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 }