作者 yangfu

factory

package repository
import "sync"
var(
mutex sync.RWMutex
DefaultUserRepository IUserRepository
DefaultConfigRepository IConfigRepository
)
func init(){
DefaultUserRepository = UserRepositoryFactory()
}
func UserRepositoryFactory()IUserRepository{
if DefaultUserRepository==nil{
mutex.Lock()
defer mutex.Unlock()
if DefaultUserRepository==nil{
DefaultUserRepository = &UserRepository{}
}
}
return DefaultUserRepository
}
func ConfigRepositoryFactory()IConfigRepository{
if DefaultConfigRepository==nil{
mutex.Lock()
defer mutex.Unlock()
if DefaultConfigRepository==nil{
DefaultConfigRepository = &ConfigRepository{}
}
}
return DefaultConfigRepository
}
\ No newline at end of file
... ...
... ... @@ -44,9 +44,9 @@ func assertImplement() {
var (
//服务
sms s_sms.ISmsService = &s_sms.YunPianSmsService{}
ConfigRepository repository.IConfigRepository = &repository.ConfigRepository{}
//仓储
UserRepository repository.IUserRepository = &repository.UserRepository{}
//ConfigRepository repository.IConfigRepository = &repository.ConfigRepository{}
////仓储
//UserRepository repository.IUserRepository = &repository.UserRepository{}
)
//登录
... ... @@ -57,7 +57,7 @@ func (s *AuthService) Login(request *protocol.LoginRequest) (rsp *protocol.Login
result bool
checkImResponse *protocol.CheckImResponse
)
user, err = UserRepository.GetUsersByMobile(request.Phone)
user, err = repository.UserRepositoryFactory().GetUsersByMobile(request.Phone)
if err != nil {
log.Error(err)
err = protocol.NewErrWithMessage(2020, err) //账号不存在
... ... @@ -84,7 +84,7 @@ func (s *AuthService) Login(request *protocol.LoginRequest) (rsp *protocol.Login
}
Success:
{
userInfo, err = UserRepository.GetUserInfoByMobile(request.Phone)
userInfo, err = repository.UserRepositoryFactory().GetUserInfoByMobile(request.Phone)
if err != nil {
log.Error(err)
return
... ... @@ -107,7 +107,7 @@ Success:
userInfo.CsAccount = imGetRandomCSAccount()
}
userInfo.AuthExp = time.Now().Add(time.Second * protocol.TokenExpire)
if err = UserRepository.UpdateUserInfoById(userInfo); err != nil {
if err = repository.UserRepositoryFactory().UpdateUserInfoById(userInfo); err != nil {
return
}
rsp = &protocol.LoginResponse{AuthCode: userInfo.Auth}
... ... @@ -196,19 +196,19 @@ func (s *AuthService) AccessToken(request *protocol.AccessTokenRequest) (rsp *pr
var (
userInfo *models.UserInfo
)
_, err = ConfigRepository.GetCfgClient(request.ClientId, request.ClientSecret)
_, err = repository.ConfigRepositoryFactory().GetCfgClient(request.ClientId, request.ClientSecret)
if err != nil {
err = protocol.NewErrWithMessage(101, err)
return
}
userInfo, err = UserRepository.GetUserInfoByAuthCode(request.AuthCode)
userInfo, err = repository.UserRepositoryFactory().GetUserInfoByAuthCode(request.AuthCode)
if err != nil {
err = protocol.NewErrWithMessage(4139, err)
return
}
userInfo.AccessToken = uid.NewV1().StringNoDash()
userInfo.RefreshToken = uid.NewV1().StringNoDash()
if err = UserRepository.UpdateUserInfoById(userInfo); err != nil {
if err = repository.UserRepositoryFactory().UpdateUserInfoById(userInfo); err != nil {
log.Error(err)
return
}
... ... @@ -226,18 +226,18 @@ func (s *AuthService) RefreshToken(request *protocol.RefreshTokenRequest) (rsp *
var (
userInfo *models.UserInfo
)
_, err = ConfigRepository.GetCfgClient(request.ClientId, request.ClientSecret)
_, err = repository.ConfigRepositoryFactory().GetCfgClient(request.ClientId, request.ClientSecret)
if err != nil {
protocol.NewErrWithMessage(101, err)
return
}
userInfo, err = UserRepository.GetUserInfoByRefreshToken(request.RefreshToken)
userInfo, err = repository.UserRepositoryFactory().GetUserInfoByRefreshToken(request.RefreshToken)
if err != nil {
protocol.NewErrWithMessage(4139, err)
return
}
userInfo.AccessToken = uid.NewV1().StringNoDash()
if err = UserRepository.UpdateUserInfoById(userInfo); err != nil {
if err = repository.UserRepositoryFactory().UpdateUserInfoById(userInfo); err != nil {
return
}
rsp = &protocol.RefreshTokenResponse{
... ...
... ... @@ -10,8 +10,8 @@ import (
func init() {
tests.Init()
UserRepository = &repository.UserMockRepository{}
ConfigRepository = &repository.ConfigMockeRepository{}
repository.DefaultUserRepository = &repository.UserMockRepository{}
repository.DefaultConfigRepository = &repository.ConfigMockeRepository{}
}
var auth = AuthService{}
... ...