create_whatsapp_logic.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package whatsapp
  2. import (
  3. "context"
  4. "wechat-api/ent"
  5. "wechat-api/ent/whatsapp"
  6. "wechat-api/hook/aliyun"
  7. "wechat-api/internal/svc"
  8. "wechat-api/internal/types"
  9. "wechat-api/internal/utils/dberrorhandler"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. )
  12. type CreateWhatsappLogic struct {
  13. ctx context.Context
  14. svcCtx *svc.ServiceContext
  15. logx.Logger
  16. }
  17. func NewCreateWhatsappLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateWhatsappLogic {
  18. return &CreateWhatsappLogic{
  19. ctx: ctx,
  20. svcCtx: svcCtx,
  21. Logger: logx.WithContext(ctx),
  22. }
  23. }
  24. func (l *CreateWhatsappLogic) CreateWhatsapp(req *types.WhatsappInfo) (*types.BaseMsgResp, error) {
  25. add, err := aliyun.AddCamsPhoneNumber(*req.Phone, *req.Cc, *req.WaId, *req.PhoneName)
  26. l.Logger.Infof("add=%v err=%v\n", add, err)
  27. if err == nil && *add.Body.Code == "OK" {
  28. //var stringSlice []string
  29. _, err := l.svcCtx.DB.Whatsapp.Query().
  30. Where(whatsapp.WaID(*req.WaId)).
  31. Where(whatsapp.Phone(*req.Phone)).
  32. Where(whatsapp.Cc(*req.Cc)).
  33. First(l.ctx)
  34. if err != nil && ent.IsNotFound(err) {
  35. _, err = l.svcCtx.DB.Whatsapp.Create().
  36. SetNotNilWaID(req.WaId).
  37. SetNotNilCallback(req.Callback).
  38. SetNotNilAccount(req.Account).
  39. SetNotNilPhone(req.Phone).
  40. SetNotNilCc(req.Cc).
  41. SetNotNilPhoneName(req.PhoneName).
  42. SetNotNilOrganizationID(req.OrganizationId).
  43. //SetNotNilAgentID(req.AgentId).
  44. //SetNotNilAPIBase(req.ApiBase).
  45. //SetNotNilAPIKey(req.ApiKey).
  46. //SetNotNilAllowList(stringSlice).
  47. //SetNotNilGroupAllowList(stringSlice).
  48. //SetNotNilBlockList(stringSlice).
  49. //SetNotNilGroupBlockList(stringSlice).
  50. Save(l.ctx)
  51. if err != nil {
  52. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  53. }
  54. _ = l.svcCtx.Rds.Del(l.ctx, "wa_info")
  55. }
  56. }
  57. resp := types.BaseMsgResp{}
  58. if err != nil {
  59. resp.Msg = err.Error()
  60. resp.Code = 1
  61. } else {
  62. resp.Msg = "添加成功"
  63. }
  64. return &resp, nil
  65. }