parent
a1587fc4f1
commit
ce58f3e924
@ -0,0 +1,71 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/zggsong/gin-vue-demo/common"
|
||||
"github.com/zggsong/gin-vue-demo/model"
|
||||
"github.com/zggsong/gin-vue-demo/util"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func Register(ctx *gin.Context) {
|
||||
DB := common.GetDB()
|
||||
// 获取参数
|
||||
name := ctx.Query("name")
|
||||
telephone := ctx.Query("telephone")
|
||||
password := ctx.Query("password")
|
||||
|
||||
// 数据验证
|
||||
if len(telephone) != 11 {
|
||||
ctx.JSON(http.StatusUnprocessableEntity, gin.H{
|
||||
"code": http.StatusUnprocessableEntity,
|
||||
"message": "手机号必须是11位",
|
||||
})
|
||||
return
|
||||
}
|
||||
if len(password) < 6 {
|
||||
ctx.JSON(http.StatusUnprocessableEntity, gin.H{
|
||||
"code": http.StatusUnprocessableEntity,
|
||||
"message": "密码必须大于6位",
|
||||
})
|
||||
return
|
||||
}
|
||||
// 如果名字为空则返回10为随机字符串
|
||||
if name == "" {
|
||||
name = util.RandomString(10)
|
||||
}
|
||||
|
||||
log.Println(name, telephone, password)
|
||||
|
||||
// 判断手机号是否存在
|
||||
if isExistTelephone(DB, telephone) {
|
||||
ctx.JSON(http.StatusUnprocessableEntity, gin.H{
|
||||
"code": http.StatusUnprocessableEntity,
|
||||
"message": "手机号已存在",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 创建用户
|
||||
newUser := model.User{
|
||||
Name: name,
|
||||
Telephone: telephone,
|
||||
Password: password,
|
||||
}
|
||||
DB.Create(&newUser)
|
||||
|
||||
// 返回结果
|
||||
ctx.JSON(200, gin.H{
|
||||
"code": http.StatusOK,
|
||||
"message": "注册成功",
|
||||
})
|
||||
}
|
||||
|
||||
func isExistTelephone(db *gorm.DB, telephone string) bool {
|
||||
var user model.User
|
||||
db.Where("telephone = ?", telephone).First(&user)
|
||||
return user.ID != 0
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package model
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type User struct {
|
||||
gorm.Model
|
||||
Name string `json:"name";gorm:"type:varchar(20);not null`
|
||||
Telephone string `json:"telephone";gorm:"type:varchar(11);not null;unique"`
|
||||
Password string `json:"password";gorm:"type:varchar(20);not null"`
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/zggsong/gin-vue-demo/controller"
|
||||
)
|
||||
|
||||
func CollectRoute(r *gin.Engine) *gin.Engine {
|
||||
r.POST("/api/auth/register", controller.Register)
|
||||
return r
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 随机字符串
|
||||
func RandomString(length int) string {
|
||||
str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
bytes := []byte(str)
|
||||
result := []byte{}
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
for i := 0; i < length; i++ {
|
||||
result = append(result, bytes[r.Intn(len(bytes))])
|
||||
}
|
||||
return string(result)
|
||||
}
|
Loading…
Reference in new issue