436 lines
12 KiB
Go
436 lines
12 KiB
Go
package controllers
|
||
|
||
import (
|
||
"bytes"
|
||
"crypto/md5"
|
||
"encoding/json"
|
||
"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/client/orm"
|
||
"github.com/astaxie/beego/server/web"
|
||
"net/http"
|
||
"net/url"
|
||
"reflect"
|
||
"strconv"
|
||
"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"
|
||
keyContent = "content"
|
||
keyMsgID = "msg_id"
|
||
keySound = "sound"
|
||
keyVolume = "volume"
|
||
keySim = "sim"
|
||
)
|
||
|
||
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
|
||
}
|
||
//判断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 {
|
||
if err == orm.ErrNoRows {
|
||
err = fmt.Errorf("未查询到该应用!app_id: %v", appID)
|
||
}
|
||
goto end
|
||
} else {
|
||
if app.Status != model.UserStatusNormal || app.DeletedAt != utils.DefaultTimeValue {
|
||
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()
|
||
goto end
|
||
}
|
||
globals.SugarLogger.Debugf("Begin API CallOpenAPI Method: %s, accessUUID:%s, sign:%s, ip:%s\n", method, accessID, sign, getRealRemoteIP(c.Ctx.Request))
|
||
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()
|
||
goto end
|
||
}
|
||
success:
|
||
{
|
||
c.Ctx.ResponseWriter.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||
callResult.Code = model.ErrCodeSuccess
|
||
if len(result) > 0 {
|
||
callResult.Data = result[0].Interface()
|
||
}
|
||
c.Data["json"] = callResult
|
||
c.ServeJSON()
|
||
}
|
||
end:
|
||
{
|
||
callResult.Code = errCode
|
||
if err != nil {
|
||
callResult.Desc = err.Error()
|
||
}
|
||
c.Data["json"] = callResult
|
||
c.ServeJSON()
|
||
}
|
||
}
|
||
|
||
//添加打印机,一次最多50条
|
||
func (c *ApiController) AddPrinter(dataMap map[string]interface{}) (data, errCode string, err error) {
|
||
var (
|
||
printers []*model.Printer
|
||
printersStr string
|
||
appID int
|
||
)
|
||
globals.SugarLogger.Debugf("Begin API AddPrinter data: [%v]", utils.Format4Output(dataMap, false))
|
||
if _, ok := dataMap["prints"].(string); !ok {
|
||
return buildParamErrCodeAndErr("prints_str_nil")
|
||
}
|
||
|
||
if err = utils.UnmarshalUseNumber([]byte(printersStr), &printers); err != nil {
|
||
return buildParamErrCodeAndErr("prints_bind_fail")
|
||
}
|
||
|
||
for _, v := range printers {
|
||
if v.PrintNo == "" {
|
||
return buildParamErrCodeAndErr("prints_no_nil")
|
||
}
|
||
}
|
||
|
||
appID = utils.Str2Int(dataMap[keyAppID].(string))
|
||
if err = cms.AddPrinter(appID, printers); err != nil {
|
||
return "", model.ErrCodeGeneralFailed, err
|
||
}
|
||
return "", errCode, err
|
||
}
|
||
|
||
//删除打印机绑定
|
||
func (c *ApiController) DelPrinter(dataMap map[string]interface{}) (data, errCode string, err error) {
|
||
var (
|
||
printNos []string
|
||
printNosStr string
|
||
appID int
|
||
)
|
||
globals.SugarLogger.Debugf("Begin API DelPrinter data: [%v]", utils.Format4Output(dataMap, false))
|
||
if _, ok := dataMap["print_nos"].(string); !ok {
|
||
return buildParamErrCodeAndErr("print_nos")
|
||
} else {
|
||
if printNosStr = dataMap["print_nos"].(string); printNosStr == "" {
|
||
return buildParamErrCodeAndErr("print_nos")
|
||
}
|
||
}
|
||
if err = utils.UnmarshalUseNumber([]byte(printNosStr), &printNos); err != nil {
|
||
return buildParamErrCodeAndErr("print_nos")
|
||
}
|
||
for _, v := range printNos {
|
||
if v == "" {
|
||
return buildParamErrCodeAndErr("print_nos")
|
||
}
|
||
}
|
||
appID = utils.Str2Int(dataMap[keyAppID].(string))
|
||
if err = cms.DelPrinter(appID, printNos); err != nil {
|
||
return "", model.ErrCodeGeneralFailed, err
|
||
}
|
||
return "", errCode, err
|
||
}
|
||
|
||
//修改打印机信息
|
||
func (c *ApiController) UpdatePrinter(dataMap map[string]interface{}) (data, errCode string, err error) {
|
||
var (
|
||
printNo string
|
||
name, sim, sound *string
|
||
volume *int
|
||
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 nameStr, ok := dataMap[keyName].(string); !ok {
|
||
name = nil
|
||
} else {
|
||
name = &nameStr
|
||
}
|
||
if simStr, ok := dataMap[keySim].(string); !ok {
|
||
sim = nil
|
||
} else {
|
||
sim = &simStr
|
||
}
|
||
if soundStr, ok := dataMap[keySound].(string); !ok {
|
||
sound = nil
|
||
} else {
|
||
sound = &soundStr
|
||
}
|
||
if volumeStr, ok := dataMap[keyVolume].(string); !ok {
|
||
volume = nil
|
||
} else {
|
||
if volumeInt64, err2 := strconv.ParseInt(volumeStr, 10, 64); err2 == nil {
|
||
volumeInt := int(volumeInt64)
|
||
volume = &volumeInt
|
||
} else {
|
||
return buildParamErrCodeAndErr(keyVolume)
|
||
}
|
||
}
|
||
appID = utils.Str2Int(dataMap[keyAppID].(string))
|
||
if err = cms.UpdatePrinter(appID, printNo, name, sim, sound, volume); err != nil {
|
||
globals.SugarLogger.Debugf("End API UpdatePrinter err: %v", err)
|
||
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 = utils.Str2Int(dataMap[keyAppID].(string))
|
||
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 (
|
||
printNo, content string
|
||
appID int
|
||
orderNo string
|
||
)
|
||
globals.SugarLogger.Debugf("Begin API DoPrint data: [%v]", utils.Format4Output(dataMap, false))
|
||
if _, ok := dataMap[keyPrintNo].(string); !ok {
|
||
return buildParamErrCodeAndErr(keyPrintNo)
|
||
}
|
||
|
||
if _, ok := dataMap[keyContent].(string); !ok {
|
||
return buildParamErrCodeAndErr(keyContent)
|
||
}
|
||
|
||
if _, ok := dataMap[keyOrderNo].(string); !ok {
|
||
return buildParamErrCodeAndErr(keyOrderNo)
|
||
}
|
||
|
||
appID = utils.Str2Int(dataMap[keyAppID].(string))
|
||
msgID := time.Now().Format("20060102150405") + "_" + jxutils.RandStringBytes(8)
|
||
|
||
// 打印文件转结构体
|
||
contentMap := make(map[string]string)
|
||
if err := json.Unmarshal([]byte(content), contentMap); err != nil {
|
||
return "", model.ErrCodeGeneralFailed, err
|
||
}
|
||
// 查询打印机设置
|
||
printSetting, err := model.GetPrintSetting(printNo)
|
||
if err != nil {
|
||
return "", model.ErrCodeGeneralFailed, err
|
||
}
|
||
printObj, err := dao.QueryUserPrinter("", printNo)
|
||
if err != nil {
|
||
return "", model.ErrCodeGeneralFailed, err
|
||
}
|
||
|
||
// 查询用户模板
|
||
model.MakePrintMsgOnTemp(contentMap, printObj.UserId, printSetting)
|
||
|
||
if err = cms.DoPrint(appID, msgID, printNo, content, orderNo); err != nil {
|
||
return "", model.ErrCodeGeneralFailed, err
|
||
}
|
||
return msgID, errCode, err
|
||
}
|
||
|
||
//获取某打印消息
|
||
func (c *ApiController) GetPrintMsg(dataMap map[string]interface{}) (data map[string]interface{}, errCode string, err error) {
|
||
var (
|
||
msgID string
|
||
appID int
|
||
)
|
||
globals.SugarLogger.Debugf("Begin API GetPrintMsg data: [%v]", utils.Format4Output(dataMap, false))
|
||
if _, ok := dataMap[keyMsgID].(string); !ok {
|
||
return data, model.ErrCodeOpenAPIParamErrNormal, fmt.Errorf("参数[%s]错误,请传入正确的值!", keyMsgID)
|
||
} else {
|
||
if msgID = dataMap[keyMsgID].(string); msgID == "" {
|
||
return data, model.ErrCodeOpenAPIParamErrNormal, fmt.Errorf("参数[%s]错误,请传入正确的值!", keyMsgID)
|
||
}
|
||
}
|
||
appID = utils.Str2Int(dataMap[keyAppID].(string))
|
||
if printMsg, err := cms.GetPrintMsg(appID, msgID); err != nil {
|
||
return data, model.ErrCodeGeneralFailed, err
|
||
} else if printMsg == nil {
|
||
return data, model.ErrCodeGeneralFailed, fmt.Errorf("未查询到该消息! msg_id: %v", msgID)
|
||
} else {
|
||
bf := bytes.NewBuffer([]byte{})
|
||
jsonEncoder := json.NewEncoder(bf)
|
||
jsonEncoder.SetEscapeHTML(false)
|
||
jsonEncoder.Encode(printMsg)
|
||
json.Unmarshal([]byte(bf.String()), &data)
|
||
return data, model.ErrCodeGeneralFailed, err
|
||
}
|
||
return data, errCode, err
|
||
}
|
||
|
||
//查询打印机状态
|
||
func (c *ApiController) GetPrinterStatus(dataMap map[string]interface{}) (data, errCode string, err error) {
|
||
var (
|
||
printNo string
|
||
//appID int
|
||
)
|
||
globals.SugarLogger.Debugf("Begin API GetPrinterStatus 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 = utils.Str2Int(dataMap[keyAppID].(string))
|
||
//if status, err := cms.GetPrinterStatus(appID, printNo); err != nil {
|
||
// return "", model.ErrCodeGeneralFailed, err
|
||
//} else {
|
||
// return utils.Int2Str(status), errCode, err
|
||
//}
|
||
return data, errCode, err
|
||
}
|
||
|
||
func getRealRemoteIP(r *http.Request) (ip string) {
|
||
if r != nil {
|
||
ip = r.Header.Get("X-Forwarded-For")
|
||
if ip == "" {
|
||
ip = r.Header.Get("X-real-ip")
|
||
}
|
||
if ip == "" {
|
||
ip = strings.Split(r.RemoteAddr, ":")[0]
|
||
} else {
|
||
ip = strings.Split(ip, ",")[0]
|
||
}
|
||
}
|
||
return ip
|
||
}
|