send_api_avatar_text_logic.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package avatar
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. avatar20220130 "github.com/alibabacloud-go/avatar-20220130/v2/client"
  7. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  8. "github.com/zeromicro/go-zero/core/errorx"
  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 SendApiAvatarTextLogic struct {
  17. logx.Logger
  18. ctx context.Context
  19. svcCtx *svc.ServiceContext
  20. }
  21. func NewSendApiAvatarTextLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendApiAvatarTextLogic {
  22. return &SendApiAvatarTextLogic{
  23. Logger: logx.WithContext(ctx),
  24. ctx: ctx,
  25. svcCtx: svcCtx,
  26. }
  27. }
  28. func (l *SendApiAvatarTextLogic) SendApiAvatarText(req *types.SendTextReq) (*types.SendTextResp, error) {
  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.SendTextResponseBodyData
  42. aiData, err = aliyun.SendText(*req.SessionId, *req.Text, *req.IsStream, *req.Index, *req.Position, jsonData)
  43. fmt.Printf("aliData=%v error=%v \n", aiData, err)
  44. if err != nil {
  45. return nil, errorx.NewInternalError(err.Error())
  46. }
  47. return &types.SendTextResp{
  48. BaseDataInfo: types.BaseDataInfo{
  49. Code: 0,
  50. Msg: errormsg.Success,
  51. },
  52. Data: types.SendText{
  53. UniqueCode: aiData.UniqueCode,
  54. SessionId: aiData.SessionId,
  55. },
  56. }, nil
  57. }