2023-01-06 10:09:23 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-01-06 19:15:52 +08:00
|
|
|
"git.hpds.cc/Component/logging"
|
2023-01-06 10:09:23 +08:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2023-01-06 16:10:18 +08:00
|
|
|
type PagingStruct struct {
|
|
|
|
List interface{} `json:"list"`
|
|
|
|
Total int64 `json:"total"`
|
|
|
|
}
|
|
|
|
|
2023-01-06 10:09:23 +08:00
|
|
|
// FillPaging 填充分页数据
|
|
|
|
func FillPaging(count int64, pageNum int64, pageSize int64, list interface{}, data *proto.BaseResponse) *proto.BaseResponse {
|
2023-01-06 16:10:18 +08:00
|
|
|
//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
|
2023-01-06 10:09:23 +08:00
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
type repo struct {
|
|
|
|
engine *xorm.Engine
|
2023-01-06 19:15:52 +08:00
|
|
|
logger *logging.Logger
|
2023-01-06 10:09:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-01-06 19:15:52 +08:00
|
|
|
func NewUserService(engine *xorm.Engine, logger *logging.Logger) UserService {
|
2023-01-06 10:09:23 +08:00
|
|
|
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
|
|
|
|
}
|