Merge branch 'feat-IRONFANS-218-Robin' into confsms

This commit is contained in:
Leufolium 2024-09-25 13:25:04 +08:00
commit 1940ea3524
18 changed files with 263 additions and 26 deletions

View File

@ -68,6 +68,7 @@ const (
StreamerFilterKey = "streamer_filter"
RavenIQTestEmailConfigKey = "Raven_IQ_test_email_config"
OfficialEmailKey = "official_email"
SmsSplitRatioKey = "sms_split_ratio"
)
// del_flag

View File

@ -0,0 +1,23 @@
package request
// 模板短信发送报文
type ZTHY10000001Req struct {
Username string `json:"username"`
Password string `json:"password"`
TKey string `json:"tKey"`
Signature string `json:"signature"`
TpId string `json:"tpId"`
Ext string `json:"ext"`
Time string `json:"time"`
Extend string `json:"extend"`
Records []*Record `json:"records"`
}
type Record struct {
Mobile string `json:"mobile"`
TpContent *ValidCodeRecord `json:"tpContent"`
}
type ValidCodeRecord struct {
ValidCode string `json:"valid_code"`
}

View File

@ -0,0 +1,11 @@
package response
import "service/api/message/request"
type ZTHY10000001Resp struct {
Code int `json:"code"`
Msg string `json:"msg"`
MsgId string `json:"msgId"`
TpId string `json:"tpId"`
InvalidList []*request.Record `json:"invalidList"`
}

View File

@ -91,6 +91,7 @@ func main() {
service.DefaultVideoModerationTaskResultHandler = service.NewVideoModerationTaskResultHandler()
service.DefaultEsbService = service.NewEsbService()
service.DefaultHvyogoService = service.NewHvyogoService(cfg.Hvyogo)
service.DefaultZthyService = service.NewZthyService(cfg.Zthy)
err = service.DefaultService.Init(cfg)
if err != nil {
msg := fmt.Sprintf("Service init fail, err: %v", err)

View File

@ -29,4 +29,5 @@ type ConfigSt struct {
Yeepay *configcenter.YeepayClientConfig `json:"yeepay" yaml:"yeepay"`
SD *configcenter.ConsulServiceDiscovery `json:"sd" yaml:"sd"` // 服务发现
Hvyogo *configcenter.HvyogoConfig `json:"hvyogo" yaml:"hvyogo"` // 慧用工
Zthy *configcenter.ZthyConfig `json:"zthy" yaml:"zthy"` // 助通融合云
}

View File

@ -3,14 +3,16 @@ package logic
import (
"fmt"
"math/rand"
"service/api/consts"
vericodeproto "service/api/proto/vericode/proto"
"service/app/mix/dao"
"service/bizcommon/util"
"service/dbstruct"
"service/library/apollo"
"service/library/configcenter"
"service/library/idgenerator"
"service/library/logger"
"service/library/mycrypto"
"service/library/redis"
"service/library/sms"
interceptor "service/library/taginterceptor"
"time"
@ -20,16 +22,22 @@ import (
goproto "google.golang.org/protobuf/proto"
)
type SendSmsFunction func(mobilephone string, vericode string) error
type VeriCode struct {
store *dao.Store
cfg *configcenter.DysmsapiConfig
store *dao.Store
cfg *configcenter.DysmsapiConfig
zthySendSmsFunc SendSmsFunction
}
func NewVeriCode(store *dao.Store, cfg *configcenter.DysmsapiConfig) (a *VeriCode) {
func NewVeriCode(store *dao.Store, cfg *configcenter.DysmsapiConfig, zthySendSmsFunc SendSmsFunction) (a *VeriCode) {
a = &VeriCode{
store: store,
cfg: cfg,
store: store,
cfg: cfg,
zthySendSmsFunc: zthySendSmsFunc,
}
// 临时增加流控标志
redis.GetRedisClient().Set("sms_index", int64(0), 0)
return
}
@ -89,23 +97,44 @@ func (p *VeriCode) OpSendVeriCode(ctx *gin.Context, req *vericodeproto.OpSendReq
return err
}
//3.发送短信
templateParam := "{\"code\": \"" + vericode + "\"}"
request := &dysmsapi.SendSmsRequest{
PhoneNumbers: goproto.String(req.MobilePhone),
SignName: goproto.String(p.cfg.SignName),
TemplateCode: goproto.String(p.cfg.TemplateCode),
TemplateParam: goproto.String(templateParam),
}
resp, err := sms.SendSms(request)
// 分流比率
smsSplitRatio, err := apollo.GetIntValue(consts.SmsSplitRatioKey, apollo.ApolloOpts().SetNamespace("application"))
if err != nil {
logger.Error("SendSms failed : %v", err)
logger.Error("Apollo read failed : %v", err)
return err
}
if util.DerefString(resp.Body.Code) != "OK" {
msg := util.DerefString(resp.Body.Message)
logger.Error("SendSms failed : %v", msg)
return fmt.Errorf("%v", msg)
smsIndex, err := redis.GetRedisClient().GetInt64("sms_index")
if err != nil {
logger.Error("GetInt64 failed : %v", err)
return err
}
err = redis.GetRedisClient().Set("sms_index", (smsIndex+1)%10, 0)
if err != nil {
logger.Error("SetInt64 failed : %v", err)
return err
}
if smsIndex < int64(smsSplitRatio) {
//3.zthy发送短信
err = p.zthySendSmsFunc(req.MobilePhone, vericode)
if err != nil {
logger.Error("SendSms failed : %v", err)
return err
}
} else {
//3.阿里云发送短信
templateParam := "{\"code\": \"" + vericode + "\"}"
request := &dysmsapi.SendSmsRequest{
PhoneNumbers: goproto.String(req.MobilePhone),
SignName: goproto.String(p.cfg.SignName),
TemplateCode: goproto.String(p.cfg.TemplateCode),
TemplateParam: goproto.String(templateParam),
}
err = sms.SendSms(request)
if err != nil {
logger.Error("SendSms failed : %v", err)
return err
}
}
return nil

View File

@ -202,7 +202,7 @@ func (s *Service) Init(c any) (err error) {
yeepaycli.Init(cfg.Yeepay)
_DefaultToken = logic.NewToken(store, cfg.Crypto)
_DefaultVeriCode = logic.NewVeriCode(store, cfg.Dysmsapi)
_DefaultVeriCode = logic.NewVeriCode(store, cfg.Dysmsapi, DefaultZthyService.SendSms)
_DefaultVeriCodeSendTimes = logic.NewVeriCodeSendTimes(store)
_DefaultAccount = logic.NewAccount(store)
_DefaultProduct = logic.NewProduct(store)

View File

@ -0,0 +1,93 @@
package service
import (
"encoding/json"
"fmt"
"service/api/message/request"
"service/api/message/response"
"service/bizcommon/util"
"service/library/configcenter"
"service/library/logger"
"service/library/mycrypto"
"time"
)
var DefaultZthyService *ZthyService
type ZthyService struct {
Username string
Password string
Signature string
TpId string
}
func NewZthyService(cfg *configcenter.ZthyConfig) *ZthyService {
return &ZthyService{
Username: cfg.Username,
Password: cfg.Password,
Signature: cfg.Signature,
TpId: cfg.TpId,
}
}
func (s *ZthyService) SendSms(mobilephone string, vericode string) error {
record := &request.Record{
Mobile: mobilephone,
TpContent: &request.ValidCodeRecord{
ValidCode: vericode,
},
}
records := make([]*request.Record, 0)
records = append(records, record)
req := &request.ZTHY10000001Req{
Records: records,
}
rsp, err := s.SendSmsReq(req)
if err != nil {
logger.Error("SendSmsReq fail, req: %v, err: %v", util.ToJson(req), err)
return err
}
if rsp.Code != 200 {
return fmt.Errorf(rsp.Msg)
}
return nil
}
func (s *ZthyService) SendSmsReq(req *request.ZTHY10000001Req) (rsp *response.ZTHY10000001Resp, err error) {
// 写参
req.Username = s.Username
req.Signature = s.Signature
req.TpId = s.TpId
// 生成时间戳
req.TKey = fmt.Sprint(time.Now().Unix())
// 生成密码
req.Password, err = mycrypto.CryptoServiceInstance().MD5.EncryptToString([]byte(s.Password + req.TKey))
if err != nil {
logger.Error("password md5 encryption fail, err: %v", err)
return
}
// 生成json
msg, err := json.Marshal(req)
if err != nil {
logger.Error("json marshal fail, err: %v", err)
return
}
// 发送消息
resp, err := DefaultEsbService.SendMsg("ZTHY10000001", msg)
if err != nil {
logger.Error("SendMsg fail, req: %v, err: %v", util.ToJson(req), err)
return
}
// 处理返回数据
rsp = &response.ZTHY10000001Resp{}
err = json.Unmarshal(resp, rsp)
if err != nil {
logger.Error("json unmarshal fail, err: %v", err)
return
}
return
}

View File

@ -170,4 +170,10 @@ yeepay:
hvyogo:
cooperator_id: "C1252339226041655296"
position_id: "P1252701159697682432"
position_id: "P1252701159697682432"
zthy:
username: "XYDLhy"
password: "e76f4c5afa1d431b20e8856f6c5a08da"
signature: "【铁粉空间】"
tpId: "157478"

View File

@ -198,4 +198,10 @@ sd:
hvyogo:
cooperator_id: "C1248233014165520384"
position_id: "P1255937692118577152"
position_id: "P1255937692118577152"
zthy:
username: "XYDLhy"
password: "e76f4c5afa1d431b20e8856f6c5a08da"
signature: "【铁粉空间】"
tpId: "157478"

View File

@ -198,4 +198,10 @@ sd:
hvyogo:
cooperator_id: "C1248233014165520384"
position_id: "P1255937692118577152"
position_id: "P1255937692118577152"
zthy:
username: "XYDLhy"
password: "e76f4c5afa1d431b20e8856f6c5a08da"
signature: "【铁粉空间】"
tpId: "157478"

View File

@ -195,4 +195,10 @@ yeepay:
hvyogo:
cooperator_id: "C1252339226041655296"
position_id: "P1252701159697682432"
position_id: "P1252701159697682432"
zthy:
username: "XYDLhy"
password: "e76f4c5afa1d431b20e8856f6c5a08da"
signature: "【铁粉空间】"
tpId: "157478"

View File

@ -198,4 +198,10 @@ yeepay:
hvyogo:
cooperator_id: "C1252339226041655296"
position_id: "P1252701159697682432"
position_id: "P1252701159697682432"
zthy:
username: "XYDLhy"
password: "e76f4c5afa1d431b20e8856f6c5a08da"
signature: "【铁粉空间】"
tpId: "157478"

View File

@ -4,4 +4,5 @@
<msg name = "HYG10000002">https://oapi.hvyogo.com/api/v2/hire/worker/findDetails</msg>
<msg name = "HYG10010001">https://oapi.hvyogo.com/api/distribute/singleDistribute</msg>
<msg name = "HYG10010002">https://oapi.hvyogo.com/api/v2/hire/worker/update</msg>
<msg name = "ZTHY10000001">https://api-shss.zthysms.com/v2/sendSmsTp</msg>
</esb_routing_table>

View File

@ -200,6 +200,14 @@ type HvyogoConfig struct {
PositionId string `json:"position_id" yaml:"position_id"`
}
// 助通融合云通信
type ZthyConfig struct {
Username string `json:"username" yaml:"username"`
Password string `json:"password" yaml:"password"`
Signature string `json:"signature" yaml:"signature"`
TpId string `json:"tpId" yaml:"tpId"`
}
func LoadConfig(configFilePath string, cfg interface{}) error {
cfgStr, err := ioutil.ReadFile(configFilePath)
if err != nil {

View File

@ -24,6 +24,7 @@ type CryptoService struct {
SHA256 *Sha256Crypto
HygSHA1WithRSA *SHAwithRSACrypto
HygAES *AesEcbCrypto
MD5 *Md5Crypto
}
func (cryptoService *CryptoService) Init(cryptoConfig *configcenter.CryptoConfig) (err error) {
@ -52,5 +53,7 @@ func (cryptoService *CryptoService) Init(cryptoConfig *configcenter.CryptoConfig
return
}
cryptoService.MD5 = NewMd5Crypto()
return
}

View File

@ -0,0 +1,25 @@
package mycrypto
import (
"crypto/md5"
"fmt"
)
type Md5Crypto struct{}
func NewMd5Crypto() (md5Crypto *Md5Crypto) {
return &Md5Crypto{}
}
func (rsaCrypto *Md5Crypto) Encrypt(msg []byte) (encryptedBytes []byte, err error) {
hash := md5.Sum(msg)
encryptedString := fmt.Sprintf("%x", hash)
encryptedBytes = []byte(encryptedString)
return
}
func (rsaCrypto *Md5Crypto) EncryptToString(msg []byte) (encryptedString string, err error) {
hash := md5.Sum(msg)
encryptedString = fmt.Sprintf("%x", hash)
return
}

View File

@ -1,6 +1,7 @@
package sms
import (
"fmt"
"service/bizcommon/util"
"service/library/configcenter"
"service/library/logger"
@ -34,8 +35,18 @@ func Init(cfg *configcenter.DysmsapiConfig) (err error) {
func SendSms(request *dysmsapi.SendSmsRequest) (_result *dysmsapi.SendSmsResponse, err error) {
_result, err = defaultDysmsapiClient.SendSms(request)
logger.Info("SendSms result:%v", _result)
if _result == nil || _result.Body == nil {
return fmt.Errorf("短信发送失败")
}
if util.DerefInt32(_result.StatusCode) != 200 {
return fmt.Errorf("短信发送失败")
}
if util.DerefString(_result.Body.Code) != "OK" {
return fmt.Errorf(util.DerefString(_result.Body.Message))
}
if err != nil {
logger.Error("SendSms failed : %v", err)
return
}
return
}