// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package gate import ( "context" "crypto/rc4" "encoding/json" "fmt" "git.getensh.com/common/gopkgs/logger" "go.uber.org/zap" "google.golang.org/grpc/status" "property-household/parser" "property-household/pb" pb_v1 "property-household/pb/v1" "time" ) func Rc4Encrypt(plain string, key string) string { dest := make([]byte, len(plain)) cipher2, _ := rc4.NewCipher([]byte(key)) cipher2.XORKeyStream(dest, []byte(plain)) return fmt.Sprintf("%X", dest) } func checkGateQcodeParam(req *pb_v1.GateQcodeRequest) error { switch { case req.DeviceId == 0 && req.Visitor: return status.Error(10003, "设备id不能为空") case req.GardenId < 1: return status.Error(10003, "小区不能为空") case req.Uid < 1: return status.Error(10003, "用户不能为空") case req.Visitor: if req.VisitorEnd < 1 || req.VisitorStart < 1 { return status.Error(10003, "访客邀约请选择时间段") } if req.VisitorEnd-req.VisitorStart > 3600*24 { return status.Error(10003, "访客二维码时间不能超过一天") } } return nil } // func GateQcode(ctx context.Context, req *pb_v1.GateQcodeRequest) (reply *pb_v1.GateQcodeReply, err error) { reply = &pb_v1.GateQcodeReply{} // 捕获各个task中的异常并返回给调用者 defer func() { if r := recover(); r != nil { err = fmt.Errorf("%+v", r) e := &status.Status{} if er := json.Unmarshal([]byte(err.Error()), e); er != nil { logger.Error("err", zap.String("system_err", err.Error()), zap.Stack("stacktrace")) } } }() if err = checkGateQcodeParam(req); err != nil { return nil, err } now := time.Now() str := fmt.Sprintf("[%d,%d,%s,%s,0,0,A]", req.Uid, req.GardenId, now.Add(-30*time.Second).Format("20060102150405"), now.Add(180*time.Second).Format("20060102150405")) if !req.Visitor { reply.Qcode = "CB01" + Rc4Encrypt(str, parser.Conf.GateKey) return reply, nil } if req.VisitorName == "" || req.VisitorName == "" || req.Comment == "" { return nil, status.Error(10003, "访客信息和是由不能为空") } mreq := pb_v1.GateVisitorAddRequest{ Uid: req.Uid, Name: req.UserName, Phone: req.Phone, Visitor: req.VisitorName, VisitorPhone: req.VisitorPhone, DeviceId: req.DeviceId, GardenId: req.GardenId, Start: req.VisitorStart, End: req.VisitorEnd, Comment: req.Comment, } mreply, err := pb.Device.GateVisitorAdd(ctx, &mreq) if err != nil { return nil, err } reply.Qcode = mreply.Qcode reply.Id = mreply.Id return reply, nil }