这次迎来第一个项目实战 简介
主要功能:实现用户注册保存数据库,从数据库提取值判断输入的数值是否正确。一个简易的小项目,代码如下:
主程序main.go
package main
import (
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.LoadHTMLGlob("template/**/*")
//静态文件接口:/staticfile
r.Static("/staticfile", "staticfile")
//创建存储引擎
store := cookie.NewStore([]byte("session"))
/*页面请求路由组*/
//登陆页面
r.GET("/", LoginPageGet())
//登陆请求
r.POST("/", sessions.Sessions("s", store), LoginPagePost())
//注册页面
r.GET("/register", RegisterPageGet())
r.POST("/register", RegisterPagePost())
r.Run(":80")
}
响应接口
hand.go
package main
import (
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"net/http"
)
//登陆注册页面Get请求响应
func LoginPageGet() gin.HandlerFunc {
return func(c *gin.Context) {
c.HTML(http.StatusOK, "index/index.html", nil)
}
}
//登陆页面Post请求
func LoginPagePost() gin.HandlerFunc {
return func(c *gin.Context) {
//登陆流程
account := c.PostForm("account")
pwd := c.PostForm("pwd")
//数据库读取密码
passworld, id, root := Read_sql(account)
//判断账号密码是否正确
//设置session
if passworld == pwd {
//初始化session对象
session := sessions.Default(c)
//存储session Key
session.Set("status", "logined")
//账号
session.Set("account", account)
//账号id
session.Set("id", id)
//账号权限
session.Set("root", root)
err := session.Save()
err_ctrl(err, "session存储错误:")
c.JSON(http.StatusOK, gin.H{
"status": "true",
"next": "/task",
})
} else {
//返回登陆状态码
c.JSON(http.StatusOK, gin.H{
"status": "false",
})
}
}
}
//注册页面请求
func RegisterPageGet() gin.HandlerFunc {
return func(c *gin.Context) {
c.HTML(http.StatusOK, "index/register.html", nil)
}
}
验证
后续创建ajax组用的
ajax.go
package main
import (
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"io/ioutil"
"net/http"
)
//注册ajax请求验证账号
func CheckAjaxAccount() gin.HandlerFunc {
return func(c *gin.Context) {
account := c.PostForm("account")
var flage string
if RegistercheckAccount(account) {
flage = "true"
} else {
flage = "false"
}
c.JSON(http.StatusOK, gin.H{
"status": flage,
})
}
}
//提交注册信息
func RegisterPagePost() gin.HandlerFunc {
return func(c *gin.Context) {
//信息入库,重定向到登陆页面
account := c.PostForm("account")
password := c.PostForm("password")
name := c.PostForm("name")
tel := c.PostForm("tel")
qqnum := c.PostForm("QQnum")
gender := c.PostForm("gender")
birthtype := c.PostForm("birthtype")
birthday := c.PostForm("birthday")
drom := c.PostForm("drom")
college := c.PostForm("college")
//插入数据库
if InsertRegisterInfo(name, tel, qqnum, gender, birthtype, birthday, drom, college, account, password) {
c.JSON(http.StatusOK, gin.H{
"static": "true",
})
} else {
c.JSON(http.StatusOK, gin.H{
"static": "false",
})
}
}
}
数据库管理
数据库处理与连接
sql.go
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
//本地服务器连接串
//"数据库用户名:数据库密码@tcp(127.0.0.1)/库名
const sqladdrs = "root:12345678@tcp(127.0.0.1)/test"
//错误处理
func err_ctrl(err error, name string) {
if err != nil {
fmt.Println(name, err)
//停止执行
return
}
}
//登陆查询密码
func Read_sql(account string) (string, string, string) {
db, err := sql.Open("mysql", sqladdrs)
err_ctrl(err, "sql连接错误:")
defer db.Close()
row, err := db.Query("SELECT passwd,id,root FROM member WHERE account=?", account)
err_ctrl(err, "查询语句错误:")
var passwd, id, root string
for row.Next() {
row.Scan(&passwd, &id, &root)
}
return passwd, id, root
}
//验证注册ajax账号重复问题
func RegistercheckAccount(account string) bool {
db, err := sql.Open("mysql", sqladdrs)
err_ctrl(err, "sql连接错误")
defer db.Close()
row, err := db.Query("SELECT account FROM member WHERE account=?", account)
err_ctrl(err, "查询错误:")
var accounts string
for row.Next() {
row.Scan(&accounts)
}
flage := true
if accounts == "" {
flage = false
}
return flage
}
//注册信息插入数据库
func InsertRegisterInfo(name string, tel string, QQ_num string, gender string, birthType string, birthday string, drom string, college string, account string, passwd string) bool {
db, err := sql.Open("mysql", sqladdrs)
err_ctrl(err, "sql连接错误")
defer db.Close()
if gender == "man" {
gender = "男"
} else {
gender = "女"
}
if birthType == "1" {
birthType = "公历"
} else {
birthType = "农历"
}
if err != nil {
fmt.Println("插入错误:", err)
return false
} else {
return true
}
}
后记
这次的小项目学会了吗,多支持一下哦!
如果你觉得文章写的还不错,欢迎你帮助宣传扩散一下,这将是我继续更新的动力,切忌不要白嫖,会让自己变得廉价,好的文章值得分享。
请记得给我一个赞哦!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)