Browse Source

add gorm demo

TongYang 3 years ago
parent
commit
0f16a75afb
9 changed files with 80 additions and 5 deletions
  1. 1 1
      config/pro.yaml
  2. 1 1
      consts/session.go
  3. 5 0
      db/init.go
  4. 21 0
      db/user.go
  5. 22 0
      main_test.go
  6. 25 0
      model/gorm_model/user.go
  7. 2 2
      model/http_model/password_login.go
  8. 2 0
      route/init.go
  9. 1 1
      service/login_auth.go

+ 1 - 1
config/pro.yaml

@@ -15,4 +15,4 @@ server:
   host: 0.0.0.0
   port: 8300
   session:
-    ttl: 60 #seconds
+    ttl: 60 #minute

+ 1 - 1
consts/session.go

@@ -1,4 +1,4 @@
 package consts
 
 const SessionAuthSchema = "session_auth"
-const SessionRedisPrefix = "m_user:"
+const SessionRedisPrefix = "b_user:"

+ 5 - 0
db/init.go

@@ -1,6 +1,7 @@
 package db
 
 import (
+	"context"
 	"fmt"
 	"youngee_b_api/model/system_model"
 
@@ -19,3 +20,7 @@ func Init(config *system_model.Mysql) {
 	}
 	client = db
 }
+
+func GetReadDB(ctx context.Context) *gorm.DB {
+	return client.WithContext(ctx)
+}

+ 21 - 0
db/user.go

@@ -0,0 +1,21 @@
+package db
+
+import (
+	"context"
+	"youngee_b_api/model/gorm_model"
+
+	"gorm.io/gorm"
+)
+
+func GetUserByPhone(ctx context.Context, phone string) (*gorm_model.User, error) {
+	db := GetReadDB(ctx)
+	user := &gorm_model.User{}
+	err := db.Model(user).Where("phone = ?", phone).First(user).Error
+	if err != nil {
+		if err == gorm.ErrRecordNotFound {
+			return nil, nil
+		}
+		return nil, err
+	}
+	return user, nil
+}

+ 22 - 0
main_test.go

@@ -0,0 +1,22 @@
+package main
+
+import (
+	"context"
+	"fmt"
+	"testing"
+	"youngee_b_api/config"
+	"youngee_b_api/db"
+)
+
+func TestGormUser(t *testing.T) {
+	ctx := context.Background()
+	config.Init()
+	res, err := db.GetUserByPhone(ctx, "123")
+	if err != nil {
+		panic(err)
+	} else {
+		fmt.Printf("%+v", res)
+	}
+}
+
+//

+ 25 - 0
model/gorm_model/user.go

@@ -0,0 +1,25 @@
+// Code generated by sql2gorm. DO NOT EDIT.
+package gorm_model
+
+import (
+	"time"
+)
+
+type User struct {
+	ID            int       `gorm:"column:id;primary_key;AUTO_INCREMENT"` // 用户表id
+	User          string    `gorm:"column:user"`                          // 账号
+	Username      string    `gorm:"column:username"`                      // 后台用户名
+	Password      string    `gorm:"column:password"`                      // 用户密码
+	RealName      string    `gorm:"column:real_name"`                     // 真实姓名
+	Role          string    `gorm:"column:role"`                          // 角色 1,超级管理员; 2,管理员;3,企业用户
+	Phone         string    `gorm:"column:phone"`                         // 绑定手机
+	Email         string    `gorm:"column:email"`                         // 电子邮件
+	LastLoginTime time.Time `gorm:"column:last_login_time"`               // 最后一次登录时间
+	UserState     string    `gorm:"column:user_state"`                    // 0,禁用,1,正常
+	CreatedAt     time.Time `gorm:"column:created_at"`                    // 创建时间
+	UpdatedAt     time.Time `gorm:"column:updated_at"`                    // 更新时间
+}
+
+func (m *User) TableName() string {
+	return "user"
+}

+ 2 - 2
model/http_model/password_login.go

@@ -1,8 +1,8 @@
 package http_model
 
 type PasswordLoginRequest struct {
-	UserPhone  string `json:"user_phone" form:"user_phone"`
-	UserPasswd string `json:"user_password" form:"	"`
+	UserPhone  string `json:"user_phone"`
+	UserPasswd string `json:"user_password"`
 }
 
 //

+ 2 - 0
route/init.go

@@ -8,7 +8,9 @@ import (
 )
 
 func InitRoute(r *gin.Engine) {
+
 	r.POST("/login", handler.WrapPasswordLoginHandler)
+
 	m := r.Group("/youngee/m")
 	{
 		m.Use(middleware.LoginAuthMiddleware)

+ 1 - 1
service/login_auth.go

@@ -39,7 +39,7 @@ func (l *loginAuth) AuthToken(ctx context.Context, token string) (*redis_model.A
 
 }
 func (l *loginAuth) AuthPassword(ctx context.Context, phone, password string) *redis_model.Auth {
-	//需要添加mysql库认证逻辑
+	// 需要添加mysql库认证逻辑
 	// todo
 	auth := &redis_model.Auth{
 		Phone: phone,