cold.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package clod
  2. import (
  3. "Cold_Logistic/internal/pkg/common/global"
  4. "Cold_Logistic/internal/pkg/common/options"
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/vmihailenco/msgpack/v5"
  9. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
  10. "io"
  11. "net/http"
  12. "net/url"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. const (
  18. ColdUserVerification = "Cold_User_verification"
  19. ColdUserPermis = "Cold_User_CheckUserPermissions"
  20. )
  21. var defaultHttpClient *http.Client = &http.Client{
  22. Timeout: time.Second * 3 * 60,
  23. Transport: &http.Transport{
  24. TLSHandshakeTimeout: 30 * time.Second,
  25. MaxIdleConns: 50,
  26. MaxConnsPerHost: 50,
  27. MaxIdleConnsPerHost: 50,
  28. },
  29. }
  30. // BzdClodService 冷链系统
  31. type BzdClodService struct {
  32. Host string
  33. }
  34. func NewBzdClodService() BzdClodService {
  35. // "https://cold.coldbaozhida.com"
  36. return BzdClodService{Host: options.OptInstance.BzdClod.Host}
  37. }
  38. // LoginVerification 验证token
  39. func (srv BzdClodService) LoginVerification(_ context.Context, tokenKey string) (bool, int, User, error) {
  40. // 请求-响应, 向 verification 发布一个 `ToKey` 请求数据,设置超时间3秒,如果有多个响应,只接收第一个收到的消息
  41. msg, err := global.CommonConnectRepoInst.NatsConn.Request(ColdUserVerification, []byte(tokenKey), 3*time.Second)
  42. if err != nil {
  43. return false, 0, User{}, errors.WithStackOnce(err)
  44. }
  45. var resp UserResp
  46. err = msgpack.Unmarshal(msg.Data, &resp)
  47. if err != nil {
  48. return false, 0, User{}, errors.WithStackOnce(err)
  49. }
  50. return true, resp.Pid, resp.Data, nil
  51. }
  52. // CheckUserPermissions 验证权限
  53. func (srv BzdClodService) CheckUserPermissions(_ context.Context, param PermisParam) (bool, error) {
  54. b, err := msgpack.Marshal(&param)
  55. if err != nil {
  56. return false, errors.WithStackOnce(err)
  57. }
  58. // 请求-响应, 向 verification 发布一个 `ToKey` 请求数据,设置超时间3秒,如果有多个响应,只接收第一个收到的消息
  59. msg, err := global.CommonConnectRepoInst.NatsConn.Request(ColdUserPermis, b, 3*time.Second)
  60. if err != nil {
  61. return false, errors.WithStackOnce(err)
  62. }
  63. var resp PermisParamResp
  64. err = msgpack.Unmarshal(msg.Data, &resp)
  65. if err != nil {
  66. return false, errors.WithStackOnce(err)
  67. }
  68. return resp.Pass, nil
  69. }
  70. // DeviceSensorList 设备探头列表
  71. func (srv BzdClodService) DeviceSensorList(ctx context.Context, sn string) (res []DeviceSensorListResp, err error) {
  72. values := url.Values{}
  73. values.Add("T_sn", sn)
  74. values.Add("page_z", "100")
  75. urlStr := srv.Host + options.OptInstance.BzdClod.DeviceSensorList
  76. //urlStr := "https://cold.coldbaozhida.com" + "/api/v3/DeviceSensor/List_BySN"
  77. resp, err := xFromRequest(ctx, urlStr, "POST", values, nil)
  78. if err != nil {
  79. return res, errors.WithStackOnce(err)
  80. }
  81. if resp.Date != nil {
  82. tmp := struct {
  83. Data []DeviceSensorListResp
  84. }{}
  85. if err = json.Unmarshal(resp.Date, &tmp); err != nil {
  86. return res, errors.WithStackOnce(err)
  87. }
  88. res = tmp.Data
  89. }
  90. return res, nil
  91. }
  92. // DeviceSensorDataList 设备监控数据列表
  93. func (srv BzdClodService) DeviceSensorDataList(ctx context.Context, param DeviceSensorDataListParam) (res DeviceSensorDataListResp, err error) {
  94. values := url.Values{}
  95. values.Add("User_tokey", param.User_tokey)
  96. values.Add("T_snid", param.T_snid)
  97. values.Add("Time_start", param.Time_start)
  98. values.Add("Time_end", param.Time_end)
  99. values.Add("page", param.Page)
  100. values.Add("page_z", param.Page_z)
  101. urlStr := srv.Host + options.OptInstance.BzdClod.DataListUrl
  102. //urlStr := "https://cold.coldbaozhida.com" + "/api/v3/Data/List"
  103. resp, err := xFromRequest(ctx, urlStr, "POST", values, nil)
  104. if err != nil {
  105. return res, errors.WithStackOnce(err)
  106. }
  107. if resp.Date != nil {
  108. if err = json.Unmarshal(resp.Date, &res); err != nil {
  109. return res, errors.WithStackOnce(err)
  110. }
  111. }
  112. return res, nil
  113. }
  114. // UserList 用户列表
  115. func (srv BzdClodService) UserList(ctx context.Context, param UserListParam) (res UserListResp, err error) {
  116. values := url.Values{}
  117. values.Add("User_tokey", param.UserToken)
  118. values.Add("T_name", param.Name)
  119. values.Add("page", strconv.Itoa(param.Page))
  120. values.Add("page_z", strconv.Itoa(param.Page_z))
  121. urlStr := srv.Host + options.OptInstance.BzdClod.UserList
  122. //urlStr := "https://cold.coldbaozhida.com" + "/api/v3/User/List"
  123. resp, err := xFromRequest(ctx, urlStr, "POST", values, nil)
  124. if err != nil {
  125. return res, errors.WithStackOnce(err)
  126. }
  127. if resp.Date != nil {
  128. if err = json.Unmarshal(resp.Date, &res); err != nil {
  129. return res, errors.WithStackOnce(err)
  130. }
  131. }
  132. return res, nil
  133. }
  134. // LogisticCompanyList 物流公司列表
  135. func (srv BzdClodService) LogisticCompanyList(ctx context.Context, param LogisticCompanyListParam) (res LogisticCompanyListResp, err error) {
  136. values := url.Values{}
  137. values.Add("User_tokey", param.UserTokey)
  138. values.Add("T_name", param.TName)
  139. values.Add("page", strconv.Itoa(param.Page))
  140. values.Add("page_z", strconv.Itoa(param.Page_z))
  141. urlStr := srv.Host + options.OptInstance.BzdClod.LogisticCompanyList
  142. //urlStr := "https://cold.coldbaozhida.com" + "/api/v3/Company/Transport/List"
  143. resp, err := xFromRequest(ctx, urlStr, "POST", values, nil)
  144. if err != nil {
  145. return res, errors.WithStackOnce(err)
  146. }
  147. if resp.Date != nil {
  148. if err = json.Unmarshal(resp.Date, &res); err != nil {
  149. return res, errors.WithStackOnce(err)
  150. }
  151. }
  152. return res, nil
  153. }
  154. // GetCompanyInfo 获取
  155. func (srv BzdClodService) GetCompanyInfo(ctx context.Context, param LogisticCompanyListParam) (res LogisticCompanyListResp, err error) {
  156. values := url.Values{}
  157. values.Add("User_tokey", param.UserTokey)
  158. values.Add("T_name", param.TName)
  159. values.Add("page", strconv.Itoa(param.Page))
  160. values.Add("page_z", strconv.Itoa(param.Page_z))
  161. urlStr := srv.Host + options.OptInstance.BzdClod.LogisticCompanyList
  162. //urlStr := "https://cold.coldbaozhida.com" + "/api/v3/Company/Transport/List"
  163. resp, err := xFromRequest(ctx, urlStr, "POST", values, nil)
  164. if err != nil {
  165. return res, errors.WithStackOnce(err)
  166. }
  167. if resp.Date != nil {
  168. if err = json.Unmarshal(resp.Date, &res); err != nil {
  169. return res, errors.WithStackOnce(err)
  170. }
  171. }
  172. return res, nil
  173. }
  174. func xFromRequest(ctx context.Context, url, method string, values url.Values, head map[string]string) (BaseRespVo, error) {
  175. req, err := http.NewRequestWithContext(ctx, method, url, strings.NewReader(values.Encode()))
  176. if err != nil {
  177. return BaseRespVo{}, errors.WithStackOnce(err)
  178. }
  179. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  180. for k, v := range head {
  181. req.Header.Set(k, v)
  182. }
  183. client := defaultHttpClient
  184. resp, err := client.Do(req)
  185. defer resp.Body.Close()
  186. if err != nil {
  187. return BaseRespVo{}, errors.WithStackOnce(err)
  188. }
  189. body, err := io.ReadAll(resp.Body)
  190. if err != nil {
  191. return BaseRespVo{}, errors.WithStackOnce(err)
  192. }
  193. if resp.StatusCode != 200 {
  194. return BaseRespVo{}, errors.New(fmt.Sprintf("请求服务失败!%s", string(body)))
  195. }
  196. baseResp := BaseRespVo{}
  197. if err = json.Unmarshal(body, &baseResp); err != nil {
  198. return BaseRespVo{}, errors.WithStackOnce(err)
  199. }
  200. if baseResp.Code != 200 {
  201. return BaseRespVo{}, errors.WithStackOnce(errors.New(fmt.Sprintf("冷链3.0系统返回错误!code: %v, msg: %v", baseResp.Code, baseResp.Msg)))
  202. }
  203. return baseResp, nil
  204. }