互动
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.

118 lines
3.3 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 NoticeLiveRedPackGet NoticeType = 258 // 某人摇中红包
  21. const NoticeShakeRedPackStart NoticeType = 259 // 通知摇红包开始了
  22. const NoticeShakeRedPackEnd NoticeType = 260 // 通知摇红包结束了
  23. type CommonResult struct {
  24. ActionStatus string `json:"ActionStatus"`
  25. ErrorCode int `json:"ErrorCode"`
  26. ErrorInfo string `json:"ErrorInfo"`
  27. }
  28. type AccountImportParam struct {
  29. Identifier string `json:"Identifier"`
  30. Nick string `json:"Nick"`
  31. FaceUrl string `json:"FaceUrl"`
  32. }
  33. func GenSig(identifier string) (string, error) {
  34. return tencentyun.GenSig(SdkAppid, SdkAppKey, identifier, 86400000)
  35. }
  36. func AccountImport(identifier, nickname, faceurl string) (string, error) {
  37. //sign, err := tencentyun.GenSig(SdkAppid, SdkAppKey, "admin", 86400*180)
  38. sig, err := GenSig("admin")
  39. if err != nil {
  40. return "", err
  41. }
  42. random := utils.RandomStr(32)
  43. u := fmt.Sprintf("%s/im_open_login_svc/account_import?sdkappid=%d&identifier=admin&usersig=%s&random=%s&contenttype=json",
  44. TxImHostV4, SdkAppid, sig, random)
  45. bs, _ := json.Marshal(&AccountImportParam{
  46. Identifier: identifier,
  47. Nick: nickname,
  48. FaceUrl: faceurl,
  49. })
  50. resp, err := HttpRequest.NewRequest().Debug(true).JSON().Post(u, string(bs))
  51. if err != nil {
  52. return "", err
  53. }
  54. res := CommonResult{}
  55. err = resp.Json(&res)
  56. if err != nil {
  57. return "", err
  58. }
  59. if res.ErrorCode != 0 {
  60. return "", errors.New(res.ErrorInfo)
  61. }
  62. //return tencentyun.GenSig(SdkAppid, SdkAppKey, identifier, 86400000)
  63. return GenSig(identifier)
  64. }
  65. // 群通知
  66. type SendGroupSystemNotificationParam struct {
  67. GroupId string `json:"GroupId"`
  68. Content string `json:"Content"`
  69. ToMembersAccount []string `json:"ToMembers_Account,omitempty"`
  70. }
  71. func SendGroupSystemNotification(groupId string, noticeType NoticeType, data map[string]interface{}, members ...string) error {
  72. sig, err := GenSig("admin")
  73. if err != nil {
  74. return err
  75. }
  76. random := utils.RandomStr(32)
  77. u := fmt.Sprintf("%s/group_open_http_svc/send_group_system_notification?sdkappid=%d&identifier=admin&usersig=%s&random=%s&contenttype=json",
  78. TxImHostV4, SdkAppid, sig, random)
  79. var m = make(map[string]interface{})
  80. m["type"] = noticeType
  81. m["data"] = data
  82. content, err := json.Marshal(&m)
  83. if err != nil {
  84. return err
  85. }
  86. bs, err := json.Marshal(&SendGroupSystemNotificationParam{
  87. GroupId: groupId,
  88. Content: string(content),
  89. ToMembersAccount: members,
  90. })
  91. if err != nil {
  92. return err
  93. }
  94. resp, err := HttpRequest.NewRequest().Debug(true).JSON().Post(u, string(bs))
  95. res := CommonResult{}
  96. err = resp.Json(&res)
  97. if err != nil {
  98. return err
  99. }
  100. if res.ErrorCode != 0 {
  101. return errors.New(res.ErrorInfo)
  102. }
  103. return nil
  104. }