340 lines
10 KiB
Go
340 lines
10 KiB
Go
package app
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"git.rosy.net.cn/jx-print/controllers"
|
|
"git.rosy.net.cn/jx-print/globals"
|
|
"git.rosy.net.cn/jx-print/model"
|
|
printModel "git.rosy.net.cn/jx-print/model/app_model"
|
|
"git.rosy.net.cn/jx-print/putils"
|
|
print "git.rosy.net.cn/jx-print/services/print_server"
|
|
printServer "git.rosy.net.cn/jx-print/services/print_server/app_server"
|
|
"github.com/gin-gonic/gin"
|
|
"time"
|
|
)
|
|
|
|
type Print struct{}
|
|
|
|
var PrintController = new(Print)
|
|
|
|
// AddPrinters 添加打印机 POST
|
|
// @Title 添加打印机
|
|
// @Description 添加打印机
|
|
// @Param token cookie string true "用户登录token"
|
|
// @Param data body app_model.AddPrintReq true "请求参数"
|
|
// @Success 200 {object} controllers.CallResult
|
|
// @Failure 200 {object} controllers.CallResult
|
|
// @router /addPrinters [post]
|
|
func (p *Print) AddPrinters(c *gin.Context) {
|
|
var (
|
|
err error
|
|
tokenInfo *model.TokenInfo
|
|
param = printModel.AddPrintReq{}
|
|
printInfo []*model.PrintInfo
|
|
setting = printServer.PrintSettingInfoServer
|
|
bill = printServer.PrintBillServer
|
|
txDB = globals.GetTxDb()
|
|
temp = printServer.TempServer{}
|
|
)
|
|
|
|
if err = c.ShouldBind(¶m); err != nil {
|
|
controllers.BuildErrJson(c, err)
|
|
return
|
|
}
|
|
if tokenInfo = controllers.CheckToken(c); tokenInfo == nil {
|
|
return
|
|
}
|
|
if err = putils.UnmarshalUseNumber([]byte(param.Prints), &printInfo); err != nil {
|
|
controllers.BuildErrJson(c, err)
|
|
return
|
|
}
|
|
|
|
controllers.CallFunc(c, func() (retVal interface{}, errCode string, err error) {
|
|
// 查询打印机心跳,服务器接收到打印机心跳请求,才可以继续绑定!防止用户无休止绑定打印机!
|
|
// 只有开机联网成功的打印机才可以绑定用户
|
|
have, err := printServer.PrintActivationServer.QueryPrintHeard(printInfo[0].PrintNo)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
if !have {
|
|
return nil, "", fmt.Errorf("未检测到打印机心跳,请激活打印机,链接无线网络")
|
|
}
|
|
|
|
// 添加打印机
|
|
if err := printServer.AddPrinters(txDB, tokenInfo, param.AppID, printInfo, param.Phone); err != nil {
|
|
txDB.Rollback()
|
|
return nil, "", err
|
|
}
|
|
|
|
// 赋予默认配置
|
|
if err := setting.SystemInitPrintSetting(txDB, printInfo[0].PrintNo); err != nil {
|
|
txDB.Rollback()
|
|
return nil, "", err
|
|
}
|
|
|
|
// 模板,查询模板是否存在,存在修改用户模板所属用户
|
|
if err := temp.QueryPrintTempByPrintNo(tokenInfo.User.UserID, printInfo[0].PrintNo); err != nil {
|
|
txDB.Rollback()
|
|
return nil, "", err
|
|
}
|
|
|
|
// 初始化打印机账户
|
|
if err := bill.InitPPrintBill(txDB, &printModel.PrintBill{
|
|
CreatedAt: time.Now(),
|
|
UpdatedAt: time.Now(),
|
|
PrintNo: printInfo[0].PrintNo,
|
|
PrintBalance: 20000,
|
|
UserId: tokenInfo.User.UserID,
|
|
}); err != nil {
|
|
txDB.Rollback()
|
|
return nil, "", err
|
|
}
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
txDB.Rollback()
|
|
} else {
|
|
txDB.Commit()
|
|
}
|
|
}()
|
|
|
|
// 初始化打印机余额,如果打印机存在,如果打印机存在,不做修改!打印机不存在则初始化打印机余额
|
|
return "", "", nil
|
|
})
|
|
|
|
}
|
|
|
|
// GetPrinters 查询打印机 GET
|
|
// @Title 查询打印机
|
|
// @Description 查询打印机
|
|
// @Param token cookie string true "用户登录token"
|
|
// @Param data body app_model.QueryPrintReq true "请求参数"
|
|
// @Success 200 {object} controllers.CallResult
|
|
// @Failure 200 {object} controllers.CallResult
|
|
// @router /getPrinters [get]
|
|
func (p *Print) GetPrinters(c *gin.Context) {
|
|
var (
|
|
err error
|
|
tokenInfo *model.TokenInfo
|
|
param = printModel.QueryPrintReq{}
|
|
)
|
|
if err = c.ShouldBind(¶m); err != nil {
|
|
controllers.BuildErrJson(c, err)
|
|
return
|
|
}
|
|
if tokenInfo = controllers.CheckToken(c); tokenInfo == nil {
|
|
return
|
|
}
|
|
|
|
controllers.CallFunc(c, func() (retVal interface{}, errCode string, err error) {
|
|
retVal, err = printServer.GetPrinters(param.AppID, param.PrintNo, param.Name, param.Status, param.IsOnline, param.Offset, param.PageSize, tokenInfo.User.UserID)
|
|
return retVal, "", err
|
|
})
|
|
}
|
|
|
|
// GetPrintIsUse 查询打印机是否被使用 GET
|
|
// @Title 查询打印机是否被使用
|
|
// @Description 查询打印机是否被使用
|
|
// @Param token cookie string true "用户登录token"
|
|
// @Param data body app_model.GetPrintIsUse true "请求参数"
|
|
// @Success 200 {object} controllers.CallResult
|
|
// @Failure 200 {object} controllers.CallResult
|
|
// @router /getPrintIsUse [get]
|
|
func (p *Print) GetPrintIsUse(c *gin.Context) {
|
|
var (
|
|
err error
|
|
param = printModel.GetPrintIsUse{}
|
|
)
|
|
if err = c.ShouldBind(¶m); err != nil {
|
|
controllers.BuildErrJson(c, err)
|
|
return
|
|
}
|
|
controllers.CallFunc(c, func() (retVal interface{}, errCode string, err error) {
|
|
have, phone, err := printServer.GetPrintIsUse(param.PrintNo)
|
|
result := make(map[string]interface{}, 0)
|
|
result["isBind"] = have
|
|
result["phone"] = phone
|
|
result["err"] = err
|
|
retVal = result
|
|
return retVal, "", nil
|
|
})
|
|
}
|
|
|
|
// DelPrinters 删除打印机 POST
|
|
// @Title 删除打印机
|
|
// @Description 删除打印机
|
|
// @Param token cookie string true "用户登录token"
|
|
// @Param data body app_model.DeletePrintReq true "请求参数"
|
|
// @Success 200 {object} controllers.CallResult
|
|
// @Failure 200 {object} controllers.CallResult
|
|
// @router /delPrinters [post]
|
|
func (p *Print) DelPrinters(c *gin.Context) {
|
|
var (
|
|
err error
|
|
tokenInfo *model.TokenInfo
|
|
param = printModel.DeletePrintReq{}
|
|
tempServer = printServer.TempServer{}
|
|
settingServer = printServer.PrintSettingServer{}
|
|
)
|
|
if err = c.ShouldBind(¶m); err != nil {
|
|
controllers.BuildErrJson(c, err)
|
|
return
|
|
}
|
|
if tokenInfo = controllers.CheckToken(c); tokenInfo == nil {
|
|
return
|
|
}
|
|
controllers.CallFunc(c, func() (retVal interface{}, errCode string, err error) {
|
|
// 手机号验证
|
|
if err := printServer.DeletePrintCheck(param.PrintNos, param.Phone, param.BizId, param.Code, param.AppID); err != nil {
|
|
return nil, "", err
|
|
}
|
|
|
|
// 删除打印机
|
|
if err := printServer.DelPrinters(param.AppID, tokenInfo, param.PrintNos); err != nil {
|
|
return nil, "", err
|
|
}
|
|
// 删除模板
|
|
err = tempServer.DeleteAllTemp(tokenInfo.User.UserID, param.PrintNos)
|
|
// 删除设置
|
|
err = settingServer.DeletePrintSetting(param.PrintNos)
|
|
// 删除打印消息
|
|
err = print.DeletePrintMsg(param.PrintNos)
|
|
|
|
return retVal, "", err
|
|
})
|
|
}
|
|
|
|
// UpdatePrinter 更新打印机 POST
|
|
// @Title 查询打印机
|
|
// @Description 查询打印机
|
|
// @Param token cookie string true "用户登录token"
|
|
// @Param data body app_model.UpdatePrintReq true "请求参数"
|
|
// @Success 200 {object} controllers.CallResult
|
|
// @Failure 200 {object} controllers.CallResult
|
|
// @router /updatePrinter [post]
|
|
func (p *Print) UpdatePrinter(c *gin.Context) {
|
|
var (
|
|
err error
|
|
tokenInfo *model.TokenInfo
|
|
param = printModel.UpdatePrintReq{}
|
|
)
|
|
if err = c.ShouldBind(¶m); err != nil {
|
|
controllers.BuildErrJson(c, err)
|
|
return
|
|
}
|
|
if tokenInfo = controllers.CheckToken(c); tokenInfo == nil {
|
|
return
|
|
}
|
|
if !controllers.CallFunc(c, func() (retVal interface{}, errCode string, err error) {
|
|
err = printServer.UpdatePrinter(param.AppID, tokenInfo, param.PrintNo, param.Name, param.Sound, param.Volume)
|
|
return retVal, "", err
|
|
}) {
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// TestPrint 测试打印 POST
|
|
// @Title 测试打印
|
|
// @Description 测试打印
|
|
// @Param token cookie string false "用户登录token"
|
|
// @Param data body app_model.TestPrintReq true "请求参数"
|
|
// @Success 200 {object} controllers.CallResult
|
|
// @Failure 200 {object} controllers.CallResult
|
|
// @router /testPrinter [post]
|
|
func (p *Print) TestPrint(c *gin.Context) {
|
|
var (
|
|
err error
|
|
tokenInfo *model.TokenInfo
|
|
param = printModel.TestPrintReq{}
|
|
)
|
|
if err = c.ShouldBind(¶m); err != nil {
|
|
controllers.BuildErrJson(c, err)
|
|
return
|
|
}
|
|
|
|
if param.OrderNo >= 999999999 {
|
|
controllers.BuildErrJson(c, errors.New("订单编号最多65536"))
|
|
return
|
|
}
|
|
|
|
//if tokenInfo = controllers.CheckToken(c); tokenInfo == nil {
|
|
// return
|
|
//}
|
|
controllers.CallFunc(c, func() (retVal interface{}, errCode string, err error) {
|
|
retVal, err = printServer.TestPrint(param.AppID, tokenInfo, param.PrintNo, param.OrderNo, param.Content)
|
|
return retVal, "", err
|
|
})
|
|
}
|
|
|
|
// GetPrintMessages 获取打印机消息 POST
|
|
// @Title 获取打印机消息
|
|
// @Description 获取打印机消息
|
|
// @Param token cookie string true "用户登录token"
|
|
// @Param data body app_model.GetPrintMsg true "请求参数"
|
|
// @Success 200 {object} controllers.CallResult
|
|
// @Failure 200 {object} controllers.CallResult
|
|
// @router /getPrintMessages [get]
|
|
func (p *Print) GetPrintMessages(c *gin.Context) {
|
|
var (
|
|
err error
|
|
tokenInfo *model.TokenInfo
|
|
param = printModel.GetPrintMsg{}
|
|
)
|
|
if err = c.ShouldBind(¶m); err != nil {
|
|
controllers.BuildErrJson(c, err)
|
|
return
|
|
}
|
|
if tokenInfo = controllers.CheckToken(c); tokenInfo == nil {
|
|
return
|
|
}
|
|
|
|
controllers.CallFunc(c, func() (retVal interface{}, errCode string, err error) {
|
|
retVal, err = printServer.GetPrintMessages(param.AppID, tokenInfo, param.PrintNo, param.MsgID, param.BeginAt, param.EndAt, param.Offset, param.PageSize)
|
|
return retVal, "", err
|
|
})
|
|
}
|
|
|
|
// QueryPrintBill 查询打印机账户余额 get
|
|
// @Title 获取打印机账户余额
|
|
// @Description 获取打印机账户余额
|
|
// @Param token cookie string true "用户登录token"
|
|
// @Param data body app_model.QueryPrintBill true "请求参数"
|
|
// @Success 200 {object} controllers.CallResult
|
|
// @Failure 200 {object} controllers.CallResult
|
|
// @router /queryPrintBill [get]
|
|
func (p *Print) QueryPrintBill(c *gin.Context) {
|
|
var (
|
|
err error
|
|
tokenInfo *model.TokenInfo
|
|
param = printModel.QueryPrintBill{}
|
|
server = printServer.PrintBillServer
|
|
)
|
|
if err = c.ShouldBind(¶m); err != nil {
|
|
controllers.BuildErrJson(c, err)
|
|
return
|
|
}
|
|
if tokenInfo = controllers.CheckToken(c); tokenInfo == nil {
|
|
return
|
|
}
|
|
|
|
controllers.CallFunc(c, func() (retVal interface{}, errCode string, err error) {
|
|
retVal, err = server.QueryPrintBill(tokenInfo.User.UserID, param.PrintNo)
|
|
return retVal, "", err
|
|
})
|
|
}
|
|
|
|
// StatPrinterReport 查询打印统计(首页) GET
|
|
func (p *Print) StatPrinterReport(c *gin.Context) {
|
|
var (
|
|
tokenInfo *model.TokenInfo
|
|
)
|
|
if tokenInfo = controllers.CheckToken(c); tokenInfo == nil {
|
|
return
|
|
}
|
|
controllers.CallFunc(c, func() (retVal interface{}, errCode string, err error) {
|
|
retVal, err = printServer.GetPrinterReport(tokenInfo)
|
|
return retVal, "", err
|
|
})
|
|
}
|