Files
jx-callback/business/partner/printer/yilianyun/yilianyun.go
苏尹岚 2b32adefcb aa
2021-02-22 10:27:51 +08:00

315 lines
10 KiB
Go

package yilianyun
import (
"fmt"
"strings"
"time"
"git.rosy.net.cn/baseapi/platformapi/yilianyunapi"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/jxutils"
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/model/dao"
"git.rosy.net.cn/jx-callback/business/partner"
"git.rosy.net.cn/jx-callback/globals"
"git.rosy.net.cn/jx-callback/globals/api"
)
var (
CurPrinterHandler *PrinterHandler
)
type PrinterHandler struct {
}
func init() {
CurPrinterHandler = new(PrinterHandler)
partner.RegisterPrinterPlatform(CurPrinterHandler)
}
func (c *PrinterHandler) getOrderContent(order *model.GoodsOrder, storeTel string, storeDetail *dao.StoreDetail) (content string) {
expectedDeliveryTime := order.ExpectedDeliveredTime
if utils.IsTimeZero(expectedDeliveryTime) {
expectedDeliveryTime = order.OrderCreatedAt.Add(1 * time.Hour)
}
getCode := ""
if order.VendorID == model.VendorIDEBAI {
getCode = fmt.Sprintf("<FS2>饿百取货码:%s</FS2>\\n\n", jxutils.GetEbaiOrderGetCode(order))
}
orderParams := []interface{}{}
orderFmt := ``
if storeDetail.BrandIsPrint == model.NO {
orderFmt += `
<FS2><center>%s</center></FS2>\n\n
`
if order.VendorOrgCode == "34665" {
orderParams = append(orderParams, globals.StoreNameEbai2)
} else {
orderParams = append(orderParams, storeDetail.BrandName)
}
}
orderFmt += `
<center>手机买菜上京西</center>
<center>极速到家送惊喜</center>\n
--------------------------------
下单时间: %s\n
预计送达: %s\n
订单编号: %s\n
\n
<FS2>%s#%d</FS2>\n\n
<QR>%s</QR>
` + getCode + `\n
客户: %s\n
电话: %s\n
地址: %s\n
\n
客户备注: \n
<FS2>%s</FS2>\n
\n
\n
商品明细: \n
品名 数量 单价 小计\n
--------------------------------\n`
// <FB>实际支付:</FB>%s\n
orderParams = append(orderParams,
utils.Time2Str(order.OrderCreatedAt),
utils.Time2Str(expectedDeliveryTime),
order.VendorOrderID,
jxutils.GetVendorName(order.VendorID),
order.OrderSeq,
order.VendorOrderID,
order.ConsigneeName,
order.ConsigneeMobile,
order.ConsigneeAddress,
order.BuyerComment,
// jxutils.IntPrice2StandardCurrencyString(order.ActualPayPrice),
)
for _, sku := range order.Skus {
orderFmt += `%s\n`
orderFmt += `%8s%10s%10s\n`
orderParams = append(orderParams, sku.SkuName, "x"+utils.Int2Str(sku.Count), jxutils.IntPrice2StandardCurrencyString(sku.SalePrice), jxutils.IntPrice2StandardCurrencyString(sku.SalePrice*int64(sku.Count)))
}
orderFmt += `\n
<FB>共%d种%d件商品</FB>
\n
--------------------------------\n
<center><FH2>商品质量问题请联系:</FH2></center>
<center><FH2>%s:%s</FH2></center>\n
更多信息请关注官方微信: %s\n
--------------------------------\n
--------------------------------\n
`
// <QR>http://weixin.qq.com/r/tkkDGzTERmk5rXB49xyk</QR>
orderParams = append(orderParams, order.SkuCount, order.GoodsCount, order.StoreName, storeTel, globals.StoreName)
return strings.Replace(fmt.Sprintf(strings.Replace(orderFmt, "\n", "", -1), orderParams...), "\\n", "\r\n", -1)
}
func (c *PrinterHandler) getOrderContentBig(order *model.GoodsOrder, storeTel string, storeDetail *dao.StoreDetail) (content string) {
expectedDeliveryTime := order.ExpectedDeliveredTime
if utils.IsTimeZero(expectedDeliveryTime) {
expectedDeliveryTime = order.OrderCreatedAt.Add(1 * time.Hour)
}
getCode := ""
if order.VendorID == model.VendorIDEBAI {
getCode = fmt.Sprintf("<FS2>饿百取货码:%s</FS2>\\n\n", jxutils.GetEbaiOrderGetCode(order))
}
orderParams := []interface{}{}
orderFmt := ``
if storeDetail.BrandIsPrint == model.NO {
orderFmt += `
<FS2><center>%s</center></FS2>\n\n
`
if order.VendorOrgCode == "34665" {
orderParams = append(orderParams, globals.StoreNameEbai2)
} else {
orderParams = append(orderParams, storeDetail.BrandName)
}
}
orderFmt += `
<center>手机买菜上京西</center>
<center>极速到家送惊喜</center>\n
--------------------------------
<FS2>下单时间: %s\n\n</FS2>
<FS2>预计送达: %s\n\n</FS2>
<FS2>订单编号: %s\n</FS2>
\n
<FS2>%s#%d</FS2>\n\n
<QR>%s</QR>
` + getCode + `\n
<FS2>客户: %s\n</FS2>
<FS2>电话: %s\n</FS2>
<FS2>地址: %s\n</FS2>
\n
<FS2>客户备注: \n</FS2>
<FS2>%s</FS2>\n
\n
\n
<FS2>商品明细: \n</FS2>
<FS2>品名数量单价小计\n</FS2>
--------------------------------\n`
// <FS2><FB>实际支付:</FB>%s\n</FS2>
orderParams = append(orderParams,
utils.Time2Str(order.OrderCreatedAt),
utils.Time2Str(expectedDeliveryTime),
order.VendorOrderID,
jxutils.GetVendorName(order.VendorID),
order.OrderSeq,
order.VendorOrderID,
order.ConsigneeName,
order.ConsigneeMobile,
order.ConsigneeAddress,
order.BuyerComment,
// jxutils.IntPrice2StandardCurrencyString(order.ActualPayPrice),
)
for _, sku := range order.Skus {
orderFmt += `<FS2>%s\n</FS2>`
orderFmt += `<FS2>%s %s %s\n\n</FS2>`
orderParams = append(orderParams, sku.SkuName, "x"+utils.Int2Str(sku.Count), jxutils.IntPrice2StandardCurrencyString(sku.SalePrice), jxutils.IntPrice2StandardCurrencyString(sku.SalePrice*int64(sku.Count)))
}
orderFmt += `\n
<FS2><FB>共%d种%d件商品</FB></FS2>
\n
--------------------------------\n
<center><FH2>商品质量问题请联系:</FH2></center>
<center><FH2>%s:%s</FH2></center>\n
<FS2>更多信息请关注官方微信: %s\n</FS2>
--------------------------------\n
--------------------------------\n
`
// <QR>http://weixin.qq.com/r/tkkDGzTERmk5rXB49xyk</QR>
orderParams = append(orderParams, order.SkuCount, order.GoodsCount, order.StoreName, storeTel, globals.StoreName)
return strings.Replace(fmt.Sprintf(strings.Replace(orderFmt, "\n", "", -1), orderParams...), "\\n", "\r\n", -1)
}
func (c *PrinterHandler) GetVendorID() int {
return model.VendorIDYiLianYun
}
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 globals.EnableStoreWrite {
err = getApiByToken(possibleToken).PrintMsgWithToken(machineCode, msgTitle, msgContent, possibleToken)
}
if err == nil {
printerStatus, err = c.GetPrinterStatus(ctx, machineCode, possibleToken)
}
} else {
printerStatus = &partner.PrinterStatus{
PrintResult: partner.PrintResultNoPrinter,
}
}
return printerStatus, err
}
func (c *PrinterHandler) GetPrinterStatus(ctx *jxcontext.Context, machineCode, possibleToken string) (printerStatus *partner.PrinterStatus, err error) {
status, err := getApiByToken(possibleToken).GetPrintStatusWithToken(machineCode, possibleToken)
if err == nil {
printerStatus = &partner.PrinterStatus{
PrintResult: partner.PrintResultSuccess,
}
if status == yilianyunapi.PrintStatusOffline {
printerStatus.PrinterStatus = partner.PrinterStatusOffline
} else if status == yilianyunapi.PrintStatusOnline {
printerStatus.PrinterStatus = partner.PrinterStatusOnlineOK
} else {
printerStatus.PrinterStatus = partner.PrinterStatusOnlineAbnormal
}
}
return printerStatus, err
}
func (c *PrinterHandler) PrintOrder(ctx *jxcontext.Context, store *model.Store, storeDetail *dao.StoreDetail, order *model.GoodsOrder) (printerStatus *partner.PrinterStatus, err error) {
globals.SugarLogger.Debugf("yilianyun PrintOrderByOrder orderID:%s, storeID:%d", order.VendorOrderID, store.ID)
if len(order.Skus) == 0 {
return
}
content := ""
if store.PrinterFontSize == partner.PrinterFontSizeBig {
content = c.getOrderContentBig(order, store.Tel1, storeDetail)
} else {
content = c.getOrderContent(order, store.Tel1, storeDetail)
}
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) {
if globals.EnableStoreWrite {
err = api.YilianyunAPI.AddPrinter(machineCode, secret, printerName)
}
return "", "", err
}
func (c *PrinterHandler) UnregisterPrinter(ctx *jxcontext.Context, machineCode, notUsed string) (err error) {
if globals.EnableStoreWrite {
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) {
var tokenInfo *yilianyunapi.TokenInfo
if globals.EnableStoreWrite {
tokenInfo, err = api.YilianyunAPI2.RefreshToken(lastBindResult.PrinterKey2)
} else {
tokenInfo = &yilianyunapi.TokenInfo{}
}
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,
ExpiresAt: time.Now().Add(time.Duration(tokenInfo.ExpiresIn) * time.Second).Unix(),
}
}
func getApiByToken(possibleToken string) *yilianyunapi.API {
if yilianyunapi.IsStrToken(possibleToken) {
return api.YilianyunAPI2
}
return api.YilianyunAPI
}
func (c *PrinterHandler) EmptyPrintList(ctx *jxcontext.Context, id1, id2 string) (err error) {
if globals.EnableStoreWrite {
err = api.YilianyunAPI.CancelAll(id1, id2)
}
return err
}
func (c *PrinterHandler) PlayText(ctx *jxcontext.Context, id1, id2, orderID, text string) (printerStatus *partner.PrinterStatus, err error) {
if globals.EnableStoreWrite {
err = api.YilianyunAPI.PlayText(id1, orderID, text, id2)
}
return nil, err
}
func (c *PrinterHandler) SetSound(ctx *jxcontext.Context, id1, id2, sound string) (err error) {
if globals.EnableStoreWrite {
err = api.YilianyunAPI.SetSound(id1, sound)
}
return err
}