diff --git a/iot/hikvision-iot.go b/iot/hikvision-iot.go index cfa7d6a..2101a7d 100644 --- a/iot/hikvision-iot.go +++ b/iot/hikvision-iot.go @@ -15,7 +15,7 @@ import ( ) var ( - baseURL = "https://api2.hik-cloud.com/oauth" + baseURL = "https://api2.hik-cloud.com" clientID = "" clientSecret = "" scope = "" @@ -104,7 +104,7 @@ func hikvisionOauth() (*string, error) { scope: scope, } - resp, err := hikvisionRequestUrlencoded("POST", `/oauth/token`, params) + resp, err := hikvisionRequestUrlencoded("POST", `/oauth/token`, params, false) if err != nil { return nil, err } @@ -127,20 +127,29 @@ func hikvisionOauth() (*string, error) { } // 使用form请求 application/x-www-form-urlencoded,公共方法 -func hikvisionRequestUrlencoded(method string, url string, body interface{}) ([]byte, error) { +// +// 传入的结构体内的字段名称必须与文档一致 +func hikvisionRequestUrlencoded(method string, url string, body interface{}, auth bool) ([]byte, error) { reader, err := hikvisioninspectStruct(body) if err != nil { return nil, err } - req, err := http.NewRequest("POST", baseURL+url, strings.NewReader(reader.Encode())) + + req, err := http.NewRequest(method, baseURL+url, strings.NewReader(reader.Encode())) if err != nil { return nil, err } req.Header.Add("Accept", "*/*") req.Header.Add("Content-Type", "application/x-www-form-urlencoded") - + if auth { + token, err := hikvisionOauth() + if err != nil { + return nil, err + } + req.Header.Add("authorization", fmt.Sprintf(" bearer %s", *token)) + } tr := &http.Transport{ TLSClientConfig: &tls.Config{ InsecureSkipVerify: true, diff --git a/iot/hikvision.http b/iot/hikvision.http index 9737e5b..4a9110e 100644 --- a/iot/hikvision.http +++ b/iot/hikvision.http @@ -33,4 +33,12 @@ POST {{url}}/mq/consumer/messages HTTP/1.1 Content-Type: application/x-www-form-urlencoded Authorization: {{token}} -autoCommit=true&consumerId=7db9420cdb2844f5ba8bba07d1417d54 \ No newline at end of file +autoCommit=true&consumerId=7db9420cdb2844f5ba8bba07d1417d54 + + +### qrcord +POST {{url}}/community/access/visitors/actions/getQrcode HTTP/1.1 +Content-Type: application/x-www-form-urlencoded +Authorization: {{token}} + +cardNo=12345&effectTime=231123150000&expireTime=231123180000&openTimes=4 \ No newline at end of file diff --git a/iot/model/qrcode-model.go b/iot/model/qrcode-model.go new file mode 100644 index 0000000..d0fa427 --- /dev/null +++ b/iot/model/qrcode-model.go @@ -0,0 +1,8 @@ +package model + +type QrCodeVisitors struct { + Hikvision + Data struct { + QrCodeUrl string `json:"qrCodeUrl"` + } `json:"data"` +} diff --git a/iot/oauth_test.go b/iot/oauth_test.go index 57cd736..bbeb782 100644 --- a/iot/oauth_test.go +++ b/iot/oauth_test.go @@ -1,10 +1,20 @@ package iot import ( + "fmt" + "log" "testing" ) func TestOauth(t *testing.T) { + + r0, err := QrCodeVisitors("1231", "231123150000", "231123180000", 9) + if err != nil { + log.Fatal(err) + return + } + fmt.Println(r0) + // groupid := "4b9e7ab1068d45269b1acb6c8b50d855" // r1, err := PermissionGroupAddDevice(groupid, true, []string{"AF8534579"}, true) // if err != nil { diff --git a/iot/qrcode-iot.go b/iot/qrcode-iot.go new file mode 100644 index 0000000..0cce777 --- /dev/null +++ b/iot/qrcode-iot.go @@ -0,0 +1,37 @@ +package iot + +import ( + "encoding/json" + + "myschools.me/suguo/hikvision/iot/model" +) + +const ( + qr_code_visitor = "/api/v1/community/access/visitors/actions/getQrcode" +) + +// 访客二维码生成 +func QrCodeVisitors(cardNo, effectTime, expireTime string, openTimes int) (*model.QrCodeVisitors, error) { + req := struct { + cardNo string + effectTime string //生效时间,时间格式yyMMddHHmmss,例如181205180000 + expireTime string //与生效时间间隔最大48小时 + openTimes int // 开门次数,最大9次 + }{ + cardNo: cardNo, + effectTime: effectTime, + expireTime: expireTime, + openTimes: openTimes, + } + + resp, err := hikvisionRequestUrlencoded("POST", qr_code_visitor, req, true) + if err != nil { + return nil, err + } + var result *model.QrCodeVisitors + if err := json.Unmarshal(resp, &result); err != nil { + return nil, err + } + + return result, nil +}