作者 唐旭辉
@@ -19,4 +19,6 @@ @@ -19,4 +19,6 @@
19 19
20 *.tmp 20 *.tmp
21 21
22 -*.sum  
  22 +*.sum
  23 +
  24 +ability
1 [dev] 1 [dev]
2 #数据库相关 2 #数据库相关
3 -data_source = "root:123456@tcp(192.168.100.102:3306)/ability_display" 3 +data_source = "root:123456@tcp(192.168.100.102:3306)/ability_display?loc=Local"
4 #data_source = "root:sutianxia2015@tcp(115.29.205.99:3306)/ability_display" 4 #data_source = "root:sutianxia2015@tcp(115.29.205.99:3306)/ability_display"
5 5
6 6
1 [prod] 1 [prod]
2 #数据库相关 2 #数据库相关
3 -data_source = "root:123456@tcp(127.0.0.1:3306)/ability_display" 3 +data_source = "root:123456@tcp(127.0.0.1:3306)/ability_display?loc=Local"
4 4
5 5
6 #redis相关配置 6 #redis相关配置
@@ -5,6 +5,7 @@ import ( @@ -5,6 +5,7 @@ import (
5 "log" 5 "log"
6 "reflect" 6 "reflect"
7 "testing" 7 "testing"
  8 + "time"
8 9
9 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego" 10 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
10 "gitlab.fjmaimaimai.com/mmm-go/gocomm/common" 11 "gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
@@ -42,3 +43,7 @@ func Benchmark_GenMessage(b *testing.B) { @@ -42,3 +43,7 @@ func Benchmark_GenMessage(b *testing.B) {
42 } 43 }
43 } 44 }
44 } 45 }
  46 +
  47 +func TestXXX(t *testing.T){
  48 + t.Log(time.Now())
  49 +}
  1 +package repository
  2 +
  3 +import "ability/models"
  4 +
  5 +type IConfigRepository interface {
  6 + GetCfgClient(clintId, clientSecret string) (v *models.CfgClient, err error)
  7 +}
  8 +
  9 +type ConfigRepository struct{}
  10 +
  11 +func (r *ConfigRepository) GetCfgClient(clintId, clientSecret string) (v *models.CfgClient, err error) {
  12 + return models.GetCfgClient(clintId, clientSecret)
  13 +}
  1 +package repository
  2 +
  3 +import "ability/models"
  4 +
  5 +type ConfigMockeRepository struct{}
  6 +
  7 +func (r *ConfigMockeRepository) GetCfgClient(clintId, clientSecret string) (v *models.CfgClient, err error) {
  8 + return
  9 +}
@@ -5,6 +5,9 @@ import "ability/models" @@ -5,6 +5,9 @@ import "ability/models"
5 type IUserRepository interface { 5 type IUserRepository interface {
6 GetUsersByMobile(mobile string) (v *models.Users, err error) 6 GetUsersByMobile(mobile string) (v *models.Users, err error)
7 GetUserInfoByMobile(mobile string) (v *models.UserInfo, err error) 7 GetUserInfoByMobile(mobile string) (v *models.UserInfo, err error)
  8 + UpdateUserInfoById(m *models.UserInfo) (err error)
  9 + GetUserInfoByAuthCode(authCode string) (v *models.UserInfo, err error)
  10 + GetUserInfoByRefreshToken(refreshToken string) (v *models.UserInfo, err error)
8 } 11 }
9 12
10 func assertImplement() { 13 func assertImplement() {
@@ -20,3 +23,15 @@ func (r *UserRepository) GetUsersByMobile(mobile string) (v *models.Users, err e @@ -20,3 +23,15 @@ func (r *UserRepository) GetUsersByMobile(mobile string) (v *models.Users, err e
20 func (r *UserRepository) GetUserInfoByMobile(mobile string) (v *models.UserInfo, err error) { 23 func (r *UserRepository) GetUserInfoByMobile(mobile string) (v *models.UserInfo, err error) {
21 return models.GetUserInfoByMobile(mobile) 24 return models.GetUserInfoByMobile(mobile)
22 } 25 }
  26 +
  27 +func (r *UserRepository) UpdateUserInfoById(m *models.UserInfo) (err error) {
  28 + return models.UpdateUserInfoById(m)
  29 +}
  30 +
  31 +func (r *UserRepository) GetUserInfoByAuthCode(authCode string) (v *models.UserInfo, err error) {
  32 + return models.GetUserInfoByAuthCode(authCode)
  33 +}
  34 +
  35 +func (r *UserRepository)GetUserInfoByRefreshToken(refreshToken string) (v *models.UserInfo, err error) {
  36 + return models.GetUserInfoByRefreshToken(refreshToken)
  37 +}
  1 +package repository
  2 +
  3 +import "ability/models"
  4 +
  5 +type UserMockRepository struct{}
  6 +
  7 +var userInfo =&models.UserInfo{
  8 + Auth:"897ca746f46b11e98771000c29ad8d6d",
  9 + AccessToken:"897ca746f46b11e98771000c29ad8d7d",
  10 + RefreshToken:"897ca746f46b11e98771000c29ad8d8d",
  11 +}
  12 +
  13 +func (r *UserMockRepository) GetUsersByMobile(mobile string) (v *models.Users, err error) {
  14 + v =&models.Users{
  15 + Username:"18065048389",
  16 + Password:"$2y$10$YWg7jPRVLBzc3kevokMkW.boswtCvhToqC.TappIwfqwJ.cI0efvy",
  17 + }
  18 + return
  19 +}
  20 +
  21 +func (r *UserMockRepository) GetUserInfoByMobile(mobile string) (v *models.UserInfo, err error) {
  22 + v =userInfo
  23 + return
  24 +}
  25 +
  26 +func (r *UserMockRepository) UpdateUserInfoById(m *models.UserInfo) (err error) {
  27 + return
  28 +}
  29 +
  30 +func (r *UserMockRepository) GetUserInfoByAuthCode(authCode string) (v *models.UserInfo, err error) {
  31 + v =userInfo
  32 + return
  33 +}
  34 +
  35 +func (r *UserMockRepository)GetUserInfoByRefreshToken(refreshToken string) (v *models.UserInfo, err error) {
  36 + v =userInfo
  37 + return
  38 +}
@@ -6,13 +6,16 @@ import ( @@ -6,13 +6,16 @@ import (
6 _ "ability/routers" 6 _ "ability/routers"
7 "github.com/astaxie/beego" 7 "github.com/astaxie/beego"
8 _ "github.com/go-sql-driver/mysql" 8 _ "github.com/go-sql-driver/mysql"
  9 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
9 "gitlab.fjmaimaimai.com/mmm-go/gocomm/config" 10 "gitlab.fjmaimaimai.com/mmm-go/gocomm/config"
10 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" 11 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
11 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/orm" 12 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/orm"
12 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/redis" 13 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/redis"
  14 + "time"
13 ) 15 )
14 16
15 func init() { 17 func init() {
  18 + time.Local = time.FixedZone("CST", 3600*8)
16 log.InitLog(config.Logger{ 19 log.InitLog(config.Logger{
17 Filename: "app.log", 20 Filename: "app.log",
18 Level: "7", 21 Level: "7",
@@ -27,6 +30,14 @@ func init() { @@ -27,6 +30,14 @@ func init() {
27 MaxIdle: 100, 30 MaxIdle: 100,
28 MaxOpen: 100, 31 MaxOpen: 100,
29 }) 32 })
  33 + //TODO:邮件服务配置
  34 + common.InitMailService(&common.MailConfig{
  35 + //Host:"smtp.qq.com",
  36 + //Port:465,
  37 + //From:"785410885@qq.com",
  38 + //Password:"ibfduqhfmgypbffe", //授权码
  39 + //IsUseSsl:true,
  40 + })
30 } 41 }
31 42
32 func main() { 43 func main() {
1 -package routers  
2 -  
3 -import (  
4 - "github.com/astaxie/beego"  
5 - "github.com/astaxie/beego/context/param"  
6 -)  
7 -  
8 -func init() {  
9 -  
10 - beego.GlobalControllerRouter["ability/controllers/v1:AuthController"] = append(beego.GlobalControllerRouter["ability/controllers/v1:AuthController"],  
11 - beego.ControllerComments{  
12 - Method: "AccessToken",  
13 - Router: `/accessToken`,  
14 - AllowHTTPMethods: []string{"post"},  
15 - MethodParams: param.Make(),  
16 - Params: nil})  
17 -  
18 - beego.GlobalControllerRouter["ability/controllers/v1:AuthController"] = append(beego.GlobalControllerRouter["ability/controllers/v1:AuthController"],  
19 - beego.ControllerComments{  
20 - Method: "Login",  
21 - Router: `/login`,  
22 - AllowHTTPMethods: []string{"post"},  
23 - MethodParams: param.Make(),  
24 - Params: nil})  
25 -  
26 - beego.GlobalControllerRouter["ability/controllers/v1:AuthController"] = append(beego.GlobalControllerRouter["ability/controllers/v1:AuthController"],  
27 - beego.ControllerComments{  
28 - Method: "RefreshToken",  
29 - Router: `/refreshToken`,  
30 - AllowHTTPMethods: []string{"post"},  
31 - MethodParams: param.Make(),  
32 - Params: nil})  
33 -  
34 - beego.GlobalControllerRouter["ability/controllers/v1:AuthController"] = append(beego.GlobalControllerRouter["ability/controllers/v1:AuthController"],  
35 - beego.ControllerComments{  
36 - Method: "SmsCode",  
37 - Router: `/smsCode`,  
38 - AllowHTTPMethods: []string{"post"},  
39 - MethodParams: param.Make(),  
40 - Params: nil})  
41 -  
42 - beego.GlobalControllerRouter["ability/controllers/v1:AuthController"] = append(beego.GlobalControllerRouter["ability/controllers/v1:AuthController"],  
43 - beego.ControllerComments{  
44 - Method: "UpdateDevice",  
45 - Router: `/updateDevice`,  
46 - AllowHTTPMethods: []string{"post"},  
47 - MethodParams: param.Make(),  
48 - Params: nil})  
49 -  
50 - beego.GlobalControllerRouter["ability/controllers/v1:UploadController"] = append(beego.GlobalControllerRouter["ability/controllers/v1:UploadController"],  
51 - beego.ControllerComments{  
52 - Method: "Image",  
53 - Router: `/image`,  
54 - AllowHTTPMethods: []string{"post"},  
55 - MethodParams: param.Make(),  
56 - Params: nil})  
57 -  
58 - beego.GlobalControllerRouter["ability/controllers/v1:UploadController"] = append(beego.GlobalControllerRouter["ability/controllers/v1:UploadController"],  
59 - beego.ControllerComments{  
60 - Method: "Voice",  
61 - Router: `/voice`,  
62 - AllowHTTPMethods: []string{"post"},  
63 - MethodParams: param.Make(),  
64 - Params: nil})  
65 -  
66 - beego.GlobalControllerRouter["ability/controllers/v1:UserController"] = append(beego.GlobalControllerRouter["ability/controllers/v1:UserController"],  
67 - beego.ControllerComments{  
68 - Method: "Login",  
69 - Router: `/v1/user/login`,  
70 - AllowHTTPMethods: []string{"post"},  
71 - MethodParams: param.Make(),  
72 - Params: nil})  
73 -  
74 -}  
@@ -40,7 +40,7 @@ func assertImplement() { @@ -40,7 +40,7 @@ func assertImplement() {
40 var ( 40 var (
41 //服务 41 //服务
42 sms s_sms.ISmsService = &s_sms.YunPianSmsService{} 42 sms s_sms.ISmsService = &s_sms.YunPianSmsService{}
43 - 43 + ConfigRepository repository.IConfigRepository = &repository.ConfigRepository{}
44 //仓储 44 //仓储
45 UserRepository repository.IUserRepository = &repository.UserRepository{} 45 UserRepository repository.IUserRepository = &repository.UserRepository{}
46 ) 46 )
@@ -87,7 +87,9 @@ Success: @@ -87,7 +87,9 @@ Success:
87 if userInfo.Auth == "" { 87 if userInfo.Auth == "" {
88 userInfo.Auth = uid.NewV1().StringNoDash() 88 userInfo.Auth = uid.NewV1().StringNoDash()
89 } 89 }
90 - if err = models.UpdateUserInfoById(userInfo); err != nil { 90 + //TODO:更新
  91 + userInfo.AuthExp = time.Now().Add(time.Second*protocol.TokenExpire)
  92 + if err = UserRepository.UpdateUserInfoById(userInfo); err != nil {
91 return 93 return
92 } 94 }
93 rsp = &protocol.LoginResponse{AuthCode: userInfo.Auth} 95 rsp = &protocol.LoginResponse{AuthCode: userInfo.Auth}
@@ -106,19 +108,20 @@ func (s *AuthService) AccessToken(request *protocol.AccessTokenRequest) (rsp *pr @@ -106,19 +108,20 @@ func (s *AuthService) AccessToken(request *protocol.AccessTokenRequest) (rsp *pr
106 var ( 108 var (
107 userInfo *models.UserInfo 109 userInfo *models.UserInfo
108 ) 110 )
109 - _, err = models.GetCfgClient(request.ClientId, request.ClientSecret) 111 + _, err = ConfigRepository.GetCfgClient(request.ClientId, request.ClientSecret)
110 if err != nil { 112 if err != nil {
111 - common.NewError(101, err) 113 + err = common.NewError(101, err)
112 return 114 return
113 } 115 }
114 - userInfo, err = models.GetUserInfoByAuthCode(request.AuthCode) 116 + userInfo, err = UserRepository.GetUserInfoByAuthCode(request.AuthCode)
115 if err != nil { 117 if err != nil {
116 - common.NewError(4139, err) 118 + err = common.NewError(4139, err)
117 return 119 return
118 } 120 }
119 userInfo.AccessToken = uid.NewV1().StringNoDash() 121 userInfo.AccessToken = uid.NewV1().StringNoDash()
120 userInfo.RefreshToken = uid.NewV1().StringNoDash() 122 userInfo.RefreshToken = uid.NewV1().StringNoDash()
121 - if err = models.UpdateUserInfoById(userInfo); err != nil { 123 + if err = UserRepository.UpdateUserInfoById(userInfo); err != nil {
  124 + log.Error(err)
122 return 125 return
123 } 126 }
124 //valid token 127 //valid token
@@ -134,25 +137,24 @@ func (s *AuthService) AccessToken(request *protocol.AccessTokenRequest) (rsp *pr @@ -134,25 +137,24 @@ func (s *AuthService) AccessToken(request *protocol.AccessTokenRequest) (rsp *pr
134 func (s *AuthService) RefreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.RefreshTokenResponse, err error) { 137 func (s *AuthService) RefreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.RefreshTokenResponse, err error) {
135 var ( 138 var (
136 userInfo *models.UserInfo 139 userInfo *models.UserInfo
137 - newAccess *protocol.Access  
138 ) 140 )
139 - _, err = models.GetCfgClient(request.ClientId, request.ClientSecret) 141 + _, err = ConfigRepository.GetCfgClient(request.ClientId, request.ClientSecret)
140 if err != nil { 142 if err != nil {
141 common.NewError(101, err) 143 common.NewError(101, err)
142 return 144 return
143 } 145 }
144 - userInfo, err = models.GetUserInfoByAuthCode(request.RefreshToken) 146 + userInfo, err = UserRepository.GetUserInfoByRefreshToken(request.RefreshToken)
145 if err != nil { 147 if err != nil {
146 common.NewError(4139, err) 148 common.NewError(4139, err)
147 return 149 return
148 } 150 }
149 userInfo.AccessToken = uid.NewV1().StringNoDash() 151 userInfo.AccessToken = uid.NewV1().StringNoDash()
150 - if err = models.UpdateUserInfoById(userInfo); err != nil { 152 + if err = UserRepository.UpdateUserInfoById(userInfo); err != nil {
151 return 153 return
152 } 154 }
153 rsp = &protocol.RefreshTokenResponse{ 155 rsp = &protocol.RefreshTokenResponse{
154 - AccessToken: newAccess.AccessToken,  
155 - RefreshToken: newAccess.RefreshToken, 156 + AccessToken: userInfo.AccessToken,
  157 + RefreshToken: userInfo.RefreshToken,
156 ExpiresIn: protocol.TokenExpire, 158 ExpiresIn: protocol.TokenExpire,
157 } 159 }
158 return 160 return
1 package auth 1 package auth
2 2
3 import ( 3 import (
  4 + "ability/internal/repository"
4 "testing" 5 "testing"
5 6
6 "ability/protocol" 7 "ability/protocol"
@@ -9,8 +10,12 @@ import ( @@ -9,8 +10,12 @@ import (
9 10
10 func init() { 11 func init() {
11 tests.Init() 12 tests.Init()
  13 + UserRepository = &repository.UserMockRepository{}
  14 + ConfigRepository = &repository.ConfigMockeRepository{}
12 } 15 }
13 16
  17 +var auth =AuthService{}
  18 +
14 func Test_SmsCode(t *testing.T) { 19 func Test_SmsCode(t *testing.T) {
15 var ( 20 var (
16 resp *protocol.SmsCodeResponse 21 resp *protocol.SmsCodeResponse
@@ -35,3 +40,35 @@ func Test_SmsCode(t *testing.T) { @@ -35,3 +40,35 @@ func Test_SmsCode(t *testing.T) {
35 } 40 }
36 } 41 }
37 } 42 }
  43 +
  44 +func Test_Login(t *testing.T){
  45 + login:=&protocol.LoginRequest{
  46 + Phone:"18065048389",
  47 + Code:"562246",
  48 + GrantType:"signInPassword",
  49 + PassWord:"$2y$10$YWg7jPRVLBzc3kevokMkW.boswtCvhToqC.TappIwfqwJ.cI0efvy",
  50 + //ClientId:"123456",
  51 + }
  52 + loginRsp,err :=auth.Login(login)
  53 + if err!=nil{
  54 + t.Fatal(err,loginRsp)
  55 + }
  56 +}
  57 +
  58 +func Test_AccessToken(t *testing.T){
  59 + req :=&protocol.AccessTokenRequest{
  60 + AuthCode:"897ca746f46b11e98771000c29ad8d6d",
  61 + }
  62 + if rsp,err :=auth.AccessToken(req);err!=nil{
  63 + t.Fatal(err,rsp)
  64 + }
  65 +}
  66 +
  67 +func Test_RefreshToken(t *testing.T) {
  68 + req :=&protocol.RefreshTokenRequest{
  69 +
  70 + }
  71 + if rsp,err :=auth.RefreshToken(req);err!=nil{
  72 + t.Fatal(err,rsp)
  73 + }
  74 +}