| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- // Package sms
- /**
- * @ File:
- * @ Date: 2021/5/21 11:12
- * @ Author: JYQ
- * @ Description:
- */
- package sms
- import (
- "crypto/md5"
- "encoding/hex"
- "encoding/json"
- "fmt"
- "io/ioutil"
- "net/http"
- pb "sportfitness/base/api/grpc/base"
- "sportfitness/base/assembly/base/repository/postgre"
- "strings"
- "git.beswell.com/gframe/application"
- )
- type Iyoogo struct {
- station string
- appKey string
- sign string
- signName string
- }
- func NewShortMsgIyoogo() *Iyoogo {
- dao := &Iyoogo{
- station: "http://sms.iyoogo.com/itf5",
- appKey: "07e54cc3bec2fc46adad77486ef857a9",
- //signName: "佰意兴科技",
- }
- SECRET := "fb25c484b5a7880ed25a0d303a5f654d"
- cry := md5.New()
- cry.Write([]byte(SECRET + dao.appKey + SECRET))
- dao.sign = hex.EncodeToString(cry.Sum(nil))
- dao.sign = strings.ToUpper(dao.sign)
- return dao
- }
- func (i Iyoogo) SendCode(vf *postgre.VCode, templateCode, sName string) {
- var smsParam []byte
- var err error
- switch vf.CodeType {
- case pb.VerifyType_SignIn:
- smsParam, err = json.Marshal(map[string]string{
- "code": vf.Code,
- })
- if err != nil {
- return
- }
- }
- url := fmt.Sprintf(
- "%s/?method=smssend&app_key=%s&sign=%s&v=1.0&sms_free_sign_name=%s&sms_param=%s&rec_num=%s&sms_template_code=%s&sms_type=normal",
- i.station, i.appKey, i.sign, sName, smsParam, vf.Account, templateCode)
- resp, err := http.Get(url)
- if err != nil {
- err = application.ErrorBusinessF(application.ErrorCodeInternal, err.Error())
- panic(err)
- }
- defer resp.Body.Close()
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- err = application.ErrorBusinessF(application.ErrorCodeInternal, err.Error())
- panic(err)
- }
- type Result struct {
- ErrCode string `json:"err_code"`
- Success bool `json:"success"`
- Msg string `json:"msg"`
- }
- type Resp struct {
- Result Result `json:"result"`
- }
- respStruct := &Resp{}
- err = json.Unmarshal(body, respStruct)
- if err != nil {
- err = application.ErrorBusinessF(application.ErrorCodeInternal, err.Error()+"|resp string:"+string(body))
- panic(err)
- }
- if !respStruct.Result.Success {
- err = application.ErrorBusinessF(application.ErrorCodeInternal, respStruct.Result.Msg)
- panic(err)
- }
- }
- type PhoneCode struct {
- }
- func (p PhoneCode) SendCode(vf *postgre.VCode, templateCode, sName string) {
- iyoogo := NewShortMsgIyoogo()
- iyoogo.SendCode(vf, templateCode, sName)
- }
|