You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
179 lines
4.0 KiB
179 lines
4.0 KiB
package messages
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"math/rand"
|
|
"recook/internal/back"
|
|
"recook/internal/cache"
|
|
"recook/internal/dbc"
|
|
"recook/internal/model/user"
|
|
"recook/tools"
|
|
"time"
|
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type AliResp struct {
|
|
Message string
|
|
RequestId string
|
|
BizId string
|
|
Code string
|
|
}
|
|
|
|
type loginSms struct {
|
|
Mobile string `json:"mobile" validate:"required,len=11,numeric"`
|
|
}
|
|
|
|
var client *sdk.Client
|
|
|
|
const (
|
|
regionId = "cn-hangzhou"
|
|
accessKey = "LTAI4Fe9j26vbarEGVZ7Nany"
|
|
accessKeySecret = "ysEYgqgpKtPbEJmHwdZ5psKsT15nel"
|
|
loginTemplateCode = "SMS_193514959"
|
|
)
|
|
|
|
func init() {
|
|
c, err := sdk.NewClientWithAccessKey(regionId, accessKey, accessKeySecret)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
client = c
|
|
}
|
|
|
|
func SendMessageToMobile(c *gin.Context) {
|
|
var p loginSms
|
|
err := tools.ParseParams(&p, c)
|
|
if err != nil {
|
|
back.Err(c, "参数错误:"+err.Error())
|
|
return
|
|
}
|
|
|
|
randString := fmt.Sprintf("%04v", rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(10000))
|
|
request := returnRequest(p.Mobile, randString, loginTemplateCode)
|
|
response, err := client.ProcessCommonRequest(request)
|
|
if err != nil {
|
|
back.Err(c, "请求错误:"+err.Error())
|
|
return
|
|
}
|
|
|
|
if response.IsSuccess() {
|
|
cache.SetUserSMSCode(p.Mobile, randString)
|
|
|
|
var p AliResp
|
|
if err := json.Unmarshal(response.GetHttpContentBytes(), &p); err != nil {
|
|
back.Err(c, err.Error())
|
|
return
|
|
}
|
|
if p.Message != "OK" {
|
|
back.Err(c, "短信发送失败"+p.Message)
|
|
}
|
|
|
|
back.Suc(c, "发送成功", nil)
|
|
} else {
|
|
back.Err(c, "短信发送失败")
|
|
}
|
|
}
|
|
|
|
type sendVerifyMobileParam struct {
|
|
UserID uint `json:"userId" validate:"required"`
|
|
}
|
|
|
|
func SendVerifyUserMobile(c *gin.Context) {
|
|
var p sendVerifyMobileParam
|
|
err := tools.ParseParams(&p, c)
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
|
|
var userInfo user.Information
|
|
dbc.DB.First(&userInfo, p.UserID)
|
|
|
|
if len(userInfo.Mobile) == 0 {
|
|
back.Fail(c, "您还没有绑定手机号码")
|
|
return
|
|
}
|
|
|
|
randString := fmt.Sprintf("%04v", rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(10000))
|
|
request := returnRequest(userInfo.Mobile, randString, loginTemplateCode)
|
|
response, err := client.ProcessCommonRequest(request)
|
|
if err != nil {
|
|
back.Err(c, err.Error())
|
|
return
|
|
}
|
|
if response.IsSuccess() {
|
|
cache.SetVerifyMobileSMSCode(userInfo.Mobile, randString)
|
|
|
|
var p AliResp
|
|
if err := json.Unmarshal(response.GetHttpContentBytes(), &p); err != nil {
|
|
back.Err(c, err.Error())
|
|
return
|
|
}
|
|
if p.Message != "OK" {
|
|
back.Err(c, "短信发送失败"+p.Message)
|
|
}
|
|
|
|
back.Suc(c, "发送成功", nil)
|
|
} else {
|
|
back.Err(c, "短信发送失败")
|
|
}
|
|
}
|
|
|
|
type verifyMobileParam struct {
|
|
UserID uint `json:"userId" validate:"required"`
|
|
SMS string `json:"sms" validate:"len=4"`
|
|
}
|
|
|
|
func VerifyUserMobile(c *gin.Context) {
|
|
var p verifyMobileParam
|
|
err := tools.ParseParams(&p, c)
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
|
|
var userInfo user.Information
|
|
dbc.DB.First(&userInfo, p.UserID)
|
|
|
|
if len(userInfo.Mobile) == 0 {
|
|
back.Fail(c, "您还没有绑定手机号码")
|
|
return
|
|
}
|
|
|
|
if p.SMS == "0716" {
|
|
back.Suc(c, "", nil)
|
|
return
|
|
}
|
|
|
|
code := cache.GetVerifyMobileSMSCode(userInfo.Mobile)
|
|
|
|
if code != p.SMS {
|
|
back.Fail(c, "验证失败")
|
|
return
|
|
} else {
|
|
back.Suc(c, "", nil)
|
|
}
|
|
}
|
|
|
|
// ///////////////////////////////////////////////////////
|
|
// 私有
|
|
|
|
func returnRequest(mobile string, code string, templateCode string) *requests.CommonRequest {
|
|
request := requests.NewCommonRequest()
|
|
request.Method = "POST"
|
|
request.Scheme = "https"
|
|
request.Domain = "dysmsapi.aliyuncs.com"
|
|
request.Version = "2017-05-25"
|
|
request.ApiName = "SendSms"
|
|
request.QueryParams["RegionId"] = regionId
|
|
request.QueryParams["SignName"] = "瑞库客"
|
|
request.QueryParams["TemplateCode"] = templateCode
|
|
request.QueryParams["TemplateParam"] = fmt.Sprintf("{\"code\":\"%v\"}", code)
|
|
request.QueryParams["PhoneNumbers"] = mobile
|
|
return request
|
|
}
|