- mobile sms verify added.

This commit is contained in:
gazebo
2018-09-05 18:03:53 +08:00
parent 76b03bf86f
commit 0faaa6b366
7 changed files with 119 additions and 0 deletions

View File

@@ -5,7 +5,10 @@ import (
"time"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/jxcallback/auth/mobile"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/globals"
"git.rosy.net.cn/jx-callback/globals/gormdb"
)
const (
@@ -78,3 +81,15 @@ func GetUserInfo(token string) (loginInfo *LoginInfo, err error) {
}
return nil, err
}
func BindMobile(token, mobileNum, code string) (err error) {
loginInfo := new(LoginInfo)
if err = globals.Cacher.GetAs(token, loginInfo); err == nil {
if mobile.VerifyCode(mobileNum, code) {
db := gormdb.GetDB()
return db.Model(model.WeiXins{}).Where("openid = ?", loginInfo.ID).Update("tel", mobileNum).Error
}
err = errors.New("验证码错")
}
return err
}

View File

@@ -0,0 +1,38 @@
package mobile
import (
"fmt"
"math/rand"
"time"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/globals"
"github.com/KenmyZhang/aliyun-communicate"
)
const (
DefVerifyCodeDuration = 5 * time.Minute
)
func SendVerifyCode(mobileNumber string) error {
smsClient := aliyunsmsclient.New("http://dysmsapi.aliyuncs.com/")
code := fmt.Sprintf("%06d", rand.Intn(1000000))
_, err := smsClient.Execute(globals.AliKey, globals.AliSecret, mobileNumber, "京西菜市", "SMS_84655036", string(utils.MustMarshal(map[string]interface{}{
"code": code,
})))
if err == nil {
globals.Cacher.Set(mobileNumber, code, DefVerifyCodeDuration)
}
// globals.SugarLogger.Debug(utils.Format4Output(result, false))
return err
}
func VerifyCode(mobileNumber, code string) bool {
if value := globals.Cacher.Get(mobileNumber); value != nil {
if code == value.(string) {
globals.Cacher.Del(mobileNumber)
return true
}
}
return false
}

View File

@@ -0,0 +1,12 @@
package mobile
import (
"testing"
)
func TestSendVerifyCode(t *testing.T) {
err := SendVerifyCode("18180948107")
if err != nil {
t.Fatal(err)
}
}

View File

@@ -17,6 +17,9 @@ autonaviKey = "4427170f870af2110becb8852d36ab08"
generateLegacyJxOrder = false
enableStore = true
aliKey = "LTAI6xJUGaP6WdMQ"
aliSecret = "CLmx5T93Bgi89EGAxWM4RTAXUsiHbM"
[dev]
freshFoodServerURL = "http://portal.beta.jxc4.com"

View File

@@ -8,6 +8,7 @@ import (
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/jxcallback/auth"
_ "git.rosy.net.cn/jx-callback/business/jxcallback/auth/localpass" // 加载本地用户密码
"git.rosy.net.cn/jx-callback/business/jxcallback/auth/mobile"
"git.rosy.net.cn/jx-callback/business/jxcallback/auth/weixin"
"git.rosy.net.cn/jx-callback/globals"
"github.com/astaxie/beego"
@@ -102,3 +103,31 @@ func (c *AuthController) GetUserInfo() {
return retVal, "", err
})
}
// @Title 发送验证码
// @Description 发送验证码
// @Param mobile formData string true "手机号"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /SendMobileVerifyCode [post]
func (c *AuthController) SendMobileVerifyCode() {
c.callSendMobileVerifyCode(func(params *tAuthSendMobileVerifyCodeParams) (retVal interface{}, errCode string, err error) {
err = mobile.SendVerifyCode(params.Mobile)
return retVal, "", err
})
}
// @Title 绑定手机
// @Description 绑定手机
// @Param token header string true "认证token"
// @Param mobile formData string true "手机号"
// @Param code formData string true "验证码"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /BindMobile [post]
func (c *AuthController) BindMobile() {
c.callBindMobile(func(params *tAuthBindMobileParams) (retVal interface{}, errCode string, err error) {
err = auth.BindMobile(params.Token, params.Mobile, params.Code)
return retVal, "", err
})
}

View File

@@ -27,6 +27,9 @@ var (
JdorderTableName string
Cacher cache.ICacher
AliKey string
AliSecret string
)
func init() {
@@ -53,4 +56,7 @@ func Init() {
JdorderTableName = "jdorder"
Cacher = redis.New(beego.AppConfig.DefaultString("redisHost", "localhost"), beego.AppConfig.DefaultInt("redisPort", 0), beego.AppConfig.DefaultString("redisPassword", ""))
AliKey = beego.AppConfig.DefaultString("aliKey", "")
AliSecret = beego.AppConfig.DefaultString("aliSecret", "")
}

View File

@@ -7,6 +7,14 @@ import (
func init() {
beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:AuthController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:AuthController"],
beego.ControllerComments{
Method: "BindMobile",
Router: `/BindMobile`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:AuthController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:AuthController"],
beego.ControllerComments{
Method: "GetUserInfo",
@@ -39,6 +47,14 @@ func init() {
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:AuthController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:AuthController"],
beego.ControllerComments{
Method: "SendMobileVerifyCode",
Router: `/SendMobileVerifyCode`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:OrderController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:OrderController"],
beego.ControllerComments{
Method: "CreateWaybillOnProviders",