300 lines
8.4 KiB
Go
300 lines
8.4 KiB
Go
package controllers
|
||
|
||
import (
|
||
"crypto/md5"
|
||
"fmt"
|
||
"git.rosy.net.cn/baseapi/utils"
|
||
"git.rosy.net.cn/jx-callback/business/jxstore/cms"
|
||
"git.rosy.net.cn/jx-callback/business/jxutils"
|
||
"git.rosy.net.cn/jx-callback/business/model"
|
||
"git.rosy.net.cn/jx-callback/business/model/dao"
|
||
"git.rosy.net.cn/jx-callback/globals"
|
||
"github.com/astaxie/beego/server/web"
|
||
"net/url"
|
||
"reflect"
|
||
"strings"
|
||
"time"
|
||
)
|
||
|
||
type ApiController struct {
|
||
web.Controller
|
||
}
|
||
|
||
const (
|
||
keyAppID = "app_id"
|
||
keyTimestamp = "timestamp"
|
||
keyMethod = "method"
|
||
keySign = "sign"
|
||
)
|
||
|
||
const (
|
||
keyPrintNo = "print_no"
|
||
keyPrintKey = "print_key"
|
||
keyName = "name"
|
||
keyStatus = "status"
|
||
keyOrderNo = "order_no"
|
||
)
|
||
|
||
var (
|
||
routerMap map[string]reflect.Value
|
||
)
|
||
|
||
func Init() {
|
||
routerMap = make(map[string]reflect.Value)
|
||
var ruTest ApiController
|
||
vf := reflect.ValueOf(&ruTest)
|
||
vft := vf.Type()
|
||
//读取方法数量
|
||
mNum := vf.NumMethod()
|
||
fmt.Println("mNum", mNum)
|
||
//遍历路由器的方法,并将其存入控制器映射变量中
|
||
for i := 0; i < mNum; i++ {
|
||
mName := vft.Method(i).Name
|
||
routerMap[mName] = vf.Method(i)
|
||
}
|
||
}
|
||
|
||
// @Title api调用入口
|
||
// @Description api调用入口
|
||
// @Param app_id formData string false "应用ID"
|
||
// @Param timestamp formData int64 false "unix时间戳"
|
||
// @Param sign formData string false "签名"
|
||
// @Param method formData string false "接口名"
|
||
// @Success 200 {object} controllers.CallResult
|
||
// @Failure 200 {object} controllers.CallResult
|
||
// @router /CallOpenAPI [post]
|
||
func (c *ApiController) CallOpenAPI() {
|
||
var (
|
||
err error
|
||
errCode = model.ErrCodeGeneralFailed
|
||
callResult = &CallResult{}
|
||
accessID = utils.GetUUID()
|
||
requireParams []string
|
||
dataMap = make(map[string]interface{})
|
||
now = time.Now().Unix()
|
||
parms, result []reflect.Value
|
||
urls url.Values
|
||
app = &model.Apps{}
|
||
db = dao.GetDB()
|
||
)
|
||
var (
|
||
appID int
|
||
sign, method string
|
||
timestamp int64
|
||
)
|
||
//判断必传参数是否传入begin
|
||
if appID, err = c.GetInt(keyAppID); err != nil {
|
||
requireParams = append(requireParams, keyAppID)
|
||
}
|
||
if method = c.GetString(keyMethod); method == "" {
|
||
requireParams = append(requireParams, keyMethod)
|
||
}
|
||
if sign = c.GetString(keySign); sign == "" {
|
||
requireParams = append(requireParams, keySign)
|
||
}
|
||
if timestamp, err = c.GetInt64(keyTimestamp); err != nil {
|
||
requireParams = append(requireParams, keyTimestamp)
|
||
}
|
||
if len(requireParams) > 0 {
|
||
err = buildParamRequiredErr(requireParams)
|
||
}
|
||
//判断必传参数是否传入end
|
||
if err != nil {
|
||
goto end
|
||
}
|
||
//判断timestamp传入是否正确begin
|
||
if len(utils.Int64ToStr(timestamp)) != len(utils.Int64ToStr(now)) {
|
||
err = buildTimestampParamErr()
|
||
errCode = model.ErrCodeOpenAPIParamErrTimeStamp
|
||
}
|
||
if now-timestamp < 5 {
|
||
err = buildTimestampParamErr()
|
||
errCode = model.ErrCodeOpenAPIParamErrTimeStamp
|
||
}
|
||
//判断timestamp传入是否正确end
|
||
if err != nil {
|
||
goto end
|
||
}
|
||
//判断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
|
||
//call
|
||
urls, _ = c.Input()
|
||
dataMap = utils.URLValues2Map(urls)
|
||
delete(dataMap, keyMethod)
|
||
delete(dataMap, keyTimestamp)
|
||
delete(dataMap, keySign)
|
||
parms = []reflect.Value{reflect.ValueOf(dataMap)}
|
||
if routerMap[method] == reflect.ValueOf(nil) {
|
||
errCode = model.ErrCodeOpenAPIParamErrMethod
|
||
err = buildMethodParamErr()
|
||
}
|
||
globals.SugarLogger.Debugf("Begin API CallOpenAPI Method: %s, accessUUID:%s, sign:%s\n", method, accessID, sign)
|
||
if err == nil {
|
||
result = routerMap[method].Call(parms)
|
||
if result[2].Interface() != nil {
|
||
err = result[2].Interface().(error)
|
||
errCode = result[1].String()
|
||
goto end
|
||
}
|
||
goto success
|
||
} else {
|
||
callResult.Code = errCode
|
||
callResult.Desc = err.Error()
|
||
}
|
||
end:
|
||
{
|
||
callResult.Code = errCode
|
||
callResult.Desc = err.Error()
|
||
c.Data["json"] = callResult
|
||
c.ServeJSON()
|
||
}
|
||
success:
|
||
{
|
||
callResult.Code = model.ErrCodeSuccess
|
||
callResult.Data = result[0].String()
|
||
c.Data["json"] = callResult
|
||
c.ServeJSON()
|
||
}
|
||
}
|
||
|
||
//添加打印机,暂不支持批量
|
||
func (c *ApiController) AddPrinter(dataMap map[string]interface{}) (data, errCode string, err error) {
|
||
var (
|
||
printNo, printKey, name string
|
||
appID int
|
||
)
|
||
globals.SugarLogger.Debugf("Begin API AddPrinter data: [%v]", utils.Format4Output(dataMap, false))
|
||
if _, ok := dataMap[keyPrintNo].(string); !ok {
|
||
return buildParamErrCodeAndErr(keyPrintNo)
|
||
} else {
|
||
if printNo = dataMap[keyPrintNo].(string); printNo == "" {
|
||
return buildParamErrCodeAndErr(keyPrintNo)
|
||
}
|
||
}
|
||
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
|
||
)
|
||
globals.SugarLogger.Debugf("Begin API DelPrinter data: [%v]", utils.Format4Output(dataMap, false))
|
||
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
|
||
)
|
||
globals.SugarLogger.Debugf("Begin API UpdatePrinter data: [%v]", utils.Format4Output(dataMap, false))
|
||
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) DelPrinterSeq(dataMap map[string]interface{}) (data, errCode string, err error) {
|
||
var (
|
||
printNo string
|
||
appID int
|
||
)
|
||
globals.SugarLogger.Debugf("Begin API DelPrinterSeq data: [%v]", utils.Format4Output(dataMap, false))
|
||
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.DelPrinterSeq(appID, printNo); err != nil {
|
||
return "", model.ErrCodeGeneralFailed, err
|
||
}
|
||
return "", errCode, err
|
||
}
|
||
|
||
func (c *ApiController) DoPrint(dataMap map[string]interface{}) (data, errCode string, err error) {
|
||
var (
|
||
content = dataMap["data"].(string)
|
||
printNo = dataMap["print_no"].(string)
|
||
orderNo = utils.Interface2Int64WithDefault(dataMap["order_no"], 0)
|
||
db = dao.GetDB()
|
||
)
|
||
globals.SugarLogger.Debugf("Begin API DoPrint data: [%v]", utils.Format4Output(dataMap, false))
|
||
if orderNo == 0 {
|
||
return data, model.ErrCodeOpenAPIParamErrNormal, fmt.Errorf("参数错误:order_no 不能为空")
|
||
}
|
||
msgID := time.Now().Format("20060102150405") + "_" + jxutils.RandStringBytes(8)
|
||
printMsg := &model.PrintMsg{
|
||
PrintNo: printNo,
|
||
Content: content,
|
||
OrderNo: int(orderNo),
|
||
MsgID: msgID,
|
||
}
|
||
dao.WrapAddIDCULDEntity(printMsg, "")
|
||
if err = dao.CreateEntity(db, printMsg); err != nil {
|
||
return data, model.ErrCodeGeneralFailed, err
|
||
}
|
||
return msgID, errCode, err
|
||
}
|