123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- package clod
- import (
- "Cold_Logistic/internal/pkg/common/global"
- "Cold_Logistic/internal/pkg/common/options"
- "context"
- "encoding/json"
- "fmt"
- "github.com/vmihailenco/msgpack/v5"
- "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
- "io"
- "net/http"
- "net/url"
- "strconv"
- "strings"
- "time"
- )
- const (
- ColdUserVerification = "Cold_User_verification"
- ColdUserPermis = "Cold_User_CheckUserPermissions"
- )
- var defaultHttpClient *http.Client = &http.Client{
- Timeout: time.Second * 3 * 60,
- Transport: &http.Transport{
- TLSHandshakeTimeout: 30 * time.Second,
- MaxIdleConns: 50,
- MaxConnsPerHost: 50,
- MaxIdleConnsPerHost: 50,
- },
- }
- // BzdClodService 冷链系统
- type BzdClodService struct {
- Host string
- }
- func NewBzdClodService() BzdClodService {
- // "https://cold.coldbaozhida.com"
- return BzdClodService{Host: options.OptInstance.BzdClod.Host}
- }
- // LoginVerification 验证token
- func (srv BzdClodService) LoginVerification(_ context.Context, tokenKey string) (bool, int, User, error) {
- // 请求-响应, 向 verification 发布一个 `ToKey` 请求数据,设置超时间3秒,如果有多个响应,只接收第一个收到的消息
- msg, err := global.CommonConnectRepoInst.NatsConn.Request(ColdUserVerification, []byte(tokenKey), 3*time.Second)
- if err != nil {
- return false, 0, User{}, errors.WithStackOnce(err)
- }
- var resp UserResp
- err = msgpack.Unmarshal(msg.Data, &resp)
- if err != nil {
- return false, 0, User{}, errors.WithStackOnce(err)
- }
- return true, resp.Pid, resp.Data, nil
- }
- // CheckUserPermissions 验证权限
- func (srv BzdClodService) CheckUserPermissions(_ context.Context, param PermisParam) (bool, error) {
- b, err := msgpack.Marshal(¶m)
- if err != nil {
- return false, errors.WithStackOnce(err)
- }
- // 请求-响应, 向 verification 发布一个 `ToKey` 请求数据,设置超时间3秒,如果有多个响应,只接收第一个收到的消息
- msg, err := global.CommonConnectRepoInst.NatsConn.Request(ColdUserPermis, b, 3*time.Second)
- if err != nil {
- return false, errors.WithStackOnce(err)
- }
- var resp PermisParamResp
- err = msgpack.Unmarshal(msg.Data, &resp)
- if err != nil {
- return false, errors.WithStackOnce(err)
- }
- return resp.Pass, nil
- }
- // DeviceSensorList 设备探头列表
- func (srv BzdClodService) DeviceSensorList(ctx context.Context, sn string) (res []DeviceSensorListResp, err error) {
- values := url.Values{}
- values.Add("T_sn", sn)
- values.Add("page_z", "100")
- urlStr := srv.Host + options.OptInstance.BzdClod.DeviceSensorList
- //urlStr := "https://cold.coldbaozhida.com" + "/api/v3/DeviceSensor/List_BySN"
- resp, err := xFromRequest(ctx, urlStr, "POST", values, nil)
- if err != nil {
- return res, errors.WithStackOnce(err)
- }
- if resp.Date != nil {
- tmp := struct {
- Data []DeviceSensorListResp
- }{}
- if err = json.Unmarshal(resp.Date, &tmp); err != nil {
- return res, errors.WithStackOnce(err)
- }
- res = tmp.Data
- }
- return res, nil
- }
- // DeviceSensorDataList 设备监控数据列表
- func (srv BzdClodService) DeviceSensorDataList(ctx context.Context, param DeviceSensorDataListParam) (res DeviceSensorDataListResp, err error) {
- values := url.Values{}
- values.Add("User_tokey", param.User_tokey)
- values.Add("T_snid", param.T_snid)
- values.Add("Time_start", param.Time_start)
- values.Add("Time_end", param.Time_end)
- values.Add("page", param.Page)
- values.Add("page_z", param.Page_z)
- urlStr := srv.Host + options.OptInstance.BzdClod.DataListUrl
- //urlStr := "https://cold.coldbaozhida.com" + "/api/v3/Data/List"
- resp, err := xFromRequest(ctx, urlStr, "POST", values, nil)
- if err != nil {
- return res, errors.WithStackOnce(err)
- }
- if resp.Date != nil {
- if err = json.Unmarshal(resp.Date, &res); err != nil {
- return res, errors.WithStackOnce(err)
- }
- }
- return res, nil
- }
- // UserList 用户列表
- func (srv BzdClodService) UserList(ctx context.Context, param UserListParam) (res UserListResp, err error) {
- values := url.Values{}
- values.Add("User_tokey", param.UserToken)
- values.Add("T_name", param.Name)
- values.Add("page", strconv.Itoa(param.Page))
- values.Add("page_z", strconv.Itoa(param.Page_z))
- urlStr := srv.Host + options.OptInstance.BzdClod.UserList
- //urlStr := "https://cold.coldbaozhida.com" + "/api/v3/User/List"
- resp, err := xFromRequest(ctx, urlStr, "POST", values, nil)
- if err != nil {
- return res, errors.WithStackOnce(err)
- }
- if resp.Date != nil {
- if err = json.Unmarshal(resp.Date, &res); err != nil {
- return res, errors.WithStackOnce(err)
- }
- }
- return res, nil
- }
- // LogisticCompanyList 物流公司列表
- func (srv BzdClodService) LogisticCompanyList(ctx context.Context, param LogisticCompanyListParam) (res LogisticCompanyListResp, err error) {
- values := url.Values{}
- values.Add("User_tokey", param.UserTokey)
- values.Add("T_name", param.TName)
- values.Add("page", strconv.Itoa(param.Page))
- values.Add("page_z", strconv.Itoa(param.Page_z))
- urlStr := srv.Host + options.OptInstance.BzdClod.LogisticCompanyList
- //urlStr := "https://cold.coldbaozhida.com" + "/api/v3/Company/Transport/List"
- resp, err := xFromRequest(ctx, urlStr, "POST", values, nil)
- if err != nil {
- return res, errors.WithStackOnce(err)
- }
- if resp.Date != nil {
- if err = json.Unmarshal(resp.Date, &res); err != nil {
- return res, errors.WithStackOnce(err)
- }
- }
- return res, nil
- }
- // GetCompanyInfo 获取
- func (srv BzdClodService) GetCompanyInfo(ctx context.Context, param LogisticCompanyListParam) (res LogisticCompanyListResp, err error) {
- values := url.Values{}
- values.Add("User_tokey", param.UserTokey)
- values.Add("T_name", param.TName)
- values.Add("page", strconv.Itoa(param.Page))
- values.Add("page_z", strconv.Itoa(param.Page_z))
- urlStr := srv.Host + options.OptInstance.BzdClod.LogisticCompanyList
- //urlStr := "https://cold.coldbaozhida.com" + "/api/v3/Company/Transport/List"
- resp, err := xFromRequest(ctx, urlStr, "POST", values, nil)
- if err != nil {
- return res, errors.WithStackOnce(err)
- }
- if resp.Date != nil {
- if err = json.Unmarshal(resp.Date, &res); err != nil {
- return res, errors.WithStackOnce(err)
- }
- }
- return res, nil
- }
- func xFromRequest(ctx context.Context, url, method string, values url.Values, head map[string]string) (BaseRespVo, error) {
- req, err := http.NewRequestWithContext(ctx, method, url, strings.NewReader(values.Encode()))
- if err != nil {
- return BaseRespVo{}, errors.WithStackOnce(err)
- }
- req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
- for k, v := range head {
- req.Header.Set(k, v)
- }
- client := defaultHttpClient
- resp, err := client.Do(req)
- defer resp.Body.Close()
- if err != nil {
- return BaseRespVo{}, errors.WithStackOnce(err)
- }
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- return BaseRespVo{}, errors.WithStackOnce(err)
- }
- if resp.StatusCode != 200 {
- return BaseRespVo{}, errors.New(fmt.Sprintf("请求服务失败!%s", string(body)))
- }
- baseResp := BaseRespVo{}
- if err = json.Unmarshal(body, &baseResp); err != nil {
- return BaseRespVo{}, errors.WithStackOnce(err)
- }
- if baseResp.Code != 200 {
- return BaseRespVo{}, errors.WithStackOnce(errors.New(fmt.Sprintf("冷链3.0系统返回错误!code: %v, msg: %v", baseResp.Code, baseResp.Msg)))
- }
- return baseResp, nil
- }
|