get_api_avatar_config_logic.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package avatar
  2. import (
  3. "context"
  4. "encoding/json"
  5. avatar20220130 "github.com/alibabacloud-go/avatar-20220130/v2/client"
  6. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  7. "github.com/zeromicro/go-zero/core/errorx"
  8. "strconv"
  9. "wechat-api/ent/wxcard"
  10. "wechat-api/hook/aliyun"
  11. "wechat-api/internal/utils/dberrorhandler"
  12. "wechat-api/internal/svc"
  13. "wechat-api/internal/types"
  14. "github.com/zeromicro/go-zero/core/logx"
  15. )
  16. type GetApiAvatarConfigLogic struct {
  17. logx.Logger
  18. ctx context.Context
  19. svcCtx *svc.ServiceContext
  20. }
  21. func NewGetApiAvatarConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetApiAvatarConfigLogic {
  22. return &GetApiAvatarConfigLogic{
  23. Logger: logx.WithContext(ctx),
  24. ctx: ctx,
  25. svcCtx: svcCtx}
  26. }
  27. func (l *GetApiAvatarConfigLogic) GetApiAvatarConfig(req *types.AvatarConfigReq) (*types.AvatarConfigResp, error) {
  28. userId := l.ctx.Value("userId").(uint64)
  29. card, err := l.svcCtx.DB.WxCard.Query().Where(wxcard.ID(*req.CardId)).Only(l.ctx)
  30. if err != nil {
  31. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  32. }
  33. if card.AiInfo == "" {
  34. return nil, errorx.NewInvalidArgumentError("AiInfo is empty")
  35. }
  36. var jsonData aliyun.AiTypes
  37. err = json.Unmarshal([]byte(card.AiInfo), &jsonData)
  38. if err != nil {
  39. return nil, errorx.NewInvalidArgumentError(err.Error())
  40. }
  41. var aiData *avatar20220130.StartInstanceResponseBodyData
  42. if jsonData.AiType == 1 { //TODO 阿里云数字人
  43. userIdStr := strconv.Itoa(int(userId))
  44. aiData, err = aliyun.StartInstance(userIdStr, aliyun.GetBizID(userId))
  45. if err != nil {
  46. return nil, errorx.NewInternalError(err.Error())
  47. }
  48. } else if jsonData.AiType == 2 { //TODO 小冰
  49. }
  50. return &types.AvatarConfigResp{
  51. BaseDataInfo: types.BaseDataInfo{
  52. Code: 0,
  53. Msg: errormsg.Success,
  54. },
  55. Data: types.AvatarInfo{
  56. RequestId: aiData.RequestId,
  57. SessionId: aiData.SessionId,
  58. Token: aiData.Token,
  59. Channel: types.Channel{
  60. ChannelId: aiData.Channel.ChannelId,
  61. Token: aiData.Channel.Token,
  62. Type: aiData.Channel.Type,
  63. ExpiredTime: aiData.Channel.ExpiredTime,
  64. Nonce: aiData.Channel.Nonce,
  65. UserId: aiData.Channel.UserId,
  66. AppId: aiData.Channel.AppId,
  67. UserInfoInChannel: aiData.Channel.UserInfoInChannel,
  68. Gslb: aiData.Channel.Gslb,
  69. },
  70. },
  71. }, nil
  72. }