add printer get printers
This commit is contained in:
@@ -106,3 +106,11 @@ func captchaVerify(c *gin.Context, code string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func buildErrJson(c *gin.Context, err error) {
|
||||||
|
c.JSON(http.StatusOK, &CallBack{
|
||||||
|
Code: model.ErrCodeNormal,
|
||||||
|
Desc: err.Error(),
|
||||||
|
})
|
||||||
|
globals.SugarLogger.Debugf("End API :%s error:%v:", c.Request.URL, err)
|
||||||
|
}
|
||||||
|
|||||||
73
controllers/print_controller.go
Normal file
73
controllers/print_controller.go
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.rosy.net.cn/jx-print/globals"
|
||||||
|
"git.rosy.net.cn/jx-print/model"
|
||||||
|
"git.rosy.net.cn/jx-print/services"
|
||||||
|
"git.rosy.net.cn/jx-print/utils"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
//添加打印机 POST
|
||||||
|
func AddPrinters(c *gin.Context) {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
tokenInfo *model.TokenInfo
|
||||||
|
param = &struct {
|
||||||
|
AppID int `json:"app_id" form:"app_id" binding:"required"`
|
||||||
|
Prints string `json:"prints" form:"prints" binding:"required"`
|
||||||
|
}{}
|
||||||
|
printInfos []*model.PrintInfo
|
||||||
|
)
|
||||||
|
globals.SugarLogger.Debugf("Begin API :%s params: %v ip: %s", c.Request.URL, c.Params, c.ClientIP())
|
||||||
|
if err = c.Bind(¶m); err != nil {
|
||||||
|
buildErrJson(c, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if tokenInfo = checkToken(c); tokenInfo == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err = utils.UnmarshalUseNumber([]byte(param.Prints), &printInfos); err != nil {
|
||||||
|
buildErrJson(c, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !callFunc(c, func() (retVal interface{}, errCode string, err error) {
|
||||||
|
err = services.AddPrinters(c, tokenInfo.User.UserID, param.AppID, printInfos)
|
||||||
|
return retVal, "", err
|
||||||
|
}) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//查询打印机 GET
|
||||||
|
func GetPrinters(c *gin.Context) {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
tokenInfo *model.TokenInfo
|
||||||
|
param = &struct {
|
||||||
|
AppID int `json:"app_id" form:"app_id" binding:"required"`
|
||||||
|
PrintNo string `json:"print_no" form:"print_no"` //打印机编号
|
||||||
|
Name string `json:"name" form:"name"` //打印机备注,模糊
|
||||||
|
Status int `json:"status" form:"status"` //打印机状态。正常还是缺纸
|
||||||
|
IsOnline int `json:"is_online" form:"is_online"` //在线状态。1在线,0离线
|
||||||
|
Offset int `json:"offset" form:"offset" binding:"required"`
|
||||||
|
PageSize int `json:"page_size" form:"page_size" binding:"required"`
|
||||||
|
}{}
|
||||||
|
)
|
||||||
|
globals.SugarLogger.Debugf("Begin API :%s params: %v ip: %s", c.Request.URL, c.Params, c.ClientIP())
|
||||||
|
if err = c.Bind(¶m); err != nil {
|
||||||
|
buildErrJson(c, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if tokenInfo = checkToken(c); tokenInfo == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !callFunc(c, func() (retVal interface{}, errCode string, err error) {
|
||||||
|
retVal, err = services.GetPrinters(c, param.AppID, param.PrintNo, param.Name, param.Status, param.IsOnline, param.Offset, param.PageSize)
|
||||||
|
return retVal, "", err
|
||||||
|
}) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
82
dao/print_dao.go
Normal file
82
dao/print_dao.go
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
package dao
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.rosy.net.cn/baseapi/utils"
|
||||||
|
"git.rosy.net.cn/jx-print/model"
|
||||||
|
"github.com/jmoiron/sqlx"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetPrinters(db *sqlx.DB, appID int, printNo string) (printers []*model.Printer, err error) {
|
||||||
|
sql := `
|
||||||
|
SELECT *
|
||||||
|
FROM printer
|
||||||
|
WHERE 1 = 1 AND deleted_at = ?
|
||||||
|
`
|
||||||
|
sqlParams := []interface{}{utils.DefaultTimeValue}
|
||||||
|
if appID != 0 {
|
||||||
|
sql += " AND app_id = ?"
|
||||||
|
sqlParams = append(sqlParams, appID)
|
||||||
|
}
|
||||||
|
if printNo != "" {
|
||||||
|
sql += " AND print_no = ?"
|
||||||
|
sqlParams = append(sqlParams, printNo)
|
||||||
|
}
|
||||||
|
if err = db.Select(&printers, sql, sqlParams...); err == nil {
|
||||||
|
return printers, err
|
||||||
|
}
|
||||||
|
return printers, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetPrintersPage(db *sqlx.DB, appID int, printNo, name string, status, isOnline, offset, pageSize int) (page *model.PagedInfo, err error) {
|
||||||
|
var (
|
||||||
|
printers []*model.Printer
|
||||||
|
count = &struct {
|
||||||
|
Count int `json:"count"`
|
||||||
|
}{}
|
||||||
|
)
|
||||||
|
sqlCount := `
|
||||||
|
SELECT COUNT(*) count
|
||||||
|
`
|
||||||
|
sql := `
|
||||||
|
SELECT *
|
||||||
|
`
|
||||||
|
sqlOhter := `
|
||||||
|
FROM printer
|
||||||
|
WHERE 1 = 1 AND deleted_at = ?
|
||||||
|
`
|
||||||
|
sqlParams := []interface{}{utils.DefaultTimeValue}
|
||||||
|
if appID != 0 {
|
||||||
|
sqlOhter += " AND app_id = ?"
|
||||||
|
sqlParams = append(sqlParams, appID)
|
||||||
|
}
|
||||||
|
if printNo != "" {
|
||||||
|
sqlOhter += " AND print_no = ?"
|
||||||
|
sqlParams = append(sqlParams, printNo)
|
||||||
|
}
|
||||||
|
if name != "" {
|
||||||
|
sqlOhter += " AND name LIKE ?"
|
||||||
|
sqlParams = append(sqlParams, "%"+name+"%")
|
||||||
|
}
|
||||||
|
if status != model.StatusAll {
|
||||||
|
sqlOhter += " AND status = ?"
|
||||||
|
sqlParams = append(sqlParams, status)
|
||||||
|
}
|
||||||
|
if isOnline != model.StatusAll {
|
||||||
|
sqlOhter += " AND is_online = ?"
|
||||||
|
sqlParams = append(sqlParams, isOnline)
|
||||||
|
}
|
||||||
|
//查总数
|
||||||
|
row := db.DB.QueryRow(sqlCount+sqlOhter, sqlParams...)
|
||||||
|
if err = row.Scan(count.Count); err != nil {
|
||||||
|
return page, err
|
||||||
|
}
|
||||||
|
sqlOhter += " LIMIT ? OFFSET ?"
|
||||||
|
sqlParams = append(sqlParams, pageSize, offset)
|
||||||
|
if err = db.Select(&printers, sql+sqlOhter, sqlParams...); err == nil {
|
||||||
|
return &model.PagedInfo{
|
||||||
|
TotalCount: count.Count,
|
||||||
|
Data: printers,
|
||||||
|
}, err
|
||||||
|
}
|
||||||
|
return page, err
|
||||||
|
}
|
||||||
@@ -7,6 +7,14 @@ import (
|
|||||||
const (
|
const (
|
||||||
SessionKey = "jxCode"
|
SessionKey = "jxCode"
|
||||||
RegisterKey = "jxRegister"
|
RegisterKey = "jxRegister"
|
||||||
|
|
||||||
|
PrinterStatusNormal = 0 //打印机状态正常
|
||||||
|
PrinterStatusWithouPaper = 1 //缺纸
|
||||||
|
|
||||||
|
PrinterOnline = 1 //在线
|
||||||
|
PrinterOffline = 0 //离线
|
||||||
|
|
||||||
|
StatusAll = -9
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -39,6 +47,17 @@ type TokenInfo struct {
|
|||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PrintInfo struct {
|
||||||
|
PrintNo string `json:"print_no" form:"print_no" binding:"required"` //打印机编号
|
||||||
|
Name string `json:"name" form:"name"` //打印机备注
|
||||||
|
SIM string `json:"sim" form:"sim"` //流量卡号码
|
||||||
|
}
|
||||||
|
|
||||||
|
type PagedInfo struct {
|
||||||
|
TotalCount int `json:"totalCount"`
|
||||||
|
Data interface{} `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
type ModelIDCULD struct {
|
type ModelIDCULD struct {
|
||||||
ID int `json:"id" db:"id"`
|
ID int `json:"id" db:"id"`
|
||||||
CreatedAt *time.Time `json:"created_at" db:"created_at"`
|
CreatedAt *time.Time `json:"created_at" db:"created_at"`
|
||||||
@@ -102,3 +121,18 @@ type Menu struct {
|
|||||||
Color string `json:"color"` //颜色
|
Color string `json:"color"` //颜色
|
||||||
Content string `json:"content"` //菜单内容
|
Content string `json:"content"` //菜单内容
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Printer struct {
|
||||||
|
ID int `json:"id" db:"id"`
|
||||||
|
CreatedAt *time.Time `json:"created_at" db:"created_at"`
|
||||||
|
UpdatedAt *time.Time `json:"updated_at" db:"updated_at"`
|
||||||
|
LastOperator string `json:"last_operator" db:"last_operator"`
|
||||||
|
DeletedAt *time.Time `json:"deleted_at" db:"deleted_at"`
|
||||||
|
AppID int `json:"app_id" db:"app_id"` //应用编号
|
||||||
|
PrintNo string `json:"print_no" db:"print_no"` //打印机编号
|
||||||
|
PrintKey string `json:"print_key" db:"print_key"` //打印机识别码
|
||||||
|
Name string `json:"name"` //打印机备注名
|
||||||
|
Status int `json:"status"` //打印机状态
|
||||||
|
IsOnline int `json:"is_online" db:"is_online"` //1在线,0离线
|
||||||
|
SIM string `json:"sim" db:"sim"` //sim卡号
|
||||||
|
}
|
||||||
|
|||||||
@@ -18,6 +18,10 @@ func Init(r *gin.Engine) {
|
|||||||
app.GET("/getApps", controllers.GetApps)
|
app.GET("/getApps", controllers.GetApps)
|
||||||
app.GET("/addApp", controllers.AddApp)
|
app.GET("/addApp", controllers.AddApp)
|
||||||
app.GET("/delApp", controllers.DelApp)
|
app.GET("/delApp", controllers.DelApp)
|
||||||
|
//print
|
||||||
|
print := v2.Group("/print")
|
||||||
|
print.GET("/addPrinters", controllers.AddPrinters)
|
||||||
|
print.GET("/getPrinters", controllers.GetPrinters)
|
||||||
|
|
||||||
//v1是不需要token的
|
//v1是不需要token的
|
||||||
v1 := r.Group("v1")
|
v1 := r.Group("v1")
|
||||||
|
|||||||
55
services/print.go
Normal file
55
services/print.go
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
package services
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"git.rosy.net.cn/baseapi/utils"
|
||||||
|
"git.rosy.net.cn/jx-print/dao"
|
||||||
|
"git.rosy.net.cn/jx-print/globals"
|
||||||
|
"git.rosy.net.cn/jx-print/model"
|
||||||
|
putils "git.rosy.net.cn/jx-print/utils"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func AddPrinters(c *gin.Context, userID string, appID int, printInfos []*model.PrintInfo) (err error) {
|
||||||
|
var (
|
||||||
|
db = globals.GetDB()
|
||||||
|
errs []error
|
||||||
|
now = time.Now()
|
||||||
|
)
|
||||||
|
if len(printInfos) > 50 {
|
||||||
|
return fmt.Errorf("每次最多添加50台!")
|
||||||
|
}
|
||||||
|
if apps, _ := dao.GetApps(db, appID, userID, ""); len(apps) == 0 {
|
||||||
|
return fmt.Errorf("未查询到此应用!app_id:%d", appID)
|
||||||
|
}
|
||||||
|
for _, v := range printInfos {
|
||||||
|
printers, _ := dao.GetPrinters(db, appID, v.PrintNo)
|
||||||
|
if len(printers) > 0 {
|
||||||
|
errs = append(errs, fmt.Errorf("此打印机已被其他应用绑定!print_no :%s 。", v.PrintNo))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
printer := &model.Printer{
|
||||||
|
CreatedAt: &now,
|
||||||
|
UpdatedAt: &now,
|
||||||
|
DeletedAt: &utils.DefaultTimeValue,
|
||||||
|
AppID: appID,
|
||||||
|
PrintNo: v.PrintNo,
|
||||||
|
Name: v.Name,
|
||||||
|
SIM: v.SIM,
|
||||||
|
Status: model.PrinterStatusNormal,
|
||||||
|
IsOnline: model.PrinterOffline,
|
||||||
|
}
|
||||||
|
if err = dao.Insert(db, printer); err != nil {
|
||||||
|
errs = append(errs, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(errs) > 0 {
|
||||||
|
err = putils.BuildErr(errs)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetPrinters(c *gin.Context, appID int, printNo, name string, status, isOnline, offset, pageSize int) (page *model.PagedInfo, err error) {
|
||||||
|
return dao.GetPrintersPage(globals.GetDB(), appID, printNo, name, status, isOnline, offset, pageSize)
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ package utils
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"git.rosy.net.cn/jx-print/globals"
|
"git.rosy.net.cn/jx-print/globals"
|
||||||
"git.rosy.net.cn/jx-print/model"
|
"git.rosy.net.cn/jx-print/model"
|
||||||
"github.com/dchest/captcha"
|
"github.com/dchest/captcha"
|
||||||
@@ -148,3 +149,11 @@ func TryUnmarshalUseNumber(data []byte, result interface{}) error {
|
|||||||
d.UseNumber()
|
d.UseNumber()
|
||||||
return d.Decode(result)
|
return d.Decode(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BuildErr(errs []error) (err error) {
|
||||||
|
var errStr = strings.Builder{}
|
||||||
|
for _, v := range errs {
|
||||||
|
errStr.WriteString(v.Error())
|
||||||
|
}
|
||||||
|
return fmt.Errorf(errStr.String())
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user