43 lines
1.2 KiB
Go
43 lines
1.2 KiB
Go
package client
|
||
|
||
import (
|
||
"context"
|
||
|
||
pbCommon "github.com/topfans/backend/pkg/proto/common"
|
||
pbUser "github.com/topfans/backend/pkg/proto/user"
|
||
)
|
||
|
||
// UserServiceClient 封装 userService 的 Dubbo 调用
|
||
type UserServiceClient struct {
|
||
userSocial pbUser.UserSocialService
|
||
}
|
||
|
||
// NewUserServiceClient 构造
|
||
func NewUserServiceClient(svc pbUser.UserSocialService) *UserServiceClient {
|
||
return &UserServiceClient{userSocial: svc}
|
||
}
|
||
|
||
// GetCrystalBalance 调 userService.GetFanProfile 取 crystal_balance
|
||
// (userService 没单独 GetCrystalBalance,FanProfile 含此字段)
|
||
// 注意: StatusCode_STATUS_OK == 200(不是 0!),之前用 resp.Base.Code != 0
|
||
// 检查等价于"任何成功响应都被当作错误",导致永远返回 0
|
||
func (c *UserServiceClient) GetCrystalBalance(ctx context.Context, userID, starID int64) (int64, error) {
|
||
resp, err := c.userSocial.GetFanProfile(ctx, &pbUser.GetFanProfileRequest{
|
||
UserId: userID,
|
||
StarId: starID,
|
||
})
|
||
if err != nil {
|
||
return 0, err
|
||
}
|
||
if resp == nil || resp.Base == nil {
|
||
return 0, nil
|
||
}
|
||
if resp.Base.Code != pbCommon.StatusCode_STATUS_OK {
|
||
return 0, nil
|
||
}
|
||
if resp.Profile == nil {
|
||
return 0, nil
|
||
}
|
||
return resp.Profile.CrystalBalance, nil
|
||
}
|