Sms.go
2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package Api
import (
"encoding/json"
"github.com/aarongao/tools"
"github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
"github.com/gin-gonic/gin"
"letu/DB"
"letu/Lib"
"time"
)
// @Title 发送短信验证码
// @Description 发送短信验证码
// @Accept json
// @Produce json
// @Param Mobile 18616619599 string true "手机号"
// @Param Location {"Latitude": 119, "Longitude": 39} string true "位置"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":"ok"}验证码1分钟内有效"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /Sms/Send? [post]
func Send(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
c.Header("Access-Control-Allow-Credentials", "true")
if c.PostForm("Mobile") == "" {
c.JSON(200, tools.ResponseError{
1,
"手机号不正确",
})
return
}
if c.PostForm("Location") == "" {
c.JSON(200, tools.ResponseError{
1,
"缺少位置信息",
})
return
}
//cacheCode := DB.Redis.Get("code_"+c.PostForm("Mobile"))
//if cacheCode != nil {
// c.JSON(200, tools.ResponseError{
// 1,
// "code没有过期",
// })
// return
//}
code := Lib.SmsCode(6)
client, err := dysmsapi.NewClientWithAccessKey("cn-hangzhou", "LTAI4FdQeNMQXRU6u5J3EFQc", "PwvyF5rRNBWLDya41WrCpvENevYZGi")
request := dysmsapi.CreateSendSmsRequest()
request.Scheme = "https"
request.PhoneNumbers = c.PostForm("Mobile")
request.SignName = "乐游图"
request.TemplateCode = "SMS_182595013"
request.TemplateParam = "{\"code\":\"" + string(code) + "\"}"
response, err := client.SendSms(request)
var reserr string
if err != nil {
println(err.Error())
reserr = err.Error()
} else {
reserr = response.Code
if response.Code == "OK" {
DB.Redis.Set("code_"+c.PostForm("Mobile"), code, time.Second*60*5)
}
}
var Location DB.SLocation
json.Unmarshal([]byte(c.PostForm("Location")), &Location)
//go func(res *dysmsapi.SendSmsResponse) {
DB.CSystemLog.InsertOne(tools.GetContext(),DB.SSystemLog{
"",
"",
c.PostForm("Mobile"),
6001,
"调用第三方发送短信接口",
time.Now().Unix(),
Location,
string(code),
reserr,
})
//}(response)
if response.Code == "OK" {
c.JSON(200, tools.ResponseSeccess{
0,
"ok",
})
} else {
c.JSON(200, tools.ResponseSeccess{
1,
"验证码发送失败",
})
}
}
func CreateAccessLog() {
}