- 新增滑动续签功能:人工触屏触发本地心跳 + 30秒节流刷新JWT,避免作业中令牌过期 - 拆分质量检验权限:过程检/完工检使用不同权限码,细化权限粒度 - 新增操作人用户ID字段:报工、拧紧记录落库时携带可信用户ID,解决身份追溯问题 - 移除编译期默认密钥,启动期校验所有必要密钥非空,防止密钥泄露风险 - 修复WMS附件越权遍历问题:使用UUID文件名替代顺序ID访问附件 - 实现登录态过期数据隔离:仅返回当前操作人的待同步数据,避免身份混淆
228 lines
8.9 KiB
Go
228 lines
8.9 KiB
Go
package handler
|
||
|
||
import (
|
||
"net/http"
|
||
"net/url"
|
||
|
||
"bj_power_mes/common/httpx"
|
||
"bj_power_mes/internal/logic"
|
||
"bj_power_mes/internal/svc"
|
||
|
||
"github.com/xuri/excelize/v2"
|
||
)
|
||
|
||
// CreateInspectionHandler POST /inspections(PAD 巡检终端提交记录)
|
||
func CreateInspectionHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
var req logic.InspectionReq
|
||
if err := httpx.ParseJSON(r, &req); err != nil {
|
||
httpx.BadRequest(w, "请求体解析失败")
|
||
return
|
||
}
|
||
// 按钮级权限:按记录类型校验(块2/8)
|
||
categoryPerm := map[string]string{
|
||
"CHECKIN": "sys.inspect:checkin",
|
||
"POINT": "sys.inspect:point",
|
||
"PROCESS": "sys.inspect:process",
|
||
"DONE": "sys.inspect:done",
|
||
"ALARM": "sys.inspect:alarm",
|
||
}
|
||
// 质量检验权限粒度拆分(补1):过程检(QC_PROCESS)操作工自检+质检员;完工检(FINAL)/来料检(INCOMING)质检员专属。
|
||
// 故按记录类型映射不同权限码,而非单一 produce.quality:edit。
|
||
// 注:PAD 巡检「过程巡检」仍用 PROCESS(走 categoryPerm 的 sys.inspect:process),与质检隔离(P1-8)。
|
||
qualityCatPerm := map[string]string{
|
||
"QC_PROCESS": "produce.quality:process",
|
||
"FINAL": "produce.quality:final",
|
||
"INCOMING": "produce.quality:final",
|
||
}
|
||
if perm, ok := qualityCatPerm[req.Category]; ok {
|
||
if !userHasPerm(r, svcCtx, perm) {
|
||
httpx.FailHTTP(w, http.StatusForbidden, "无操作权限:"+perm)
|
||
return
|
||
}
|
||
} else if code, ok := categoryPerm[req.Category]; ok {
|
||
if !userHasPerm(r, svcCtx, code) {
|
||
httpx.FailHTTP(w, http.StatusForbidden, "无操作权限:"+code)
|
||
return
|
||
}
|
||
} else {
|
||
// 未识别的检验类型,拒绝提交,避免越权放行
|
||
httpx.FailHTTP(w, http.StatusBadRequest, "未知的检验类型:"+req.Category)
|
||
return
|
||
}
|
||
msg, err := logic.New(svcCtx).CreateInspection(r.Context(), req, operator(r, ""))
|
||
if err != nil {
|
||
httpx.Fail(w, 2301, err.Error())
|
||
return
|
||
}
|
||
// message 放 data 返回:质量检验页需读取「库存联动失败/已联动」提示(含 WMS 原文原因)
|
||
httpx.Ok(w, map[string]any{"message": msg})
|
||
}
|
||
}
|
||
|
||
// ListInspectionsHandler GET /inspections?category=&operator=&from=&to=&materialCode=&materialName=&reportNo=&page=&pageSize=
|
||
// 返回 {list, total},后端真分页(created_at desc / id desc,最新置顶)
|
||
// materialCode/materialName/reportNo:质量检验模糊查询(每框内模糊、多框取交集)
|
||
func ListInspectionsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
q := r.URL.Query()
|
||
page := atoiDefault(q.Get("page"), 1)
|
||
pageSize := atoiDefault(q.Get("pageSize"), 50)
|
||
if page < 1 {
|
||
page = 1
|
||
}
|
||
if pageSize < 1 || pageSize > 1000 {
|
||
pageSize = 50
|
||
}
|
||
data, total, err := logic.New(svcCtx).ListInspectionsPaged(r.Context(),
|
||
q.Get("category"), q.Get("operator"), q.Get("from"), q.Get("to"),
|
||
q.Get("orderNo"), q.Get("stationNo"),
|
||
q.Get("materialCode"), q.Get("materialName"), q.Get("reportNo"), page, pageSize)
|
||
if err != nil {
|
||
httpx.Fail(w, 2302, err.Error())
|
||
return
|
||
}
|
||
httpx.Ok(w, map[string]any{"list": data, "total": total})
|
||
}
|
||
}
|
||
|
||
// UploadPhotoHandler POST /inspections/upload(巡检/异常拍照上传)
|
||
// 统一存储:<Upload.Dir>/年/月/日/INSPECTION_PHOTO/<uuid>.<ext>;返回相对路径 + 免鉴权直链。
|
||
func UploadPhotoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
if err := r.ParseMultipartForm(svcCtx.Config.Upload.MaxMB << 20); err != nil {
|
||
httpx.BadRequest(w, "上传文件过大或格式错误")
|
||
return
|
||
}
|
||
res, err := saveTypedUpload(r, "file", svcCtx.Config.Upload.Dir, "inspection", FileTypeInspectionPhoto,
|
||
uploadMaxBytes(svcCtx.Config.Upload.MaxSize, svcCtx.Config.Upload.MaxMB, FileTypeInspectionPhoto))
|
||
if err != nil {
|
||
httpx.Fail(w, 2303, "照片保存失败:"+err.Error())
|
||
return
|
||
}
|
||
httpx.Ok(w, map[string]any{"filename": res.RelPath, "url": "/api/v1/files/" + res.RelPath})
|
||
}
|
||
}
|
||
|
||
// FinishedPendingHandler GET /quality/finished-pending?page=&pageSize=
|
||
// 待完工检成品:完工确认(DONE)后尚无对应完工检记录的成品 SN,只读分页(完工时间倒序)
|
||
func FinishedPendingHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
q := r.URL.Query()
|
||
page := atoiDefault(q.Get("page"), 1)
|
||
pageSize := atoiDefault(q.Get("pageSize"), 20)
|
||
list, total, err := logic.New(svcCtx).FinishedPendingList(r.Context(), page, pageSize)
|
||
if err != nil {
|
||
httpx.Fail(w, 2308, err.Error())
|
||
return
|
||
}
|
||
httpx.Ok(w, map[string]any{"list": list, "total": total})
|
||
}
|
||
}
|
||
|
||
// InspectionDisposalHandler POST /quality/disposal 质量检验处置(退货/返修/退换)
|
||
func InspectionDisposalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
var req struct {
|
||
Id int `json:"id"`
|
||
DisposalType string `json:"disposalType"`
|
||
DisposalRemark string `json:"disposalRemark"`
|
||
}
|
||
if err := httpx.ParseJSON(r, &req); err != nil || req.Id <= 0 {
|
||
httpx.BadRequest(w, "缺少 id")
|
||
return
|
||
}
|
||
// 权限粒度拆分(补1):处置按记录类型校验——过程检(QC_PROCESS)用 produce.quality:process,
|
||
// 完工检/来料检(FINAL/INCOMING)用 produce.quality:final。后端兜底校验,与前端 can() 一致。
|
||
rec, err := logic.New(svcCtx).GetInspectionRecord(r.Context(), req.Id)
|
||
if err != nil {
|
||
httpx.Fail(w, 2305, err.Error())
|
||
return
|
||
}
|
||
dispPerm := "produce.quality:final"
|
||
if rec.Category == "QC_PROCESS" {
|
||
dispPerm = "produce.quality:process"
|
||
}
|
||
if !userHasPerm(r, svcCtx, dispPerm) {
|
||
httpx.FailHTTP(w, http.StatusForbidden, "无操作权限:"+dispPerm)
|
||
return
|
||
}
|
||
msg, err := logic.New(svcCtx).SetInspectionDisposal(r.Context(), req.Id, req.DisposalType, req.DisposalRemark, operator(r, ""))
|
||
if err != nil {
|
||
httpx.Fail(w, 2305, err.Error())
|
||
return
|
||
}
|
||
httpx.OkMessage(w, msg, nil)
|
||
}
|
||
}
|
||
|
||
// InspectionBindsHandler GET /inspections/binds?sn= 检验记录详情附带的装配物料绑定明细
|
||
// (按 SN 跨工艺查 workpiece_bind,标注工艺/工位;客户需求:检验记录附装配明细)
|
||
func InspectionBindsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
sn := r.URL.Query().Get("sn")
|
||
if sn == "" {
|
||
httpx.BadRequest(w, "缺少 sn")
|
||
return
|
||
}
|
||
httpx.Ok(w, logic.New(svcCtx).InspectionBinds(r.Context(), sn))
|
||
}
|
||
}
|
||
|
||
// UnqualifiedReportHandler GET /quality/unqualified-report?id= 一键生成《不合格单》xlsx 模板(预填检验数据)
|
||
func UnqualifiedReportHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
id := atoiDefault(r.URL.Query().Get("id"), 0)
|
||
if id <= 0 {
|
||
httpx.Fail(w, 2306, "缺少 id")
|
||
return
|
||
}
|
||
rec, err := logic.New(svcCtx).GetInspectionRecord(r.Context(), id)
|
||
if err != nil {
|
||
httpx.Fail(w, 2307, err.Error())
|
||
return
|
||
}
|
||
f := excelize.NewFile()
|
||
sheet := "不合格单"
|
||
f.SetSheetName("Sheet1", sheet)
|
||
headers := []string{"报检单号", "图号", "物料名称", "生产厂家", "检验单号", "结论", "处置方式", "处置说明", "记录时间"}
|
||
for c, h := range headers {
|
||
cell, _ := excelize.CoordinatesToCellName(c+1, 1)
|
||
_ = f.SetCellValue(sheet, cell, h)
|
||
}
|
||
row := []any{
|
||
rec.ReportNo, rec.MaterialCode, rec.MaterialName, rec.Manufacturer, rec.InspectionNo,
|
||
rec.Result, rec.DisposalType, rec.DisposalRemark, rec.CreatedAt.Format("2006-01-02 15:04:05"),
|
||
}
|
||
for c, v := range row {
|
||
cell, _ := excelize.CoordinatesToCellName(c+1, 2)
|
||
_ = f.SetCellValue(sheet, cell, v)
|
||
}
|
||
// 装配物料绑定明细区(明细下方加区块,照主表风格):该 SN 跨工艺全部装机绑定
|
||
if rec.Sn != "" {
|
||
if binds := logic.New(svcCtx).InspectionBinds(r.Context(), rec.Sn); len(binds) > 0 {
|
||
titleCell, _ := excelize.CoordinatesToCellName(1, 4)
|
||
_ = f.SetCellValue(sheet, titleCell, "装配物料绑定明细")
|
||
bindHeaders := []string{"工件SN", "工艺", "工位", "物料编码", "物料名称", "规格", "绑定值(SN/批次)", "绑定方式", "绑定人", "绑定时间"}
|
||
for c, h := range bindHeaders {
|
||
cell, _ := excelize.CoordinatesToCellName(c+1, 5)
|
||
_ = f.SetCellValue(sheet, cell, h)
|
||
}
|
||
for i, b := range binds {
|
||
bindRow := []any{
|
||
b.Sn, b.FlowName, b.StationNo, b.MaterialCode, b.MaterialName, b.Spec,
|
||
b.BindValue, b.BindMode, b.Operator, b.CreatedAt.Format("2006-01-02 15:04:05"),
|
||
}
|
||
for c, v := range bindRow {
|
||
cell, _ := excelize.CoordinatesToCellName(c+1, 6+i)
|
||
_ = f.SetCellValue(sheet, cell, v)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
w.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
||
w.Header().Set("Content-Disposition", "attachment; filename*=UTF-8''"+url.PathEscape("不合格单.xlsx"))
|
||
_, _ = f.WriteTo(w)
|
||
}
|
||
}
|