|
|
@@ -5,13 +5,16 @@ import (
|
|
|
"demo/configs"
|
|
|
"demo/router"
|
|
|
"demo/share"
|
|
|
+ "demo/util"
|
|
|
"fmt"
|
|
|
"github.com/gin-gonic/gin"
|
|
|
"github.com/mojocn/base64Captcha"
|
|
|
"github.com/spf13/cast"
|
|
|
"log"
|
|
|
+ "math/big"
|
|
|
"net/http"
|
|
|
"regexp"
|
|
|
+ "strconv"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
@@ -22,8 +25,10 @@ func UserRouth(engine *gin.RouterGroup) {
|
|
|
{
|
|
|
user.POST("/login", login)
|
|
|
user.POST("/register", register)
|
|
|
- user.POST("sendSms", SendVerificationCode)
|
|
|
+ user.POST("/registerCode", registerCode)
|
|
|
+ user.PUT("/name", SetNameValue)
|
|
|
|
|
|
+ user.POST("/sendSms", SendVerificationCode)
|
|
|
user.GET("/captcha", VerificationCode)
|
|
|
}
|
|
|
}
|
|
|
@@ -118,42 +123,219 @@ func login(c *gin.Context) {
|
|
|
c.JSON(http.StatusOK, router.CreateResultError(401, "用户密码错误!!!"))
|
|
|
}
|
|
|
}
|
|
|
-func register(c *gin.Context) {
|
|
|
- //data := share.GetJsonAnyParam(c)
|
|
|
- //var user configs.User
|
|
|
- //var err error
|
|
|
- //username, _ := data("username")
|
|
|
- //password, _ := data("password")
|
|
|
- //// 定义正则表达式
|
|
|
- //regexPattern := `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`
|
|
|
- //
|
|
|
- //// 编译正则表达式
|
|
|
- //reg, err := regexp.Compile(regexPattern)
|
|
|
- //if err != nil {
|
|
|
- // fmt.Println("Error compiling regex:", err)
|
|
|
- // return
|
|
|
- //}
|
|
|
- //判断是否为邮箱格式
|
|
|
- //matched := reg.MatchString(cast.ToString(username))
|
|
|
|
|
|
- //user, err = logins(cast.ToString(username), cast.ToString(password), matched)
|
|
|
+func registerCode(c *gin.Context) {
|
|
|
+ data := share.GetJsonAnyParam(c)
|
|
|
+ phone, _ := data("phone")
|
|
|
+ //判断是否为手机格式
|
|
|
+ if !checkPhone(c, cast.ToString(phone)) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //判断是否已经注册
|
|
|
+ b, err := existsPhone(cast.ToString(phone))
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println(err)
|
|
|
+ c.JSON(http.StatusOK, router.CreateResultError(200, "数据库错误!!!"))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if b {
|
|
|
+ c.JSON(http.StatusOK, router.CreateResultError(200, "手机号已注册!!!"))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //判断是否重复注册
|
|
|
+ get := configs.RedisDb.Exists(ctx, "PhoneVerificationCodeTime_"+cast.ToString(phone))
|
|
|
+ if get.Val() == 1 {
|
|
|
+ c.JSON(http.StatusOK, router.CreateResultError(200, "验证码已经发送,请等待"))
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ //发送短信
|
|
|
+ //sms := sendSms(b, cast.ToString(phone))
|
|
|
+ //TODO 模拟发送短信
|
|
|
+ sms := testSendSms()
|
|
|
+ fmt.Println("发送成功!!!:验证码为:", sms)
|
|
|
+ set := configs.RedisDb.Set(ctx, "PhoneVerificationCode_"+cast.ToString(phone), sms, 60*5*time.Second)
|
|
|
+ configs.RedisDb.Set(ctx, "PhoneVerificationCodeTime_"+cast.ToString(phone), time.Now(), 60*time.Second)
|
|
|
+ fmt.Println(set)
|
|
|
+ fmt.Println("发送成功!!!:验证码为:", sms)
|
|
|
+ c.JSON(http.StatusOK, router.CreateResult())
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+func testSendSms() string {
|
|
|
+ mins := big.NewInt(100000)
|
|
|
+ maxs := big.NewInt(999999)
|
|
|
+ randomNum := share.RandomInt(mins, maxs)
|
|
|
+ //if matched {
|
|
|
+ // //bools, err = configs.Engine.Table("user").Where("username = ?", username).Exist(&user)
|
|
|
+ //
|
|
|
+ // share.SendMail(username, strconv.Itoa(int(randomNum.Int64())))
|
|
|
//
|
|
|
- //if err == nil && user.Id != 0 {
|
|
|
- // fmt.Println("用户登录成功!!!")
|
|
|
- // token, err := share.GenerateToken(cast.ToString(user.Id))
|
|
|
- // if err == nil {
|
|
|
- // c.Header("auth-sign", token)
|
|
|
- // c.JSON(http.StatusOK, router.CreateResult())
|
|
|
- // } else {
|
|
|
- // c.JSON(http.StatusOK, router.CreateResultError(400, "生成token失败!!!"))
|
|
|
- // }
|
|
|
//} else {
|
|
|
- // fmt.Println("用户登录失败!!!")
|
|
|
- // c.JSON(http.StatusOK, router.CreateResultError(401, "用户密码错误!!!"))
|
|
|
+ // share.SendSms(strconv.Itoa(int(randomNum.Int64())), username)
|
|
|
//}
|
|
|
+ return randomNum.String()
|
|
|
+}
|
|
|
+
|
|
|
+func register(c *gin.Context) {
|
|
|
+
|
|
|
+ data := share.GetJsonAnyParam(c)
|
|
|
+
|
|
|
+ username, _ := data("username")
|
|
|
+ password, _ := data("password")
|
|
|
+ code, _ := data("code")
|
|
|
+ phone, _ := data("phone")
|
|
|
+
|
|
|
+ if !checkEmail(c, cast.ToString(username)) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if !checkPhone(c, cast.ToString(phone)) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if !checkCode(c, cast.ToString(code)) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if !checkPassword(c, cast.ToString(password)) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 验证验证码
|
|
|
+ get := configs.RedisDb.Get(ctx, "PhoneVerificationCode_"+cast.ToString(phone))
|
|
|
+ fmt.Println(code, get.Val())
|
|
|
+ if code != get.Val() {
|
|
|
+ c.JSON(http.StatusOK, router.CreateResultError(400, "验证码错误!"))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ configs.RedisDb.Del(ctx, "PhoneVerificationCode_"+cast.ToString(phone))
|
|
|
+
|
|
|
+ //判断是否已经注册
|
|
|
+ b, err := GetUserByPhoneOrEmail(cast.ToString(phone), cast.ToString(username))
|
|
|
+ if err != nil {
|
|
|
+ c.JSON(http.StatusOK, router.CreateResultError(400, "验证手机号及用户名,数据错误"))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if b {
|
|
|
+ c.JSON(http.StatusOK, router.CreateResultError(400, "手机号或邮箱已注册!!!"))
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ timestamp := time.Now().Unix()
|
|
|
+ user := configs.User{
|
|
|
+ Username: cast.ToString(username),
|
|
|
+ Password: cast.ToString(password),
|
|
|
+ Creation_time: int(timestamp),
|
|
|
+ Status: 0,
|
|
|
+ Role_id: 2,
|
|
|
+ Phone: cast.ToString(phone),
|
|
|
+ Email: cast.ToString(username),
|
|
|
+ Name: cast.ToString(username),
|
|
|
+ RecommendCode: fmt.Sprint(strconv.FormatInt(timestamp, 16), util.RandomArrays(13, 62)),
|
|
|
+ }
|
|
|
+
|
|
|
+ user, err = SaveNewUser(user)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println(err)
|
|
|
+ c.JSON(http.StatusOK, router.CreateResultError(400, "数据库错误!!!"))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if user.Id != 0 {
|
|
|
+ fmt.Println("用户注册成功!!!")
|
|
|
+ c.JSON(http.StatusOK, router.CreateResultData(map[string]any{"user": user}))
|
|
|
+ return
|
|
|
+ } else {
|
|
|
+ fmt.Println("用户注册失败!!!")
|
|
|
+ c.JSON(http.StatusOK, router.CreateResultError(400, "用户注册失败!!!"))
|
|
|
+ return
|
|
|
+ }
|
|
|
+}
|
|
|
+func SetNameValue(c *gin.Context) {
|
|
|
+ data := share.GetJsonAnyParam(c)
|
|
|
+ id, _ := data("id")
|
|
|
+ name, _ := data("name")
|
|
|
+ user, err := SetName(cast.ToInt(id), cast.ToString(name))
|
|
|
+ if err != nil {
|
|
|
+ c.JSON(http.StatusOK, router.CreateResultError(400, "数据库错误!!!"))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ fmt.Println("用户修改名字成功!!!")
|
|
|
+ c.JSON(http.StatusOK, router.CreateResultData(map[string]any{"user": user}))
|
|
|
+ return
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+func checkEmail(c *gin.Context, email string) bool {
|
|
|
+ //// 定义正则表达式
|
|
|
+ regexPattern := `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`
|
|
|
+ reg, err := regexp.Compile(regexPattern)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("Error compiling regex:", err)
|
|
|
+ c.JSON(500, router.CreateResultError(500, "邮箱格式错误!!!"))
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ //判断是否为邮箱格式
|
|
|
+ matched := reg.MatchString(cast.ToString(email))
|
|
|
+ if !matched {
|
|
|
+ fmt.Println("Error compiling regex:", err)
|
|
|
+ c.JSON(500, router.CreateResultError(500, "邮箱格式错误!!!"))
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ return true
|
|
|
+}
|
|
|
+func checkPhone(c *gin.Context, phone string) bool {
|
|
|
+ //// 定义正则表达式
|
|
|
+ regexPattern := `^1[3-9]\d{9}$`
|
|
|
+ reg, err := regexp.Compile(regexPattern)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("1 Error compiling regex:", err)
|
|
|
+ c.JSON(200, router.CreateResultError(500, "手机号格式错误!!!"))
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ matched := reg.MatchString(cast.ToString(phone))
|
|
|
+ if !matched {
|
|
|
+ fmt.Println("2 Error compiling regex:", err)
|
|
|
+ c.JSON(200, router.CreateResultError(500, "手机号格式错误!!!"))
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ return true
|
|
|
+}
|
|
|
+func checkPassword(c *gin.Context, email string) bool {
|
|
|
+ //// 定义正则表达式
|
|
|
+ regexPattern := `^[a-zA-Z0-9,./<>?;'\\:"|\[\]{}~!@#$%^&*()_+=-]{6,32}$`
|
|
|
+ reg, err := regexp.Compile(regexPattern)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("Error compiling regex:", err)
|
|
|
+ c.JSON(500, router.CreateResultError(500, "密码格式错误!!!"))
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ //判断是否为邮箱格式
|
|
|
+ matched := reg.MatchString(cast.ToString(email))
|
|
|
+ if !matched {
|
|
|
+ fmt.Println("Error compiling regex:", err)
|
|
|
+ c.JSON(500, router.CreateResultError(500, "密码格式错误!!!"))
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ return true
|
|
|
+}
|
|
|
+func checkCode(c *gin.Context, code string) bool {
|
|
|
+ //// 定义正则表达式
|
|
|
+ regexPattern := `^\d{6}$`
|
|
|
+ reg, err := regexp.Compile(regexPattern)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("Error compiling regex:", err)
|
|
|
+ c.JSON(500, router.CreateResultError(500, "验证码格式错误!!!"))
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ //判断是否为邮箱格式
|
|
|
+ matched := reg.MatchString(cast.ToString(code))
|
|
|
+ if !matched {
|
|
|
+ fmt.Println("Error compiling regex:", err)
|
|
|
+ c.JSON(500, router.CreateResultError(500, "验证码格式错误!!!"))
|
|
|
+ return false
|
|
|
+ }
|
|
|
+
|
|
|
+ return true
|
|
|
}
|
|
|
|
|
|
-// SendVerificationCode
|
|
|
+// SendVerificationCode 发送验证码
|
|
|
func SendVerificationCode(c *gin.Context) {
|
|
|
data := share.GetJsonAnyParam(c)
|
|
|
var err error
|