Files
jx-callback/business/jxutils/smsmsg/smsmsg.go
2019-09-06 13:37:47 +08:00

65 lines
2.2 KiB
Go

package smsmsg
import (
"fmt"
aliyunsmsclient "github.com/KenmyZhang/aliyun-communicate"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/baseapi/utils/errlist"
"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"
"git.rosy.net.cn/jx-callback/globals/api"
)
var (
warningMap = map[string]int{
"isv.AMOUNT_NOT_ENOUGH": 1,
"isv.ACCOUNT_ABNORMAL": 1,
"isv.OUT_OF_SERVICE": 1,
"isv.DAY_LIMIT_CONTROL": 1,
}
)
func SendSMSMsg(mobileList []string, signName, templateCode string, templateParam map[string]interface{}) (err error) {
errList := errlist.New()
mobileList = jxutils.StringMap2List(jxutils.StringList2Map(mobileList))
for _, mobileNum := range mobileList {
if mobileNum != "" {
globals.SugarLogger.Debugf("SendSMSMsg mobileNum:%s, templateCode:%s", mobileNum, templateCode)
if globals.EnableStoreWrite {
if response, err := api.SMSClient.Execute(globals.AliKey, globals.AliSecret, mobileNum, signName, templateCode, string(utils.MustMarshal(templateParam))); err != nil {
globals.SugarLogger.Warnf("SendSMSMsg mobileNum:%s failed with error:%v", mobileNum, err)
errList.AddErr(err)
} else if response.Code != aliyunsmsclient.ResponseCodeOk {
errMsg := fmt.Sprintf("SendSMSMsg mobileNum:%s failed with response:%s", mobileNum, utils.Format4Output(response, false))
errList.AddErr(fmt.Errorf(errMsg))
if warningMap[response.Code] == 1 {
globals.SugarLogger.Warnf(errMsg)
} else {
globals.SugarLogger.Infof(errMsg)
}
}
}
}
}
return errList.GetErrListAsOne()
}
func NotifyNewOrder(order *model.GoodsOrder) (err error) {
store := &model.Store{}
store.ID = jxutils.GetSaleStoreIDFromOrder(order)
if err = dao.GetEntity(dao.GetDB(), store); err == nil {
if store.SMSNotify != 0 {
err = SendSMSMsg([]string{store.Tel1, store.Tel2}, "京西菜市", "SMS_173477895", map[string]interface{}{
"daySeq": order.OrderSeq,
"consigneeName": order.ConsigneeName,
"payMoney": jxutils.IntPrice2StandardString(order.ActualPayPrice),
})
}
}
return err
}