create_whatsapp_logic.go 2.3 KB

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