内容简介:自己做记录,方便查看
自己做记录,方便查看
package main
import (
"fmt"
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
)
type User struct {
Id int `json:"id",gorm:"auto-increment"`
Name string `json:"name"`
Tel string `json:"tel"`
Password string `json:"password"`
}
type ResponseData struct {
Code int `json:"code"`
Status string `json:"status"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
var db *gorm.DB
var err error
func main() {
// db
db, err = gorm.Open("mysql", "root:03140314@/golang_test?charset=utf8&parseTime=True&loc=Local")
checkErr()
//db.SingularTable(true)
db.AutoMigrate(&User{})
defer db.Close()
// http
router := gin.Default()
v1 := router.Group("/api/v1")
{
// user
v1.POST("/user/login", login)
v1.POST("/user/register", register)
v1.GET("/user/get_info", getUserInfo)
}
router.Run()
}
func checkErr() {
if err != nil {
fmt.Println(err)
err = nil
}
}
// post form
func login(c *gin.Context) {
// 获取表单数据 tel,password
tel := c.PostForm("tel")
psd := c.PostForm("password")
if len(tel) == 0 || len(psd) == 0 {
response := ResponseData{
Code: 50001,
Status: "error",
Message: "账号或密码不能为空",
Data: "",
}
c.JSON(200, response)
} else {
var user User
db.Where("tel=?", tel).First(&user)
if user.Tel == "" {
response := ResponseData{
Code: 50001,
Status: "error",
Message: "用户不存在",
Data: "",
}
c.JSON(200, response)
} else {
if user.Password == psd {
response := ResponseData{
Code: 200,
Status: "success",
Message: "登录成功",
Data: "",
}
c.JSON(200, response)
} else {
response := ResponseData{
Code: 5001,
Status: "error",
Message: "密码错误",
Data: "",
}
c.JSON(200, response)
}
}
}
}
// post form
func register(c *gin.Context) {
tel := c.PostForm("tel")
psd := c.PostForm("password")
if len(tel) == 0 || len(psd) == 0 {
response := ResponseData{
Code: 50001,
Status: "error",
Message: "账号或密码不能为空",
Data: "",
}
c.JSON(200, response)
return
}
var user User
db.Where("tel=?", tel).First(&user)
if user.Tel == tel {
response := ResponseData{
Code: 50001,
Status: "error",
Message: "手机号已注册",
Data: "",
}
c.JSON(200, response)
} else {
newUser := User{Tel: tel, Password: psd}
db.Create(&newUser)
response := ResponseData{
Code: 200,
Status: "success",
Message: "注册成功",
Data: "",
}
c.JSON(200, response)
}
}
// get url
func getUserInfo(c *gin.Context) {
id := c.Query("id")
if id == "" {
response := ResponseData{
Code: 50001,
Status: "error",
Message: "参数错误",
Data: "",
}
c.JSON(200, response)
return
}
var user User
db.First(&user, id)
if user.Id > 0 {
response := ResponseData{
Code: 200,
Status: "success",
Message: "",
Data: user,
}
c.JSON(200, response)
} else {
response := ResponseData{
Code: 50001,
Status: "error",
Message: "用户不存在",
Data: "",
}
c.JSON(200, response)
}
}
以上所述就是小编给大家介绍的《Golang中gorm+gin的简单使用》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- RecyclerView使用指南(一)—— 基本使用
- 如何使用Meteorjs使用URL参数
- 使用 defer 还是不使用 defer?
- 使用 Typescript 加强 Vuex 使用体验
- [译] 何时使用 Rust?何时使用 Go?
- UDP协议的正确使用场合(谨慎使用)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Introduction to Linear Optimization
Dimitris Bertsimas、John N. Tsitsiklis / Athena Scientific / 1997-02-01 / USD 89.00
"The true merit of this book, however, lies in its pedagogical qualities which are so impressive..." "Throughout the book, the authors make serious efforts to give geometric and intuitive explanations......一起来看看 《Introduction to Linear Optimization》 这本书的介绍吧!