Home >Backend Development >Golang >How to log in as golang user

How to log in as golang user

尚
Original
2019-12-16 14:58:033990browse

How to log in as golang user

The method for golang to implement user login registration is as follows:

The first step is to register models

Create models.go under models
models.go File

package models

import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
)

func RegisterDB() {

//注册驱动
orm.RegisterDriver("mysql", orm.DRMySQL)
//数据库链接
//注册默认数据库
var db_url string = beego.AppConfig.String("username_DB") + ":" + beego.AppConfig.String("password_DB") + "@tcp(" + beego.AppConfig.String("host_DB") + ")/" + beego.AppConfig.String("name_DB") + "?charset=" + beego.AppConfig.String("charset")
beego.Info(db_url)
orm.RegisterDataBase("default", "mysql", db_url)
// orm.RegisterDataBase("default", "mysql", "an:111@tcp(127.0.0.1:3306)/yoo_home?charset=utf8")
// //注册model
orm.RegisterModel(new(TUser))

}

The second step requires database connection

The app.conf file under conf

appname = an
httpport = 8080
runmode = dev
sessionon = true

#数据库为mysql
host_DB = "127.0.0.1"
port_DB = "3306"
charset = "utf8"
name_DB = "ancg"
username_DB = "an"
password_DB = 111

The third step writes a simple front-end view interface

## Create the client.html file under #views

<!DOCTYPE html>
<html>
<head>
<title>客户端接口测试</title>
</head>
<body>
<label>注册</label>
<form action="/client " method="POST">
<label>[options == register 注册]</label>
<div>options:<input type="text" value="register" name="options"></div>
<div>tel:<input type="text" name="Tel"></div>
<div>pwd:<input type="text" name="Pwd"></div>
<input type="submit" name="注册"Submit/>
</form>
<br>
<label>登录</label>
<form action="/client " method="POST">
<label>[options == login 登录]</label>
<div>options:<input type="text" value="login" name="options"></div>
<div>tel:<input type="text" name="Tel"></div>
<div>pwd:<input type="text" name="Pwd"></div>
<input type="submit" name="注册"Submit/>
</form>
</body>
</html>

The fourth step is to create TUser in models to automatically create tables for the database.

TUser.go

package models

import (
"github.com/astaxie/beego/orm"
//_"github.com/go-sql-driver/mysql"
)

//用户表
type TUser struct {
//用户序号
Id int64
//电话号码
Tep string
//密码
Pwd string
//收款人
Payee string
//地址
Address string
//收款帐号
Amount string
//账号类别
AmountType string
//是否消费者
IsCustomer bool
//是否商家
IsSeller bool
//是否配送员
IsDiliver bool
//是否管理员
IsManager bool
//微信openId
Vid string
//是否冻结
IsLock bool
//创建时间 --- 时间戳
AddTime int64
}

//新建用户
func AddUser(user *TUser) (int64, error) {
o := orm.NewOrm() //数据库
userId, err := o.Insert(user) //插入数据
return userId, err
}

//查询账号
func GetUserById(userId int64) (*TUser, error) {
o := orm.NewOrm() //数据库
user := new(TUser) //TUser就是第9行struct的数据库,就是mysql的表
qs := o.QueryTable("t_user") //表名为t_user
err := qs.Filter("id", userId).One(user) //One是指查询一条数据,One(user)是查询mysql表中一条数据
return user, err
}

//手机号查询账号
func GetUserByTel(tel string) (*TUser, error) {
o := orm.NewOrm()
user := new(TUser) //TUser就是第9行struct的数据库,就是mysql的表
qs := o.QueryTable("t_user") //表名为t_user
err := qs.Filter("tel", tel).One(user) //One是指查询一条数据,One(user)是查询mysql表中一条数据
return user, err
}

//微信Id查询账号
func GetUserByVid(vid int64) (*TUser, error) {
o := orm.NewOrm()
user := new(TUser) //TUser就是第9行struct的数据库,就是mysql的表
qs := o.QueryTable("t_user") //表名为t_user
err := qs.Filter("vid", vid).One(user) //One是指查询一条数据,One(user)是查询mysql表中一条数据
return user, err
}

The fifth step Create a file in controllers that connects to options, and use the corresponding options to call other controllers

client.go file

package controllers

import (
"github.com/astaxie/beego"
"time"
)

type ClientController struct {
beego.Controller
}

func (this *ClientController) Get() {
this.TplName = "client.html"
}

func (this *ClientController) Post() {
options := this.Input().Get("options")
beego.Info(options)
//请求检查方法
if options != "" {
switch options {
case "login":
this.login()
case "register":
this.register()
default:
this.Data["json"] = map[string]interface{}{"status": 400, "msg": "无对应处理方法!", "time": time.Now().Format("2006-12-12 12:12:12")}
this.ServeJSON()
return
}
this.Data["json"] = map[string]interface{}{"status": 400, "msg": "options为空", "time": time.Now().Format("2006-12-12 12:12:12")}
this.ServeJSON()
return

}

}

For more golang knowledge, please pay attention to the

golang tutorial column .

The above is the detailed content of How to log in as golang user. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn