卡片操作

This commit is contained in:
wyh 2023-11-21 16:26:51 +08:00
parent 5e7857502a
commit ac6cc166ef
2 changed files with 91 additions and 0 deletions

84
iot/card-model.go Normal file
View File

@ -0,0 +1,84 @@
package iot
import (
"encoding/json"
"fmt"
"myschools.me/suguo/hikvision/iot/model"
)
const (
CARD_BASE = "https://api2.hik-cloud.com/api/v1/open/basic/cards"
CARD_CREATE = CARD_BASE + "/batchCreate"
CARD_DELETE = CARD_BASE + "/batchDelete"
CARD_DELETE_CARDS = CARD_BASE + "/deletePersonCards"
)
// 新增卡片
func CardCreate(cards []model.CardCreateReq) (*model.Hikvision, error) {
req := &struct{ Cards []model.CardCreateReq }{
Cards: cards,
}
resp, err := hikvisionRequest("POST", CARD_CREATE, req)
if err != nil {
return nil, err
}
var result = &model.Hikvision{}
if err := json.Unmarshal(resp, &result); err != nil {
return nil, err
}
if result.Code != 200 {
return nil, fmt.Errorf("result errcode:%d errmsg:%s", result.Code, result.Message)
}
return result, nil
}
// 删除卡片
func CardDelete(cardNos []string) (*model.Hikvision, error) {
req := &struct {
CardNos []string `json:"cardNos"`
}{
CardNos: cardNos,
}
resp, err := hikvisionRequest("POST", CARD_DELETE, req)
if err != nil {
return nil, err
}
var result = &model.Hikvision{}
if err := json.Unmarshal(resp, &result); err != nil {
return nil, err
}
if result.Code != 200 {
return nil, fmt.Errorf("result errcode:%d errmsg:%s", result.Code, result.Message)
}
return result, nil
}
// 根据人员编号删除卡片
func CardDeleteByEmployeeNo(employeeNo string, cardNos []string) (*model.Hikvision, error) {
req := &struct {
EmployeeNo string `json:"employeeNo"`
CardNos []string `json:"cardNos"`
}{
EmployeeNo: employeeNo,
CardNos: cardNos,
}
resp, err := hikvisionRequest("POST", CARD_DELETE_CARDS, req)
if err != nil {
return nil, err
}
var result = &model.Hikvision{}
if err := json.Unmarshal(resp, &result); err != nil {
return nil, err
}
if result.Code != 200 {
return nil, fmt.Errorf("result errcode:%d errmsg:%s", result.Code, result.Message)
}
return result, nil
}

7
iot/model/card-model.go Normal file
View File

@ -0,0 +1,7 @@
package model
type CardCreateReq struct {
CardNo string `json:"cardNo"`
CardType string `json:"cardType"`
EmployeeNo string `json:"employeeNo"`
}