Files
jx-callback/business/jxutils/weixinmsg/weixinmsg.go
2018-09-28 10:51:00 +08:00

220 lines
8.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package weixinmsg
import (
"fmt"
"strings"
"time"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/jxutils"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/globals"
"git.rosy.net.cn/jx-callback/globals/api"
"github.com/astaxie/beego/orm"
)
const (
WX_TO_ORDER_PAGE_URL = "http://www.jx.scaqda.com/jx/h5/#/?jxStoreId="
WX_TO_SHOW_COMMENTS_DETAIL_URL = "http://www.jx.scaqda.com/jx/h5/#/assess-list?jxStoreId=" //展示差评详情的页面
//新订单模板消息文字颜色
WX_NEW_ORDER_TEMPLATE_COLOR = "#173177"
WX_HIGHLEVEL_TEMPLATE_COLOR = "#FF0000" //红色
WX_TEMPLATE_VENDERCOLOR_JDDJ = "#47B34F"
WX_TEMPLATE_VENDERCOLOR_MT = "#F4A800"
WX_TEMPLATE_VENDERCOLOR_ELM = "#0191EA" //蓝色
WX_TEMPLATE_VENDERCOLOR_BAD_COMMENTS = "#4F4DA0"
WX_NEWORDER_TEMPLATE_ID = "_DtNGwmOeR6TkkTVUblxLIlkV2MAPOX57TkvfdqG6nY" //微信新订单推送
WX_MTPS_DELIVERY_GRABDONE_TEMPLATE_ID = "h4dkON6AgnHz1XmaksEUB_8Bcir4V8MSexUhC149pPE" //微信美团配送员接单推送
WX_MTPS_DELIVERY_DONE_TEMPLATE_ID = "YXdCrQAHZlcZX1htYUiarrLmtkmKAjp7rynjwObgODo" //微信美团配送员配送完成推送
WX_MTPS_UNABLE_DELIVER_TEMPLATE_ID = "ZFph5Hp7oLlrzVRXbsKIC_StmaBeB9Dlp4tlHeAmUQ8" //微信美团配送配送能力不足推送
WX_MTPS_DELIVERY_EXCEPTION_TEMPLATE_ID = "RkfOFHgR1N75L4-a6Gv0DljpCsVfOHhLm_vyXh8MR-w" //微信美团配送异常推送
WX_BAD_COMMENT_PUSH_TEMPLATE_ID = "zMZH5Ek0k1OHlWnsDb98UaHEOlkJZYok2QOJUfwfJWs" //微信差评消息推送
WX_DADA_DELIVERY_GRABDONE_TEMPLATE_ID = "h4dkON6AgnHz1XmaksEUB_8Bcir4V8MSexUhC149pPE" //微信达达众包配送员接单推送
WX_DADA_DELIVERY_DONE_TEMPLATE_ID = "YXdCrQAHZlcZX1htYUiarrLmtkmKAjp7rynjwObgODo" //微信达达众包配送员配送完成推送
)
var (
venderColors = map[int]string{
model.VendorIDJD: WX_TEMPLATE_VENDERCOLOR_JDDJ,
model.VendorIDMTWM: WX_TEMPLATE_VENDERCOLOR_MT,
model.VendorIDELM: WX_TEMPLATE_VENDERCOLOR_ELM,
model.VendorIDEBAI: WX_TEMPLATE_VENDERCOLOR_ELM,
}
)
func GetWeixinOpenIDsFromStoreID(storeID int) (retVal []string) {
db := orm.NewOrm()
var lists []orm.ParamsList
num, err := db.Raw(`
SELECT openid
FROM weixins t1
JOIN
(SELECT id
FROM weixins
WHERE jxstoreid = ? AND parentid = -1) t2 ON t2.id = t1.parentid
WHERE openid IS NOT NULL
UNION
SELECT openid
FROM weixins
WHERE jxstoreid = ? AND parentid = -1 AND openid IS NOT NULL
`, storeID, storeID).ValuesList(&lists)
if err != nil || num == 0 {
globals.SugarLogger.Infof("GetWeixinOpenIDsFromStoreID can not find openid for store:%d, num:%d, error:%v", storeID, num, err)
return retVal
}
for _, v := range lists {
retVal = append(retVal, v[0].(string))
}
if !globals.ReallyCallPlatformAPI {
// todo调试只发给我
globals.SugarLogger.Debugf("GetWeixinOpenIDsFromStoreID store:%d, openids:%v", storeID, retVal)
if storeID == 100146 {
retVal = []string{"oYN_ust9hXKEvEv0X6Mq6nlAWs_E"}
} else {
retVal = nil
}
}
return retVal
}
func SendMsgToStore(storeID int, templateID, downloadURL string, data interface{}) (err error) {
if globals.ReallySendWeixinMsg {
openIDs := GetWeixinOpenIDsFromStoreID(storeID)
successCount := 0
for _, openID := range openIDs {
if err2 := api.WeixinAPI.MessageTemplateSend(openID, templateID, downloadURL, nil, data); err2 == nil {
successCount++
} else {
err = err2
}
}
if successCount > 0 {
err = nil // 只要成功一个都当成成功
}
if err != nil {
globals.SugarLogger.Debugf("SendMsgToStore all failed storeID:%d, templateID:%s, error:%v", storeID, templateID, err)
}
}
return err
}
func NotifyNewOrder(order *model.GoodsOrder) (err error) {
globals.SugarLogger.Debugf("NotifyNewOrder orderID:%s", order.VendorOrderID)
if !model.IsOrderSolid(order) {
globals.SugarLogger.Infof("NotifyNewOrder orderID:%s is not solid", order.VendorOrderID)
return nil
}
sb := new(strings.Builder)
sb.WriteString("老板,")
sb.WriteString(order.ConsigneeName)
sb.WriteString("购买了商品")
sb.WriteString(order.Skus[0].SkuName)
sb.WriteString("共")
sb.WriteString(utils.Int2Str(order.Skus[0].Count))
sb.WriteString("份(")
sb.WriteString(jxutils.IntPrice2StandardString(order.Skus[0].SalePrice))
sb.WriteString("元/份)等,共支付了")
sb.WriteString(jxutils.IntPrice2StandardString(order.ActualPayPrice))
sb.WriteString("元")
data := map[string]interface{}{
"first": map[string]interface{}{
"value": sb.String(),
"color": WX_NEW_ORDER_TEMPLATE_COLOR,
},
"Day": map[string]interface{}{
"value": FormatDeliveryTime(order),
"color": WX_NEW_ORDER_TEMPLATE_COLOR,
},
"orderId": map[string]interface{}{
"value": order.VendorOrderID,
"color": WX_NEW_ORDER_TEMPLATE_COLOR,
},
"orderType": map[string]interface{}{
"value": fmt.Sprintf("%s 第%d号订单", model.VendorChineseNames[order.VendorID], order.OrderSeq),
"color": venderColors[order.VendorID],
},
"customerName": map[string]interface{}{
"value": order.ConsigneeName,
"color": WX_NEW_ORDER_TEMPLATE_COLOR,
},
"customerPhone": map[string]interface{}{
"value": order.ConsigneeMobile,
"color": WX_NEW_ORDER_TEMPLATE_COLOR,
},
}
storeID := jxutils.GetJxStoreIDFromOrder(order)
return SendMsgToStore(storeID, WX_NEWORDER_TEMPLATE_ID, fmt.Sprintf("%s%d", WX_TO_ORDER_PAGE_URL, storeID), data)
}
func NotifyWaybillStatus(bill *model.Waybill, order *model.GoodsOrder) error {
globals.SugarLogger.Debugf("NotifyWaybillStatus orderID:%s bill:%v", order.VendorOrderID, bill)
if !model.IsOrderSolid(order) {
globals.SugarLogger.Infof("NotifyWaybillStatus orderID:%s is not solid", order.VendorOrderID)
return nil
}
var title string
templateID := ""
remark := ""
titleColor := ""
switch bill.Status {
case model.WaybillStatusAccepted:
if bill.WaybillVendorID == model.VendorIDMTPS {
templateID = WX_MTPS_DELIVERY_GRABDONE_TEMPLATE_ID
} else if bill.WaybillVendorID == model.VendorIDDada {
templateID = WX_DADA_DELIVERY_GRABDONE_TEMPLATE_ID
}
titleColor = WX_HIGHLEVEL_TEMPLATE_COLOR
remark = FormatDeliveryTime(order)
title = fmt.Sprintf("%s 第%d号订单长时间无人配送我们已安排%s配送员%s电话号码%s负责配送。^_^", model.VendorChineseNames[bill.OrderVendorID], order.OrderSeq, model.VendorChineseNames[bill.WaybillVendorID], bill.CourierName, bill.CourierMobile)
case model.WaybillStatusDelivered:
if bill.WaybillVendorID == model.VendorIDMTPS {
templateID = WX_MTPS_DELIVERY_DONE_TEMPLATE_ID
} else if bill.WaybillVendorID == model.VendorIDDada {
templateID = WX_DADA_DELIVERY_DONE_TEMPLATE_ID
}
titleColor = venderColors[bill.OrderVendorID]
title = fmt.Sprintf("%s 第%d号订单的配送完成", model.VendorChineseNames[bill.OrderVendorID], order.OrderSeq)
}
if templateID != "" {
data := map[string]interface{}{
"first": map[string]interface{}{
"value": title,
"color": titleColor,
},
"keyword1": map[string]interface{}{
"value": bill.VendorOrderID,
"color": WX_NEW_ORDER_TEMPLATE_COLOR,
},
"keyword2": map[string]interface{}{
"value": fmt.Sprintf("%s%s", bill.CourierName, model.VendorChineseNames[bill.WaybillVendorID]),
"color": WX_NEW_ORDER_TEMPLATE_COLOR,
},
"keyword3": map[string]interface{}{
"value": bill.CourierMobile,
"color": WX_NEW_ORDER_TEMPLATE_COLOR,
},
"remark": map[string]interface{}{
"value": remark,
"color": WX_NEW_ORDER_TEMPLATE_COLOR,
},
}
return SendMsgToStore(jxutils.GetJxStoreIDFromOrder(order), templateID, "", data)
}
return nil
}
func FormatDeliveryTime(order *model.GoodsOrder) string {
var tmpTime time.Time
if order.ExpectedDeliveredTime == utils.DefaultTimeValue {
tmpTime = order.OrderCreatedAt.Add(1 * time.Hour)
} else {
tmpTime = order.ExpectedDeliveredTime
}
left := tmpTime.Sub(time.Now()) / time.Minute
leftHours := left / 60
leftMinutes := left % 60
return fmt.Sprintf("请于%s前送达剩余时间%d小时%d分钟", utils.Time2Str(tmpTime), leftHours, leftMinutes)
}