request.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package external
  2. import (
  3. "context"
  4. "os"
  5. "github.com/rs/zerolog/log"
  6. )
  7. type UnreadMessageRequest struct {
  8. Type string `json:"type"` // 向谁发的消息:shop(门店), customer(客户)
  9. ShopId int32 `json:"shopId"`
  10. UserId int32 `json:"userId"`
  11. MsgCount int `json:"msgCount"`
  12. }
  13. // 请求函数 -- 根据请求项目选择请求方式
  14. func RequestToPHP(receiver string, shopId int32, userId int32, count int) error {
  15. var project string
  16. if receiver == "shop" {
  17. project = "huaZhangGui"
  18. } else if receiver == "customer" {
  19. project = "huaHuiBao"
  20. }
  21. ctx := context.Background()
  22. switch project {
  23. case "huaZhangGui":
  24. //url := os.Getenv("HUA_ZHANG_GUI_URL")
  25. url := os.Getenv("HUA_HUI_BAO_URL")
  26. reqData := UnreadMessageRequest{
  27. Type: "shop",
  28. ShopId: shopId,
  29. UserId: userId,
  30. MsgCount: count,
  31. }
  32. huaZhangGuiClient := NewHuaZhangGuiClient(url)
  33. res, err := huaZhangGuiClient.PostUnReadMessage(ctx, reqData)
  34. if err != nil {
  35. log.Error().Str("receiverType", receiver).Int32("receiverID", shopId).Int("count", count).Err(err).Msg("向 PHP 服务发送未读消息通知失败")
  36. return err
  37. }
  38. log.Info().Str("receiverType", receiver).Int32("receiverID", shopId).Int("count", count).Str("res", res).Msg("向 PHP 服务发送未读消息通知成功")
  39. case "huaHuiBao":
  40. ////url := os.Getenv("HUA_HUI_BAO_URL")
  41. // url := os.Getenv("HUA_ZHANG_GUI_URL")
  42. // reqData := UnreadMessageRequest{
  43. // Type: "customer",
  44. // Id: receiverID,
  45. // MsgCount: count,
  46. // }
  47. // huaHuiBaoClient := NewHuaHuiBaoClient(url)
  48. // res, err := huaHuiBaoClient.PostUnReadMessage(ctx, reqData)
  49. // if err != nil {
  50. // log.Error().Str("receiverType", receiverType).Int32("receiverID", receiverID).Int("count", count).Err(err).Msg("向 PHP 服务发送未读消息通知失败")
  51. // return err
  52. // }
  53. // log.Info().Str("receiverType", receiverType).Int32("receiverID", receiverID).Int("count", count).Str("res", res).Msg("向 PHP 服务发送未读消息通知成功")
  54. }
  55. return nil
  56. }