- 支持易联云开放应用

This commit is contained in:
gazebo
2019-07-11 10:41:45 +08:00
parent e8f93aed2d
commit 630f1b02e6
15 changed files with 249 additions and 61 deletions

View File

@@ -11,7 +11,6 @@ import (
"git.rosy.net.cn/jx-callback/business/auth2"
"git.rosy.net.cn/baseapi/platformapi/dadaapi"
"git.rosy.net.cn/baseapi/platformapi/feieapi"
"git.rosy.net.cn/baseapi/platformapi/jdapi"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/jxutils"
@@ -511,6 +510,9 @@ func UpdateStore(ctx *jxcontext.Context, storeID int, payload map[string]interfa
if valid["originalName"] != nil {
delete(valid, "originalName")
}
if valid["printerBindInfo"] != nil {
delete(valid, "printerBindInfo")
}
syncStatus := model.SyncFlagModifiedMask
if valid["name"] != nil {
valid["name"] = jxutils.FormalizeName(valid["name"].(string))
@@ -691,27 +693,6 @@ func CreateStore(ctx *jxcontext.Context, storeExt *StoreExt, userName string) (i
return 0, err
}
func addNetPrinter(sn, key, storeName string) (err error) {
name := storeName
_, no, err := api.FeieAPI.PrinterAddList([]*feieapi.PrinterInfo{
&feieapi.PrinterInfo{
SN: sn,
Key: key,
Name: name,
},
})
if err == nil {
if no[sn] != "" {
if no[sn] == feieapi.ErrMsgAlredyAdded {
api.FeieAPI.PrinterEdit(sn, name, "")
} else {
err = fmt.Errorf("添加打印机出错:%s", no[sn])
}
}
}
return err
}
func GetStoreVendorMaps(ctx *jxcontext.Context, db *dao.DaoDB, storeID int, vendorID int) (storeMaps []*model.StoreMap, err error) {
cond := map[string]interface{}{
model.FieldStoreID: storeID,

View File

@@ -153,6 +153,7 @@ func doDailyWork() {
cms.EnableHaveRestStores(jxcontext.AdminCtx, true, true)
// cms.CurVendorSync.FullSyncStoresSkus(jxcontext.AdminCtx, dao.GetDB(), []int{model.VendorIDJD}, nil, true, true)
cms.CurVendorSync.SyncStoresSkus(jxcontext.AdminCtx, dao.GetDB(), []int{model.VendorIDJD, model.VendorIDEBAI, model.VendorIDMTWM}, nil, nil, false, true, true)
// netprinter.RebindAllPrinters(jxcontext.AdminCtx, false, true)
}
func ScheduleTimerFunc(handler func(), timeList []string) {

View File

@@ -2,7 +2,11 @@ package netprinter
import (
"fmt"
"time"
"git.rosy.net.cn/jx-callback/business/jxutils/tasksch"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/partner"
"git.rosy.net.cn/jx-callback/business/jxutils"
@@ -91,3 +95,88 @@ func GetHandlerFromStore(store *model.Store) (printerHandler partner.IPrinterHan
}
return printerHandler, nil
}
func BindPrinter(ctx *jxcontext.Context, storeID int, data string) (printResult *partner.PrinterStatus, err error) {
store := &model.Store{}
store.ID = storeID
db := dao.GetDB()
if err = dao.GetEntity(db, store); err == nil {
var mapData map[string]interface{}
if err = utils.UnmarshalUseNumber([]byte(data), &mapData); err == nil {
printerVendorID := model.VendorIDUnknown
if mapData["machineCode"] != nil {
printerVendorID = model.VendorIDYiLianYun
}
if handler := partner.GetPrinterPlatformFromVendorID(printerVendorID); handler != nil {
if store.PrinterVendorID > 0 && store.PrinterVendorID != printerVendorID && store.PrinterBindInfo == "" {
err = fmt.Errorf("门店:%d已经绑定了%s打印机如果需要重新绑定请联系运营先解绑", storeID, model.VendorChineseNames[store.PrinterVendorID])
} else {
bindResult, err2 := handler.BindPrinter(ctx, mapData)
if err = err2; err == nil {
store.PrinterVendorID = printerVendorID
store.PrinterSN = bindResult.PrinterSN
store.PrinterKey = bindResult.PrinterKey
store.PrinterBindInfo = string(utils.MustMarshal(bindResult))
if _, err = dao.UpdateEntity(db, store); err == nil {
printResult, err = handler.GetPrinterStatus(ctx, bindResult.PrinterSN, bindResult.PrinterKey)
}
}
}
} else {
err = fmt.Errorf("打印机类型:%d当前不支持", printerVendorID)
}
}
}
return printResult, err
}
func RebindAllPrinters(ctx *jxcontext.Context, isForce, isAsync bool) (hint string, err error) {
storeList, err := dao.GetRebindPrinterStoreList(dao.GetDB())
if err == nil {
var needRebindList []*model.Store
bindResultMap := make(map[int]*partner.BindPrinterResult)
now := time.Now()
for _, v := range storeList {
var bindResult partner.BindPrinterResult
if err = utils.UnmarshalUseNumber([]byte(v.PrinterBindInfo), &bindResult); err == nil {
if isForce || now.Sub(utils.Timestamp2Time(bindResult.ExpiresIn)) > -24*time.Hour {
needRebindList = append(needRebindList, v)
bindResultMap[v.ID] = &bindResult
}
}
}
if len(needRebindList) > 0 {
db := dao.GetDB()
task := tasksch.NewSeqTask("RebindAllPrinters", ctx,
func(task *tasksch.SeqTask, step int, params ...interface{}) (result interface{}, err error) {
store := needRebindList[step]
if handler := partner.GetPrinterPlatformFromVendorID(store.PrinterVendorID); handler != nil {
bindResult, err2 := handler.RebindPrinter(ctx, bindResultMap[store.ID])
if err = err2; err == nil {
store.PrinterSN = bindResult.PrinterSN
store.PrinterKey = bindResult.PrinterKey
store.PrinterBindInfo = string(utils.MustMarshal(bindResult))
if _, err = dao.UpdateEntity(db, store); err == nil {
result = []int{1}
}
}
} else {
globals.SugarLogger.Warnf("RebindAllPrinters strange PrinterVendorID:%d", store.PrinterVendorID)
}
return result, err
}, len(needRebindList))
tasksch.ManageTask(task).Run()
if !isAsync {
hint = task.GetID()
} else {
result, err2 := task.GetResult(0)
if err = err2; err == nil {
hint = utils.Int2Str(len(result))
}
}
}
}
return hint, err
}

View File

@@ -293,3 +293,12 @@ func DeleteVendorStoreSnapshot(db *DaoDB, minSnapshotAt time.Time) (err error) {
`, minSnapshotAt)
return err
}
func GetRebindPrinterStoreList(db *DaoDB) (storeList []*model.Store, err error) {
err = GetRows(db, &storeList, `
SELECT *
FROM store t1
WHERE t1.deleted_at = ? AND printer_vendor_id >= ? AND printer_bind_info <> ''
`, utils.DefaultTimeValue, model.VendorIDPrinterBegin)
return storeList, err
}

View File

@@ -149,7 +149,8 @@ type Store struct {
PrinterDisabled int8 `orm:"default(0)" json:"printerDisabled"` // 是否禁用网络打印机
PrinterVendorID int `orm:"column(printer_vendor_id);" json:"printerVendorID"`
PrinterSN string `orm:"size(32);column(printer_sn);index" json:"printerSN"`
PrinterKey string `orm:"size(32)" json:"printerKey"`
PrinterKey string `orm:"size(64)" json:"printerKey"`
PrinterBindInfo string `orm:"size(1024)" json:"_"`
IDCardFront string `orm:"size(255);column(id_card_front)" json:"idCardFront"`
IDCardBack string `orm:"size(255);column(id_card_back)" json:"idCardBack"`

View File

@@ -0,0 +1,52 @@
package partner
import (
"fmt"
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
"git.rosy.net.cn/jx-callback/business/model"
)
type BindPrinterResult struct {
PrinterSN string
PrinterKey string
PrinterKey2 string
ExpiresIn int64 `json:"expires_in"`
}
type IPrinterHandler interface {
GetVendorID() int
PrintMsg(ctx *jxcontext.Context, id1, id2, msgTitle, msgContent string) (printerStatus *PrinterStatus, err error)
GetPrinterStatus(ctx *jxcontext.Context, id1, id2 string) (printerStatus *PrinterStatus, err error)
RegisterPrinter(ctx *jxcontext.Context, id1, id2, printerName string) (newID1, newID2 string, err error)
UnregisterPrinter(ctx *jxcontext.Context, id1, id2 string) (err error)
BindPrinter(ctx *jxcontext.Context, mapData map[string]interface{}) (bindResult *BindPrinterResult, err error)
RebindPrinter(ctx *jxcontext.Context, lastBindResult *BindPrinterResult) (bindResult *BindPrinterResult, err error)
PrintOrder(ctx *jxcontext.Context, store *model.Store, order *model.GoodsOrder) (printerStatus *PrinterStatus, err error)
}
var (
PrinterPlatformHandlers map[int]IPrinterHandler
)
func init() {
PrinterPlatformHandlers = make(map[int]IPrinterHandler)
}
func RegisterPrinterPlatform(handler IPrinterHandler) {
vendorID := handler.GetVendorID()
if !(model.IsPrinterVendorExist(vendorID)) {
panic(fmt.Sprintf("printer vendor:%d is illegal", vendorID))
}
if _, ok := PrinterPlatformHandlers[vendorID]; ok {
panic(fmt.Sprintf("printer vendor:%d, already exists", vendorID))
}
PrinterPlatformHandlers[vendorID] = handler
}
func GetPrinterPlatformFromVendorID(vendorID int) IPrinterHandler {
return PrinterPlatformHandlers[vendorID]
}

View File

@@ -123,7 +123,6 @@ var (
PurchasePlatformHandlers map[int]IPurchasePlatformHandler
DeliveryPlatformHandlers map[int]*DeliveryPlatformHandlerInfo
UseableDeliveryVendorIDs []int
PrinterPlatformHandlers map[int]IPrinterHandler
)
type IOrderManager interface {
@@ -279,17 +278,6 @@ type IDeliveryPlatformHandler interface {
GetWaybillFee(order *model.GoodsOrder) (deliveryFeeInfo *WaybillFeeInfo, err error)
}
type IPrinterHandler interface {
GetVendorID() int
PrintMsg(ctx *jxcontext.Context, id1, id2, msgTitle, msgContent string) (printerStatus *PrinterStatus, err error)
GetPrinterStatus(ctx *jxcontext.Context, id1, id2 string) (printerStatus *PrinterStatus, err error)
RegisterPrinter(ctx *jxcontext.Context, id1, id2, printerName string) (newID1, newID2 string, err error)
UnregisterPrinter(ctx *jxcontext.Context, id1, id2 string) (err error)
PrintOrder(ctx *jxcontext.Context, store *model.Store, order *model.GoodsOrder) (printerStatus *PrinterStatus, err error)
}
type DeliveryPlatformHandlerInfo struct {
Handler IDeliveryPlatformHandler
Use4CreateWaybill bool
@@ -305,7 +293,6 @@ func (p *BasePurchasePlatform) GetStatusActionTimeout(order *model.GoodsOrder, s
func init() {
PurchasePlatformHandlers = make(map[int]IPurchasePlatformHandler)
DeliveryPlatformHandlers = make(map[int]*DeliveryPlatformHandlerInfo)
PrinterPlatformHandlers = make(map[int]IPrinterHandler)
}
func InitOrderManager(curOrderManager IOrderManager) {
@@ -342,17 +329,6 @@ func RegisterDeliveryPlatform(handler IDeliveryPlatformHandler, isUse4CreateWayb
UseableDeliveryVendorIDs = append(UseableDeliveryVendorIDs, vendorID)
}
func RegisterPrinterPlatform(handler IPrinterHandler) {
vendorID := handler.GetVendorID()
if !(model.IsPrinterVendorExist(vendorID)) {
panic(fmt.Sprintf("printer vendor:%d is illegal", vendorID))
}
if _, ok := PrinterPlatformHandlers[vendorID]; ok {
panic(fmt.Sprintf("printer vendor:%d, already exists", vendorID))
}
PrinterPlatformHandlers[vendorID] = handler
}
func GetPurchasePlatformFromVendorID(vendorID int) IPurchasePlatformHandler {
return PurchasePlatformHandlers[vendorID]
}
@@ -360,7 +336,3 @@ func GetPurchasePlatformFromVendorID(vendorID int) IPurchasePlatformHandler {
func GetDeliveryPlatformFromVendorID(vendorID int) *DeliveryPlatformHandlerInfo {
return DeliveryPlatformHandlers[vendorID]
}
func GetPrinterPlatformFromVendorID(vendorID int) IPrinterHandler {
return PrinterPlatformHandlers[vendorID]
}

View File

@@ -161,3 +161,11 @@ func (c *PrinterHandler) UnregisterPrinter(ctx *jxcontext.Context, printerSN, no
_, _, err = api.FeieAPI.PrinterDelList([]string{printerSN})
return err
}
func (c *PrinterHandler) BindPrinter(ctx *jxcontext.Context, mapData map[string]interface{}) (bindResult *partner.BindPrinterResult, err error) {
return nil, fmt.Errorf("%s打印机当前不支持扫码绑定", model.VendorChineseNames[model.VendorIDFeiE])
}
func (c *PrinterHandler) RebindPrinter(ctx *jxcontext.Context, lastBindResult *partner.BindPrinterResult) (bindResult *partner.BindPrinterResult, err error) {
return nil, fmt.Errorf("%s打印机当前不支持扫码绑定", model.VendorChineseNames[model.VendorIDFeiE])
}

View File

@@ -259,3 +259,11 @@ func escapeString4Printer(params []interface{}) []interface{} {
}
return params
}
func (c *PrinterHandler) BindPrinter(ctx *jxcontext.Context, mapData map[string]interface{}) (bindResult *partner.BindPrinterResult, err error) {
return nil, fmt.Errorf("%s打印机当前不支持扫码绑定", model.VendorChineseNames[model.VendorIDXiaoWM])
}
func (c *PrinterHandler) RebindPrinter(ctx *jxcontext.Context, lastBindResult *partner.BindPrinterResult) (bindResult *partner.BindPrinterResult, err error) {
return nil, fmt.Errorf("%s打印机当前不支持扫码绑定", model.VendorChineseNames[model.VendorIDXiaoWM])
}

View File

@@ -99,11 +99,11 @@ func (c *PrinterHandler) GetVendorID() int {
return model.VendorIDYiLianYun
}
func (c *PrinterHandler) PrintMsg(ctx *jxcontext.Context, machineCode, notUsed, msgTitle, msgContent string) (printerStatus *partner.PrinterStatus, err error) {
func (c *PrinterHandler) PrintMsg(ctx *jxcontext.Context, machineCode, possibleToken, msgTitle, msgContent string) (printerStatus *partner.PrinterStatus, err error) {
globals.SugarLogger.Debugf("PrintMsg machineCode:%s", machineCode)
if machineCode != "" {
if err = api.YilianyunAPI.PrintMsg(machineCode, msgTitle, msgContent); err == nil {
printerStatus, err = c.GetPrinterStatus(ctx, machineCode, "")
if err = api.YilianyunAPI.PrintMsgWithToken(machineCode, msgTitle, msgContent, possibleToken); err == nil {
printerStatus, err = c.GetPrinterStatus(ctx, machineCode, possibleToken)
}
} else {
printerStatus = &partner.PrinterStatus{
@@ -113,8 +113,8 @@ func (c *PrinterHandler) PrintMsg(ctx *jxcontext.Context, machineCode, notUsed,
return printerStatus, err
}
func (c *PrinterHandler) GetPrinterStatus(ctx *jxcontext.Context, machineCode, notUsed string) (printerStatus *partner.PrinterStatus, err error) {
status, err := api.YilianyunAPI.GetPrintStatus(machineCode)
func (c *PrinterHandler) GetPrinterStatus(ctx *jxcontext.Context, machineCode, possibleToken string) (printerStatus *partner.PrinterStatus, err error) {
status, err := api.YilianyunAPI.GetPrintStatusWithToken(machineCode, possibleToken)
if err == nil {
printerStatus = &partner.PrinterStatus{
PrintResult: partner.PrintResultSuccess,
@@ -133,7 +133,7 @@ func (c *PrinterHandler) GetPrinterStatus(ctx *jxcontext.Context, machineCode, n
func (c *PrinterHandler) PrintOrder(ctx *jxcontext.Context, store *model.Store, order *model.GoodsOrder) (printerStatus *partner.PrinterStatus, err error) {
globals.SugarLogger.Debugf("yilianyun PrintOrderByOrder orderID:%s", order.VendorOrderID)
content := c.getOrderContent(order, store.Tel1)
return c.PrintMsg(ctx, store.PrinterSN, "", order.VendorOrderID, content)
return c.PrintMsg(ctx, store.PrinterSN, store.PrinterKey, order.VendorOrderID, content)
}
func (c *PrinterHandler) RegisterPrinter(ctx *jxcontext.Context, machineCode, secret, printerName string) (notUsed1, notUsed2 string, err error) {
@@ -145,3 +145,33 @@ func (c *PrinterHandler) UnregisterPrinter(ctx *jxcontext.Context, machineCode,
err = api.YilianyunAPI.DeletePrinter(machineCode)
return err
}
func (c *PrinterHandler) BindPrinter(ctx *jxcontext.Context, mapData map[string]interface{}) (bindResult *partner.BindPrinterResult, err error) {
machineCode := utils.Interface2String(mapData["machineCode"])
qrKey := utils.Interface2String(mapData["qrKey"])
if machineCode == "" || qrKey == "" {
return nil, fmt.Errorf("易联云扫描数据格式不正确")
}
tokenInfo, err := api.YilianyunAPI2.GetPrinterToken(machineCode, qrKey)
if err != nil {
return nil, err
}
return yilianyunToken2BindResult(tokenInfo), nil
}
func (c *PrinterHandler) RebindPrinter(ctx *jxcontext.Context, lastBindResult *partner.BindPrinterResult) (bindResult *partner.BindPrinterResult, err error) {
tokenInfo, err := api.YilianyunAPI2.RefreshToken(lastBindResult.PrinterKey2)
if err == nil {
bindResult = yilianyunToken2BindResult(tokenInfo)
}
return bindResult, err
}
func yilianyunToken2BindResult(tokenInfo *yilianyunapi.TokenInfo) (bindResult *partner.BindPrinterResult) {
return &partner.BindPrinterResult{
PrinterSN: tokenInfo.MachineCode,
PrinterKey: tokenInfo.AccessToken,
PrinterKey2: tokenInfo.RefreshToken,
ExpiresIn: tokenInfo.ExpiresIn,
}
}

View File

@@ -156,3 +156,11 @@ func (c *PrinterHandler) translateStatus(status int) (printerStatus *partner.Pri
}
return printerStatus
}
func (c *PrinterHandler) BindPrinter(ctx *jxcontext.Context, mapData map[string]interface{}) (bindResult *partner.BindPrinterResult, err error) {
return nil, fmt.Errorf("%s打印机当前不支持扫码绑定", model.VendorChineseNames[model.VendorIDZhongWu])
}
func (c *PrinterHandler) RebindPrinter(ctx *jxcontext.Context, lastBindResult *partner.BindPrinterResult) (bindResult *partner.BindPrinterResult, err error) {
return nil, fmt.Errorf("%s打印机当前不支持扫码绑定", model.VendorChineseNames[model.VendorIDZhongWu])
}

View File

@@ -78,6 +78,8 @@ xiaoWMAppKey = "94337"
yilianyunClientID = "1039586024"
yilianyunClientSecret = "4885d07c2997b661102e4b6099c0bf3b"
yilianyunClientID2 = "1098307169"
yilianyunClientSecret2 = "d5eedb40c99e6691b1ca2ba82a363d6a"
zhongwuAppID = 8000192
zhongwuAppSecret = "29435497822f52f3cf659c65da548a79"

View File

@@ -4,6 +4,7 @@ import (
"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/jxutils/netprinter"
"git.rosy.net.cn/jx-callback/business/model"
"github.com/astaxie/beego"
)
@@ -392,3 +393,18 @@ func (c *StoreController) GetCorporationInfo() {
return retVal, "", err
})
}
// @Title 门店绑定打印
// @Description 门店绑定打印
// @Param token header string true "认证token"
// @Param storeID formData int true "京西门店ID"
// @Param data formData string true "原始数据"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /BindPrinter [post]
func (c *StoreController) BindPrinter() {
c.callBindPrinter(func(params *tStoreBindPrinterParams) (retVal interface{}, errCode string, err error) {
retVal, err = netprinter.BindPrinter(params.Ctx, params.StoreID, params.Data)
return retVal, "", err
})
}

View File

@@ -47,6 +47,7 @@ var (
FeieAPI *feieapi.API
XiaoWMAPI *xiaowmapi.API
YilianyunAPI *yilianyunapi.API
YilianyunAPI2 *yilianyunapi.API
ZhongwuAPI *zhongwuapi.API
Cacher cache.ICacher
@@ -131,5 +132,6 @@ func Init() {
FeieAPI = feieapi.New(beego.AppConfig.DefaultString("feieUser", ""), beego.AppConfig.DefaultString("feieKey", ""))
XiaoWMAPI = xiaowmapi.New(beego.AppConfig.DefaultInt("xiaoWMAppID", 0), beego.AppConfig.DefaultString("xiaoWMAppKey", ""))
YilianyunAPI = yilianyunapi.New(beego.AppConfig.DefaultString("yilianyunClientID", ""), beego.AppConfig.DefaultString("yilianyunClientSecret", ""))
YilianyunAPI2 = yilianyunapi.New(beego.AppConfig.DefaultString("yilianyunClientID2", ""), beego.AppConfig.DefaultString("yilianyunClientSecret2", ""))
ZhongwuAPI = zhongwuapi.New(beego.AppConfig.DefaultInt("zhongwuAppID", 0), beego.AppConfig.DefaultString("zhongwuAppSecret", ""))
}

View File

@@ -1150,6 +1150,15 @@ func init() {
Filters: nil,
Params: nil})
beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:StoreController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:StoreController"],
beego.ControllerComments{
Method: "BindPrinter",
Router: `/BindPrinter`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Filters: nil,
Params: nil})
beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:StoreController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:StoreController"],
beego.ControllerComments{
Method: "CreateStore",