123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- package controllers
- import (
- "bytes"
- "encoding/json"
- "fmt"
- "github.com/gin-gonic/gin"
- "gopkg"
- "io/ioutil"
- "lzyd-user-api/common"
- "lzyd-user-api/models"
- "lzyd-user-api/services"
- "lzyd-user-api/utils"
- "net/http"
- "strconv"
- "time"
- )
- // @tags Sms
- // @Summary 获取短信验证码
- // @Description 获取短信验证码
- // @Produce json
- // @Param mobile query string true "手机号码"
- // @Param type query int true "短信验证码类型(1=注册登录)" Enums(1, 2, 3, 4) default(1)
- // @Success 200 {string} json "{"code":200, "message":"success"}"
- // @Router /sms/get [get]
- // @Security ApiKeyAuth
- func GetSms(c *gin.Context) {
- mobile := c.Query("mobile")
- smsType, _ := strconv.Atoi(c.Query("type"))
- tokenStr := c.GetHeader("x-app-id")
- if tokenStr != "lzyd" {
- common.GenMessageResp(c, common.InvalidParametes, "非法操作", nil)
- return
- }
- // 先校验手机号与短信类型
- if !gopkg.ValidMobilePhone(mobile) {
- common.GenMessageResp(c, common.InvalidParametes, "无效的手机号码", nil)
- return
- }
- if smsType < 1 || smsType > 5 {
- common.GenMessageResp(c, common.InvalidParametes, "不支持的短信类型", nil)
- return
- }
- // 校验手机号是否已被注册, 已被注册的手机号不下发短信
- //if smsType == 1 {
- // if services.ExistedMobile(mobile) {
- // common.GenMessageResp(c, common.InvalidParametes, "手机号码已被注册", nil)
- // return
- // }
- //}
- // 限制发送总量
- count := services.CountOfToday(common.DB, "lzyd_sms_valid")
- fmt.Println(count)
- if count > 5000 {
- common.GenMessageResp(c, 500, "超过短信限制", nil)
- return
- }
- // 先查看该手机号码的最后一条短信是否在一分钟分钟内
- var sms models.ValidSms
- sms = services.GetLastValidSms(common.DB, mobile, smsType)
- if time.Now().Unix()-common.SmsValidSeconds < sms.CreateAt {
- common.GenResp(c, -1, "发送频繁", nil)
- return
- }
- // 调用模板发送短信
- validCode, err := services.SendSingleSms(mobile, smsType)
- if err != nil {
- common.GenMessageResp(c, 500, err.Error(), nil)
- return
- }
- // 发送成功后插入数据
- sms.Mobile = mobile
- sms.ValidCode = validCode
- sms.SmsType = smsType
- if err := sms.Save(common.DB); err != nil {
- common.GenMessageResp(c, -1, err.Error(), nil)
- return
- }
- common.GenResp(c, 200, "SUCCESS", nil)
- }
- // @tags Sms
- // @Summary 发送提醒短信
- // @Description 发送提醒短信
- // @Accept json
- // @Produce json
- // @Param body body models.SendSmsReq true "短信请求内容"
- // @Success 200 {string} json "{"code":200, "message":"success"}"
- // @Router /sms/remind [post]
- // @Security ApiKeyAuth
- func SendRemindSms(c *gin.Context) {
- tokenStr := c.GetHeader("x-app-id")
- fmt.Println(tokenStr)
- if tokenStr != "lzyd" {
- common.GenMessageResp(c, common.InvalidParametes, "非法操作", nil)
- return
- }
- var req models.SendSmsReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- panic(err)
- }
- // 限制发送总量
- count := services.CountOfToday(common.DB, "sms_remind_info")
- if count > 2000 {
- common.GenMessageResp(c, -1, "超过下发次数", nil)
- return
- }
- timeUnix := time.Now().Unix()
- random := strconv.FormatInt(timeUnix, 10)
- url := common.SdkSmsUrl + "?sdkappid=" + common.SdkAppId + "&random=" + random
- // 请求包体
- var templ models.SmsTempl
- templ.Ext = ""
- templ.Extend = ""
- templ.Params = []string{req.Content}
- templ.Sig = utils.CalculateSigForTempl(common.SdkAppKey, random, []string{req.Mobile}, timeUnix)
- templ.Sign = ""
- templ.Time = timeUnix
- templ.TplID = common.SdkTemplId
- templ.Tel.NationCode = "86"
- templ.Tel.Mobile = req.Mobile
- // 请求腾讯云短信API
- jsonBytes, _ := json.Marshal(templ)
- resp, err := http.Post(url, "application/x-www-form-urlencoded", bytes.NewReader(jsonBytes))
- if err != nil {
- common.GenResp(c, 500, err.Error(), nil)
- return
- }
- defer func() {
- _ = resp.Body.Close()
- }()
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- common.GenResp(c, 500, err.Error(), nil)
- return
- }
- // 发送成功后插入数据
- sms := &models.RemindSms{
- Mobile: req.Mobile,
- SmsType: 1,
- Content: req.Content,
- IpAddr: gopkg.GetRemoteIP(c.Request),
- }
- if err := sms.Save(common.DB); err != nil {
- common.GenMessageResp(c, 500, err.Error(), nil)
- return
- }
- //fmt.Println(string(body))
- var smsResp models.SmsTemplResp
- _ = json.Unmarshal(body, &smsResp)
- if smsResp.Result == 0 {
- common.GenResp(c, common.Success, "SUCCESS", nil)
- } else {
- common.GenResp(c, 500, smsResp.ErrMsg, nil)
- }
- }
|