agora.go 20.3 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
package agora

import (
	"bytes"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"git.hilo.cn/hilo-common/mylogrus"
	"git.hilo.cn/hilo-common/resource/config"
	rtctokenbuilder "github.com/AgoraIO/Tools/DynamicKey/AgoraDynamicKey/go/src/RtcTokenBuilder"
	"io/ioutil"
	"net/http"
	"strconv"
	"time"
)

const urlBase = "https://api.agora.io/v1/apps/"

//建立MatchConfirm
func CreateAgora(channelId string, expireTimestamp uint32, agoraId1 uint32, agoraId2 uint32) (string, string, uint32, string, uint32, error) {
	appId := config.GetAgoraAppId()
	appCertificate := config.GetAgoraAppCertificate()
	//channelId := strings.Replace(uuid.NewV4().String(), "-", "", -1)

	//expireTimeInSeconds := uint32(60)
	//currentTimestamp := uint32(time.Now().UTC().Unix())
	//expireTimestamp := currentTimestamp + expireTimeInSeconds

	token1, err := rtctokenbuilder.BuildTokenWithUID(appId, appCertificate, channelId, agoraId1, rtctokenbuilder.RoleAttendee, expireTimestamp)
	if err != nil {
		mylogrus.MyLog.Printf("Fail to obtain token for user 1 %v", err.Error())
		return "", "", 0, "", 0, err
	}

	token2, err := rtctokenbuilder.BuildTokenWithUID(appId, appCertificate, channelId, agoraId2, rtctokenbuilder.RoleAttendee, expireTimestamp)
	if err != nil {
		mylogrus.MyLog.Printf("Fail to obtain token for user 2 %v", err.Error())
		return "", "", 0, "", 0, err
	}
	return channelId, token1, agoraId1, token2, agoraId2, nil
}

//创建群组的声网通道,token有效时长:一个星期, 参数:userId uint32, channelId,业务保证唯一性。返回:channelId, token, err
func CreateGroupAgora(channelId string, uid uint32) (string, string, error) {
	appId := config.GetAgoraAppId()
	appCertificate := config.GetAgoraAppCertificate()
	//channelId := strings.Replace(uuid.NewV4().String(), "-", "", -1)

	expireTimeInSeconds := uint32(60 * 60 * 24 * 7) //一个星期
	currentTimestamp := uint32(time.Now().UTC().Unix())
	expireTimestamp := currentTimestamp + expireTimeInSeconds

	token, err := rtctokenbuilder.BuildTokenWithUID(appId, appCertificate, channelId, uid, rtctokenbuilder.RoleAttendee, expireTimestamp)
	if err != nil {
		mylogrus.MyLog.Printf("Fail to obtain token for user 1 %v", err.Error())
		return "", "", err
	}
	return channelId, token, nil
}

// 生成RESTful API 凭证
func createCredential() string {
	customerKey := config.GetAgoraCustomerKey()
	customerSecret := config.GetAgoraCustomerSecret()

	// 拼接客户 ID 和客户密钥并使用 base64 进行编码
	plainCredentials := customerKey + ":" + customerSecret
	base64Credentials := base64.StdEncoding.EncodeToString([]byte(plainCredentials))

	mylogrus.MyLog.Infof("agora customerKey :%s customerSecret:%s encodeString:%s", customerKey, customerSecret, base64Credentials)
	return base64Credentials
}

type AcquireResourceBody struct {
	ResourceId string `json:"resourceId"`
}

type StartResourceBody struct {
	Sid        string `json:"sid"`
	ResourceId string `json:"resourceId"`
}

type FileListItem struct {
	Filename       string `json:"filename"`
	TrackType      string `json:"trackType"`
	Uid            string `json:"uid"`
	MixedAllUser   bool   `json:"mixedAllUser"`
	IsPlayable     bool   `json:"isPlayable"`
	SliceStartTime int64  `json:"sliceStartTime"`
}

type ServerResponse struct {
	FileListMode    string         `json:"fileListMode"`
	FileList        []FileListItem `json:"fileList"`
	UploadingStatus string         `json:"uploadingStatus"`
}

type StopResponseBody struct {
	ResourceId     string         `json:"resourceId"`
	Sid            string         `json:"sid"`
	ServerResponse ServerResponse `json:"serverResponse"`
}

func generateUid() uint32 {
	// 1609430400就是2021-01-01 00:00:00
	return uint32(time.Now().Unix() - 1609430400)
}

func GetRecordPathPrefix() string {
	return "nextvideo/agoraRecording/"
}

//获取资源Id
func acquireResourceId(cname string) (string, uint32, error) {
	appId := config.GetAgoraAppId()
	url := urlBase + appId + "/cloud_recording/acquire"

	clientRequest := make(map[string]interface{})
	clientRequest["resourceExpiredHour"] = 24
	clientRequest["scene"] = 2 // 申请延时转码资源

	params := make(map[string]interface{})
	params["cname"] = cname
	uid := generateUid()
	params["uid"] = strconv.FormatUint(uint64(uid), 10)
	params["clientRequest"] = clientRequest
	bytesData, err := json.Marshal(params)
	if err != nil {
		return "", 0, err
	}
	reader := bytes.NewReader(bytesData)
	//mylogrus.MyLog.Infof("agora api:acquire url:%v, param:%v", url, string(bytesData))

	client := &http.Client{}
	req, err := http.NewRequest("POST", url, reader)
	if err != nil {
		mylogrus.MyLog.Errorf("agora api:acquire request err:%v", err)
		return "", 0, err
		// handle error
	}
	req.Header.Set("Content-Type", "application/json;charset=utf-8")
	req.Header.Set("Authorization", "Basic "+createCredential())
	resp, err := client.Do(req)
	if err != nil {
		mylogrus.MyLog.Errorf("agora api:acquire do err:%v", err)
		return "", 0, err
	}
	defer resp.Body.Close()

	mylogrus.MyLog.Infof("req header: %v, StatusCode = %d, %s", req.Header, resp.StatusCode, resp.Status)

	if resp.StatusCode != http.StatusOK {
		return "", 0, fmt.Errorf("%w", resp.Status)
	}

	body, err := ioutil.ReadAll(resp.Body)
	mylogrus.MyLog.Infof("agora api:acquire resp body:%v", string(body))
	if err != nil {
		mylogrus.MyLog.Errorf("agora api:acquire resp err:%v", err)
		return "", 0, err
	}

	var resourceBody AcquireResourceBody
	if err := json.Unmarshal(body, &resourceBody); err != nil {
		mylogrus.MyLog.Errorf("agora api:acquire resp toStruts err:%v, body:%v", err, string(body))
		return "", 0, err
	}

	mylogrus.MyLog.Infof("agora api:acquire resp success sid:%v", resourceBody.ResourceId)
	return resourceBody.ResourceId, uid, nil
}

func StartRecording(token string, cname string) (string, string, uint32, error) {
	mylogrus.MyLog.Infof("agora start recording, token: %s, cname: %s", token, cname)

	resourceId, uid, err := acquireResourceId(cname)
	if err != nil {
		return "", "", 0, err
	}
	appId := config.GetAgoraAppId()
	url := urlBase + appId + "/cloud_recording/resourceid/" + resourceId + "/mode/individual/start"

	appsCollection := make(map[string]interface{})
	appsCollection["combinationPolicy"] = "postpone_transcoding" // 打开延时转码

	recordingConfig := map[string]interface{}{
		"channelType":       0,
		"streamTypes":       2,
		"subscribeUidGroup": 0,
		"maxIdleTime":       5,
	}

	/*
		snapshotConfig := map[string]interface{}{
			"captureInterval": 5,
			"fileType":        []string{"jpg"},
		}*/

	storageConfig := map[string]interface{}{
		"vendor":         2,
		"region":         6,
		"bucket":         config.GetConfigOss().OSS_BUCKET,
		"accessKey":      config.GetConfigOss().OSS_ACCESS_KEY_ID,
		"secretKey":      config.GetConfigOss().OSS_ACCESS_KEY_SECRET,
		"fileNamePrefix": []string{"nextvideo", "agoraRecording"},
	}

	clientRequest := map[string]interface{}{
		"token":           token,
		"appsCollection":  appsCollection,
		"recordingConfig": recordingConfig,
		//"snapshotConfig":  snapshotConfig,
		"storageConfig": storageConfig,
	}

	params := make(map[string]interface{})
	params["cname"] = cname
	params["uid"] = strconv.FormatUint(uint64(uid), 10)
	params["clientRequest"] = clientRequest

	bytesData, err := json.Marshal(params)
	if err != nil {
		return "", "", 0, err
	}
	reader := bytes.NewReader(bytesData)
	mylogrus.MyLog.Infof("agora start recording, url:%s, param:%s", url, string(bytesData))

	client := &http.Client{}
	req, err := http.NewRequest("POST", url, reader)
	if err != nil {
		mylogrus.MyLog.Errorf("agora api:start request err:%v", err)
		return "", "", 0, err
		// handle error
	}
	req.Header.Set("Content-Type", "application/json;charset=utf-8")
	req.Header.Set("Authorization", "Basic "+createCredential())
	resp, err := client.Do(req)
	if err != nil {
		mylogrus.MyLog.Errorf("agora api:start do err:%v", err)
		return "", "", 0, err
	}
	defer resp.Body.Close()

	mylogrus.MyLog.Infof("req header: %v, StatusCode = %d, %s", req.Header, resp.StatusCode, resp.Status)

	if resp.StatusCode != http.StatusOK {
		return "", "", 0, fmt.Errorf("%w", resp.Status)
	}

	body, err := ioutil.ReadAll(resp.Body)
	mylogrus.MyLog.Infof("agora api:start resp body:%v", string(body))
	if err != nil {
		mylogrus.MyLog.Errorf("agora api:start resp err:%v", err)
		return "", "", 0, err
	}

	var resourceBody StartResourceBody
	if err := json.Unmarshal(body, &resourceBody); err != nil {
		mylogrus.MyLog.Errorf("agora api:start resp toStruts err:%v, body:%v", err, string(body))
		return "", "", 0, err
	}
	return resourceId, resourceBody.Sid, uid, nil
}

func StopRecording(token, resourceId, cname, sid string, uid uint32) (*StopResponseBody, error) {
	mylogrus.MyLog.Debugf("agora stop recording token: %s, resourceId: %s, cname: %s, sid: %s",
		token, resourceId, cname, sid)

	appId := config.GetAgoraAppId()
	url := urlBase + appId + "/cloud_recording/resourceid/" + resourceId + "/sid/" + sid + "/mode/individual/stop"

	clientRequest := map[string]interface{}{}

	params := make(map[string]interface{})
	params["cname"] = cname
	params["uid"] = strconv.FormatUint(uint64(uid), 10)
	params["clientRequest"] = clientRequest

	bytesData, err := json.Marshal(params)
	if err != nil {
		return nil, err
	}
	reader := bytes.NewReader(bytesData)
	mylogrus.MyLog.Debugf("agora stop recording, url:%s, param:%s", url, string(bytesData))

	client := &http.Client{}
	req, err := http.NewRequest("POST", url, reader)
	if err != nil {
		mylogrus.MyLog.Errorf("agora api:stop request err:%v", err)
		return nil, err
		// handle error
	}
	req.Header.Set("Content-Type", "application/json;charset=utf-8")
	req.Header.Set("Authorization", "Basic "+createCredential())
	resp, err := client.Do(req)
	if err != nil {
		mylogrus.MyLog.Errorf("agora api:stop do err:%v", err)
		return nil, err
	}
	defer resp.Body.Close()

	mylogrus.MyLog.Debugf("req header: %v, StatusCode = %d, %s", req.Header, resp.StatusCode, resp.Status)

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("%w", resp.Status)
	}

	body, err := ioutil.ReadAll(resp.Body)
	mylogrus.MyLog.Debugf("agora api:stop resp body:%v", string(body))
	if err != nil {
		mylogrus.MyLog.Errorf("agora api:stop resp err:%v", err)
		return nil, err
	}

	var stopResponseBody StopResponseBody
	if err := json.Unmarshal(body, &stopResponseBody); err != nil {
		mylogrus.MyLog.Errorf("agora api:stop resp toStruts err:%v, body:%v", err, string(body))
		return nil, err
	}
	return &stopResponseBody, nil
}

func Query(resourceId, sid string) (int, error) {
	appId := config.GetAgoraAppId()
	url := urlBase + appId + "/cloud_recording/resourceid/" + resourceId + "/sid/" + sid + "/mode/individual/query"

	client := &http.Client{}
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		mylogrus.MyLog.Errorf("agora api:acquire request err:%v", err)
		return 0, err
		// handle error
	}
	req.Header.Set("Content-Type", "application/json;charset=utf-8")
	req.Header.Set("Authorization", "Basic "+createCredential())
	resp, err := client.Do(req)
	if err != nil {
		mylogrus.MyLog.Errorf("agora api:acquire do err:%v", err)
		return 0, err
	}
	defer resp.Body.Close()

	mylogrus.MyLog.Infof("req header: %v, StatusCode = %d, %s", req.Header, resp.StatusCode, resp.Status)

	if resp.StatusCode != http.StatusOK {
		return resp.StatusCode, fmt.Errorf("%w", resp.Status)
	}

	body, err := ioutil.ReadAll(resp.Body)
	mylogrus.MyLog.Infof("agora api:acquire resp body:%v", string(body))
	if err != nil {
		mylogrus.MyLog.Errorf("agora api:acquire resp err:%v", err)
		return resp.StatusCode, err
	}
	mylogrus.MyLog.Info(body)
	return 0, nil
}

type KickingResponseBody struct {
	Status string `json:"status"`
	Id     uint64 `json:"id"`
}

//增加封禁封禁规则
func AddKickingGroupRule(channelId, uid, ip string) (*KickingResponseBody, error) {
	mylogrus.MyLog.Infoln("agora AddKickingGroupRule channelId: %s, %s, %s", channelId, uid, ip)

	appId := config.GetAgoraAppId()
	url := "https://api.agora.io/dev/v1/kicking-rule"
	params := make(map[string]interface{})
	params["appid"] = appId
	params["cname"] = channelId
	params["uid"] = uid
	params["ip"] = ip
	if len(channelId) > 0 {
		params["time"] = 60 * 24 //封禁时长,不用担心,因为封禁之后,业务能控制不生产token
	} else {
		params["time"] = 60
	}
	params["privileges"] = []string{"publish_audio"}

	bytesData, err := json.Marshal(params)
	if err != nil {
		return nil, err
	}
	reader := bytes.NewReader(bytesData)
	mylogrus.MyLog.Infof("agora AddKickingGroupRule, url:%v, param:%s", url, string(bytesData))

	client := &http.Client{}
	req, err := http.NewRequest("POST", url, reader)
	if err != nil {
		mylogrus.MyLog.Errorf("agora AddKickingGroupRule api:stop request err:%v", err)
		return nil, err
		// handle error
	}
	req.Header.Set("Content-Type", "application/json;charset=utf-8")
	req.Header.Set("Authorization", "Basic "+createCredential())
	resp, err := client.Do(req)
	if err != nil {
		mylogrus.MyLog.Errorf("agora AddKickingGroupRule api:stop do err:%v", err)
		return nil, err
	}
	defer resp.Body.Close()

	mylogrus.MyLog.Infof("req AddKickingGroupRule header: %v, StatusCode = %d, %s", req.Header, resp.StatusCode, resp.Status)

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("%w", resp.Status)
	}

	body, err := ioutil.ReadAll(resp.Body)
	mylogrus.MyLog.Debugf("agora AddKickingGroupRule api:stop resp body:%v", string(body))
	if err != nil {
		mylogrus.MyLog.Errorf("agora AddKickingGroupRule api:stop resp err:%v", err)
		return nil, err
	}

	var kickingResponseBody KickingResponseBody
	if err := json.Unmarshal(body, &kickingResponseBody); err != nil {
		mylogrus.MyLog.Errorf("agora AddKickingGroupRule api:stop resp toStruts err:%v, body:%v", err, string(body))
		return nil, err
	}
	return &kickingResponseBody, nil
}

//删除封禁频道规则
func DelKickingRule(agoraRuleId uint64) (*KickingResponseBody, error) {
	mylogrus.MyLog.Infof("agora DelKickingRule agoraRuleId: %s", agoraRuleId)

	appId := config.GetAgoraAppId()
	url := "https://api.agora.io/dev/v1/kicking-rule"
	params := make(map[string]interface{})
	params["appid"] = appId
	params["id"] = agoraRuleId

	bytesData, err := json.Marshal(params)
	if err != nil {
		return nil, err
	}
	reader := bytes.NewReader(bytesData)
	mylogrus.MyLog.Infof("agora DelKickingRule, url:%v, param:%s", url, string(bytesData))

	client := &http.Client{}
	req, err := http.NewRequest("DELETE", url, reader)
	if err != nil {
		mylogrus.MyLog.Errorf("agora DelKickingRule api:stop request err:%v", err)
		return nil, err
		// handle error
	}
	req.Header.Set("Content-Type", "application/json;charset=utf-8")
	req.Header.Set("Authorization", "Basic "+createCredential())
	resp, err := client.Do(req)
	if err != nil {
		mylogrus.MyLog.Errorf("agora DelKickingRule api:stop do err:%v", err)
		return nil, err
	}
	defer resp.Body.Close()

	mylogrus.MyLog.Info("req DelKickingRule header: %v, StatusCode = %d, %s", req.Header, resp.StatusCode, resp.Status)

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("%w", resp.Status)
	}

	body, err := ioutil.ReadAll(resp.Body)
	mylogrus.MyLog.Debugf("agora DelKickingRule api:stop resp body:%v", string(body))
	if err != nil {
		mylogrus.MyLog.Errorf("agora DelKickingRule api:stop resp err:%v", err)
		return nil, err
	}

	var kickingResponseBody KickingResponseBody
	if err := json.Unmarshal(body, &kickingResponseBody); err != nil {
		mylogrus.MyLog.Errorf("agora DelKickingRule api:stop resp toStruts err:%v, body:%v", err, string(body))
		return nil, err
	}
	return &kickingResponseBody, nil
}

type ruleElem struct {
	Id         int64     `json:"id"`
	Uid        string    `json:"uid"`
	StrUid     bool      `json:"str_uid"`
	Cname      string    `json:"cname"`
	Ip         *string   `json:"ip"`
	Ts         time.Time `json:"ts"`
	Privileges []string  `json:"privileges"`
	ClientIp   string    `json:"clientIp"`
	Reason     *string   `json:"reason"`
	CreateAt   time.Time `json:"createAt"`
	UpdateAt   time.Time `json:"updateAt"`
}

type GetKickingResponseBody struct {
	Status string     `json:"status"`
	Rules  []ruleElem `json:"rules"`
}

// 查询封禁规则
func GetKickingRule() (*GetKickingResponseBody, error) {
	mylogrus.MyLog.Infoln("agora GetKickingRule")

	appId := config.GetAgoraAppId()
	url := "https://api.agora.io/dev/v1/kicking-rule?appid=" + appId

	client := &http.Client{}
	req, err := http.NewRequest(http.MethodGet, url, nil)
	if err != nil {
		mylogrus.MyLog.Errorf("agora GetKickingRule api:stop request err:%v", err)
		return nil, err
	}
	req.Header.Set("Content-Type", "application/json;charset=utf-8")
	req.Header.Set("Authorization", "Basic "+createCredential())
	resp, err := client.Do(req)
	if err != nil {
		mylogrus.MyLog.Errorf("agora GetKickingRule api:stop do err:%v", err)
		return nil, err
	}
	defer resp.Body.Close()

	mylogrus.MyLog.Infof("req GetKickingRule header: %v, StatusCode = %d, %s", req.Header, resp.StatusCode, resp.Status)

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("%w", resp.Status)
	}

	body, err := ioutil.ReadAll(resp.Body)
	mylogrus.MyLog.Debugf("agora GetKickingRule api:stop resp body:%v", string(body))
	if err != nil {
		mylogrus.MyLog.Errorf("agora GetKickingRule api:stop resp err:%v", err)
		return nil, err
	}

	var getResponse GetKickingResponseBody
	if err := json.Unmarshal(body, &getResponse); err != nil {
		mylogrus.MyLog.Errorf("agora GetKickingRule api:stop resp toStruts err:%v, body:%v", err, string(body))
		return nil, err
	}
	return &getResponse, nil
}

type GetUserStatusRsp struct {
	Success bool `json:"success"`
	Data    struct {
		Join      int64 `json:"join"`
		Uid       int64 `json:"uid"`
		InChannel bool  `json:"in_channel"`
		Platform  int   `json:"platform"`
		Role      int   `json:"role"`
	} `json:"data"`
}

// 查询用户状态
func GetUserStatus(uid int, chanName string) (*GetUserStatusRsp, error) {
	logger := mylogrus.MyLog.WithField("agora", "GetUserStatus")

	appId := config.GetAgoraAppId()
	url := "https://api.agora.io/dev/v1/channel/user/property/" + appId + "/" + strconv.Itoa(uid) + "/" + chanName

	client := &http.Client{}
	req, err := http.NewRequest(http.MethodGet, url, nil)
	if err != nil {
		logger.Errorf("NewRequest err:%v", err)
		return nil, err
	}
	req.Header.Set("Content-Type", "application/json;charset=utf-8")
	req.Header.Set("Authorization", "Basic "+createCredential())
	resp, err := client.Do(req)
	if err != nil {
		logger.Errorf("Do err:%v", err)
		return nil, err
	}
	defer resp.Body.Close()

	logger.Infof("header: %v, StatusCode = %d, %s", req.Header, resp.StatusCode, resp.Status)

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("%w", resp.Status)
	}

	body, err := ioutil.ReadAll(resp.Body)
	logger.Infof("resp body:%v", string(body))

	if err != nil {
		logger.Errorf("resp err:%v", err)
		return nil, err
	}

	var getResponse GetUserStatusRsp
	if err := json.Unmarshal(body, &getResponse); err != nil {
		logger.Errorf("unmarshal err:%v, body:%v", err, string(body))
		return nil, err
	}
	logger.Infof("Rsp: %+v", getResponse)
	return &getResponse, nil
}

type GetUserListRsp struct {
	Success bool `json:"success"`
	Data    struct {
		ChannelExist  bool     `json:"channel_exist"`
		Mode          int      `json:"mode"`
		Broadcasters  []uint64 `json:"broadcasters"`
		Audience      []uint64 `json:"audience"`
		AudienceTotal uint     `json:"audience_total"`
	} `json:"data"`
}

// 查询用户列表
func GetUserList(chanName string) (*GetUserListRsp, error) {
	logger := mylogrus.MyLog.WithField("agora", "GetUserList")

	appId := config.GetAgoraAppId()
	url := "https://api.agora.io/dev/v1/channel/user/" + appId + "/" + chanName

	client := &http.Client{}
	req, err := http.NewRequest(http.MethodGet, url, nil)
	if err != nil {
		logger.Errorf("NewRequest err:%v", err)
		return nil, err
	}
	req.Header.Set("Content-Type", "application/json;charset=utf-8")
	req.Header.Set("Authorization", "Basic "+createCredential())
	resp, err := client.Do(req)
	if err != nil {
		logger.Errorf("Do err:%v", err)
		return nil, err
	}
	defer resp.Body.Close()

	logger.Infof("header: %v, StatusCode = %d, %s", req.Header, resp.StatusCode, resp.Status)

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("%w", resp.Status)
	}

	body, err := ioutil.ReadAll(resp.Body)
	logger.Infof("resp body:%v", string(body))

	if err != nil {
		logger.Errorf("resp err:%v", err)
		return nil, err
	}

	var getResponse GetUserListRsp
	if err := json.Unmarshal(body, &getResponse); err != nil {
		logger.Errorf("unmarshal err:%v, body:%v", err, string(body))
		return nil, err
	}
	logger.Infof("Rsp: %+v", getResponse)
	return &getResponse, nil
}