添加修改删除打印机接口
This commit is contained in:
60
business/jxstore/cms/print.go
Normal file
60
business/jxstore/cms/print.go
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
package cms
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"git.rosy.net.cn/jx-callback/business/model"
|
||||||
|
"git.rosy.net.cn/jx-callback/business/model/dao"
|
||||||
|
)
|
||||||
|
|
||||||
|
func AddPrinter(appID int, printNo, printKey, name string) (err error) {
|
||||||
|
var (
|
||||||
|
db = dao.GetDB()
|
||||||
|
)
|
||||||
|
//看有没有重复的
|
||||||
|
if printers, _ := dao.GetPrinters(db, appID, printNo); len(printers) > 0 {
|
||||||
|
return fmt.Errorf("此应用已经绑定了该打印机!print_no : %v", printNo)
|
||||||
|
}
|
||||||
|
printer := &model.Printer{
|
||||||
|
AppID: appID,
|
||||||
|
PrintNo: printNo,
|
||||||
|
PrintKey: printKey,
|
||||||
|
Name: name,
|
||||||
|
Status: model.PrinterStatusNormal,
|
||||||
|
}
|
||||||
|
dao.WrapAddIDCULDEntity(printer, "")
|
||||||
|
if err = dao.CreateEntity(db, printer); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func DelPrinter(appID int, printNo string) (err error) {
|
||||||
|
var (
|
||||||
|
db = dao.GetDB()
|
||||||
|
)
|
||||||
|
//看有没有
|
||||||
|
if printers, _ := dao.GetPrinters(db, appID, printNo); len(printers) == 0 {
|
||||||
|
return fmt.Errorf("该应用下未找到该打印机!print_no : %v", printNo)
|
||||||
|
} else {
|
||||||
|
if _, err = dao.DeleteEntity(db, printers[0]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpdatePrinter(appID int, printNo, name string) (err error) {
|
||||||
|
var (
|
||||||
|
db = dao.GetDB()
|
||||||
|
)
|
||||||
|
//看有没有
|
||||||
|
if printers, _ := dao.GetPrinters(db, appID, printNo); len(printers) == 0 {
|
||||||
|
return fmt.Errorf("该应用下未找到该打印机!print_no : %v", printNo)
|
||||||
|
} else {
|
||||||
|
printers[0].Name = name
|
||||||
|
if _, err = dao.UpdateEntity(db, printers[0], "Name"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -1 +1,27 @@
|
|||||||
package dao
|
package dao
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.rosy.net.cn/baseapi/utils"
|
||||||
|
"git.rosy.net.cn/jx-callback/business/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetPrinters(db *DaoDB, 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)
|
||||||
|
}
|
||||||
|
err = GetRows(db, &printers, sql, sqlParams)
|
||||||
|
return printers, err
|
||||||
|
}
|
||||||
|
|||||||
@@ -11,10 +11,11 @@ const (
|
|||||||
|
|
||||||
ErrCodeJsonSyncErr = "-105"
|
ErrCodeJsonSyncErr = "-105"
|
||||||
|
|
||||||
ErrCodeOpenAPIParamErrMethod = "1001" //参数错误,method
|
ErrCodeOpenAPIParamErrMethod = "-1001" //参数错误,method
|
||||||
ErrCodeOpenAPIParamErrTimeStamp = "1002" //参数错误,timestamp
|
ErrCodeOpenAPIParamErrTimeStamp = "-1002" //参数错误,timestamp
|
||||||
|
ErrCodeOpenAPIParamErrSign = "-1003" //参数错误,sign
|
||||||
|
|
||||||
ErrCodeOpenAPIParamErrNormal = "1000" //api非通用参数错误
|
ErrCodeOpenAPIParamErrNormal = "-1000" //api非通用参数错误
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
|
const (
|
||||||
|
PrinterStatusNormal = 0 //正常
|
||||||
|
)
|
||||||
|
|
||||||
type Printer struct {
|
type Printer struct {
|
||||||
ModelIDCULD
|
ModelIDCULD
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,18 @@
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"crypto/md5"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.rosy.net.cn/baseapi/utils"
|
"git.rosy.net.cn/baseapi/utils"
|
||||||
|
"git.rosy.net.cn/jx-callback/business/jxstore/cms"
|
||||||
"git.rosy.net.cn/jx-callback/business/model"
|
"git.rosy.net.cn/jx-callback/business/model"
|
||||||
"git.rosy.net.cn/jx-callback/business/model/dao"
|
"git.rosy.net.cn/jx-callback/business/model/dao"
|
||||||
"git.rosy.net.cn/jx-callback/globals"
|
"git.rosy.net.cn/jx-callback/globals"
|
||||||
"github.com/astaxie/beego/server/web"
|
"github.com/astaxie/beego/server/web"
|
||||||
"net/url"
|
"net/url"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ApiController struct {
|
type ApiController struct {
|
||||||
@@ -23,9 +26,16 @@ const (
|
|||||||
keySign = "sign"
|
keySign = "sign"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
keyPrintNo = "print_no"
|
||||||
|
keyPrintKey = "print_key"
|
||||||
|
keyName = "name"
|
||||||
|
keyStatus = "status"
|
||||||
|
keyOrderNo = "order_no"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
routerMap map[string]reflect.Value
|
routerMap map[string]reflect.Value
|
||||||
textChan chan string
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Init() {
|
func Init() {
|
||||||
@@ -60,16 +70,19 @@ func (c *ApiController) CallOpenAPI() {
|
|||||||
accessID = utils.GetUUID()
|
accessID = utils.GetUUID()
|
||||||
requireParams []string
|
requireParams []string
|
||||||
dataMap = make(map[string]interface{})
|
dataMap = make(map[string]interface{})
|
||||||
//now = time.Now().Unix()
|
now = time.Now().Unix()
|
||||||
parms, result []reflect.Value
|
parms, result []reflect.Value
|
||||||
urls url.Values
|
urls url.Values
|
||||||
|
app = &model.Apps{}
|
||||||
|
db = dao.GetDB()
|
||||||
)
|
)
|
||||||
var (
|
var (
|
||||||
appID, sign, method string
|
appID int
|
||||||
//timestamp int64
|
sign, method string
|
||||||
|
timestamp int64
|
||||||
)
|
)
|
||||||
//判断必传参数是否传入begin
|
//判断必传参数是否传入begin
|
||||||
if appID = c.GetString(keyAppID); appID == "" {
|
if appID, err = c.GetInt(keyAppID); err != nil {
|
||||||
requireParams = append(requireParams, keyAppID)
|
requireParams = append(requireParams, keyAppID)
|
||||||
}
|
}
|
||||||
if method = c.GetString(keyMethod); method == "" {
|
if method = c.GetString(keyMethod); method == "" {
|
||||||
@@ -78,9 +91,9 @@ func (c *ApiController) CallOpenAPI() {
|
|||||||
if sign = c.GetString(keySign); sign == "" {
|
if sign = c.GetString(keySign); sign == "" {
|
||||||
requireParams = append(requireParams, keySign)
|
requireParams = append(requireParams, keySign)
|
||||||
}
|
}
|
||||||
//if timestamp, err = c.GetInt64("timestamp"); err != nil {
|
if timestamp, err = c.GetInt64(keyTimestamp); err != nil {
|
||||||
// requireParams = append(requireParams, "timestamp")
|
requireParams = append(requireParams, keyTimestamp)
|
||||||
//}
|
}
|
||||||
if len(requireParams) > 0 {
|
if len(requireParams) > 0 {
|
||||||
err = buildParamRequiredErr(requireParams)
|
err = buildParamRequiredErr(requireParams)
|
||||||
}
|
}
|
||||||
@@ -89,26 +102,43 @@ func (c *ApiController) CallOpenAPI() {
|
|||||||
goto end
|
goto end
|
||||||
}
|
}
|
||||||
//判断timestamp传入是否正确begin
|
//判断timestamp传入是否正确begin
|
||||||
//if len(utils.Int64ToStr(timestamp)) != len(utils.Int64ToStr(now)) {
|
if len(utils.Int64ToStr(timestamp)) != len(utils.Int64ToStr(now)) {
|
||||||
// err = buildTimestampParamErr()
|
err = buildTimestampParamErr()
|
||||||
// errCode = model.ErrCodeOpenAPIParamErrTimeStamp
|
errCode = model.ErrCodeOpenAPIParamErrTimeStamp
|
||||||
//}
|
}
|
||||||
//if now-timestamp < 5 {
|
if now-timestamp < 5 {
|
||||||
// err = buildTimestampParamErr()
|
err = buildTimestampParamErr()
|
||||||
// errCode = model.ErrCodeOpenAPIParamErrTimeStamp
|
errCode = model.ErrCodeOpenAPIParamErrTimeStamp
|
||||||
//}
|
}
|
||||||
//判断timestamp传入是否正确end
|
//判断timestamp传入是否正确end
|
||||||
if err != nil {
|
if err != nil {
|
||||||
goto end
|
goto end
|
||||||
}
|
}
|
||||||
//判断app_id和sign匹配begin
|
//判断app_id和sign匹配begin ,sign = app_id + app_key + timestamp的MD5加密
|
||||||
|
app.ID = appID
|
||||||
|
if err = dao.GetEntity(db, app); err != nil {
|
||||||
|
goto end
|
||||||
|
} else {
|
||||||
|
if app.Status != model.UserStatusNormal {
|
||||||
|
errCode = model.ErrCodeGeneralFailed
|
||||||
|
err = fmt.Errorf("很抱歉您的应用已失效!")
|
||||||
|
goto end
|
||||||
|
}
|
||||||
|
appKey := app.AppKey
|
||||||
|
signOrigin := fmt.Sprintf("%x", md5.Sum([]byte(utils.Int2Str(appID)+appKey+utils.Int64ToStr(timestamp))))
|
||||||
|
if strings.Compare(sign, signOrigin) != 0 {
|
||||||
|
errCode = model.ErrCodeOpenAPIParamErrSign
|
||||||
|
err = buildMethodParamSign()
|
||||||
|
goto end
|
||||||
|
}
|
||||||
|
}
|
||||||
//判断app_id和sign匹配end
|
//判断app_id和sign匹配end
|
||||||
//call
|
//call
|
||||||
urls, _ = c.Input()
|
urls, _ = c.Input()
|
||||||
dataMap = utils.URLValues2Map(urls)
|
dataMap = utils.URLValues2Map(urls)
|
||||||
delete(dataMap, keyMethod)
|
delete(dataMap, keyMethod)
|
||||||
delete(dataMap, keyTimestamp)
|
delete(dataMap, keyTimestamp)
|
||||||
|
delete(dataMap, keySign)
|
||||||
parms = []reflect.Value{reflect.ValueOf(dataMap)}
|
parms = []reflect.Value{reflect.ValueOf(dataMap)}
|
||||||
if routerMap[method] == reflect.ValueOf(nil) {
|
if routerMap[method] == reflect.ValueOf(nil) {
|
||||||
errCode = model.ErrCodeOpenAPIParamErrMethod
|
errCode = model.ErrCodeOpenAPIParamErrMethod
|
||||||
@@ -143,12 +173,75 @@ success:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ApiController) AddPrint(dataMap map[string]interface{}) (data, errCode string, err error) {
|
func (c *ApiController) AddPrinter(dataMap map[string]interface{}) (data, errCode string, err error) {
|
||||||
if dataMap[keySign].(string) != "222" {
|
var (
|
||||||
return "", errCode, buildParamErr(keySign)
|
printNo, printKey, name string
|
||||||
|
appID int
|
||||||
|
)
|
||||||
|
if _, ok := dataMap[keyPrintNo].(string); !ok {
|
||||||
|
return buildParamErrCodeAndErr(keyPrintNo)
|
||||||
|
} else {
|
||||||
|
if printNo = dataMap[keyPrintNo].(string); printNo == "" {
|
||||||
|
return buildParamErrCodeAndErr(keyPrintNo)
|
||||||
}
|
}
|
||||||
data2, err := json.Marshal(dataMap)
|
}
|
||||||
return string(data2), errCode, err
|
if _, ok := dataMap[keyPrintKey].(string); !ok {
|
||||||
|
return buildParamErrCodeAndErr(keyPrintKey)
|
||||||
|
} else {
|
||||||
|
if printKey = dataMap[keyPrintKey].(string); printKey == "" {
|
||||||
|
return buildParamErrCodeAndErr(keyPrintKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
appID = dataMap[keyAppID].(int)
|
||||||
|
name = dataMap[keyName].(string)
|
||||||
|
if err = cms.AddPrinter(appID, printNo, printKey, name); err != nil {
|
||||||
|
return "", model.ErrCodeGeneralFailed, err
|
||||||
|
}
|
||||||
|
return "", errCode, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ApiController) DelPrinter(dataMap map[string]interface{}) (data, errCode string, err error) {
|
||||||
|
var (
|
||||||
|
printNo string
|
||||||
|
appID int
|
||||||
|
)
|
||||||
|
if _, ok := dataMap[keyPrintNo].(string); !ok {
|
||||||
|
return buildParamErrCodeAndErr(keyPrintNo)
|
||||||
|
} else {
|
||||||
|
if printNo = dataMap[keyPrintNo].(string); printNo == "" {
|
||||||
|
return buildParamErrCodeAndErr(keyPrintNo)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
appID = dataMap[keyAppID].(int)
|
||||||
|
if err = cms.DelPrinter(appID, printNo); err != nil {
|
||||||
|
return "", model.ErrCodeGeneralFailed, err
|
||||||
|
}
|
||||||
|
return "", errCode, err
|
||||||
|
}
|
||||||
|
func (c *ApiController) UpdatePrinter(dataMap map[string]interface{}) (data, errCode string, err error) {
|
||||||
|
var (
|
||||||
|
printNo, name string
|
||||||
|
appID int
|
||||||
|
)
|
||||||
|
if _, ok := dataMap[keyPrintNo].(string); !ok {
|
||||||
|
return buildParamErrCodeAndErr(keyPrintNo)
|
||||||
|
} else {
|
||||||
|
if printNo = dataMap[keyPrintNo].(string); printNo == "" {
|
||||||
|
return buildParamErrCodeAndErr(keyPrintNo)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if _, ok := dataMap[keyName].(string); !ok {
|
||||||
|
return buildParamErrCodeAndErr(keyName)
|
||||||
|
} else {
|
||||||
|
if name = dataMap[keyName].(string); name == "" {
|
||||||
|
return buildParamErrCodeAndErr(keyName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
appID = dataMap[keyAppID].(int)
|
||||||
|
if err = cms.UpdatePrinter(appID, printNo, name); err != nil {
|
||||||
|
return "", model.ErrCodeGeneralFailed, err
|
||||||
|
}
|
||||||
|
return "", errCode, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ApiController) DoPrint(dataMap map[string]interface{}) (data, errCode string, err error) {
|
func (c *ApiController) DoPrint(dataMap map[string]interface{}) (data, errCode string, err error) {
|
||||||
@@ -169,7 +262,7 @@ func (c *ApiController) DoPrint(dataMap map[string]interface{}) (data, errCode s
|
|||||||
}
|
}
|
||||||
dao.WrapAddIDCULDEntity(printMsg, "")
|
dao.WrapAddIDCULDEntity(printMsg, "")
|
||||||
if err = dao.CreateEntity(db, printMsg); err != nil {
|
if err = dao.CreateEntity(db, printMsg); err != nil {
|
||||||
return data, errCode, err
|
return data, model.ErrCodeGeneralFailed, err
|
||||||
}
|
}
|
||||||
return data, errCode, err
|
return data, errCode, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"git.rosy.net.cn/jx-callback/business/model"
|
||||||
|
)
|
||||||
|
|
||||||
type CallResult struct {
|
type CallResult struct {
|
||||||
Code string `json:"code"`
|
Code string `json:"code"`
|
||||||
@@ -21,10 +24,18 @@ func buildParamRequiredErr(str []string) (err error) {
|
|||||||
return fmt.Errorf(msg)
|
return fmt.Errorf(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func buildParamErrCodeAndErr(str string) (data, errCode string, err error) {
|
||||||
|
return data, model.ErrCodeOpenAPIParamErrNormal, fmt.Errorf("参数[%s]错误,请传入正确的值!", str)
|
||||||
|
}
|
||||||
|
|
||||||
func buildParamErr(str string) (err error) {
|
func buildParamErr(str string) (err error) {
|
||||||
return fmt.Errorf("参数[%s]错误,请传入正确的值!", str)
|
return fmt.Errorf("参数[%s]错误,请传入正确的值!", str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func buildMethodParamSign() (err error) {
|
||||||
|
return fmt.Errorf("参数['sign']校验失败,请传入正确的值!")
|
||||||
|
}
|
||||||
|
|
||||||
func buildMethodParamErr() (err error) {
|
func buildMethodParamErr() (err error) {
|
||||||
return fmt.Errorf("参数['method']错误,请传入正确的值!")
|
return fmt.Errorf("参数['method']错误,请传入正确的值!")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user