123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- // Copyright 2019 github.com. All rights reserved.
- // Use of this source code is governed by github.com.
- package v1
- import (
- "access-control-monitor/errors"
- "access-control-monitor/impl/v1"
- "access-control-monitor/param/base"
- param_v1 "access-control-monitor/param/v1"
- "access-control-monitor/utils"
- "bytes"
- "encoding/base64"
- "fmt"
- "net/http"
- "google.golang.org/grpc/status"
- "github.com/jaryhe/gopkgs/logger"
- "github.com/jaryhe/gopkgs/tasker/httptasker"
- "github.com/jaryhe/gopkgs/util"
- "github.com/gin-gonic/gin"
- "go.uber.org/zap"
- )
- // 授权获取key
- // @Summary 授权获取key
- // @Description 授权获取key
- // @Tags user
- // @Accept json
- // @Produce json
- // @Param body body v1.LoginBody true "授权信息"
- // @Success 200 {object} v1.LoginResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/auth [put]
- func (c *Controller) Auth(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.LoginRequest{}
- resp := param_v1.LoginResponse{Result: base.Result{
- Code: 0,
- Message: "成功",
- }}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, nil, nil, nil, &req.LoginBody)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- // 响应数据
- // 第一步本地缓存获取,本地缓存有直接返回,本地缓存无调用接口获取
- data, err := utils.GetDeviceInfo(req.Sn)
- if err != nil {
- return err
- }
- resp.Data.Key = data.Key
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
- // 上传员工信息
- // @Summary 上传员工信息
- // @Description 上传员工信息
- // @Tags user
- // @Accept json
- // @Produce json
- // @Param body body v1.RegisterRequest true "上传员工信息"
- // @Success 200 {object} v1.RegisterResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/register [post]
- func (c *Controller) Register(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.RegisterRequest{}
- resp := param_v1.RegisterResponse{Result: base.Result{
- Code: 0,
- Message: "成功",
- }}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, nil, nil, nil, &req.RegisterBody)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- bytes, _ := json.Marshal(req)
- fmt.Printf("register read:%s\n", bytes)
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- // 响应数据
- // 第一步校验key
- value, err := utils.GetDeviceInfo(req.Sn)
- if err != nil {
- return err
- }
- if req.IdPhoto == "" {
- return status.Error(10003, "参数错误,身份证照片为空")
- }
- if req.Photo == "" {
- return status.Error(10003, "参数错误,现场人员可见光照片为ko")
- }
- // 上传身份证照片
- phone, err := base64.StdEncoding.DecodeString(req.IdPhoto)
- if err != nil {
- logger.Error("func",
- zap.String("call", "base64.StdEncoding.DecodeString"),
- zap.String("args", "idphoto"+req.Name),
- zap.String("error", err.Error()))
- return status.Error(10003, "参数错误,图片base64编码格式不对")
- }
- reader := bytes.NewReader(phone)
- req.IdPhoto, err = utils.UploadToMinio(req.Name+"身份证照片", reader, reader.Size(), "image/png")
- if err != nil {
- logger.Error("func",
- zap.String("call", "UploadToMinio"),
- zap.String("args", req.Name+"身份证照片"),
- zap.String("error", err.Error()))
- return errors.SystemError
- }
- // 上传现场照片
- phone, err = base64.StdEncoding.DecodeString(req.Photo)
- if err != nil {
- logger.Error("func",
- zap.String("call", "base64.StdEncoding.DecodeString"),
- zap.String("args", "photo"+req.Name),
- zap.String("error", err.Error()))
- return status.Error(10003, "参数错误,图片base64编码格式不对")
- }
- reader = bytes.NewReader(phone)
- req.Photo, err = utils.UploadToMinio(req.Name+"现场照片", reader, reader.Size(), "image/png")
- if err != nil {
- logger.Error("func",
- zap.String("call", "UploadToMinio"),
- zap.String("args", req.Name+"现场照片"),
- zap.String("error", err.Error()))
- return errors.SystemError
- }
- // 注册
- err = v1.Register(req.RegisterBody, value.ProjectId)
- if err != nil {
- return err
- }
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
- // 删除员工信息
- // @Summary 删除员工信息
- // @Description 删除员工信息
- // @Tags user
- // @Accept json
- // @Produce json
- // @Param body body v1.StaffDeleteBody true "删除人员"
- // @Success 200 {object} v1.StaffDeleteResp
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/delete [put]
- func (c *Controller) StaffDelete(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.StaffDeleteRequest{}
- resp := param_v1.StaffDeleteResponse{Result: base.Result{
- Code: 0,
- Message: "成功",
- }}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, nil, nil, nil, &req.StaffDeleteBody)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- bytes, _ := json.Marshal(req)
- fmt.Printf("delete read:%s\n", bytes)
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- // 第一步校验key
- _, err := utils.GetDeviceInfo(req.Sn)
- if err != nil {
- return err
- }
- // 响应数据
- err = v1.StaffDelete(req.Sn, req.WorkNos)
- if err != nil {
- return err
- }
- //resp.Data.WorkNos = workNos
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
- // 上传考勤数据
- // @Summary 上传考勤数据
- // @Description 上传考勤数据
- // @Tags user
- // @Accept json
- // @Produce json
- // @Param body body v1.AttendanceRequest true "上传考勤数据"
- // @Success 200 {object} v1.AttendanceResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/attendance [put]
- func (c *Controller) Attendance(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.AttendanceRequest{}
- resp := param_v1.AttendanceResponse{Result: base.Result{
- Code: 0,
- Message: "成功",
- }}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, nil, nil, nil, &req.AttendanceBody)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- bytes, _ := json.Marshal(req)
- fmt.Printf("attendance read:%s\n", bytes)
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- // 响应数据
- // 第一步校验key
- value, err := utils.GetDeviceInfo(req.Sn)
- if err != nil {
- return err
- }
- // 添加考勤数据
- err = v1.AddAttendance(req.Content, value.ProjectId)
- if err != nil {
- return err
- }
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
- /*
- // 批量下发人员信息
- // @Summary 批量下发人员信息
- // @Description 批量下发人员信息
- // @Tags user
- // @Accept json
- // @Produce json
- // @Param sn query string true "设备序列号"
- // @Success 200 {object} v1.BatchDownloadResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/batch_download [get]
- func (c *Controller) BatchDownload(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.BatchDownloadRequet{}
- resp := param_v1.BatchDownloadResponse{Result: base.Result{
- Code: 0,
- Message: "成功",
- }}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, nil, nil, &req.BatchDownloadQuery, 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 {
- // 响应数据
- data, err := v1.GetBatchDownload(req.Sn)
- if err != nil {
- return err
- }
- resp.Data = data
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
- // 结果反馈
- // @Summary 结果反馈
- // @Description 结果反馈
- // @Tags user
- // @Accept json
- // @Produce json
- // @Param body body v1.FeedBackRequet true "结果反馈"
- // @Success 200 {object} v1.FeedBackResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/feedback [put]
- func (c *Controller) FeedBack(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.FeedBackRequet{}
- resp := param_v1.FeedBackResponse{Result: base.Result{
- Code: 0,
- Message: "成功",
- }}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, nil, nil, nil, &req.FeedBackBody)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- // 响应数据
- var err error
- switch req.Type {
- case consts.DeleteSucess:
- // 删除成功后删除本地数据
- err = v1.Delete(req.Sn)
- case consts.DeleteFail:
- // 删除失败不做任何处理
- case consts.DownLoadSucess:
- // 下发成功设置为已下发
- err = v1.SetDownloadStatus(req.Sn, consts.StaffAlreadyDownload)
- case consts.DownLoadFail:
- // 下发失败回滚为新增
- err = v1.SetDownloadStatus(req.Sn, consts.StaffNew)
- }
- if err != nil {
- return err
- }
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
- */
|