修改登录、管理员详情、对接销售服务
This commit is contained in:
@@ -25,6 +25,8 @@ type UserInfo struct {
|
|||||||
SaleMobile string // 销售手机
|
SaleMobile string // 销售手机
|
||||||
SaleProvince int64 // 销售省份
|
SaleProvince int64 // 销售省份
|
||||||
GroupId int64 // 分组ID
|
GroupId int64 // 分组ID
|
||||||
|
GroupName string // 分组名称
|
||||||
|
Type uint8 // 用户类型
|
||||||
Valid bool
|
Valid bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,6 +46,8 @@ func GetUserFromCtx(ctx context.Context) UserInfo {
|
|||||||
saleName := md.Get("x-sale-name")
|
saleName := md.Get("x-sale-name")
|
||||||
saleId := md.Get("x-sale-id")
|
saleId := md.Get("x-sale-id")
|
||||||
groupId := md.Get("x-group-id")
|
groupId := md.Get("x-group-id")
|
||||||
|
groupName := md.Get("x-group-name")
|
||||||
|
Type := md.Get("x-user-type")
|
||||||
province := md.Get("x-sale-province")
|
province := md.Get("x-sale-province")
|
||||||
mobile := md.Get("x-sale-mobile")
|
mobile := md.Get("x-sale-mobile")
|
||||||
|
|
||||||
@@ -60,6 +64,15 @@ func GetUserFromCtx(ctx context.Context) UserInfo {
|
|||||||
if len(storeName) > NumberZero {
|
if len(storeName) > NumberZero {
|
||||||
userInfo.StoreName = storeName[NumberZero]
|
userInfo.StoreName = storeName[NumberZero]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(groupName) > NumberZero {
|
||||||
|
userInfo.GroupName = groupName[NumberZero]
|
||||||
|
}
|
||||||
|
if len(Type) > NumberZero {
|
||||||
|
TypeInt, _ := strconv.ParseInt(Type[NumberZero], NumberTen, NumberSixtyFourth)
|
||||||
|
userInfo.Type = uint8(TypeInt)
|
||||||
|
}
|
||||||
|
|
||||||
if len(storeId) > NumberZero {
|
if len(storeId) > NumberZero {
|
||||||
userInfo.StoreId, _ = strconv.ParseInt(storeId[NumberZero], NumberTen, NumberSixtyFourth)
|
userInfo.StoreId, _ = strconv.ParseInt(storeId[NumberZero], NumberTen, NumberSixtyFourth)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,11 @@ package dao
|
|||||||
|
|
||||||
import "lone-services/pkg/utils"
|
import "lone-services/pkg/utils"
|
||||||
|
|
||||||
|
const (
|
||||||
|
AdminTypeAdmin = 2
|
||||||
|
AdminTypeSale = 1
|
||||||
|
)
|
||||||
|
|
||||||
// Admin 公司内部人员表
|
// Admin 公司内部人员表
|
||||||
type Admin struct {
|
type Admin struct {
|
||||||
Id int64 `gorm:"column:id;type:int(11);primary_key;AUTO_INCREMENT;comment:ID" json:"id"`
|
Id int64 `gorm:"column:id;type:int(11);primary_key;AUTO_INCREMENT;comment:ID" json:"id"`
|
||||||
@@ -53,6 +58,9 @@ type AdminInfo struct {
|
|||||||
Type uint8 `json:"type" gorm:"type"` // 1:销售,2:管理员
|
Type uint8 `json:"type" gorm:"type"` // 1:销售,2:管理员
|
||||||
SaleId int64 `json:"sale_id" gorm:"sale_id"` // 销售ID
|
SaleId int64 `json:"sale_id" gorm:"sale_id"` // 销售ID
|
||||||
FailNumber int `gorm:"column:fail_number;type:tinyint(2);default:0;comment:连续登录失败次数" json:"fail_number"`
|
FailNumber int `gorm:"column:fail_number;type:tinyint(2);default:0;comment:连续登录失败次数" json:"fail_number"`
|
||||||
|
SaleName string `json:"sale_name" gorm:"-"`
|
||||||
|
GroupId int64 `json:"group_id" gorm:"-"`
|
||||||
|
GroupName string `json:"group_name" gorm:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type AdminInfoRet struct {
|
type AdminInfoRet struct {
|
||||||
|
|||||||
@@ -12,6 +12,11 @@ type JwtInfo struct {
|
|||||||
Time int `json:"time"`
|
Time int `json:"time"`
|
||||||
Roles []JwtInfoServices `json:"roles"`
|
Roles []JwtInfoServices `json:"roles"`
|
||||||
Refresh string `json:"refresh"`
|
Refresh string `json:"refresh"`
|
||||||
|
SaleId int64 `json:"sale_id" gorm:"sale_id"` // 销售ID
|
||||||
|
Type uint8 `json:"type" gorm:"type"` // 销售ID
|
||||||
|
SaleName string `json:"sale_name" gorm:"-"`
|
||||||
|
GroupId int64 `json:"group_id" gorm:"-"`
|
||||||
|
GroupName string `json:"group_name" gorm:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type JwtOutInfo struct {
|
type JwtOutInfo struct {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"lone-services/pkg/modelbase"
|
"lone-services/pkg/modelbase"
|
||||||
"lone-services/pkg/utils"
|
"lone-services/pkg/utils"
|
||||||
admin "lone-services/rpc/admin/pb"
|
admin "lone-services/rpc/admin/pb"
|
||||||
|
sale "lone-services/rpc/sale/pb"
|
||||||
"lone-services/services/admin/internal/dao"
|
"lone-services/services/admin/internal/dao"
|
||||||
"lone-services/services/admin/internal/model"
|
"lone-services/services/admin/internal/model"
|
||||||
"lone-services/services/admin/internal/svc"
|
"lone-services/services/admin/internal/svc"
|
||||||
@@ -57,6 +58,24 @@ func (l *AdminInfoLogic) AdminInfo(in *admin.AdminInfoRequest) (*admin.Response,
|
|||||||
roles = append(roles, r)
|
roles = append(roles, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if info.Type == dao.AdminTypeSale {
|
||||||
|
cli, err := svc.GetRpcClient(l.svcCtx.SaleSvcName)
|
||||||
|
if err != nil {
|
||||||
|
logx.Errorf("get rpc client err: %v", err)
|
||||||
|
return l.fail(utils.ErrorInternalServer)
|
||||||
|
}
|
||||||
|
saleClient := sale.NewSaleClient(cli.Conn())
|
||||||
|
saleInfo, err := saleClient.InfoInternal(context.Background(), &sale.InfoReq{Id: info.SaleId})
|
||||||
|
if err != nil {
|
||||||
|
logx.Errorf("get sale err: %v", err)
|
||||||
|
return l.fail(utils.ErrorInternalServer)
|
||||||
|
}
|
||||||
|
|
||||||
|
info.GroupName = saleInfo.Group.Name
|
||||||
|
info.GroupId = saleInfo.GroupId
|
||||||
|
info.SaleName = saleInfo.Name
|
||||||
|
}
|
||||||
|
|
||||||
retData := dao.AdminInfoRet{
|
retData := dao.AdminInfoRet{
|
||||||
AdminInfo: info,
|
AdminInfo: info,
|
||||||
Roles: roles,
|
Roles: roles,
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"lone-services/pkg/redis"
|
"lone-services/pkg/redis"
|
||||||
"lone-services/pkg/utils"
|
"lone-services/pkg/utils"
|
||||||
admin "lone-services/rpc/admin/pb"
|
admin "lone-services/rpc/admin/pb"
|
||||||
|
sale "lone-services/rpc/sale/pb"
|
||||||
"lone-services/services/admin/internal/dao"
|
"lone-services/services/admin/internal/dao"
|
||||||
"lone-services/services/admin/internal/model"
|
"lone-services/services/admin/internal/model"
|
||||||
"lone-services/services/admin/internal/svc"
|
"lone-services/services/admin/internal/svc"
|
||||||
@@ -69,10 +70,30 @@ func (l *LoginLogic) Login(in *admin.LoginRequest) (*admin.Response, error) {
|
|||||||
Id: info.Id,
|
Id: info.Id,
|
||||||
Name: info.Name,
|
Name: info.Name,
|
||||||
Email: info.Email,
|
Email: info.Email,
|
||||||
|
Type: info.Type,
|
||||||
Avatar: info.Avatar,
|
Avatar: info.Avatar,
|
||||||
LastTime: utils.Now(),
|
LastTime: utils.Now(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if info.Type == dao.AdminTypeSale {
|
||||||
|
cli, err := svc.GetRpcClient(l.svcCtx.SaleSvcName)
|
||||||
|
if err != nil {
|
||||||
|
logx.Errorf("get rpc client err: %v", err)
|
||||||
|
return l.fail(utils.ErrorInternalServer)
|
||||||
|
}
|
||||||
|
saleClient := sale.NewSaleClient(cli.Conn())
|
||||||
|
saleInfo, err := saleClient.InfoInternal(context.Background(), &sale.InfoReq{Id: info.SaleId})
|
||||||
|
if err != nil {
|
||||||
|
logx.Errorf("get sale err: %v", err)
|
||||||
|
return l.fail(utils.ErrorInternalServer)
|
||||||
|
}
|
||||||
|
|
||||||
|
data.GroupName = saleInfo.Group.Name
|
||||||
|
data.GroupId = saleInfo.GroupId
|
||||||
|
data.SaleId = saleInfo.Id
|
||||||
|
data.SaleName = saleInfo.Name
|
||||||
|
}
|
||||||
|
|
||||||
data.Phone, _ = utils.DecryptPhone(info.Phone)
|
data.Phone, _ = utils.DecryptPhone(info.Phone)
|
||||||
|
|
||||||
retData := dao.Token{
|
retData := dao.Token{
|
||||||
|
|||||||
@@ -1,22 +1,96 @@
|
|||||||
package svc
|
package svc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"lone-services/pkg/discovery"
|
||||||
"lone-services/pkg/utils"
|
"lone-services/pkg/utils"
|
||||||
"lone-services/services/admin/internal/config"
|
"lone-services/services/admin/internal/config"
|
||||||
|
"net"
|
||||||
|
"strconv"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
|
"github.com/zeromicro/go-zero/zrpc"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type rpcClientCacheEntry struct {
|
||||||
|
cli zrpc.Client
|
||||||
|
target string
|
||||||
|
expireAt time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
const rpcClientCacheTTL = 30 * time.Second //缓存过期时间
|
||||||
|
|
||||||
|
var (
|
||||||
|
rpcCacheLock sync.Mutex
|
||||||
|
rpcCache = make(map[string]*rpcClientCacheEntry)
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetRpcClient(serviceName string) (zrpc.Client, error) {
|
||||||
|
if serviceName == utils.StringEmpty {
|
||||||
|
err := fmt.Errorf("rpc serviceName is empty")
|
||||||
|
logx.Error(err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
cacheKey := serviceName
|
||||||
|
|
||||||
|
rpcCacheLock.Lock()
|
||||||
|
entry, ok := rpcCache[cacheKey]
|
||||||
|
if ok && time.Now().Before(entry.expireAt) {
|
||||||
|
rpcCacheLock.Unlock()
|
||||||
|
return entry.cli, nil
|
||||||
|
}
|
||||||
|
delete(rpcCache, cacheKey)
|
||||||
|
rpcCacheLock.Unlock()
|
||||||
|
|
||||||
|
inst, err := discovery.Pick(serviceName)
|
||||||
|
if err != nil {
|
||||||
|
err = fmt.Errorf("discovery pick %s failed: %w", serviceName, err)
|
||||||
|
logx.Error(err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
target := net.JoinHostPort(inst.IP, strconv.FormatUint(inst.Port, utils.NumberTen))
|
||||||
|
|
||||||
|
cli := zrpc.MustNewClient(zrpc.RpcClientConf{
|
||||||
|
Target: target,
|
||||||
|
Timeout: utils.RpcTimeOut, //rpc调用超时5s
|
||||||
|
})
|
||||||
|
|
||||||
|
rpcCacheLock.Lock()
|
||||||
|
rpcCache[cacheKey] = &rpcClientCacheEntry{
|
||||||
|
cli: cli,
|
||||||
|
target: target,
|
||||||
|
expireAt: time.Now().Add(rpcClientCacheTTL),
|
||||||
|
}
|
||||||
|
rpcCacheLock.Unlock()
|
||||||
|
|
||||||
|
return cli, nil
|
||||||
|
}
|
||||||
|
|
||||||
type ServiceContext struct {
|
type ServiceContext struct {
|
||||||
Config config.Config
|
Config config.Config
|
||||||
DB *gorm.DB
|
DB *gorm.DB
|
||||||
Prefix string
|
Prefix string
|
||||||
|
|
||||||
|
SaleSvcName string //服务名
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServiceContext(c config.Config, db *gorm.DB) *ServiceContext {
|
func NewServiceContext(c config.Config, db *gorm.DB) *ServiceContext {
|
||||||
|
//启动仅读取配置,不建立rpc连接 多个服务就多个
|
||||||
|
SaleSvc := utils.GetConfigString("services.sale")
|
||||||
|
|
||||||
|
if SaleSvc == utils.StringEmpty {
|
||||||
|
logx.Error("config services.sale empty")
|
||||||
|
}
|
||||||
|
|
||||||
return &ServiceContext{
|
return &ServiceContext{
|
||||||
Config: c,
|
Config: c,
|
||||||
DB: db,
|
DB: db,
|
||||||
Prefix: utils.GetConfigString("mysql.prefix"),
|
Prefix: utils.GetConfigString("mysql.prefix"),
|
||||||
|
SaleSvcName: SaleSvc,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user