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

58 lines
1.5 KiB

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. TxImHost = "https://console.tim.qq.com"
  16. )
  17. type AccountImportResult struct {
  18. ActionStatus string `json:"ActionStatus"`
  19. ErrorCode int `json:"ErrorCode"`
  20. ErrorInfo string `json:"ErrorInfo"`
  21. }
  22. type AccountImportParam struct {
  23. Identifier string `json:"Identifier"`
  24. Nick string `json:"Nick"`
  25. FaceUrl string `json:"FaceUrl"`
  26. }
  27. func AccountImport(identifier, nickname, faceurl string) (string, error) {
  28. sign, err := tencentyun.GenSig(SdkAppid, SdkAppKey, "admin", 86400*180)
  29. if err != nil {
  30. return "", err
  31. }
  32. random := utils.RandomStr(32)
  33. u := fmt.Sprintf("%s/v4/im_open_login_svc/account_import?sdkappid=%d&identifier=admin&usersig=%s&random=%s&contenttype=json", TxImHost, SdkAppid, sign, random)
  34. bs, _ := json.Marshal(&AccountImportParam{
  35. Identifier: identifier,
  36. Nick: nickname,
  37. FaceUrl: faceurl,
  38. })
  39. resp, err := HttpRequest.NewRequest().Debug(true).JSON().Post(u, string(bs))
  40. if err != nil {
  41. return "", err
  42. }
  43. res := AccountImportResult{}
  44. err = resp.Json(&res)
  45. if err != nil {
  46. return "", err
  47. }
  48. if res.ErrorCode != 0 {
  49. return "", errors.New(res.ErrorInfo)
  50. }
  51. return tencentyun.GenSig(SdkAppid, SdkAppKey, identifier, 86400000)
  52. }