staff.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package v1
  4. import (
  5. "access-control-monitor/errors"
  6. "access-control-monitor/impl/v1"
  7. "access-control-monitor/param/base"
  8. param_v1 "access-control-monitor/param/v1"
  9. "access-control-monitor/utils"
  10. "bytes"
  11. "encoding/base64"
  12. "fmt"
  13. "net/http"
  14. "google.golang.org/grpc/status"
  15. "github.com/jaryhe/gopkgs/logger"
  16. "github.com/jaryhe/gopkgs/tasker/httptasker"
  17. "github.com/jaryhe/gopkgs/util"
  18. "github.com/gin-gonic/gin"
  19. "go.uber.org/zap"
  20. )
  21. // 授权获取key
  22. // @Summary 授权获取key
  23. // @Description 授权获取key
  24. // @Tags user
  25. // @Accept json
  26. // @Produce json
  27. // @Param body body v1.LoginBody true "授权信息"
  28. // @Success 200 {object} v1.LoginResponse
  29. // @Failure 500 {object} base.HTTPError
  30. // @Router /api/v1/auth [put]
  31. func (c *Controller) Auth(ctx *gin.Context) {
  32. // 解析参数
  33. req := &param_v1.LoginRequest{}
  34. resp := param_v1.LoginResponse{Result: base.Result{
  35. Code: 0,
  36. Message: "成功",
  37. }}
  38. parseParamTask := func() error {
  39. err := util.ShouldBind(ctx, nil, nil, nil, &req.LoginBody)
  40. if err != nil {
  41. logger.Error("func",
  42. zap.String("call", "util.ShouldBind"),
  43. zap.String("error", err.Error()))
  44. return errors.ParamsError
  45. }
  46. return nil
  47. }
  48. // 业务处理
  49. handleServiceTask := func() error {
  50. // 响应数据
  51. // 第一步本地缓存获取,本地缓存有直接返回,本地缓存无调用接口获取
  52. data, err := utils.GetDeviceInfo(req.Sn)
  53. if err != nil {
  54. return err
  55. }
  56. resp.Data.Key = data.Key
  57. ctx.JSON(http.StatusOK, resp)
  58. return nil
  59. }
  60. // 执行任务
  61. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  62. }
  63. // 上传员工信息
  64. // @Summary 上传员工信息
  65. // @Description 上传员工信息
  66. // @Tags user
  67. // @Accept json
  68. // @Produce json
  69. // @Param body body v1.RegisterRequest true "上传员工信息"
  70. // @Success 200 {object} v1.RegisterResponse
  71. // @Failure 500 {object} base.HTTPError
  72. // @Router /api/v1/register [post]
  73. func (c *Controller) Register(ctx *gin.Context) {
  74. // 解析参数
  75. req := &param_v1.RegisterRequest{}
  76. resp := param_v1.RegisterResponse{Result: base.Result{
  77. Code: 0,
  78. Message: "成功",
  79. }}
  80. parseParamTask := func() error {
  81. err := util.ShouldBind(ctx, nil, nil, nil, &req.RegisterBody)
  82. if err != nil {
  83. logger.Error("func",
  84. zap.String("call", "util.ShouldBind"),
  85. zap.String("error", err.Error()))
  86. return errors.ParamsError
  87. }
  88. bytes, _ := json.Marshal(req)
  89. fmt.Printf("register read:%s\n", bytes)
  90. return nil
  91. }
  92. // 业务处理
  93. handleServiceTask := func() error {
  94. // 响应数据
  95. // 第一步校验key
  96. value, err := utils.GetDeviceInfo(req.Sn)
  97. if err != nil {
  98. return err
  99. }
  100. if req.IdPhoto == "" {
  101. return status.Error(10003, "参数错误,身份证照片为空")
  102. }
  103. if req.Photo == "" {
  104. return status.Error(10003, "参数错误,现场人员可见光照片为ko")
  105. }
  106. // 上传身份证照片
  107. phone, err := base64.StdEncoding.DecodeString(req.IdPhoto)
  108. if err != nil {
  109. logger.Error("func",
  110. zap.String("call", "base64.StdEncoding.DecodeString"),
  111. zap.String("args", "idphoto"+req.Name),
  112. zap.String("error", err.Error()))
  113. return status.Error(10003, "参数错误,图片base64编码格式不对")
  114. }
  115. reader := bytes.NewReader(phone)
  116. req.IdPhoto, err = utils.UploadToMinio(req.Name+"身份证照片", reader, reader.Size(), "image/png")
  117. if err != nil {
  118. logger.Error("func",
  119. zap.String("call", "UploadToMinio"),
  120. zap.String("args", req.Name+"身份证照片"),
  121. zap.String("error", err.Error()))
  122. return errors.SystemError
  123. }
  124. // 上传现场照片
  125. phone, err = base64.StdEncoding.DecodeString(req.Photo)
  126. if err != nil {
  127. logger.Error("func",
  128. zap.String("call", "base64.StdEncoding.DecodeString"),
  129. zap.String("args", "photo"+req.Name),
  130. zap.String("error", err.Error()))
  131. return status.Error(10003, "参数错误,图片base64编码格式不对")
  132. }
  133. reader = bytes.NewReader(phone)
  134. req.Photo, err = utils.UploadToMinio(req.Name+"现场照片", reader, reader.Size(), "image/png")
  135. if err != nil {
  136. logger.Error("func",
  137. zap.String("call", "UploadToMinio"),
  138. zap.String("args", req.Name+"现场照片"),
  139. zap.String("error", err.Error()))
  140. return errors.SystemError
  141. }
  142. // 注册
  143. err = v1.Register(req.RegisterBody, value.ProjectId)
  144. if err != nil {
  145. return err
  146. }
  147. ctx.JSON(http.StatusOK, resp)
  148. return nil
  149. }
  150. // 执行任务
  151. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  152. }
  153. // 删除员工信息
  154. // @Summary 删除员工信息
  155. // @Description 删除员工信息
  156. // @Tags user
  157. // @Accept json
  158. // @Produce json
  159. // @Param body body v1.StaffDeleteBody true "删除人员"
  160. // @Success 200 {object} v1.StaffDeleteResp
  161. // @Failure 500 {object} base.HTTPError
  162. // @Router /api/v1/delete [put]
  163. func (c *Controller) StaffDelete(ctx *gin.Context) {
  164. // 解析参数
  165. req := &param_v1.StaffDeleteRequest{}
  166. resp := param_v1.StaffDeleteResponse{Result: base.Result{
  167. Code: 0,
  168. Message: "成功",
  169. }}
  170. parseParamTask := func() error {
  171. err := util.ShouldBind(ctx, nil, nil, nil, &req.StaffDeleteBody)
  172. if err != nil {
  173. logger.Error("func",
  174. zap.String("call", "util.ShouldBind"),
  175. zap.String("error", err.Error()))
  176. return errors.ParamsError
  177. }
  178. bytes, _ := json.Marshal(req)
  179. fmt.Printf("delete read:%s\n", bytes)
  180. return nil
  181. }
  182. // 业务处理
  183. handleServiceTask := func() error {
  184. // 第一步校验key
  185. _, err := utils.GetDeviceInfo(req.Sn)
  186. if err != nil {
  187. return err
  188. }
  189. // 响应数据
  190. err = v1.StaffDelete(req.Sn, req.WorkNos)
  191. if err != nil {
  192. return err
  193. }
  194. //resp.Data.WorkNos = workNos
  195. ctx.JSON(http.StatusOK, resp)
  196. return nil
  197. }
  198. // 执行任务
  199. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  200. }
  201. // 上传考勤数据
  202. // @Summary 上传考勤数据
  203. // @Description 上传考勤数据
  204. // @Tags user
  205. // @Accept json
  206. // @Produce json
  207. // @Param body body v1.AttendanceRequest true "上传考勤数据"
  208. // @Success 200 {object} v1.AttendanceResponse
  209. // @Failure 500 {object} base.HTTPError
  210. // @Router /api/v1/attendance [put]
  211. func (c *Controller) Attendance(ctx *gin.Context) {
  212. // 解析参数
  213. req := &param_v1.AttendanceRequest{}
  214. resp := param_v1.AttendanceResponse{Result: base.Result{
  215. Code: 0,
  216. Message: "成功",
  217. }}
  218. parseParamTask := func() error {
  219. err := util.ShouldBind(ctx, nil, nil, nil, &req.AttendanceBody)
  220. if err != nil {
  221. logger.Error("func",
  222. zap.String("call", "util.ShouldBind"),
  223. zap.String("error", err.Error()))
  224. return errors.ParamsError
  225. }
  226. bytes, _ := json.Marshal(req)
  227. fmt.Printf("attendance read:%s\n", bytes)
  228. return nil
  229. }
  230. // 业务处理
  231. handleServiceTask := func() error {
  232. // 响应数据
  233. // 第一步校验key
  234. value, err := utils.GetDeviceInfo(req.Sn)
  235. if err != nil {
  236. return err
  237. }
  238. // 添加考勤数据
  239. err = v1.AddAttendance(req.Content, value.ProjectId)
  240. if err != nil {
  241. return err
  242. }
  243. ctx.JSON(http.StatusOK, resp)
  244. return nil
  245. }
  246. // 执行任务
  247. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  248. }
  249. /*
  250. // 批量下发人员信息
  251. // @Summary 批量下发人员信息
  252. // @Description 批量下发人员信息
  253. // @Tags user
  254. // @Accept json
  255. // @Produce json
  256. // @Param sn query string true "设备序列号"
  257. // @Success 200 {object} v1.BatchDownloadResponse
  258. // @Failure 500 {object} base.HTTPError
  259. // @Router /api/v1/batch_download [get]
  260. func (c *Controller) BatchDownload(ctx *gin.Context) {
  261. // 解析参数
  262. req := &param_v1.BatchDownloadRequet{}
  263. resp := param_v1.BatchDownloadResponse{Result: base.Result{
  264. Code: 0,
  265. Message: "成功",
  266. }}
  267. parseParamTask := func() error {
  268. err := util.ShouldBind(ctx, nil, nil, &req.BatchDownloadQuery, nil)
  269. if err != nil {
  270. logger.Error("func",
  271. zap.String("call", "util.ShouldBind"),
  272. zap.String("error", err.Error()))
  273. return errors.ParamsError
  274. }
  275. return nil
  276. }
  277. // 业务处理
  278. handleServiceTask := func() error {
  279. // 响应数据
  280. data, err := v1.GetBatchDownload(req.Sn)
  281. if err != nil {
  282. return err
  283. }
  284. resp.Data = data
  285. ctx.JSON(http.StatusOK, resp)
  286. return nil
  287. }
  288. // 执行任务
  289. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  290. }
  291. // 结果反馈
  292. // @Summary 结果反馈
  293. // @Description 结果反馈
  294. // @Tags user
  295. // @Accept json
  296. // @Produce json
  297. // @Param body body v1.FeedBackRequet true "结果反馈"
  298. // @Success 200 {object} v1.FeedBackResponse
  299. // @Failure 500 {object} base.HTTPError
  300. // @Router /api/v1/feedback [put]
  301. func (c *Controller) FeedBack(ctx *gin.Context) {
  302. // 解析参数
  303. req := &param_v1.FeedBackRequet{}
  304. resp := param_v1.FeedBackResponse{Result: base.Result{
  305. Code: 0,
  306. Message: "成功",
  307. }}
  308. parseParamTask := func() error {
  309. err := util.ShouldBind(ctx, nil, nil, nil, &req.FeedBackBody)
  310. if err != nil {
  311. logger.Error("func",
  312. zap.String("call", "util.ShouldBind"),
  313. zap.String("error", err.Error()))
  314. return errors.ParamsError
  315. }
  316. return nil
  317. }
  318. // 业务处理
  319. handleServiceTask := func() error {
  320. // 响应数据
  321. var err error
  322. switch req.Type {
  323. case consts.DeleteSucess:
  324. // 删除成功后删除本地数据
  325. err = v1.Delete(req.Sn)
  326. case consts.DeleteFail:
  327. // 删除失败不做任何处理
  328. case consts.DownLoadSucess:
  329. // 下发成功设置为已下发
  330. err = v1.SetDownloadStatus(req.Sn, consts.StaffAlreadyDownload)
  331. case consts.DownLoadFail:
  332. // 下发失败回滚为新增
  333. err = v1.SetDownloadStatus(req.Sn, consts.StaffNew)
  334. }
  335. if err != nil {
  336. return err
  337. }
  338. ctx.JSON(http.StatusOK, resp)
  339. return nil
  340. }
  341. // 执行任务
  342. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  343. }
  344. */