wcj 5 years ago
parent
commit
9665bd7f68
2 changed files with 106 additions and 0 deletions
  1. 66 0
      common/src/qfw/util/jy/checksendmsg.go
  2. 40 0
      common/src/qfw/util/jy/jy.go

+ 66 - 0
common/src/qfw/util/jy/checksendmsg.go

@@ -0,0 +1,66 @@
+package jy
+
+import (
+	"fmt"
+	"log"
+	"net/url"
+	"qfw/util"
+	"qfw/util/redis"
+
+	//"strconv"
+	"strings"
+	"time"
+)
+
+var se *util.SimpleEncrypt = &util.SimpleEncrypt{Key: "topnet2015topnet2015"}
+var ActivityEndCode, ActivityStartCode int64
+var AC = &util.AES_CBC{
+	Key: "mGlAgnIBB8bx2nch",
+	Iv:  "1389461544135476",
+}
+
+//发短信,验证token
+func CheckSendMsg(token string) string {
+	if token == "" {
+		return ""
+	}
+	log.Println("短信解析前token", token)
+	key := fmt.Sprintf("smstoken_%s", token)
+	ok, e := redis.Exists("other", key)
+	if e != nil {
+		log.Println("redis中token error", e)
+		return ""
+	}
+	if ok {
+		log.Println("redis中token已存在", token)
+		return ""
+	}
+	token, e = url.QueryUnescape(token)
+	if e != nil {
+		log.Println("短信token QueryUnescape error", e)
+	}
+	v, err := AC.Decrypt(token)
+	if err != nil {
+		log.Println("短信token Decrypt error", err)
+		return ""
+	}
+	log.Println("短信解析后token", v)
+	vs := strings.Split(v, "_")
+	if len(vs) != 3 {
+		log.Println("短信token error", vs)
+		return ""
+	}
+	now := time.Now()
+	if !strings.HasPrefix(vs[1], util.FormatDate(&now, util.Date_yyyyMMdd)) {
+		log.Println("短信token date错误", vs)
+		return ""
+	}
+	if vs[2] != util.GetMd5String(fmt.Sprintf("%s&%s", vs[0], vs[1])) {
+		log.Println("短信token sing错误", vs)
+		return ""
+	}
+	if vs[0] != "" {
+		redis.Put("other", key, 1, 86400)
+	}
+	return vs[0]
+}

+ 40 - 0
common/src/qfw/util/jy/jy.go

@@ -9,9 +9,12 @@ import (
 	"net/url"
 	"qfw/util"
 	"qfw/util/mail"
+	"qfw/util/sms"
 	"regexp"
 	"strings"
 	"time"
+
+	"github.com/go-xweb/httpsession"
 )
 
 //获取用户合并以前,合并以后的openid
@@ -146,3 +149,40 @@ func SendMailIdentCode(to, code string, auth []*mail.GmailAuth) bool {
 
 	return false
 }
+
+//根据模板发送短信,模板是运营商设定的。
+//第三个参数是可变参数,可以传入多个,但要和模板相匹配
+func SendSMS(tplcode /*模板代码*/, mobile /*手机号码*/ string, param map[string]string) {
+	tmp := []string{}
+	for k, v := range param {
+		tmp = append(tmp, "#"+k+"#="+v)
+	}
+	text := strings.Join(tmp, "&")
+	sms.SendSms(mobile, tplcode, text)
+}
+
+//发送验证码
+func SendPhoneIdentCode(phone string, session *httpsession.Session) bool {
+	lastSentTime, _ := session.Get("identCodeTime").(int64)
+	//60秒之内不允许重复发
+	if lastSentTime > 0 && time.Now().Unix()-lastSentTime <= 60 {
+		return false
+	}
+	s_ranNum := util.GetRandom(6) //生成随机数
+	session.Set("identCodeValue", s_ranNum)
+	session.Set("identCodeKey", phone)
+	session.Set("identCodeTime", time.Now().Unix())
+	//发送短信
+	param := make(map[string]string)
+	param["code"] = s_ranNum
+	log.Println("短信验证码", phone, s_ranNum)
+	SendSMS("2828060", phone, param)
+	return true
+}
+
+//删除短信验证码有关的session
+func ClearPhoneIdentSession(session *httpsession.Session) {
+	session.Del("identCodeValue")
+	session.Del("identCodeKey")
+	session.Del("identCodeTime")
+}