互动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.2 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package im
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/kirinlabs/HttpRequest"
  7. "github.com/tencentyun/tls-sig-api-v2-golang/tencentyun"
  8. "hudongzhuanjia/utils"
  9. )
  10. var (
  11. //SdkAppid = 1400345581
  12. //SdkAppKey = "e3d095ce8253fe18109d6c1ad068907978cac0a428fbad181d8958631b78e930"
  13. SdkAppKey = "9683fc9d2e50857f33c7ef5431942458f2dbc7042ba526f87042092afb646331"
  14. SdkAppid = 1400337419
  15. TxImHostV4 = "https://console.tim.qq.com/v4"
  16. )
  17. type NoticeType int
  18. const NoticeLiveRedPackStart NoticeType = 256 // 通知直播用户红包开始了
  19. const NoticeLiveRedPackEnd NoticeType = 257 // 通知直播用户红包结束了
  20. const NoticeShakeRedPackStart NoticeType = 258 // 通知摇红包开始了
  21. const NoticeShakeRedPackEnd NoticeType = 259 // 通知摇红包结束了
  22. type CommonResult struct {
  23. ActionStatus string `json:"ActionStatus"`
  24. ErrorCode int `json:"ErrorCode"`
  25. ErrorInfo string `json:"ErrorInfo"`
  26. }
  27. type AccountImportParam struct {
  28. Identifier string `json:"Identifier"`
  29. Nick string `json:"Nick"`
  30. FaceUrl string `json:"FaceUrl"`
  31. }
  32. func GenSig(identifier string) (string, error) {
  33. return tencentyun.GenSig(SdkAppid, SdkAppKey, identifier, 86400000)
  34. }
  35. func AccountImport(identifier, nickname, faceurl string) (string, error) {
  36. //sign, err := tencentyun.GenSig(SdkAppid, SdkAppKey, "admin", 86400*180)
  37. sig, err := GenSig("admin")
  38. if err != nil {
  39. return "", err
  40. }
  41. random := utils.RandomStr(32)
  42. u := fmt.Sprintf("%s/im_open_login_svc/account_import?sdkappid=%d&identifier=admin&usersig=%s&random=%s&contenttype=json",
  43. TxImHostV4, SdkAppid, sig, random)
  44. bs, _ := json.Marshal(&AccountImportParam{
  45. Identifier: identifier,
  46. Nick: nickname,
  47. FaceUrl: faceurl,
  48. })
  49. resp, err := HttpRequest.NewRequest().Debug(true).JSON().Post(u, string(bs))
  50. if err != nil {
  51. return "", err
  52. }
  53. res := CommonResult{}
  54. err = resp.Json(&res)
  55. if err != nil {
  56. return "", err
  57. }
  58. if res.ErrorCode != 0 {
  59. return "", errors.New(res.ErrorInfo)
  60. }
  61. //return tencentyun.GenSig(SdkAppid, SdkAppKey, identifier, 86400000)
  62. return GenSig(identifier)
  63. }
  64. // 群通知
  65. type SendGroupSystemNotificationParam struct {
  66. GroupId string `json:"GroupId"`
  67. Content string `json:"Content"`
  68. ToMembersAccount []string `json:"ToMembers_Account,omitempty"`
  69. }
  70. func SendGroupSystemNotification(groupId string, noticeType NoticeType, notice string, members ...string) error {
  71. sig, err := GenSig("admin")
  72. if err != nil {
  73. return err
  74. }
  75. random := utils.RandomStr(32)
  76. u := fmt.Sprintf("%s/group_open_http_svc/send_group_system_notification?sdkappid=%d&identifier=admin&usersig=%s&random=%s&contenttype=json",
  77. TxImHostV4, SdkAppid, sig, random)
  78. var m = make(map[string]interface{})
  79. m["type"] = noticeType
  80. m["notice"] = notice
  81. content, err := json.Marshal(&m)
  82. if err != nil {
  83. return err
  84. }
  85. bs, err := json.Marshal(&SendGroupSystemNotificationParam{
  86. GroupId: groupId,
  87. Content: string(content),
  88. ToMembersAccount: members,
  89. })
  90. if err != nil {
  91. return err
  92. }
  93. resp, err := HttpRequest.NewRequest().Debug(true).JSON().Post(u, string(bs))
  94. res := CommonResult{}
  95. err = resp.Json(&res)
  96. if err != nil {
  97. return err
  98. }
  99. if res.ErrorCode != 0 {
  100. return errors.New(res.ErrorInfo)
  101. }
  102. return nil
  103. }