|
|
@ -22,7 +22,6 @@ var (
|
|
|
|
cacheUsersIdPrefix = "cache#users#id#"
|
|
|
|
cacheUsersIdPrefix = "cache#users#id#"
|
|
|
|
cacheUsersOpenidPrefix = "cache#users#openid#"
|
|
|
|
cacheUsersOpenidPrefix = "cache#users#openid#"
|
|
|
|
cacheUsersPhonePrefix = "cache#users#phone#"
|
|
|
|
cacheUsersPhonePrefix = "cache#users#phone#"
|
|
|
|
cacheUsersSecretPrefix = "cache#users#secret#"
|
|
|
|
|
|
|
|
cacheUsersUidPrefix = "cache#users#uid#"
|
|
|
|
cacheUsersUidPrefix = "cache#users#uid#"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
@ -32,7 +31,6 @@ type (
|
|
|
|
FindOne(id int64) (*Users, error)
|
|
|
|
FindOne(id int64) (*Users, error)
|
|
|
|
FindOneByOpenid(openid string) (*Users, error)
|
|
|
|
FindOneByOpenid(openid string) (*Users, error)
|
|
|
|
FindOneByPhone(phone string) (*Users, error)
|
|
|
|
FindOneByPhone(phone string) (*Users, error)
|
|
|
|
FindOneBySecret(secret string) (*Users, error)
|
|
|
|
|
|
|
|
FindOneByUid(uid string) (*Users, error)
|
|
|
|
FindOneByUid(uid string) (*Users, error)
|
|
|
|
Update(data Users) error
|
|
|
|
Update(data Users) error
|
|
|
|
Delete(id int64) error
|
|
|
|
Delete(id int64) error
|
|
|
@ -45,14 +43,15 @@ type (
|
|
|
|
|
|
|
|
|
|
|
|
Users struct {
|
|
|
|
Users struct {
|
|
|
|
Id int64 `db:"id"`
|
|
|
|
Id int64 `db:"id"`
|
|
|
|
Uid string `db:"uid"` // 用户ID
|
|
|
|
Uid string `db:"uid"` // 用户ID
|
|
|
|
Openid string `db:"openid"` // 微信openid
|
|
|
|
Openid string `db:"openid"` // 微信openid
|
|
|
|
Secret string `db:"secret"` // secret
|
|
|
|
Secret string `db:"secret"` // secret
|
|
|
|
Phone string `db:"phone"` // 手机号
|
|
|
|
Phone string `db:"phone"` // 手机号
|
|
|
|
Avatar string `db:"avatar"` // 头像url
|
|
|
|
Password string `db:"password"` // 密码
|
|
|
|
Name string `db:"name"` // 昵称
|
|
|
|
Avatar string `db:"avatar"` // 头像url
|
|
|
|
Gender int64 `db:"gender"` // 性别
|
|
|
|
Name string `db:"name"` // 昵称
|
|
|
|
Score int64 `db:"score"` // 金币数量
|
|
|
|
Gender int64 `db:"gender"` // 性别
|
|
|
|
|
|
|
|
Score int64 `db:"score"` // 金币数量
|
|
|
|
CreateTime time.Time `db:"create_time"`
|
|
|
|
CreateTime time.Time `db:"create_time"`
|
|
|
|
UpdateTime time.Time `db:"update_time"`
|
|
|
|
UpdateTime time.Time `db:"update_time"`
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -68,12 +67,11 @@ func NewUsersModel(conn sqlx.SqlConn, c cache.CacheConf) UsersModel {
|
|
|
|
func (m *defaultUsersModel) Insert(data Users) (sql.Result, error) {
|
|
|
|
func (m *defaultUsersModel) Insert(data Users) (sql.Result, error) {
|
|
|
|
usersOpenidKey := fmt.Sprintf("%s%v", cacheUsersOpenidPrefix, data.Openid)
|
|
|
|
usersOpenidKey := fmt.Sprintf("%s%v", cacheUsersOpenidPrefix, data.Openid)
|
|
|
|
usersPhoneKey := fmt.Sprintf("%s%v", cacheUsersPhonePrefix, data.Phone)
|
|
|
|
usersPhoneKey := fmt.Sprintf("%s%v", cacheUsersPhonePrefix, data.Phone)
|
|
|
|
usersSecretKey := fmt.Sprintf("%s%v", cacheUsersSecretPrefix, data.Secret)
|
|
|
|
|
|
|
|
usersUidKey := fmt.Sprintf("%s%v", cacheUsersUidPrefix, data.Uid)
|
|
|
|
usersUidKey := fmt.Sprintf("%s%v", cacheUsersUidPrefix, data.Uid)
|
|
|
|
ret, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
|
|
|
|
ret, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
|
|
|
|
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?)", m.table, usersRowsExpectAutoSet)
|
|
|
|
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, usersRowsExpectAutoSet)
|
|
|
|
return conn.Exec(query, data.Uid, data.Openid, data.Secret, data.Phone, data.Avatar, data.Name, data.Gender, data.Score)
|
|
|
|
return conn.Exec(query, data.Uid, data.Openid, data.Secret, data.Phone, data.Password, data.Avatar, data.Name, data.Gender, data.Score)
|
|
|
|
}, usersOpenidKey, usersPhoneKey, usersSecretKey, usersUidKey)
|
|
|
|
}, usersOpenidKey, usersPhoneKey, usersUidKey)
|
|
|
|
return ret, err
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -134,26 +132,6 @@ func (m *defaultUsersModel) FindOneByPhone(phone string) (*Users, error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *defaultUsersModel) FindOneBySecret(secret string) (*Users, error) {
|
|
|
|
|
|
|
|
usersSecretKey := fmt.Sprintf("%s%v", cacheUsersSecretPrefix, secret)
|
|
|
|
|
|
|
|
var resp Users
|
|
|
|
|
|
|
|
err := m.QueryRowIndex(&resp, usersSecretKey, m.formatPrimary, func(conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
|
|
|
|
|
|
|
query := fmt.Sprintf("select %s from %s where `secret` = ? limit 1", usersRows, m.table)
|
|
|
|
|
|
|
|
if err := conn.QueryRow(&resp, query, secret); err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Id, nil
|
|
|
|
|
|
|
|
}, m.queryPrimary)
|
|
|
|
|
|
|
|
switch err {
|
|
|
|
|
|
|
|
case nil:
|
|
|
|
|
|
|
|
return &resp, nil
|
|
|
|
|
|
|
|
case sqlc.ErrNotFound:
|
|
|
|
|
|
|
|
return nil, ErrNotFound
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (m *defaultUsersModel) FindOneByUid(uid string) (*Users, error) {
|
|
|
|
func (m *defaultUsersModel) FindOneByUid(uid string) (*Users, error) {
|
|
|
|
usersUidKey := fmt.Sprintf("%s%v", cacheUsersUidPrefix, uid)
|
|
|
|
usersUidKey := fmt.Sprintf("%s%v", cacheUsersUidPrefix, uid)
|
|
|
|
var resp Users
|
|
|
|
var resp Users
|
|
|
@ -175,15 +153,14 @@ func (m *defaultUsersModel) FindOneByUid(uid string) (*Users, error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *defaultUsersModel) Update(data Users) error {
|
|
|
|
func (m *defaultUsersModel) Update(data Users) error {
|
|
|
|
usersUidKey := fmt.Sprintf("%s%v", cacheUsersUidPrefix, data.Uid)
|
|
|
|
|
|
|
|
usersIdKey := fmt.Sprintf("%s%v", cacheUsersIdPrefix, data.Id)
|
|
|
|
usersIdKey := fmt.Sprintf("%s%v", cacheUsersIdPrefix, data.Id)
|
|
|
|
usersOpenidKey := fmt.Sprintf("%s%v", cacheUsersOpenidPrefix, data.Openid)
|
|
|
|
usersOpenidKey := fmt.Sprintf("%s%v", cacheUsersOpenidPrefix, data.Openid)
|
|
|
|
usersPhoneKey := fmt.Sprintf("%s%v", cacheUsersPhonePrefix, data.Phone)
|
|
|
|
usersPhoneKey := fmt.Sprintf("%s%v", cacheUsersPhonePrefix, data.Phone)
|
|
|
|
usersSecretKey := fmt.Sprintf("%s%v", cacheUsersSecretPrefix, data.Secret)
|
|
|
|
usersUidKey := fmt.Sprintf("%s%v", cacheUsersUidPrefix, data.Uid)
|
|
|
|
_, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
|
|
|
|
_, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
|
|
|
|
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, usersRowsWithPlaceHolder)
|
|
|
|
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, usersRowsWithPlaceHolder)
|
|
|
|
return conn.Exec(query, data.Uid, data.Openid, data.Secret, data.Phone, data.Avatar, data.Name, data.Gender, data.Score, data.Id)
|
|
|
|
return conn.Exec(query, data.Uid, data.Openid, data.Secret, data.Phone, data.Password, data.Avatar, data.Name, data.Gender, data.Score, data.Id)
|
|
|
|
}, usersIdKey, usersOpenidKey, usersPhoneKey, usersSecretKey, usersUidKey)
|
|
|
|
}, usersUidKey, usersIdKey, usersOpenidKey, usersPhoneKey)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -195,13 +172,12 @@ func (m *defaultUsersModel) Delete(id int64) error {
|
|
|
|
|
|
|
|
|
|
|
|
usersOpenidKey := fmt.Sprintf("%s%v", cacheUsersOpenidPrefix, data.Openid)
|
|
|
|
usersOpenidKey := fmt.Sprintf("%s%v", cacheUsersOpenidPrefix, data.Openid)
|
|
|
|
usersPhoneKey := fmt.Sprintf("%s%v", cacheUsersPhonePrefix, data.Phone)
|
|
|
|
usersPhoneKey := fmt.Sprintf("%s%v", cacheUsersPhonePrefix, data.Phone)
|
|
|
|
usersSecretKey := fmt.Sprintf("%s%v", cacheUsersSecretPrefix, data.Secret)
|
|
|
|
|
|
|
|
usersUidKey := fmt.Sprintf("%s%v", cacheUsersUidPrefix, data.Uid)
|
|
|
|
usersUidKey := fmt.Sprintf("%s%v", cacheUsersUidPrefix, data.Uid)
|
|
|
|
usersIdKey := fmt.Sprintf("%s%v", cacheUsersIdPrefix, id)
|
|
|
|
usersIdKey := fmt.Sprintf("%s%v", cacheUsersIdPrefix, id)
|
|
|
|
_, err = m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
|
|
|
|
_, err = m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
|
|
|
|
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
|
|
|
|
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
|
|
|
|
return conn.Exec(query, id)
|
|
|
|
return conn.Exec(query, id)
|
|
|
|
}, usersIdKey, usersOpenidKey, usersPhoneKey, usersSecretKey, usersUidKey)
|
|
|
|
}, usersIdKey, usersOpenidKey, usersPhoneKey, usersUidKey)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|