This commit is contained in:
27
pkg/model/login.go
Normal file
27
pkg/model/login.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"drive-linked/pkg/dao"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const (
|
||||
LoginMethodName = "name"
|
||||
LoginMethodEmail = "email"
|
||||
)
|
||||
|
||||
type Login struct {
|
||||
ID int64
|
||||
Name string
|
||||
Password string
|
||||
}
|
||||
|
||||
func (u *Login) GetLoginInfo(account, method string) error {
|
||||
exec := fmt.Sprintf(`SELECT id, name, password FROM users WHERE %s=?`, method)
|
||||
|
||||
err := dao.DB.Get(u, exec, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
39
pkg/model/user.go
Normal file
39
pkg/model/user.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"drive-linked/pkg/dao"
|
||||
"fmt"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID int64 `json:"id,string"`
|
||||
Name string `json:"name"`
|
||||
Nickname string `json:"nickname"`
|
||||
Email string `json:"email"`
|
||||
Password string `json:"-"`
|
||||
Status int32 `json:"status"`
|
||||
Avatar string `json:"avatar"`
|
||||
Roles string `json:"roles"`
|
||||
}
|
||||
|
||||
func (user *User) GetProfileWithConditions(conditions *map[string]interface{}) error {
|
||||
// 支持多条件查询
|
||||
//TODO:分离多条件查询部分,有利于代码复用
|
||||
var where []string
|
||||
var values []interface{}
|
||||
|
||||
for k, v := range *conditions {
|
||||
values = append(values, v)
|
||||
where = append(where, fmt.Sprintf(`"%s" = %s`, k, "?"))
|
||||
}
|
||||
|
||||
exec := sqlx.Rebind(sqlx.QUESTION, "SELECT * FROM users WHERE "+strings.Join(where, "AND")+" LIMIT 1")
|
||||
err := dao.DB.Get(user, exec, values...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
22
pkg/model/user_test.go
Normal file
22
pkg/model/user_test.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"drive-linked/pkg/dao"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUser_GetWithConditions(t *testing.T) {
|
||||
dao.Conn()
|
||||
|
||||
var user User
|
||||
// 存在的用户
|
||||
conditions := &map[string]interface{}{
|
||||
"name": "eigeen",
|
||||
"email": "375109735@qq.com",
|
||||
}
|
||||
err := user.GetProfileWithConditions(conditions)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
t.Log(user)
|
||||
}
|
||||
Reference in New Issue
Block a user