hpds_jkw_web/internal/service/index.go

248 lines
6.2 KiB
Go

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 int64, pageNum int64, pageSize int64, list interface{}, data *proto.BaseResponse) *proto.BaseResponse {
//var tp int64
//if count%pageSize > 0 {
// tp = count/pageSize + 1
//} else {
// tp = count / 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)
}
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
}