gate.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package gate
  4. import (
  5. "context"
  6. "crypto/rc4"
  7. "encoding/json"
  8. "fmt"
  9. "git.getensh.com/common/gopkgs/logger"
  10. "go.uber.org/zap"
  11. "google.golang.org/grpc/status"
  12. "property-household/parser"
  13. "property-household/pb"
  14. pb_v1 "property-household/pb/v1"
  15. "time"
  16. )
  17. func Rc4Encrypt(plain string, key string) string {
  18. dest := make([]byte, len(plain))
  19. cipher2, _ := rc4.NewCipher([]byte(key))
  20. cipher2.XORKeyStream(dest, []byte(plain))
  21. return fmt.Sprintf("%X", dest)
  22. }
  23. func checkGateQcodeParam(req *pb_v1.GateQcodeRequest) error {
  24. switch {
  25. case req.DeviceId == 0 && req.Visitor:
  26. return status.Error(10003, "设备id不能为空")
  27. case req.GardenId < 1:
  28. return status.Error(10003, "小区不能为空")
  29. case req.Uid < 1:
  30. return status.Error(10003, "用户不能为空")
  31. case req.Visitor:
  32. if req.VisitorEnd < 1 || req.VisitorStart < 1 {
  33. return status.Error(10003, "访客邀约请选择时间段")
  34. }
  35. if req.VisitorEnd-req.VisitorStart > 3600*24 {
  36. return status.Error(10003, "访客二维码时间不能超过一天")
  37. }
  38. }
  39. return nil
  40. }
  41. //
  42. func GateQcode(ctx context.Context, req *pb_v1.GateQcodeRequest) (reply *pb_v1.GateQcodeReply, err error) {
  43. reply = &pb_v1.GateQcodeReply{}
  44. // 捕获各个task中的异常并返回给调用者
  45. defer func() {
  46. if r := recover(); r != nil {
  47. err = fmt.Errorf("%+v", r)
  48. e := &status.Status{}
  49. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  50. logger.Error("err",
  51. zap.String("system_err", err.Error()),
  52. zap.Stack("stacktrace"))
  53. }
  54. }
  55. }()
  56. if err = checkGateQcodeParam(req); err != nil {
  57. return nil, err
  58. }
  59. now := time.Now()
  60. str := fmt.Sprintf("[%d,%d,%s,%s,0,0,A]",
  61. req.Uid,
  62. req.GardenId,
  63. now.Add(-30*time.Second).Format("20060102150405"),
  64. now.Add(180*time.Second).Format("20060102150405"))
  65. if !req.Visitor {
  66. reply.Qcode = "CB01" + Rc4Encrypt(str, parser.Conf.GateKey)
  67. return reply, nil
  68. }
  69. if req.VisitorName == "" || req.VisitorName == "" || req.Comment == "" {
  70. return nil, status.Error(10003, "访客信息和是由不能为空")
  71. }
  72. mreq := pb_v1.GateVisitorAddRequest{
  73. Uid: req.Uid,
  74. Name: req.UserName,
  75. Phone: req.Phone,
  76. Visitor: req.VisitorName,
  77. VisitorPhone: req.VisitorPhone,
  78. DeviceId: req.DeviceId,
  79. GardenId: req.GardenId,
  80. Start: req.VisitorStart,
  81. End: req.VisitorEnd,
  82. Comment: req.Comment,
  83. }
  84. mreply, err := pb.Device.GateVisitorAdd(ctx, &mreq)
  85. if err != nil {
  86. return nil, err
  87. }
  88. reply.Qcode = mreply.Qcode
  89. reply.Id = mreply.Id
  90. return reply, nil
  91. }