package v1 import ( "fmt" "git.getensh.com/common/gopkgs/logger" "git.getensh.com/common/gopkgs/tasker/httptasker" "git.getensh.com/common/gopkgs/util" "github.com/gin-gonic/gin" "go.uber.org/zap" "net/http" "property-household-gateway/errors" param_v1 "property-household-gateway/param/v1" "property-household-gateway/pb" "property-household-gateway/pb/v1" "property-household-gateway/utils" ) // // @Summary 门禁列表 // @Description 门禁列表 // @Tags 门禁 // @Accept json // @Produce json // @Param token header string true " " // @Param garden_id query int true "小区id" // @Success 200 {object} v1.GateListResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/gate [get] func (c *Controller) GateList(ctx *gin.Context) { // 解析参数 req := ¶m_v1.GateListRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, &req.GateListQuery, nil) if err != nil { logger.Error("func", zap.String("call", "util.ShouldBind"), zap.String("error", err.Error())) return errors.ParamsError } if req.GardenId == 0 { return errors.ParamsError } return nil } // 业务处理 handleServiceTask := func() error { tokenInfo, err := utils.GetJwtTokenInfo(ctx) if err != nil { return err } resp := param_v1.GateListResponse{} greq := &v1.GardenHouseholdUnitIdsRequest{GardenId: req.GardenId, Uids: []int64{tokenInfo.Uid}} greply, err := pb.Garden.GardenHouseholdUnitIds(ctx, greq) if err != nil { s, _ := json.MarshalToString(greq) logger.Error("func", zap.String("call", "Garden.GardenHouseholdUnitIds"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } if len(greply.List) == 0 { ctx.JSON(http.StatusOK, resp) return nil } rpcReq := &v1.GateUnitDeviceRequest{ GardenId: req.GardenId, UnitId: greply.List[0].UnitIds, } rpcRsp, err := pb.Device.GateUnitDevice(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "Device.GateUnitDevice"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } if rpcRsp.List == nil { rpcRsp.List = make([]*v1.GateItem, 0) } resp.Data = *rpcRsp ctx.JSON(http.StatusOK, resp) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) } // // @Summary 录入人脸 // @Description 录入人脸 // @Tags 门禁 // @Accept json // @Produce json // @Param token header string true " " // @Param body body v1.GateUserPicAddBody true " " // @Success 200 {object} v1.GateUserPicAddResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/gate/face [post] func (c *Controller) GateUserPicAdd(ctx *gin.Context) { // 解析参数 req := ¶m_v1.GateUserPicAddRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.GateUserPicAddBody) if err != nil { logger.Error("func", zap.String("call", "util.ShouldBind"), zap.String("error", err.Error())) return errors.ParamsError } if req.GardenId == 0 { return errors.ParamsError } return nil } // 业务处理 handleServiceTask := func() error { tokenInfo, err := utils.GetJwtTokenInfo(ctx) if err != nil { return err } resp := param_v1.GateUserPicAddResponse{} rpcReq := &v1.GateUserPicAddRequest{ GardenId: req.GardenId, PicUrl: req.PicUrl, Uid: tokenInfo.Uid, Phone: tokenInfo.Phone, //todo IdNumber: tokenInfo.IdNumber, Name: tokenInfo.RealName, } _, err = pb.Device.GateUserPicAdd(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "Device.GateUserPicAdd"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } ctx.JSON(http.StatusOK, resp) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) } // // @Summary 人脸信息 // @Description 人脸信息 // @Tags 门禁 // @Accept json // @Produce json // @Param token header string true " " // @Param garden_id query int true "小区id" // @Success 200 {object} v1.GateUserPicInfoResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/gate/face [get] func (c *Controller) GateUserPicInfo(ctx *gin.Context) { // 解析参数 req := ¶m_v1.GateUserPicInfoRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, &req.GateUserPicInfoQuery, nil) if err != nil { logger.Error("func", zap.String("call", "util.ShouldBind"), zap.String("error", err.Error())) return errors.ParamsError } if req.GardenId == 0 { return errors.ParamsError } return nil } // 业务处理 handleServiceTask := func() error { tokenInfo, err := utils.GetJwtTokenInfo(ctx) if err != nil { return err } resp := param_v1.GateUserPicInfoResponse{} rpcReq := &v1.GateUserPicInfoRequest{ GardenId: req.GardenId, Uid: tokenInfo.Uid, } rpcRsp, err := pb.Device.GateUserPicInfo(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "Device.GateUserPicInfo"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } resp.Data = *rpcRsp ctx.JSON(http.StatusOK, resp) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) } // // @Summary 是否有人脸设备 // @Description 是否有人脸设备 // @Tags 门禁 // @Accept json // @Produce json // @Param token header string true " " // @Param garden_id query int true "小区id" // @Success 200 {object} v1.GateHasFaceResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/gate/has_face_device [get] func (c *Controller) GateHasFace(ctx *gin.Context) { // 解析参数 req := ¶m_v1.GateHasFaceRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, &req.GateHasFaceQuery, nil) if err != nil { logger.Error("func", zap.String("call", "util.ShouldBind"), zap.String("error", err.Error())) return errors.ParamsError } if req.GardenId == 0 { return errors.ParamsError } return nil } // 业务处理 handleServiceTask := func() error { tokenInfo, err := utils.GetJwtTokenInfo(ctx) if err != nil { return err } resp := param_v1.GateHasFaceResponse{} greq := &v1.GardenHouseholdUnitIdsRequest{GardenId: req.GardenId, Uids: []int64{tokenInfo.Uid}} greply, err := pb.Garden.GardenHouseholdUnitIds(ctx, greq) if err != nil { s, _ := json.MarshalToString(greq) logger.Error("func", zap.String("call", "Garden.GardenHouseholdUnitIds"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } if len(greply.List) == 0 { ctx.JSON(http.StatusOK, resp) return nil } rpcReq := &v1.GateUnitDeviceRequest{ GardenId: req.GardenId, UnitId: greply.List[0].UnitIds, OnlyHas: true, } rpcRsp, err := pb.Device.GateUnitDevice(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "Device.GateUnitDevice"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } resp.Data.HasFaceDevice = rpcRsp.HasDevice ctx.JSON(http.StatusOK, resp) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) } // // @Summary 门禁二维码 // @Description 门禁二维码 // @Tags 门禁 // @Accept json // @Produce json // @Param token header string true " " // @Param garden_id query int true "小区id" // @Success 200 {object} v1.GateQcodeResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/gate/qcode [get] func (c *Controller) GateQcode(ctx *gin.Context) { // 解析参数 req := ¶m_v1.GateQcodeRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, &req.GateQcodeQuery, nil) if err != nil { logger.Error("func", zap.String("call", "util.ShouldBind"), zap.String("error", err.Error())) return errors.ParamsError } if req.GardenId == 0 { return errors.ParamsError } return nil } // 业务处理 handleServiceTask := func() error { tokenInfo, err := utils.GetJwtTokenInfo(ctx) if err != nil { return errors.TokenFailedError } resp := param_v1.GateQcodeResponse{} rpcReq := &v1.GateQcodeRequest{ GardenId: req.GardenId, Uid: tokenInfo.Uid, } rpcRsp, err := pb.Household.GateQcode(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "Household.GateQcode"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } resp.Data = *rpcRsp ctx.JSON(http.StatusOK, resp) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) } // // @Summary 门禁访客邀约二维码 // @Description 门禁访客邀约二维码 // @Tags 门禁 // @Accept json // @Produce json // @Param token header string true " " // @Param device_id query int true "设备id" // @Param garden_id query int true "小区id" // @Param start query int true "开始时间戳" // @Param end query int true "截止时间戳" // @Param visitor_name query string true "访客姓名" // @Param visitor_phone query string true "访客联系方式" // @Param comment query string true "访问是由" // @Success 200 {object} v1.GateQcodeVisitorResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/gate/qcode_visitor [get] func (c *Controller) GateQcodeVisitor(ctx *gin.Context) { // 解析参数 req := ¶m_v1.GateQcodeVisitorRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, &req.GateQcodeVisitorQuery, nil) if err != nil { logger.Error("func", zap.String("call", "util.ShouldBind"), zap.String("error", err.Error())) return errors.ParamsError } if req.GardenId == 0 { return errors.ParamsError } return nil } // 业务处理 handleServiceTask := func() error { tokenInfo, err := utils.GetJwtTokenInfo(ctx) if err != nil { return errors.TokenFailedError } resp := param_v1.GateQcodeVisitorResponse{} rpcReq := &v1.GateQcodeRequest{ GardenId: req.GardenId, DeviceId: req.DeviceId, Uid: tokenInfo.Uid, Visitor: true, VisitorStart: req.Start, VisitorEnd: req.End, VisitorName: req.VisitorName, VisitorPhone: req.VisitorPhone, UserName: tokenInfo.RealName, Phone: tokenInfo.Phone, Comment: req.Comment, } rpcRsp, err := pb.Household.GateQcode(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "Household.GateQcode"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } resp.Data = *rpcRsp ctx.JSON(http.StatusOK, resp) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) } // // @Summary 我的邀约 // @Description 我的邀约 // @Tags 门禁 // @Accept json // @Produce json // @Param token header string true " " // @Param page query int false " " // @Param page_size query int false " " // @Param gate_id query int false "门禁id选填" // @Success 200 {object} v1.GateVisitorListResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/gate/visitor [get] func (c *Controller) GateVisitorList(ctx *gin.Context) { // 解析参数 req := ¶m_v1.GateVisitorListRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, &req.GateVisitorListQuery, nil) if err != nil { logger.Error("func", zap.String("call", "util.ShouldBind"), zap.String("error", err.Error())) return errors.ParamsError } return nil } // 业务处理 handleServiceTask := func() error { tokenInfo, err := utils.GetJwtTokenInfo(ctx) if err != nil { return err } resp := param_v1.GateVisitorListResponse{} rpcReq := &v1.GateVisitorListRequest{ Page: req.Page, PageSize: req.PageSize, DeviceId: req.GateId, Uid: tokenInfo.Uid, } fmt.Printf("token:%v\n", tokenInfo.Uid) rpcRsp, err := pb.Device.GateVisitorList(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "Device.GateVisitorList"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } if rpcRsp.List == nil { rpcRsp.List = make([]*v1.GateVisitorListItem, 0) } resp.Data = *rpcRsp ctx.JSON(http.StatusOK, resp) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) } // // @Summary 删除访客记录 // @Description 删除访客记录 // @Tags 门禁 // @Accept json // @Produce json // @Param token header string true " " // @Param id query int true " 记录id" // @Success 200 {object} v1.GateVisitorDelResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/gate/visitor [delete] func (c *Controller) GateVisitorDel(ctx *gin.Context) { // 解析参数 req := ¶m_v1.GateVisitorDelRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, &req.GateVisitorDelQuery, nil) if err != nil { logger.Error("func", zap.String("call", "util.ShouldBind"), zap.String("error", err.Error())) return errors.ParamsError } return nil } // 业务处理 handleServiceTask := func() error { tokenInfo, err := utils.GetJwtTokenInfo(ctx) if err != nil { return err } resp := param_v1.GateVisitorDelResponse{} rpcReq := &v1.GateVisitorDelRequest{ Id: req.Id, Uid: tokenInfo.Uid, } _, err = pb.Device.GateVisitorDel(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "Device.GateVisitorDel"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } ctx.JSON(http.StatusOK, resp) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) } // // @Summary 访客获取到的分享页面 // @Description 访客获取到的分享页面 // @Tags 门禁 // @Accept json // @Produce json // @Param id query int true " 访客邀约记录id" // @Success 200 {object} v1.GateVisitorPageResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/gate/visitor/share [get] func (c *Controller) GateVisitorPage(ctx *gin.Context) { // 解析参数 req := ¶m_v1.GateVisitorPageRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, nil, nil, &req.GateVisitorPageQuery, nil) if err != nil { logger.Error("func", zap.String("call", "util.ShouldBind"), zap.String("error", err.Error())) return errors.ParamsError } return nil } // 业务处理 handleServiceTask := func() error { resp := param_v1.GateVisitorPageResponse{} rpcReq := &v1.GateVisitorListRequest{ Id: req.Id, } rpcRsp, err := pb.Device.GateVisitorList(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "Device.GateVisitorList"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } if len(rpcRsp.List) == 0 { return errors.ErrRecordNotFound } resp.Data = *rpcRsp.List[0] ctx.JSON(http.StatusOK, resp) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) }