jyq 4 gadi atpakaļ
vecāks
revīzija
429994419a
5 mainītis faili ar 141 papildinājumiem un 35 dzēšanām
  1. 1 0
      errors/error.go
  2. 20 35
      service/auth.go
  3. 9 0
      utils/short_msg/init.go
  4. 16 0
      utils/short_msg/interface.go
  5. 95 0
      utils/short_msg/iyoogo.go

+ 1 - 0
errors/error.go

@@ -30,6 +30,7 @@ const (
 	BirthdayErr             ErrorCode = 1036
 	StaticHrErr             ErrorCode = 1037
 	CodeParamErr            ErrorCode = 5000
+	SmsErr                  ErrorCode = 7000
 	CodeDATABASE            ErrorCode = 9000
 	CodeREDIS               ErrorCode = 10000
 	HrSensorsTimeOutErr     ErrorCode = 30012

+ 20 - 35
service/auth.go

@@ -1,8 +1,8 @@
 package service
 
 import (
-	"fmt"
-	"github.com/mojocn/base64Captcha"
+	"math/rand"
+	"strconv"
 	"time"
 	"video_course/errors"
 	"video_course/global"
@@ -10,6 +10,10 @@ import (
 	"video_course/model"
 	h "video_course/repository/http"
 	rdb "video_course/repository/redis"
+	"video_course/utils"
+	"video_course/utils/short_msg"
+
+	"github.com/mojocn/base64Captcha"
 )
 
 var store = base64Captcha.DefaultMemStore
@@ -78,7 +82,7 @@ func (Auth) GenVerifyPic(config *base64Captcha.DriverString) (id string, pic str
 }
 
 // 手机号发送短信验证码
-func (a Auth) GenVFCode(codeType int, phone string, ip string, picCode string) (err error) {
+func (a Auth) GenVFCode(codeTypeInt int, phone string, ip string, picCode string) (err error) {
 	//图形验证码验证
 	//if !store.Verify(picId, picCode, true) {
 	//	return errors.ErrPicVerifyCode
@@ -89,11 +93,16 @@ func (a Auth) GenVFCode(codeType int, phone string, ip string, picCode string) (
 		return errors.ErrUserNotExists
 	}
 	//生成短信验证码
-	//rand.Seed(time.Now().Unix())
-	//smsInt := rand.Int31n(10000)
-	//smsCode :=  strconv.Itoa(smsInt)
-	smsCode := "4321"
+	rand.Seed(time.Now().Unix())
+	//smsInt := rand.Intn(10000)
+	//smsCode := strconv.Itoa(smsInt)
+	smsCode := strconv.Itoa(utils.RandomInt(6))
 
+	codeType := model.VFCodeCodeTypeFromInt(codeTypeInt)
+	//// 手机号发送间隔
+	//const NameSendInterval = time.Second * 60
+	//ip 发送间隔
+	//const IpSendInterval = time.Second * 1
 	//存储短信验证码前判断redis中是否有该手机号对应验证码,有说明还没失效
 	redisdb := rdb.GetRedis()
 	tmpCode, _ := redisdb.Get(global.ProjectName + ":" + global.AppName + ":" + "smsCode:" + phone).Result()
@@ -101,36 +110,12 @@ func (a Auth) GenVFCode(codeType int, phone string, ip string, picCode string) (
 		err = errors.ErrSmsCodeRepeat
 	}
 	redisdb.Set(global.ProjectName+":"+global.AppName+":"+"smsCode:"+phone, []byte(smsCode), 3*time.Minute)
-	//发送短信
-	fmt.Println(smsCode)
 
-	return
-	//dao := a.getUserDao()
-	//_ = dao.User{}.GetUserByPhone(name)
-	//h.LoalloutServer{}.CheckPhone(phone)
-	//codeType := model.VFCodeCodeTypeFromInt(codeTypeInt)
-	//vfcodeDao := dao.VFCode{}
-	//nameLastSend := vfcodeDao.NameLastSendTime(codeType, name)
-	//ipLastSend := vfcodeDao.IpLastSendTime(ip)
-	//
-	//// 手机号发送间隔
-	//const NameSendInterval = time.Second * 60
-	//// ip 发送间隔
-	//const IpSendInterval = time.Second * 1
-	//
-	//if time.Now().Sub(nameLastSend) < NameSendInterval {
-	//	panic(errors.ErrSendTooFast)
-	//}
-	//if time.Now().Sub(ipLastSend) < IpSendInterval {
-	//	panic(errors.ErrSendTooFast)
-	//}
-	//
-	//code := strconv.Itoa(utils.RandomInt(6))
-	//
-	//smDAO := short_msg.NewShortMsg()
-	//smDAO.SendVFCode(name, code, codeType)
-	//
+	smDAO := short_msg.NewShortMsg()
+	smDAO.SendVFCode(phone, smsCode, codeType)
+
 	//vfcodeDao.Save(codeType, name, code, time.Now().Add(time.Minute*3), ip)
+	return
 
 }
 

+ 9 - 0
utils/short_msg/init.go

@@ -0,0 +1,9 @@
+/*
+@Time : 2019-11-05 15:48
+@Author : zr
+*/
+package short_msg
+
+func init() {
+
+}

+ 16 - 0
utils/short_msg/interface.go

@@ -0,0 +1,16 @@
+/*
+@Time : 2019-11-05 15:41
+@Author : zr
+*/
+package short_msg
+
+import "video_course/model"
+
+type ShortMsg interface {
+	SendVFCode(phone, code string, codeType model.VFCodeCodeTypeEnum)
+}
+
+func NewShortMsg() (shortMsg ShortMsg) {
+	dao := NewShortMsgIyoogo()
+	return dao
+}

+ 95 - 0
utils/short_msg/iyoogo.go

@@ -0,0 +1,95 @@
+/*
+@Time : 2019-11-05 16:41
+@Author : zr
+*/
+package short_msg
+
+import (
+	"crypto/md5"
+	"encoding/hex"
+	"encoding/json"
+	"fmt"
+	"io/ioutil"
+	"net/http"
+	"strings"
+	"video_course/errors"
+	"video_course/model"
+)
+
+type Iyoogo struct {
+	station  string
+	appKey   string
+	sign     string
+	signName string
+}
+
+func NewShortMsgIyoogo() *Iyoogo {
+	dao := &Iyoogo{
+		station:  "http://sms.iyoogo.com/itf2",
+		appKey:   "f736bcf2429d08457eee83527e871069",
+		signName: "小飞龙",
+	}
+
+	SECRET := "746c3978d0dd8696a1ffe7a9a9fa7a2b"
+	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) SendVFCode(phone, code string, codeType model.VFCodeCodeTypeEnum) {
+
+	var (
+		smsParam []byte
+		err      error
+	)
+	templateCode := ""
+	switch codeType {
+	case model.VFCodeCodeTypeLogin:
+		smsParam, err = json.Marshal(map[string]string{
+			"code":    code,
+			"product": i.signName,
+		})
+		if err != nil {
+			panic(errors.NewServiceErr(errors.SmsErr, err.Error()))
+		}
+		templateCode = "42"
+	}
+
+	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, i.signName, smsParam, phone, templateCode)
+
+	resp, err := http.Get(url)
+	if err != nil {
+		panic(errors.NewServiceErr(errors.SmsErr, err.Error()))
+	}
+	defer resp.Body.Close()
+	body, err := ioutil.ReadAll(resp.Body)
+	if err != nil {
+		panic(errors.NewServiceErr(errors.SmsErr, err.Error()))
+	}
+	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 {
+		panic(errors.NewServiceErr(errors.SmsErr, err.Error()+"|resp string:"+string(body)))
+	}
+
+	if !respStruct.Result.Success {
+		panic(errors.NewServiceErr(errors.SmsErr, respStruct.Result.Msg))
+	}
+
+	return
+}