package isapi import ( "bytes" "errors" "fmt" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" "io/ioutil" "lc-fangdaosha/model/common/response" "net/http" "net/url" "strings" ) const ( contentTypeXML = `application/xml; charset="UTF-8"` contentTypeJSON = `application/json; charset="UTF-8"` contentTypeHTML = `text/html` ) type Client struct { Client *http.Client BaseURL string } // NewClient 获取新的连接 func NewClient(host, username, password string) (*Client, error) { u, err := url.Parse("http://" + host) if err != nil { return nil, err } return &Client{ Client: &http.Client{ Transport: NewAuthTransport(username, password), }, BaseURL: u.String(), }, nil } // TouChuan 透传 func TouChuan(host string, o Operation, data []byte, c *gin.Context) ([]byte, error) { info, err := Com(host, o, data) if err != nil { return nil, fmt.Errorf("TouChuan错误: %e", err) } if err != nil { response.FailWithMessage(err.Error(), c) logrus.Errorf("%s error: %e", o, err) return nil, err } c.Data(200, `application/xml; charset="UTF-8"`, info) return info, nil } // Com 透传 func Com(host string, o Operation, data []byte) ([]byte, error) { c, err := NewClient(host, "admin", "kk176@lc") if err != nil { return nil, err } switch o { case GetDeviceInfo: return c.GetDeviceInfo() case GetSystemCap: return c.GetSystemCap() case GetChanCap: return c.GetOneChanEventCap() //越界侦测 case GetLineDetectionCap: return c.GetLineDetectionCap() case GetLineDetectionCal: return c.GetSizeLd() case PutLineDetectionCal: return c.PutSizeLd(data) case GetLineDetection: return c.GetLineDetection() case PutLineDetection: return c.PutLineDetection(data) //区域入侵 case GetFieldDetectionCap: return c.GetFieldDetectionCap() case GetFieldDetectionCal: return c.GetSizeFd() case PutFieldDetectionCal: return c.PutSizeFd(data) case GetFieldDetection: return c.GetFieldDetection() case PutFieldDetection: return c.PutFieldDetection(data) //进入区域 case GetRegionEntranceCap: return c.GetRegionEntranceCap() case GetRegionEntranceCal: return c.GetSizeRe() case PutRegionEntranceCal: return c.PutSizeRe(data) case GetRegionEntrance: return c.GetRegionEntrance() case PutRegionEntrance: return c.PutRegionEntrance(data) //离开区域 case SetHosts: return c.PutHost(data) case SetSIP: return c.PutSip(data) case SetSIPInfo: return c.PutSipInfo(data) case PutEmail: return c.PutEmail(data) } return nil, errors.New("未定义操作") } func (c *Client) Do(r *http.Request) ([]byte, error) { resp, err := c.Client.Do(r) if err != nil { return nil, err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } return body, nil } func (c *Client) CommonGet(path string) (resp []byte, err error) { u, err := url.Parse(c.BaseURL + path) if err != nil { return nil, err } resp, err = c.Get(u) if err != nil { return nil, err } return resp, nil } func (c *Client) CommonPut(data []byte, path string) (resp []byte, err error) { u, err := url.Parse(c.BaseURL + path) if err != nil { return nil, err } resp, err = c.PutXML(u, data) if err != nil { return nil, err } return resp, nil } func (c *Client) CommonDel(data []byte, path string) (resp []byte, err error) { u, err := url.Parse(c.BaseURL + path) if err != nil { return nil, err } resp, err = c.DeleteXML(u, data) if err != nil { return nil, err } return resp, nil } func (c *Client) Get(u *url.URL) ([]byte, error) { req, err := http.NewRequest("GET", u.String(), nil) if err != nil { return nil, err } return c.Do(req) } func (c *Client) put(u *url.URL, contentType string, data []byte) ([]byte, error) { b := bytes.NewBuffer(data) req, err := http.NewRequest("PUT", u.String(), b) if err != nil { return nil, err } req.Header.Set("Content-Type", contentType) return c.Do(req) } func (c *Client) PutXML(u *url.URL, data []byte) ([]byte, error) { return c.put(u, contentTypeXML, data) } func (c *Client) PutJSON(u *url.URL, data []byte) ([]byte, error) { return c.put(u, contentTypeJSON, data) } func (c *Client) post(u *url.URL, contentType string, data []byte) ([]byte, error) { b := bytes.NewBuffer(data) req, err := http.NewRequest("POST", u.String(), b) if err != nil { return nil, err } req.Header.Set("Content-Type", contentType) return c.Do(req) } func (c *Client) PostXML(u *url.URL, data []byte) ([]byte, error) { return c.post(u, contentTypeXML, data) } func (c *Client) PostJSON(u *url.URL, data []byte) ([]byte, error) { return c.post(u, contentTypeJSON, data) } func (c *Client) delete(u *url.URL, contentType string, data []byte) ([]byte, error) { b := bytes.NewBuffer(data) req, err := http.NewRequest("DELETE", u.String(), b) if err != nil { return nil, err } req.Header.Set("Content-Type", contentType) return c.Do(req) } func (c *Client) DeleteXML(u *url.URL, data []byte) ([]byte, error) { return c.delete(u, contentTypeXML, data) } func (c *Client) DeleteJSON(u *url.URL, data []byte) ([]byte, error) { return c.delete(u, contentTypeJSON, data) } func DoModFacePicRecord(url, json, faceimage, boundary string) { var bodyParam = "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"FaceDataRecord\";\r\n" + "Content-Type: text/json\r\n" + "Content-Length: " + fmt.Sprintf("%d", len(json)) + "\r\n\r\n" + json + "\r\n" + "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"FaceImage\";\r\n" + "Content-Type: image/jpeg\r\n" + "Content-Length: " + fmt.Sprintf("%d", len(faceimage)) + "\r\n\r\n" + faceimage + "\r\n--" + boundary + "--\r\n" req, err := http.NewRequest("PUT", url, strings.NewReader(bodyParam)) if err != nil { logrus.Error(err) } req.SetBasicAuth("", "") req.Header.Add("Accept", "text/html, application/xhtml+xml") req.Header.Add("Accept-Language", "zh-CN") req.Header.Add("Content-Type", "multipart/form-data; boundary="+boundary) req.Header.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)") req.Header.Add("Accept-Encoding", "gzip, deflate") req.Header.Add("Connection", "Keep-Alive") req.Header.Add("Cache-Control", "no-cache") } func DoPostStorageCloud(url, json, faceimage, boundary string) { var bodyParam = "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"FaceDataRecord\";\r\n" + "Content-Type: text/json\r\n" + "Content-Length: " + fmt.Sprintf("%d", len(json)) + "\r\n\r\n" + json + "\r\n" + "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"FaceImage\";\r\n" + "Content-Type: image/jpeg\r\n" + "Content-Length: " + fmt.Sprintf("%d", len(faceimage)) + "\r\n\r\n" + faceimage + "\r\n--" + boundary + "--\r\n" req, err := http.NewRequest("POST", url, strings.NewReader(bodyParam)) if err != nil { logrus.Error(err) } req.SetBasicAuth("", "") req.Header.Add("Accept", "text/html, application/xhtml+xml") req.Header.Add("Accept-Language", "zh-CN") req.Header.Add("Content-Type", "multipart/form-data; boundary="+boundary) req.Header.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)") req.Header.Add("Accept-Encoding", "gzip, deflate") req.Header.Add("Connection", "Keep-Alive") req.Header.Add("Cache-Control", "no-cache") }