package avatar import ( "context" "encoding/json" "fmt" avatar20220130 "github.com/alibabacloud-go/avatar-20220130/v2/client" "github.com/suyuan32/simple-admin-common/msg/errormsg" "github.com/zeromicro/go-zero/core/errorx" "strconv" "wechat-api/ent/wxcard" "wechat-api/hook/aliyun" "wechat-api/internal/utils/dberrorhandler" "wechat-api/internal/svc" "wechat-api/internal/types" "github.com/zeromicro/go-zero/core/logx" ) type GetApiAvatarConfigLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewGetApiAvatarConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetApiAvatarConfigLogic { return &GetApiAvatarConfigLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx} } func (l *GetApiAvatarConfigLogic) GetApiAvatarConfig(req *types.AvatarConfigReq) (*types.AvatarConfigResp, error) { userId := l.ctx.Value("userId").(uint64) card, err := l.svcCtx.DB.WxCard.Query().Where(wxcard.ID(*req.CardId)).Only(l.ctx) if err != nil { return nil, dberrorhandler.DefaultEntError(l.Logger, err, req) } if card.AiInfo == "" { return nil, errorx.NewInvalidArgumentError("AiInfo is empty") } var jsonData aliyun.AiTypes err = json.Unmarshal([]byte(card.AiInfo), &jsonData) if err != nil { return nil, errorx.NewInvalidArgumentError(err.Error()) } var aiData *avatar20220130.StartInstanceResponseBodyData if jsonData.AiType == 1 { // 阿里云数字人 userIdStr := strconv.Itoa(int(userId)) aiData, err = aliyun.StartInstance(userIdStr, aliyun.GetBizID(userId)) fmt.Printf("aliData=%v error=%v \n", aiData, err) if err != nil { return nil, errorx.NewInternalError(err.Error()) } } else if jsonData.AiType == 2 { //TODO 小冰 } return &types.AvatarConfigResp{ BaseDataInfo: types.BaseDataInfo{ Code: 0, Msg: errormsg.Success, }, Data: types.AvatarInfo{ RequestId: aiData.RequestId, SessionId: aiData.SessionId, Token: aiData.Token, Channel: types.Channel{ ChannelId: aiData.Channel.ChannelId, Token: aiData.Channel.Token, Type: aiData.Channel.Type, ExpiredTime: aiData.Channel.ExpiredTime, Nonce: aiData.Channel.Nonce, UserId: aiData.Channel.UserId, AppId: aiData.Channel.AppId, UserInfoInChannel: aiData.Channel.UserInfoInChannel, Gslb: aiData.Channel.Gslb, }, }, }, nil }