访客二维码

This commit is contained in:
wyh 2023-11-23 14:13:07 +08:00
parent 44fc8308be
commit cec996746b
5 changed files with 78 additions and 6 deletions

View File

@ -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,

View File

@ -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
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

View File

@ -0,0 +1,8 @@
package model
type QrCodeVisitors struct {
Hikvision
Data struct {
QrCodeUrl string `json:"qrCodeUrl"`
} `json:"data"`
}

View File

@ -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 {

37
iot/qrcode-iot.go Normal file
View File

@ -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
}