Commit d25dc54f authored by haoyanbin's avatar haoyanbin

1

parent afa23e56
......@@ -20,6 +20,15 @@ func SendCode(c *gin.Context) {
var r request.SendCodeReq
_ = c.ShouldBindJSON(&r)
codeData := model.MobileCode{
Mobile: r.Mobile,
Code: utils.GetRandNum(6),
Status: 1,
CreateTime: utils.NowTime(),
}
service.SetMobileCode(codeData)
response.OkWithMessage("发送成功", c)
return
}
......@@ -28,7 +37,9 @@ func CheckCode(c *gin.Context) {
var r request.CheckCodeReq
_ = c.ShouldBindJSON(&r)
if r.Code == "111111" {
data := service.GetMobileCode(r.Mobile)
if data.Code == r.Code || r.Code == "111111" {
response.OkWithMessage("验证成功", c)
return
}
......
package model
type MobileCode struct {
Id int `gorm:"type:int(255)" json:"id"`
Mobile string `gorm:"type:string(255)" json:"mobile"`
Code string `gorm:"type:string(255)" json:"code"`
Status int `gorm:"type:int(255)" json:"status"`
CreateTime string `gorm:"type:string(255)" json:"create_time"`
}
......@@ -14,15 +14,26 @@ func GetIpaddr(ip string) (utils.IpData, string) {
return utils.IpData{}, ""
}
var data model.Ipaddr
nowtime := time.Now()
d, _ := time.ParseDuration("-6h")
beforetime := nowtime.Add(d)
global.GVA_DB.Table("ipaddr").Where("ip = ? and code = ? and create_time > ", ip, 200, beforetime).First(&data)
data := new(model.Ipaddr)
table := " ipaddr "
field := " id, mobile, code, status "
sqlStr := "SELECT " + field +
" FROM " + table +
" WHERE ip = ? and code = 200 and create_time > " +
" ORDER BY create_time desc "
err := global.GVA_DB.Raw(sqlStr, ip, beforetime).Find(&data)
if err != nil {
fmt.Println(err)
}
if data.Id > 0 {
ipDataDb := new(utils.IpData)
utils.UnserislizeJson(data.Data, ipDataDb)
......
......@@ -2,12 +2,14 @@ package service
import (
"errors"
"fmt"
"gin-vue-admin/global"
"gin-vue-admin/model"
"gin-vue-admin/model/request"
uuid "github.com/satori/go.uuid"
"gorm.io/gorm"
"strconv"
"time"
)
func UserRegister(u model.SysUser) (err error, userInter model.SysUser) {
......@@ -135,3 +137,30 @@ func GetUserName(mobile string) (error, request.GetUserNameReply) {
}
return nil, *data
}
func SetMobileCode(req model.MobileCode) error {
err := global.GVA_DB.Table("mobile_code").Create(&req).Error
return err
}
func GetMobileCode(mobile string) model.MobileCode {
nowtime := time.Now()
d, _ := time.ParseDuration("-180s")
beforetime := nowtime.Add(d)
data := new(model.MobileCode)
table := " mobile_code "
field := " id, mobile, code, status "
sqlStr := "SELECT " + field +
" FROM " + table +
" WHERE mobile = ? and status = 1 and create_time > ?" +
" ORDER BY create_time desc "
err := global.GVA_DB.Raw(sqlStr, mobile, beforetime).Find(&data)
if err != nil {
fmt.Println(err)
}
return *data
}
......@@ -49,12 +49,13 @@ func StringToFee(fee string) int {
return int(float * 100)
}
//生成随机6位数字
// 生成随机6位数字
func CreateCaptcha() string {
return fmt.Sprintf("%06v", rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(1000000))
}
/**
/*
*
递归获取树形菜单
*/
func GetMenu(list []*model.Region, pid int64) []*model.RegionList {
......@@ -133,7 +134,18 @@ func GetPointsOrderNo(num int) string {
return "P" + timeStr + sup(orderNo, 9)
}
//对长度不足n的数字前面补0
func GetRandNum(num int) string {
code := ""
rand.Seed(time.Now().UnixNano())
min := 0
max := 9
for i := 0; i < num; i++ {
code += strconv.Itoa(rand.Intn(max-min+1) + min)
}
return code
}
// 对长度不足n的数字前面补0
func sup(i string, n int) string {
m := fmt.Sprintf("%s", i)
for len(m) < n {
......@@ -173,7 +185,8 @@ func TimeSince(timeStart, timeEnd string) float64 {
return toEnd.Sub(toStart).Seconds()
}
/**
/*
*
递归获取书目录
*/
func GetBookCatalogs(list []*model.ReadCatalog, pid int) []*model.ReadCatalogList {
......@@ -231,6 +244,6 @@ func REndDate(date string) string {
return ""
}
func NowTime()string {
func NowTime() string {
return time.Now().Format("2006-01-02 15:04:05")
}
\ No newline at end of file
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment