正在显示
27 个修改的文件
包含
1599 行增加
和
42 行删除
pkg/application/auth/command/access_token.go
0 → 100644
1 | +package command | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + | ||
6 | + "github.com/beego/beego/v2/core/validation" | ||
7 | +) | ||
8 | + | ||
9 | +type AccessTokenCommand struct { | ||
10 | + AuthCode string `json:"authCode" valid:"Required"` | ||
11 | + SessionMode int `json:"sessionMode"` | ||
12 | +} | ||
13 | + | ||
14 | +func (orgAddCommand *AccessTokenCommand) Valid(validation *validation.Validation) { | ||
15 | + | ||
16 | +} | ||
17 | + | ||
18 | +func (orgAddCommand *AccessTokenCommand) ValidateCommand() error { | ||
19 | + valid := validation.Validation{} | ||
20 | + b, err := valid.Valid(orgAddCommand) | ||
21 | + if err != nil { | ||
22 | + return err | ||
23 | + } | ||
24 | + if !b { | ||
25 | + for _, validErr := range valid.Errors { | ||
26 | + return fmt.Errorf("%s %s", validErr.Key, validErr.Message) | ||
27 | + } | ||
28 | + } | ||
29 | + return nil | ||
30 | +} |
1 | +package command | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + | ||
6 | + "github.com/beego/beego/v2/core/validation" | ||
7 | +) | ||
8 | + | ||
9 | +type CheckSmsCodeCommand struct { | ||
10 | + Phone string `json:"phone" valid:"Required"` | ||
11 | + SmsCode string `json:"smsCode" valid:"Required"` | ||
12 | + // [1:登录][2:修改密码][3:找回密码][4:注册][5:修改手机号] | ||
13 | + Action int `json:"action" valid:"Required"` | ||
14 | +} | ||
15 | + | ||
16 | +func (checkSmsCodeCommand *CheckSmsCodeCommand) Valid(validation *validation.Validation) { | ||
17 | + | ||
18 | +} | ||
19 | + | ||
20 | +func (checkSmsCodeCommand *CheckSmsCodeCommand) ValidateCommand() error { | ||
21 | + valid := validation.Validation{} | ||
22 | + b, err := valid.Valid(checkSmsCodeCommand) | ||
23 | + if err != nil { | ||
24 | + return err | ||
25 | + } | ||
26 | + if !b { | ||
27 | + for _, validErr := range valid.Errors { | ||
28 | + return fmt.Errorf("%s %s", validErr.Key, validErr.Message) | ||
29 | + } | ||
30 | + } | ||
31 | + return nil | ||
32 | +} |
1 | +package command | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + "reflect" | ||
6 | + "strings" | ||
7 | + | ||
8 | + "github.com/beego/beego/v2/core/validation" | ||
9 | +) | ||
10 | + | ||
11 | +type CompanySignUpCommand struct { | ||
12 | + // 企业名称 | ||
13 | + CompanyName string `cname:"企业名称" json:"companyName" valid:"Required"` | ||
14 | + // 联系人 | ||
15 | + Contacts string `cname:"联系人" json:"userName" valid:"Required"` | ||
16 | + // 手机号码 | ||
17 | + Phone string `cname:"手机号码" json:"phone" valid:"Required"` | ||
18 | + // 规模 | ||
19 | + Scale string `cname:"规模" json:"scale" valid:"Required"` | ||
20 | + // 所属行业 | ||
21 | + IndustryCategory string `cname:"所属行业" json:"industryCategory" valid:"Required"` | ||
22 | + // 密码 | ||
23 | + Password string `cname:"密码" json:"password" valid:"Required"` | ||
24 | + // 短信验证码 | ||
25 | + SmsCode string `cname:"短信验证码" json:"smsCode" valid:"Required"` | ||
26 | +} | ||
27 | + | ||
28 | +func (companySignUpCommand *CompanySignUpCommand) Valid(validation *validation.Validation) { | ||
29 | + //validation.SetError("CustomValid", "未实现的自定义认证") | ||
30 | +} | ||
31 | + | ||
32 | +func (companySignUpCommand *CompanySignUpCommand) ValidateCommand() error { | ||
33 | + valid := validation.Validation{} | ||
34 | + b, err := valid.Valid(companySignUpCommand) | ||
35 | + if err != nil { | ||
36 | + return err | ||
37 | + } | ||
38 | + if !b { | ||
39 | + elem := reflect.TypeOf(companySignUpCommand).Elem() | ||
40 | + for _, validErr := range valid.Errors { | ||
41 | + field, isExist := elem.FieldByName(validErr.Field) | ||
42 | + if isExist { | ||
43 | + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1)) | ||
44 | + } else { | ||
45 | + return fmt.Errorf(validErr.Message) | ||
46 | + } | ||
47 | + } | ||
48 | + } | ||
49 | + return nil | ||
50 | +} |
pkg/application/auth/command/login.go
0 → 100644
1 | +package command | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + | ||
6 | + "github.com/beego/beego/v2/core/validation" | ||
7 | +) | ||
8 | + | ||
9 | +type LoginCommand struct { | ||
10 | + Phone string `json:"phone" valid:"Required"` | ||
11 | + GrantType string `json:"grantType" valid:"Required"` //登录方式(signInPassword 密码登录、signInCaptcha 验证码登录) | ||
12 | + Password string `json:"password"` | ||
13 | + Captcha string `json:"captcha"` | ||
14 | + SessionMode int `json:"sessionMode"` | ||
15 | +} | ||
16 | + | ||
17 | +func (orgAddCommand *LoginCommand) Valid(validation *validation.Validation) { | ||
18 | + | ||
19 | +} | ||
20 | + | ||
21 | +func (orgAddCommand *LoginCommand) ValidateCommand() error { | ||
22 | + valid := validation.Validation{} | ||
23 | + b, err := valid.Valid(orgAddCommand) | ||
24 | + if err != nil { | ||
25 | + return err | ||
26 | + } | ||
27 | + if !b { | ||
28 | + for _, validErr := range valid.Errors { | ||
29 | + return fmt.Errorf("%s %s", validErr.Key, validErr.Message) | ||
30 | + } | ||
31 | + } | ||
32 | + return nil | ||
33 | +} |
pkg/application/auth/command/login_pwd.go
0 → 100644
1 | +package command | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + | ||
6 | + "github.com/beego/beego/v2/core/validation" | ||
7 | +) | ||
8 | + | ||
9 | +type LoginPwdCommand struct { | ||
10 | + Username string `json:"username" valid:"Required"` | ||
11 | + Password string `json:"password"` | ||
12 | + // 图形验证码操作成功的识别字段,服务端需使用图形验证插件(geetest)提供的验证模块 | ||
13 | + // 对相应数据进行验证,用以确定该次请求为用户手动操作的正确行为 | ||
14 | + CaptchaChallenge string `json:"captchaChallenge"` | ||
15 | + CaptchaValidate string `json:"captchaValidate"` | ||
16 | + CaptchaSeccode string `json:"captchaSeccode"` | ||
17 | + SessionMode int `json:"sessionMode"` | ||
18 | +} | ||
19 | + | ||
20 | +func (cmd *LoginPwdCommand) Valid(validation *validation.Validation) { | ||
21 | + | ||
22 | +} | ||
23 | + | ||
24 | +func (cmd *LoginPwdCommand) ValidateCommand() error { | ||
25 | + valid := validation.Validation{} | ||
26 | + b, err := valid.Valid(cmd) | ||
27 | + if err != nil { | ||
28 | + return err | ||
29 | + } | ||
30 | + if !b { | ||
31 | + for _, validErr := range valid.Errors { | ||
32 | + return fmt.Errorf("%s %s", validErr.Key, validErr.Message) | ||
33 | + } | ||
34 | + } | ||
35 | + return nil | ||
36 | +} |
pkg/application/auth/command/login_sms.go
0 → 100644
1 | +package command | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + | ||
6 | + "github.com/beego/beego/v2/core/validation" | ||
7 | +) | ||
8 | + | ||
9 | +type LoginSmsCommand struct { | ||
10 | + Phone string `json:"phone" valid:"Required"` | ||
11 | + Code string `json:"code" valid:"Required"` //登录方式(signInPassword 密码登录、signInCaptcha 验证码登录) | ||
12 | + SessionMode int `json:"sessionMode"` | ||
13 | +} | ||
14 | + | ||
15 | +func (cmd *LoginSmsCommand) Valid(validation *validation.Validation) { | ||
16 | + | ||
17 | +} | ||
18 | + | ||
19 | +func (cmd *LoginSmsCommand) ValidateCommand() error { | ||
20 | + valid := validation.Validation{} | ||
21 | + b, err := valid.Valid(cmd) | ||
22 | + if err != nil { | ||
23 | + return err | ||
24 | + } | ||
25 | + if !b { | ||
26 | + for _, validErr := range valid.Errors { | ||
27 | + return fmt.Errorf("%s %s", validErr.Key, validErr.Message) | ||
28 | + } | ||
29 | + } | ||
30 | + return nil | ||
31 | +} |
1 | +package command | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + | ||
6 | + "github.com/beego/beego/v2/core/validation" | ||
7 | +) | ||
8 | + | ||
9 | +type RefreshTokenCommand struct { | ||
10 | + RefreshToken string `json:"refreshToken" valid:"Required"` | ||
11 | +} | ||
12 | + | ||
13 | +func (orgAddCommand *RefreshTokenCommand) Valid(validation *validation.Validation) { | ||
14 | + | ||
15 | +} | ||
16 | + | ||
17 | +func (orgAddCommand *RefreshTokenCommand) ValidateCommand() error { | ||
18 | + valid := validation.Validation{} | ||
19 | + b, err := valid.Valid(orgAddCommand) | ||
20 | + if err != nil { | ||
21 | + return err | ||
22 | + } | ||
23 | + if !b { | ||
24 | + for _, validErr := range valid.Errors { | ||
25 | + return fmt.Errorf("%s %s", validErr.Key, validErr.Message) | ||
26 | + } | ||
27 | + } | ||
28 | + return nil | ||
29 | +} |
1 | +package command | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + "reflect" | ||
6 | + "strings" | ||
7 | + | ||
8 | + "github.com/beego/beego/v2/core/validation" | ||
9 | +) | ||
10 | + | ||
11 | +type ResetPasswordCommand struct { | ||
12 | + // 手机号码 | ||
13 | + // Phone string `cname:"手机号码" json:"phone" valid:"Required"` | ||
14 | + // 密码 | ||
15 | + Password string `cname:"密码" json:"newPassword"` | ||
16 | + // 密码 | ||
17 | + RepeatNewPassword string `cname:"密码" json:"repeatNewPassword" valid:"Required"` | ||
18 | + // 密码 | ||
19 | + SmsCodeIdentity string `cname:"密码" json:"smsCodeIdentity" valid:"Required"` | ||
20 | +} | ||
21 | + | ||
22 | +func (resetPasswordCommand *ResetPasswordCommand) Valid(validation *validation.Validation) { | ||
23 | + if len(resetPasswordCommand.Password) == 0 { | ||
24 | + validation.Error("登录密码不能为空") | ||
25 | + return | ||
26 | + } | ||
27 | + if resetPasswordCommand.Password != resetPasswordCommand.RepeatNewPassword { | ||
28 | + validation.Error("两次密码输入不一致") | ||
29 | + return | ||
30 | + } | ||
31 | +} | ||
32 | + | ||
33 | +func (resetPasswordCommand *ResetPasswordCommand) ValidateCommand() error { | ||
34 | + valid := validation.Validation{} | ||
35 | + b, err := valid.Valid(resetPasswordCommand) | ||
36 | + if err != nil { | ||
37 | + return err | ||
38 | + } | ||
39 | + if !b { | ||
40 | + elem := reflect.TypeOf(resetPasswordCommand).Elem() | ||
41 | + for _, validErr := range valid.Errors { | ||
42 | + field, isExist := elem.FieldByName(validErr.Field) | ||
43 | + if isExist { | ||
44 | + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1)) | ||
45 | + } else { | ||
46 | + return fmt.Errorf(validErr.Message) | ||
47 | + } | ||
48 | + } | ||
49 | + } | ||
50 | + return nil | ||
51 | +} |
pkg/application/auth/command/send_smscode.go
0 → 100644
1 | +package command | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + | ||
6 | + "github.com/beego/beego/v2/core/validation" | ||
7 | +) | ||
8 | + | ||
9 | +type SendSmsCodeCommand struct { | ||
10 | + Phone string `json:"phone" valid:"Required"` | ||
11 | +} | ||
12 | + | ||
13 | +func (orgAddCommand *SendSmsCodeCommand) Valid(validation *validation.Validation) { | ||
14 | + | ||
15 | +} | ||
16 | + | ||
17 | +func (orgAddCommand *SendSmsCodeCommand) ValidateCommand() error { | ||
18 | + valid := validation.Validation{} | ||
19 | + b, err := valid.Valid(orgAddCommand) | ||
20 | + if err != nil { | ||
21 | + return err | ||
22 | + } | ||
23 | + if !b { | ||
24 | + for _, validErr := range valid.Errors { | ||
25 | + return fmt.Errorf("%s %s", validErr.Key, validErr.Message) | ||
26 | + } | ||
27 | + } | ||
28 | + return nil | ||
29 | +} |
pkg/application/auth/command/switch_org.go
0 → 100644
1 | +package command | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + "github.com/beego/beego/v2/core/validation" | ||
6 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain" | ||
7 | +) | ||
8 | + | ||
9 | +type SwitchOrgCommand struct { | ||
10 | + //操作人 | ||
11 | + Operator domain.Operator `json:"-"` | ||
12 | + // 组织ID | ||
13 | + OrgId int64 `json:"orgId,string"` | ||
14 | +} | ||
15 | + | ||
16 | +func (switchOrgCommand *SwitchOrgCommand) Valid(validation *validation.Validation) { | ||
17 | + | ||
18 | +} | ||
19 | + | ||
20 | +func (switchOrgCommand *SwitchOrgCommand) ValidateCommand() error { | ||
21 | + valid := validation.Validation{} | ||
22 | + b, err := valid.Valid(switchOrgCommand) | ||
23 | + if err != nil { | ||
24 | + return err | ||
25 | + } | ||
26 | + if !b { | ||
27 | + for _, validErr := range valid.Errors { | ||
28 | + return fmt.Errorf("%s %s", validErr.Key, validErr.Message) | ||
29 | + } | ||
30 | + } | ||
31 | + return nil | ||
32 | +} |
pkg/application/auth/command/user_info.go
0 → 100644
1 | +package command | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain" | ||
6 | + | ||
7 | + "github.com/beego/beego/v2/core/validation" | ||
8 | +) | ||
9 | + | ||
10 | +type UserInfoCommand struct { | ||
11 | + //操作人 | ||
12 | + Operator domain.Operator `json:"-"` | ||
13 | +} | ||
14 | + | ||
15 | +func (userInfoCommand *UserInfoCommand) Valid(validation *validation.Validation) { | ||
16 | + | ||
17 | +} | ||
18 | + | ||
19 | +func (userInfoCommand *UserInfoCommand) ValidateCommand() error { | ||
20 | + valid := validation.Validation{} | ||
21 | + b, err := valid.Valid(userInfoCommand) | ||
22 | + if err != nil { | ||
23 | + return err | ||
24 | + } | ||
25 | + if !b { | ||
26 | + for _, validErr := range valid.Errors { | ||
27 | + return fmt.Errorf("%s %s", validErr.Key, validErr.Message) | ||
28 | + } | ||
29 | + } | ||
30 | + return nil | ||
31 | +} |
pkg/application/auth/command/user_menu.go
0 → 100644
1 | +package command | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain" | ||
6 | + | ||
7 | + "github.com/beego/beego/v2/core/validation" | ||
8 | +) | ||
9 | + | ||
10 | +type UserMenusCommand struct { | ||
11 | + //操作人 | ||
12 | + Operator domain.Operator `json:"-"` | ||
13 | +} | ||
14 | + | ||
15 | +func (userMenusCommand *UserMenusCommand) Valid(validation *validation.Validation) { | ||
16 | + | ||
17 | +} | ||
18 | + | ||
19 | +func (userMenusCommand *UserMenusCommand) ValidateCommand() error { | ||
20 | + valid := validation.Validation{} | ||
21 | + b, err := valid.Valid(userMenusCommand) | ||
22 | + if err != nil { | ||
23 | + return err | ||
24 | + } | ||
25 | + if !b { | ||
26 | + for _, validErr := range valid.Errors { | ||
27 | + return fmt.Errorf("%s %s", validErr.Key, validErr.Message) | ||
28 | + } | ||
29 | + } | ||
30 | + return nil | ||
31 | +} |
pkg/application/auth/command/user_orgs.go
0 → 100644
1 | +package command | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + "github.com/beego/beego/v2/core/validation" | ||
6 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain" | ||
7 | +) | ||
8 | + | ||
9 | +type UserOrgCommand struct { | ||
10 | + //操作人 | ||
11 | + Operator domain.Operator `json:"-"` | ||
12 | +} | ||
13 | + | ||
14 | +func (userOrgCommand *UserOrgCommand) Valid(validation *validation.Validation) { | ||
15 | + | ||
16 | +} | ||
17 | + | ||
18 | +func (userOrgCommand *UserOrgCommand) ValidateCommand() error { | ||
19 | + valid := validation.Validation{} | ||
20 | + b, err := valid.Valid(userOrgCommand) | ||
21 | + if err != nil { | ||
22 | + return err | ||
23 | + } | ||
24 | + if !b { | ||
25 | + for _, validErr := range valid.Errors { | ||
26 | + return fmt.Errorf("%s %s", validErr.Key, validErr.Message) | ||
27 | + } | ||
28 | + } | ||
29 | + return nil | ||
30 | +} |
1 | +package query | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + | ||
6 | + "github.com/beego/beego/v2/core/validation" | ||
7 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain" | ||
8 | +) | ||
9 | + | ||
10 | +type GetCompanyOrgsByUserQuery struct { | ||
11 | + //操作人 | ||
12 | + Operator domain.Operator `json:"-"` | ||
13 | + Phone string `json:"phone" valid:"Required"` //手机号 | ||
14 | +} | ||
15 | + | ||
16 | +func (orgAddCommand *GetCompanyOrgsByUserQuery) Valid(validation *validation.Validation) { | ||
17 | + | ||
18 | +} | ||
19 | + | ||
20 | +func (orgAddCommand *GetCompanyOrgsByUserQuery) ValidateCommand() error { | ||
21 | + valid := validation.Validation{} | ||
22 | + b, err := valid.Valid(orgAddCommand) | ||
23 | + if err != nil { | ||
24 | + return err | ||
25 | + } | ||
26 | + if !b { | ||
27 | + for _, validErr := range valid.Errors { | ||
28 | + return fmt.Errorf("%s %s", validErr.Key, validErr.Message) | ||
29 | + } | ||
30 | + } | ||
31 | + return nil | ||
32 | +} |
pkg/application/auth/service/service.go
0 → 100644
1 | +package service | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/google/uuid" | ||
5 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/auth/query" | ||
6 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/auth/dto" | ||
7 | + "time" | ||
8 | + | ||
9 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/log" | ||
10 | + | ||
11 | + "github.com/linmadan/egglib-go/core/application" | ||
12 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/auth/command" | ||
13 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/factory" | ||
14 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain" | ||
15 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/cache" | ||
16 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_user" | ||
17 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/sms_serve" | ||
18 | +) | ||
19 | + | ||
20 | +// 组织管理 | ||
21 | +type AuthService struct { | ||
22 | +} | ||
23 | + | ||
24 | +//AuthLogin 用户登录 | ||
25 | +func (srv AuthService) AuthLogin(loginCommand *command.LoginCommand) (interface{}, error) { | ||
26 | + var ( | ||
27 | + authCode string | ||
28 | + result interface{} | ||
29 | + err error | ||
30 | + ) | ||
31 | + creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{}) | ||
32 | + _, err = creationUserGateway.AuthRefreshIM(allied_creation_user.ReqAuthRefreshIM{ | ||
33 | + Phone: loginCommand.Phone, | ||
34 | + }) | ||
35 | + if err != nil { | ||
36 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
37 | + } | ||
38 | + switch loginCommand.GrantType { | ||
39 | + case "signInPassword": | ||
40 | + authCode, err = srv.SignInPassword(loginCommand.Phone, loginCommand.Password) | ||
41 | + case "signInCaptcha": | ||
42 | + authCode, err = srv.SignInCaptcha(loginCommand.Phone, loginCommand.Captcha) | ||
43 | + default: | ||
44 | + err = application.ThrowError(application.TRANSACTION_ERROR, "登录方式无法解析") | ||
45 | + } | ||
46 | + result, err = srv.GetAuthAccessToken(&command.AccessTokenCommand{ | ||
47 | + AuthCode: authCode, | ||
48 | + SessionMode: loginCommand.SessionMode, | ||
49 | + }) | ||
50 | + return map[string]interface{}{ | ||
51 | + "access": result, | ||
52 | + }, err | ||
53 | +} | ||
54 | + | ||
55 | +//AuthLogin 用户登录 | ||
56 | +func (srv AuthService) AuthLoginPwd(loginCommand *command.LoginPwdCommand) (interface{}, error) { | ||
57 | + if err := loginCommand.ValidateCommand(); err != nil { | ||
58 | + return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | ||
59 | + } | ||
60 | + login := &command.LoginCommand{ | ||
61 | + GrantType: "signInPassword", | ||
62 | + Phone: loginCommand.Username, | ||
63 | + Password: loginCommand.Password, | ||
64 | + SessionMode: loginCommand.SessionMode, | ||
65 | + } | ||
66 | + return srv.AuthLogin(login) | ||
67 | +} | ||
68 | + | ||
69 | +//AuthLogin 用户登录 | ||
70 | +func (srv AuthService) AuthLoginSms(loginCommand *command.LoginSmsCommand) (interface{}, error) { | ||
71 | + if err := loginCommand.ValidateCommand(); err != nil { | ||
72 | + return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | ||
73 | + } | ||
74 | + login := &command.LoginCommand{ | ||
75 | + GrantType: "signInCaptcha", | ||
76 | + Phone: loginCommand.Phone, | ||
77 | + Captcha: loginCommand.Code, | ||
78 | + SessionMode: loginCommand.SessionMode, | ||
79 | + } | ||
80 | + return srv.AuthLogin(login) | ||
81 | +} | ||
82 | + | ||
83 | +//SendSmsCaptcha 发送验证码短信 | ||
84 | +func (srv AuthService) SendSmsCaptcha(smsCodeCommand *command.SendSmsCodeCommand) error { | ||
85 | + smsServeGateway := sms_serve.NewHttplibHttplibSmsServe() | ||
86 | + err := smsServeGateway.SendSms(smsCodeCommand.Phone) | ||
87 | + if err != nil { | ||
88 | + return application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
89 | + } | ||
90 | + return nil | ||
91 | +} | ||
92 | + | ||
93 | +//SignInPassword 使用账号密码校验 | ||
94 | +func (srv AuthService) SignInPassword(account string, password string) (string, error) { | ||
95 | + creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{}) | ||
96 | + _, err := creationUserGateway.AuthCheckPassword(allied_creation_user.ReqAuthCheckPassword{ | ||
97 | + Password: password, | ||
98 | + Phone: account, | ||
99 | + }) | ||
100 | + if err != nil { | ||
101 | + return "", application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
102 | + } | ||
103 | + ltoken := domain.LoginToken{ | ||
104 | + UserId: 0, | ||
105 | + Account: account, | ||
106 | + Platform: domain.LoginPlatformApp, | ||
107 | + CompanyId: 0, | ||
108 | + } | ||
109 | + authcode, err := ltoken.GenerateAuthCode() | ||
110 | + if err != nil { | ||
111 | + return "", application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
112 | + } | ||
113 | + //result := map[string]string{ | ||
114 | + // "authCode": authcode, | ||
115 | + //} | ||
116 | + return authcode, nil | ||
117 | +} | ||
118 | + | ||
119 | +//SignInCaptcha 使用手机验证码登录 | ||
120 | +func (srv AuthService) SignInCaptcha(phone string, captcha string) (string, error) { | ||
121 | + smsServeGateway := sms_serve.NewHttplibHttplibSmsServe() | ||
122 | + err := smsServeGateway.CheckSmsCode(phone, captcha) | ||
123 | + if err != nil { | ||
124 | + return "", application.ThrowError(application.BUSINESS_ERROR, err.Error()) | ||
125 | + } | ||
126 | + ltoken := domain.LoginToken{ | ||
127 | + UserId: 0, | ||
128 | + Account: phone, | ||
129 | + Platform: domain.LoginPlatformApp, | ||
130 | + CompanyId: 0, | ||
131 | + } | ||
132 | + authcode, err := ltoken.GenerateAuthCode() | ||
133 | + if err != nil { | ||
134 | + return "", application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
135 | + } | ||
136 | + return authcode, nil | ||
137 | +} | ||
138 | + | ||
139 | +//GetAuthAccessToken 获取令牌Token | ||
140 | +func (srv AuthService) GetAuthAccessToken(accessTokenCommand *command.AccessTokenCommand) (interface{}, error) { | ||
141 | + if err := accessTokenCommand.ValidateCommand(); err != nil { | ||
142 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
143 | + } | ||
144 | + ltoken := &domain.LoginToken{} | ||
145 | + err := ltoken.ParseToken(accessTokenCommand.AuthCode) | ||
146 | + if err != nil { | ||
147 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
148 | + } | ||
149 | + phone := ltoken.Account | ||
150 | + creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{}) | ||
151 | + userSeachResult, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{ | ||
152 | + CompanyId: ltoken.CompanyId, | ||
153 | + Phone: phone, | ||
154 | + }) | ||
155 | + if err != nil { | ||
156 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
157 | + } | ||
158 | + if len(userSeachResult.Users) == 0 { | ||
159 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, "获取用户信息失败") | ||
160 | + } | ||
161 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
162 | + if err != nil { | ||
163 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
164 | + } | ||
165 | + if err := transactionContext.StartTransaction(); err != nil { | ||
166 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
167 | + } | ||
168 | + defer func() { | ||
169 | + transactionContext.RollbackTransaction() | ||
170 | + }() | ||
171 | + var loginAccessRepository domain.LoginAccessRepository | ||
172 | + if loginAccessRepository, err = factory.CreateLoginAccessRepository(map[string]interface{}{ | ||
173 | + "transactionContext": transactionContext, | ||
174 | + }); err != nil { | ||
175 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
176 | + } | ||
177 | + _, lAccess, err := loginAccessRepository.Find(map[string]interface{}{ | ||
178 | + "account": phone, | ||
179 | + "platform": domain.LoginPlatformApp, | ||
180 | + }) | ||
181 | + if err != nil { | ||
182 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
183 | + } | ||
184 | + var currentAccess *domain.LoginAccess | ||
185 | + if len(lAccess) > 0 { | ||
186 | + currentAccess = lAccess[0] | ||
187 | + currentAccess.UpdatedTime = time.Now() | ||
188 | + } else { | ||
189 | + currentAccess = &domain.LoginAccess{ | ||
190 | + UserBaseId: int64(userSeachResult.Users[0].UserBaseId), | ||
191 | + UserId: int64(userSeachResult.Users[0].UserId), | ||
192 | + Account: userSeachResult.Users[0].UserInfo.Phone, | ||
193 | + Platform: domain.LoginPlatformApp, | ||
194 | + OrganizationId: int64(userSeachResult.Users[0].Org.OrgId), | ||
195 | + AccessToken: "", | ||
196 | + RefreshToken: "", | ||
197 | + AccessExpired: 0, | ||
198 | + RefreshExpired: 0, | ||
199 | + CreatedTime: time.Now(), | ||
200 | + UpdatedTime: time.Now(), | ||
201 | + } | ||
202 | + if userSeachResult.Users[0].Company != nil { | ||
203 | + currentAccess.CompanyId = int64(userSeachResult.Users[0].Company.CompanyId) | ||
204 | + } | ||
205 | + } | ||
206 | + //判定当前凭证的companyId,OrganizationId 是否在用户列表中 | ||
207 | + var currentOrgIsOK bool | ||
208 | +loopUser1: | ||
209 | + for _, v := range userSeachResult.Users { | ||
210 | + if v.Company.CompanyId == int(currentAccess.CompanyId) { | ||
211 | + for _, vv := range v.UserOrg { | ||
212 | + if vv.OrgID == int(currentAccess.OrganizationId) { | ||
213 | + currentOrgIsOK = true | ||
214 | + currentAccess.UserId = int64(v.UserId) | ||
215 | + currentAccess.UserBaseId = int64(v.UserBaseId) | ||
216 | + break loopUser1 | ||
217 | + } | ||
218 | + } | ||
219 | + } | ||
220 | + } | ||
221 | + //记录上一次的登录公司组织信息不可用 ,重置登录记录 | ||
222 | + //使用找到的第一个可用的公司组织 | ||
223 | + if !currentOrgIsOK { | ||
224 | + loopUser2: | ||
225 | + for _, v := range userSeachResult.Users { | ||
226 | + currentAccess.CompanyId = int64(v.Company.CompanyId) | ||
227 | + for _, vv := range v.UserOrg { | ||
228 | + currentAccess.UserId = int64(v.UserId) | ||
229 | + currentAccess.UserBaseId = int64(v.UserBaseId) | ||
230 | + currentAccess.OrganizationId = int64(vv.OrgID) | ||
231 | + currentOrgIsOK = true | ||
232 | + break loopUser2 | ||
233 | + } | ||
234 | + } | ||
235 | + } | ||
236 | + | ||
237 | + loginToken := domain.LoginToken{ | ||
238 | + UserId: currentAccess.UserId, | ||
239 | + Account: currentAccess.Account, | ||
240 | + UserBaseId: currentAccess.UserBaseId, | ||
241 | + CompanyId: currentAccess.CompanyId, | ||
242 | + OrgId: currentAccess.OrganizationId, | ||
243 | + Platform: currentAccess.Platform, | ||
244 | + } | ||
245 | + | ||
246 | + accessTokenStr, err := loginToken.GenerateAccessToken() | ||
247 | + if err != nil { | ||
248 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
249 | + } | ||
250 | + currentAccess.AccessToken = accessTokenStr | ||
251 | + currentAccess.AccessExpired = loginToken.ExpiresAt | ||
252 | + refreshTokenStr, err := loginToken.GenerateRefreshToken() | ||
253 | + if err != nil { | ||
254 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
255 | + } | ||
256 | + currentAccess.RefreshToken = refreshTokenStr | ||
257 | + currentAccess.AccessExpired = loginToken.ExpiresAt | ||
258 | + //先存数据库 | ||
259 | + _, err = loginAccessRepository.Save(currentAccess) | ||
260 | + if err != nil { | ||
261 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
262 | + } | ||
263 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
264 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
265 | + } | ||
266 | + //后处理redis缓存 | ||
267 | + tokenCache := cache.LoginTokenCache{} | ||
268 | + tokenCache.RemoveAccessToken(currentAccess.Account, domain.LoginPlatformApp) | ||
269 | + tokenCache.RemoveRefreshToken(currentAccess.Account, domain.LoginPlatformApp) | ||
270 | + tokenCache.SaveAccessToken(currentAccess) | ||
271 | + tokenCache.SaveRefreshToken(currentAccess) | ||
272 | + nowTime := time.Now().Unix() | ||
273 | + return map[string]interface{}{ | ||
274 | + "refreshToken": accessTokenStr, | ||
275 | + "accessToken": refreshTokenStr, | ||
276 | + "expiresIn": currentAccess.AccessExpired - nowTime, | ||
277 | + }, nil | ||
278 | +} | ||
279 | + | ||
280 | +func (srv AuthService) RefreshAuthAccessToken(refreshTokenCommand *command.RefreshTokenCommand) (interface{}, error) { | ||
281 | + if err := refreshTokenCommand.ValidateCommand(); err != nil { | ||
282 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
283 | + } | ||
284 | + ltoken := domain.LoginToken{} | ||
285 | + err := ltoken.ParseToken(refreshTokenCommand.RefreshToken) | ||
286 | + if err != nil { | ||
287 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, "refreshToken 不可用,"+err.Error()) | ||
288 | + } | ||
289 | + token, err := srv.getToken(domain.Operator{}, ltoken) | ||
290 | + // phone := ltoken.Account | ||
291 | + // creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{}) | ||
292 | + // userSearchResult, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{ | ||
293 | + // Phone: phone, | ||
294 | + // }) | ||
295 | + // if err != nil { | ||
296 | + // return nil, application.ThrowError(application.TRANSACTION_ERROR, "用户信息获取失败,"+err.Error()) | ||
297 | + // } | ||
298 | + // //判定当前凭证的companyId,OrganizationId 是否在用户列表中 | ||
299 | + // var currentOrgIsOK bool | ||
300 | + //loopUser1: | ||
301 | + // for _, v := range userSearchResult.Users { | ||
302 | + // if v.Company.CompanyId == int(ltoken.CompanyId) { | ||
303 | + // for _, vv := range v.UserOrg { | ||
304 | + // if vv.OrgID == int(ltoken.OrgId) { | ||
305 | + // currentOrgIsOK = true | ||
306 | + // break loopUser1 | ||
307 | + // } | ||
308 | + // } | ||
309 | + // } | ||
310 | + // } | ||
311 | + // if !currentOrgIsOK { | ||
312 | + // return nil, application.ThrowError(application.TRANSACTION_ERROR, "登录的公司组织不可用") | ||
313 | + // } | ||
314 | + // | ||
315 | + // transactionContext, err := factory.CreateTransactionContext(nil) | ||
316 | + // if err != nil { | ||
317 | + // return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
318 | + // } | ||
319 | + // if err := transactionContext.StartTransaction(); err != nil { | ||
320 | + // return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
321 | + // } | ||
322 | + // defer func() { | ||
323 | + // transactionContext.RollbackTransaction() | ||
324 | + // }() | ||
325 | + // var loginAccessRepository domain.LoginAccessRepository | ||
326 | + // if loginAccessRepository, err = factory.CreateLoginAccessRepository(map[string]interface{}{ | ||
327 | + // "transactionContext": transactionContext, | ||
328 | + // }); err != nil { | ||
329 | + // return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
330 | + // } | ||
331 | + // _, lAccess, err := loginAccessRepository.Find(map[string]interface{}{ | ||
332 | + // "account": phone, | ||
333 | + // "platform": domain.LoginPlatformApp, | ||
334 | + // }) | ||
335 | + // if err != nil { | ||
336 | + // return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
337 | + // } | ||
338 | + // var currentAccess *domain.LoginAccess | ||
339 | + // if len(lAccess) > 0 { | ||
340 | + // currentAccess = lAccess[0] | ||
341 | + // currentAccess.UpdatedTime = time.Now() | ||
342 | + // } else { | ||
343 | + // currentAccess = &domain.LoginAccess{ | ||
344 | + // UserBaseId: ltoken.UserBaseId, | ||
345 | + // UserId: ltoken.UserId, | ||
346 | + // Account: ltoken.Account, | ||
347 | + // Platform: domain.LoginPlatformApp, | ||
348 | + // CompanyId: ltoken.CompanyId, | ||
349 | + // OrganizationId: ltoken.OrgId, | ||
350 | + // AccessToken: "", | ||
351 | + // RefreshToken: "", | ||
352 | + // AccessExpired: 0, | ||
353 | + // RefreshExpired: 0, | ||
354 | + // CreatedTime: time.Now(), | ||
355 | + // UpdatedTime: time.Now(), | ||
356 | + // } | ||
357 | + // } | ||
358 | + // accessTokenStr, err := ltoken.GenerateAccessToken() | ||
359 | + // if err != nil { | ||
360 | + // return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
361 | + // } | ||
362 | + // currentAccess.AccessToken = accessTokenStr | ||
363 | + // currentAccess.AccessExpired = ltoken.ExpiresAt | ||
364 | + // refreshTokenStr, err := ltoken.GenerateRefreshToken() | ||
365 | + // if err != nil { | ||
366 | + // return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
367 | + // } | ||
368 | + // currentAccess.RefreshToken = refreshTokenStr | ||
369 | + // currentAccess.RefreshExpired = ltoken.ExpiresAt | ||
370 | + // //先存数据库 | ||
371 | + // _, err = loginAccessRepository.Save(currentAccess) | ||
372 | + // if err != nil { | ||
373 | + // return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
374 | + // } | ||
375 | + // if err := transactionContext.CommitTransaction(); err != nil { | ||
376 | + // return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
377 | + // } | ||
378 | + // //后处理redis缓存 | ||
379 | + // tokenCache := cache.LoginTokenCache{} | ||
380 | + // tokenCache.RemoveAccessToken(currentAccess.Account, domain.LoginPlatformApp) | ||
381 | + // tokenCache.RemoveRefreshToken(currentAccess.Account, domain.LoginPlatformApp) | ||
382 | + // tokenCache.SaveAccessToken(currentAccess) | ||
383 | + // tokenCache.SaveRefreshToken(currentAccess) | ||
384 | + // nowTime := time.Now().Unix() | ||
385 | + // return map[string]interface{}{ | ||
386 | + // "refreshToken": accessTokenStr, | ||
387 | + // "accessToken": refreshTokenStr, | ||
388 | + // "expiresIn": currentAccess.AccessExpired - nowTime, | ||
389 | + // }, nil | ||
390 | + return token["token"], err | ||
391 | +} | ||
392 | + | ||
393 | +//GetUserMenus 获取用户信息 | ||
394 | +func (srv AuthService) GetUserInfo(userInfoCommand *command.UserInfoCommand) (interface{}, error) { | ||
395 | + user, err := srv.getUserInfo(userInfoCommand.Operator) | ||
396 | + if err != nil { | ||
397 | + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) | ||
398 | + } | ||
399 | + return map[string]interface{}{ | ||
400 | + "user": user, | ||
401 | + }, nil | ||
402 | +} | ||
403 | + | ||
404 | +//GetUserMenus 获取用户菜单 | ||
405 | +func (srv AuthService) GetUserMenus(userMenusCommand *command.UserMenusCommand) (interface{}, error) { | ||
406 | + creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser( | ||
407 | + userMenusCommand.Operator) | ||
408 | + resultMenu, err := creationUserGateway.UserAccessMenus(allied_creation_user.ReqUserAccessMenus{ | ||
409 | + UserId: int(userMenusCommand.Operator.UserId), | ||
410 | + }) | ||
411 | + if err != nil { | ||
412 | + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) | ||
413 | + } | ||
414 | + return map[string]interface{}{ | ||
415 | + "accessMenus": resultMenu.Menus, | ||
416 | + }, nil | ||
417 | +} | ||
418 | + | ||
419 | +//GetUserMenus 获取用户组织 | ||
420 | +func (srv AuthService) GetUserOrg(userOrgCommand *command.UserOrgCommand) (interface{}, error) { | ||
421 | + creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(userOrgCommand.Operator) | ||
422 | + result, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{ | ||
423 | + Offset: 0, | ||
424 | + Limit: 100, | ||
425 | + UserBaseId: userOrgCommand.Operator.UserBaseId, | ||
426 | + UserType: domain.UserTypeEmployee, | ||
427 | + EnableStatus: domain.UserStatusEnable, | ||
428 | + PullRealTime: true, | ||
429 | + }) | ||
430 | + if err != nil { | ||
431 | + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) | ||
432 | + } | ||
433 | + var res = make([]interface{}, 0) | ||
434 | + for i := range result.Users { | ||
435 | + for j := range result.Users[i].UserOrg { | ||
436 | + org := result.Users[i].UserOrg[j] | ||
437 | + res = append(res, map[string]interface{}{ | ||
438 | + "orgId": org.OrgID, | ||
439 | + "orgName": org.OrgName, | ||
440 | + }) | ||
441 | + } | ||
442 | + } | ||
443 | + return map[string]interface{}{ | ||
444 | + "orgs": res, | ||
445 | + }, nil | ||
446 | +} | ||
447 | + | ||
448 | +//OrgSwitch 组织切换 | ||
449 | +func (srv AuthService) OrgSwitch(switchOrgCommand *command.SwitchOrgCommand) (interface{}, error) { | ||
450 | + if err := switchOrgCommand.ValidateCommand(); err != nil { | ||
451 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
452 | + } | ||
453 | + ltoken := domain.LoginToken{} | ||
454 | + err := ltoken.ParseToken(switchOrgCommand.Operator.Token) | ||
455 | + if err != nil { | ||
456 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, "accessToken 不可用,"+err.Error()) | ||
457 | + } | ||
458 | + ltoken.OrgId = switchOrgCommand.OrgId | ||
459 | + token, err := srv.getToken(domain.Operator{}, ltoken) | ||
460 | + if err != nil { | ||
461 | + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) | ||
462 | + } | ||
463 | + var userId int64 | ||
464 | + if v, ok := token["userId"]; ok { | ||
465 | + if userId, ok = v.(int64); !ok { | ||
466 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, "用户不存在") | ||
467 | + } | ||
468 | + } | ||
469 | + user, err := srv.getUserInfo(domain.Operator{UserId: userId}) | ||
470 | + if err != nil { | ||
471 | + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) | ||
472 | + } | ||
473 | + creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser( | ||
474 | + switchOrgCommand.Operator) | ||
475 | + resultMenu, err := creationUserGateway.UserAccessMenus(allied_creation_user.ReqUserAccessMenus{ | ||
476 | + UserId: int(userId), | ||
477 | + }) | ||
478 | + if err != nil { | ||
479 | + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) | ||
480 | + } | ||
481 | + var res = map[string]interface{}{ | ||
482 | + "user": user, | ||
483 | + "accessMenus": resultMenu.Menus, | ||
484 | + "token": token["token"], | ||
485 | + } | ||
486 | + return res, nil | ||
487 | +} | ||
488 | + | ||
489 | +// CompanySignUp 企业注册 | ||
490 | +func (srv AuthService) CompanySignUp(companySignUpCommand *command.CompanySignUpCommand) (interface{}, error) { | ||
491 | + //TODO:验证码验证测试去掉,后期恢复回来 | ||
492 | + //smsServeGateway := sms_serve.NewHttplibHttplibSmsServe() | ||
493 | + //err := smsServeGateway.CheckSmsCode(companySignUpCommand.Phone, companySignUpCommand.SmsCode) | ||
494 | + //if err != nil { | ||
495 | + // return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) | ||
496 | + //} | ||
497 | + creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{}) | ||
498 | + _, err := creationUserGateway.AuthCompanySignUp(allied_creation_user.ReqAuthCompanySignUp{ | ||
499 | + CompanyName: companySignUpCommand.CompanyName, | ||
500 | + Phone: companySignUpCommand.Phone, | ||
501 | + Password: companySignUpCommand.Password, | ||
502 | + Contacts: companySignUpCommand.Contacts, | ||
503 | + IndustryCategory: companySignUpCommand.IndustryCategory, | ||
504 | + Scale: companySignUpCommand.Scale, | ||
505 | + }) | ||
506 | + if err != nil { | ||
507 | + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) | ||
508 | + } | ||
509 | + return companySignUpCommand, err | ||
510 | +} | ||
511 | + | ||
512 | +// ResetPassword 重置密码(找回密码) | ||
513 | +func (srv AuthService) ResetPassword(resetPasswordCommand *command.ResetPasswordCommand) (interface{}, error) { | ||
514 | + if err := resetPasswordCommand.ValidateCommand(); err != nil { | ||
515 | + return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | ||
516 | + } | ||
517 | + //var phone string | ||
518 | + pcc := cache.PhoneCheckCache{} | ||
519 | + var item = &cache.PhoneCheckItem{} | ||
520 | + if err := pcc.Get(resetPasswordCommand.SmsCodeIdentity, item); err != nil { | ||
521 | + log.Logger.Error(err.Error()) | ||
522 | + return nil, application.ThrowError(application.BUSINESS_ERROR, "验证码已失效") | ||
523 | + } | ||
524 | + // 2.重置密码 | ||
525 | + creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{}) | ||
526 | + result, err := creationUserGateway.AuthResetPassword(allied_creation_user.ReqAuthResetPassword{ | ||
527 | + Phone: item.Phone, | ||
528 | + Password: resetPasswordCommand.Password, | ||
529 | + }) | ||
530 | + if err != nil { | ||
531 | + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) | ||
532 | + } | ||
533 | + return result, err | ||
534 | +} | ||
535 | + | ||
536 | +func (srv AuthService) getUserInfo(operator domain.Operator) (interface{}, error) { | ||
537 | + creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser( | ||
538 | + operator) | ||
539 | + resultUser, err := creationUserGateway.UserGet(allied_creation_user.ReqGetUser{ | ||
540 | + UserId: int(operator.UserId), | ||
541 | + }) | ||
542 | + if err != nil { | ||
543 | + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) | ||
544 | + } | ||
545 | + var user = map[string]interface{}{ | ||
546 | + "userId": resultUser.UserId, | ||
547 | + "userInfo": map[string]interface{}{ | ||
548 | + "userName": resultUser.UserInfo.UserName, | ||
549 | + "userPhone": resultUser.UserInfo.Phone, | ||
550 | + "userAvatar": resultUser.UserInfo.Avatar, | ||
551 | + //"userCode": resultUser.UserInfo.UserCode, | ||
552 | + "email": resultUser.UserInfo.Email, | ||
553 | + }, | ||
554 | + "department": resultUser.Department, | ||
555 | + "company": map[string]interface{}{ | ||
556 | + "companyId": resultUser.Company.CompanyId, | ||
557 | + "companyName": resultUser.Company.CompanyName, | ||
558 | + "logo": resultUser.Company.Log, | ||
559 | + }, | ||
560 | + "im": resultUser.IM, | ||
561 | + "org": resultUser.Org, | ||
562 | + } | ||
563 | + return user, nil | ||
564 | +} | ||
565 | + | ||
566 | +func (srv AuthService) getToken(operator domain.Operator, ltoken domain.LoginToken) (map[string]interface{}, error) { | ||
567 | + creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{}) | ||
568 | + userSearchResult, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{ | ||
569 | + Phone: ltoken.Account, | ||
570 | + }) | ||
571 | + if err != nil { | ||
572 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, "用户信息获取失败,"+err.Error()) | ||
573 | + } | ||
574 | + //判定当前凭证的companyId,OrganizationId 是否在用户列表中 | ||
575 | + var currentOrgIsOK bool | ||
576 | + var currentUserId int64 | ||
577 | +loopUser1: | ||
578 | + for _, v := range userSearchResult.Users { | ||
579 | + //if v.Company.CompanyId == int(ltoken.CompanyId) { | ||
580 | + for _, vv := range v.UserOrg { | ||
581 | + if vv.OrgID == int(ltoken.OrgId) { | ||
582 | + currentOrgIsOK = true | ||
583 | + currentUserId = int64(v.UserId) | ||
584 | + break loopUser1 | ||
585 | + } | ||
586 | + } | ||
587 | + //} | ||
588 | + } | ||
589 | + if !currentOrgIsOK { | ||
590 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, "登录的公司组织不可用") | ||
591 | + } | ||
592 | + | ||
593 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
594 | + if err != nil { | ||
595 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
596 | + } | ||
597 | + if err := transactionContext.StartTransaction(); err != nil { | ||
598 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
599 | + } | ||
600 | + defer func() { | ||
601 | + transactionContext.RollbackTransaction() | ||
602 | + }() | ||
603 | + var loginAccessRepository domain.LoginAccessRepository | ||
604 | + if loginAccessRepository, err = factory.CreateLoginAccessRepository(map[string]interface{}{ | ||
605 | + "transactionContext": transactionContext, | ||
606 | + }); err != nil { | ||
607 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
608 | + } | ||
609 | + _, lAccess, err := loginAccessRepository.Find(map[string]interface{}{ | ||
610 | + "account": ltoken.Account, | ||
611 | + "platform": domain.LoginPlatformApp, | ||
612 | + }) | ||
613 | + if err != nil { | ||
614 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
615 | + } | ||
616 | + var currentAccess *domain.LoginAccess | ||
617 | + if len(lAccess) > 0 { | ||
618 | + currentAccess = lAccess[0] | ||
619 | + currentAccess.UpdatedTime = time.Now() | ||
620 | + } else { | ||
621 | + currentAccess = &domain.LoginAccess{ | ||
622 | + UserBaseId: ltoken.UserBaseId, | ||
623 | + UserId: ltoken.UserId, | ||
624 | + Account: ltoken.Account, | ||
625 | + Platform: domain.LoginPlatformApp, | ||
626 | + CompanyId: ltoken.CompanyId, | ||
627 | + OrganizationId: ltoken.OrgId, | ||
628 | + AccessToken: "", | ||
629 | + RefreshToken: "", | ||
630 | + AccessExpired: 0, | ||
631 | + RefreshExpired: 0, | ||
632 | + CreatedTime: time.Now(), | ||
633 | + UpdatedTime: time.Now(), | ||
634 | + } | ||
635 | + } | ||
636 | + accessTokenStr, err := ltoken.GenerateAccessToken() | ||
637 | + if err != nil { | ||
638 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
639 | + } | ||
640 | + currentAccess.AccessToken = accessTokenStr | ||
641 | + currentAccess.AccessExpired = ltoken.ExpiresAt | ||
642 | + refreshTokenStr, err := ltoken.GenerateRefreshToken() | ||
643 | + if err != nil { | ||
644 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
645 | + } | ||
646 | + currentAccess.RefreshToken = refreshTokenStr | ||
647 | + currentAccess.RefreshExpired = ltoken.ExpiresAt | ||
648 | + //先存数据库 | ||
649 | + _, err = loginAccessRepository.Save(currentAccess) | ||
650 | + if err != nil { | ||
651 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
652 | + } | ||
653 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
654 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
655 | + } | ||
656 | + //后处理redis缓存 | ||
657 | + tokenCache := cache.LoginTokenCache{} | ||
658 | + tokenCache.RemoveAccessToken(currentAccess.Account, domain.LoginPlatformApp) | ||
659 | + tokenCache.RemoveRefreshToken(currentAccess.Account, domain.LoginPlatformApp) | ||
660 | + tokenCache.SaveAccessToken(currentAccess) | ||
661 | + tokenCache.SaveRefreshToken(currentAccess) | ||
662 | + nowTime := time.Now().Unix() | ||
663 | + token := map[string]interface{}{ | ||
664 | + "refreshToken": accessTokenStr, | ||
665 | + "accessToken": refreshTokenStr, | ||
666 | + "expiresIn": currentAccess.AccessExpired - nowTime, | ||
667 | + } | ||
668 | + return map[string]interface{}{ | ||
669 | + "token": token, | ||
670 | + "userId": currentUserId, | ||
671 | + }, nil | ||
672 | +} | ||
673 | + | ||
674 | +//GetCompanyOrgsByUser 获取登录用户的公司组织列表 | ||
675 | +func (srv AuthService) GetCompanyOrgsByUser(queryParam *query.GetCompanyOrgsByUserQuery) (interface{}, error) { | ||
676 | + | ||
677 | + creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(queryParam.Operator) | ||
678 | + result, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{ | ||
679 | + Phone: queryParam.Phone, | ||
680 | + }) | ||
681 | + if err != nil { | ||
682 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
683 | + } | ||
684 | + var ( | ||
685 | + companys []dto.CompanyItem | ||
686 | + orgs []dto.OrgItem | ||
687 | + ) | ||
688 | + | ||
689 | + for _, v := range result.Users { | ||
690 | + companys = append(companys, dto.CompanyItem{ | ||
691 | + CompanyId: v.Company.CompanyId, | ||
692 | + CompanyName: v.Company.CompanyName, | ||
693 | + }) | ||
694 | + for _, vv := range v.UserOrg { | ||
695 | + orgs = append(orgs, dto.OrgItem{ | ||
696 | + OrganizationId: vv.OrgID, | ||
697 | + OrganizationName: vv.OrgName, | ||
698 | + CompanyId: v.Company.CompanyId, | ||
699 | + }) | ||
700 | + } | ||
701 | + } | ||
702 | + | ||
703 | + data := map[string]interface{}{ | ||
704 | + "companys": companys, | ||
705 | + "organizations": orgs, | ||
706 | + } | ||
707 | + return data, nil | ||
708 | +} | ||
709 | + | ||
710 | +//GetQrcode 获取扫码登录需要的二维码 | ||
711 | +func (srv AuthService) GetQrcode() (interface{}, error) { | ||
712 | + qrmsg := domain.QrcodeMessage{} | ||
713 | + _, err := qrmsg.GenerateImageBase64() //imgBase64 | ||
714 | + if err != nil { | ||
715 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
716 | + } | ||
717 | + qrCache := cache.LoginQrcodeCache{} | ||
718 | + err = qrCache.Save(qrmsg) | ||
719 | + if err != nil { | ||
720 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
721 | + } | ||
722 | + data := map[string]interface{}{ | ||
723 | + //"image": imgBase64, | ||
724 | + "key": qrmsg.Token, | ||
725 | + } | ||
726 | + return data, nil | ||
727 | +} | ||
728 | + | ||
729 | +//QrcodeLoginStatus 询问扫码登录状态 | ||
730 | +func (srv AuthService) QrcodeLoginStatus(queryParam *query.QrcodeLoginStatusQuery) (interface{}, error) { | ||
731 | + qrmsg := domain.QrcodeMessage{} | ||
732 | + err := qrmsg.ParseToken(queryParam.Key) | ||
733 | + if err != nil { | ||
734 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
735 | + } | ||
736 | + qrCache := cache.LoginQrcodeCache{} | ||
737 | + qrmsgCache, err := qrCache.Get(qrmsg.Id) | ||
738 | + if err != nil { | ||
739 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
740 | + } | ||
741 | + loginToken := domain.LoginToken{ | ||
742 | + UserId: qrmsgCache.UserId, | ||
743 | + UserBaseId: qrmsgCache.UserBaseId, | ||
744 | + Account: qrmsgCache.Account, | ||
745 | + Platform: domain.LoginPlatformWeb, | ||
746 | + CompanyId: qrmsgCache.CompanyId, | ||
747 | + OrgId: qrmsgCache.OrgId, | ||
748 | + } | ||
749 | + accessToken, err := loginToken.GenerateAccessToken() | ||
750 | + if err != nil { | ||
751 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
752 | + } | ||
753 | + _ = accessToken | ||
754 | + //TODO 填充token数据 | ||
755 | + data := map[string]interface{}{ | ||
756 | + "isLogin": qrmsgCache.IsLogin, | ||
757 | + "access": "", | ||
758 | + } | ||
759 | + return data, nil | ||
760 | +} | ||
761 | + | ||
762 | +//CheckSmsCode 验证手机短信验证码 | ||
763 | +func (srv AuthService) CheckSmsCode(smsCodeCommand *command.CheckSmsCodeCommand) (interface{}, error) { | ||
764 | + smsServeGateway := sms_serve.NewHttplibHttplibSmsServe() | ||
765 | + err := smsServeGateway.CheckSmsCode(smsCodeCommand.Phone, smsCodeCommand.SmsCode) | ||
766 | + if err != nil { | ||
767 | + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) | ||
768 | + } | ||
769 | + uid := uuid.New() | ||
770 | + pcc := cache.PhoneCheckCache{} | ||
771 | + if err := pcc.Add(uid.String(), cache.PhoneCheckItem{ | ||
772 | + Phone: smsCodeCommand.Phone, | ||
773 | + SmsCodeIdentity: uid.String(), | ||
774 | + Action: smsCodeCommand.Action, | ||
775 | + }); err != nil { | ||
776 | + log.Logger.Error(err.Error()) | ||
777 | + return nil, application.ThrowError(application.BUSINESS_ERROR, "系统错误") | ||
778 | + } | ||
779 | + return map[string]interface{}{ | ||
780 | + "smsCodeIdentity": uid.String(), | ||
781 | + }, nil | ||
782 | +} |
@@ -51,10 +51,11 @@ type CooperationProjectInfo struct { | @@ -51,10 +51,11 @@ type CooperationProjectInfo struct { | ||
51 | Department domain.Department `json:"department"` //项目发起部门 | 51 | Department domain.Department `json:"department"` //项目发起部门 |
52 | Status int `json:"status"` //项目状态 | 52 | Status int `json:"status"` //项目状态 |
53 | CooperationProjectSponsor struct { | 53 | CooperationProjectSponsor struct { |
54 | - UsersId int `json:"usersId"` | ||
55 | - UserInfo domain.UserInfo `json:"UserInfo"` | 54 | + UsersId int `json:"userId,string"` |
55 | + UserInfo domain.UserInfo `json:"userInfo"` | ||
56 | } `json:"cooperationProjectSponsor"` //共创发起人 | 56 | } `json:"cooperationProjectSponsor"` //共创发起人 |
57 | Attachment []domain.Attachment `json:"attachment"` //图片附件 | 57 | Attachment []domain.Attachment `json:"attachment"` //图片附件 |
58 | + Images []string `json:"images"` | ||
58 | } | 59 | } |
59 | 60 | ||
60 | func ToCooperationProjectInfo(projecetParam *allied_creation_cooperation.CooperationProject) *CooperationProjectInfo { | 61 | func ToCooperationProjectInfo(projecetParam *allied_creation_cooperation.CooperationProject) *CooperationProjectInfo { |
@@ -70,6 +71,10 @@ func ToCooperationProjectInfo(projecetParam *allied_creation_cooperation.Coopera | @@ -70,6 +71,10 @@ func ToCooperationProjectInfo(projecetParam *allied_creation_cooperation.Coopera | ||
70 | Attachment: projecetParam.Attachment, | 71 | Attachment: projecetParam.Attachment, |
71 | CooperationProjectDescription: projecetParam.CooperationProjectDescription, | 72 | CooperationProjectDescription: projecetParam.CooperationProjectDescription, |
72 | } | 73 | } |
74 | + for i := range projecetParam.Attachment { | ||
75 | + attachment := projecetParam.Attachment[i] | ||
76 | + data.Images = append(data.Images, attachment.Url) | ||
77 | + } | ||
73 | data.CooperationProjectSponsor.UsersId = projecetParam.CooperationProjectSponsor.UsersId | 78 | data.CooperationProjectSponsor.UsersId = projecetParam.CooperationProjectSponsor.UsersId |
74 | data.CooperationProjectSponsor.UserInfo.UsersId = projecetParam.CooperationProjectSponsor.UsersId | 79 | data.CooperationProjectSponsor.UserInfo.UsersId = projecetParam.CooperationProjectSponsor.UsersId |
75 | data.CooperationProjectSponsor.UserInfo.Phone = projecetParam.CooperationProjectSponsor.UserInfo.Phone | 80 | data.CooperationProjectSponsor.UserInfo.Phone = projecetParam.CooperationProjectSponsor.UserInfo.Phone |
@@ -40,7 +40,7 @@ type CooperationProjectInfo struct { | @@ -40,7 +40,7 @@ type CooperationProjectInfo struct { | ||
40 | UsersId int `json:"userId,string"` | 40 | UsersId int `json:"userId,string"` |
41 | } `json:"userInfo"` | 41 | } `json:"userInfo"` |
42 | } `json:"cooperationProjectSponsor"` //项目发起人 | 42 | } `json:"cooperationProjectSponsor"` //项目发起人 |
43 | - CooperationProjectUndertakerType []int `json:"cooperationProjectUndertakerType"` //共创合约承接对象,1员工,2共创用户,3公开 | 43 | + CooperationProjectUndertakerType []int `json:"cooperationProjectUndertakerTypes"` //共创合约承接对象,1员工,2共创用户,3公开 |
44 | Department struct { | 44 | Department struct { |
45 | DepartmentNumber string `json:"departmentNumber"` | 45 | DepartmentNumber string `json:"departmentNumber"` |
46 | DepartmentId int `json:"departmentId,string,"` | 46 | DepartmentId int `json:"departmentId,string,"` |
1 | package domain | 1 | package domain |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | - "bytes" | ||
5 | - "encoding/base64" | ||
6 | "fmt" | 4 | "fmt" |
7 | - "image/png" | ||
8 | "time" | 5 | "time" |
9 | 6 | ||
10 | - "github.com/boombuler/barcode" | ||
11 | - "github.com/boombuler/barcode/qr" | ||
12 | jwt "github.com/dgrijalva/jwt-go" | 7 | jwt "github.com/dgrijalva/jwt-go" |
13 | ) | 8 | ) |
14 | 9 | ||
@@ -51,22 +46,22 @@ func (qrmsg *QrcodeMessage) GenerateImageBase64() ([]byte, error) { | @@ -51,22 +46,22 @@ func (qrmsg *QrcodeMessage) GenerateImageBase64() ([]byte, error) { | ||
51 | qrmsg.Token = str | 46 | qrmsg.Token = str |
52 | qrmsg.IsLogin = false | 47 | qrmsg.IsLogin = false |
53 | 48 | ||
54 | - qrCode, err := qr.Encode(str, qr.M, qr.Auto) | ||
55 | - if err != nil { | ||
56 | - return nil, err | ||
57 | - } | ||
58 | - qrCode, err = barcode.Scale(qrCode, 200, 200) | ||
59 | - if err != nil { | ||
60 | - return nil, err | ||
61 | - } | ||
62 | - var buf bytes.Buffer | ||
63 | - err = png.Encode(&buf, qrCode) | ||
64 | - if err != nil { | ||
65 | - return nil, err | ||
66 | - } | ||
67 | - var result []byte | ||
68 | - base64.StdEncoding.Encode(result, buf.Bytes()) | ||
69 | - return result, err | 49 | + //qrCode, err := qr.Encode(str, qr.M, qr.Auto) |
50 | + //if err != nil { | ||
51 | + // return nil, err | ||
52 | + //} | ||
53 | + //qrCode, err = barcode.Scale(qrCode, 200, 200) | ||
54 | + //if err != nil { | ||
55 | + // return nil, err | ||
56 | + //} | ||
57 | + //var buf bytes.Buffer | ||
58 | + //err = png.Encode(&buf, qrCode) | ||
59 | + //if err != nil { | ||
60 | + // return nil, err | ||
61 | + //} | ||
62 | + //var result []byte | ||
63 | + //base64.StdEncoding.Encode(result, buf.Bytes()) | ||
64 | + return []byte(str), err | ||
70 | } | 65 | } |
71 | 66 | ||
72 | func (qrmsg *QrcodeMessage) ParseToken(str string) error { | 67 | func (qrmsg *QrcodeMessage) ParseToken(str string) error { |
@@ -2,7 +2,7 @@ package domain | @@ -2,7 +2,7 @@ package domain | ||
2 | 2 | ||
3 | //用户 | 3 | //用户 |
4 | type UserInfo struct { | 4 | type UserInfo struct { |
5 | - UsersName string `json:"usersName"` | 5 | + UsersName string `json:"userName"` |
6 | Phone string `json:"phone"` | 6 | Phone string `json:"phone"` |
7 | UsersId int `json:"userId,string"` | 7 | UsersId int `json:"userId,string"` |
8 | UserCode string `json:"userCode"` | 8 | UserCode string `json:"userCode"` |
@@ -24,7 +24,7 @@ func (lq LoginQrcodeCache) Save(qrcode domain.QrcodeMessage) error { | @@ -24,7 +24,7 @@ func (lq LoginQrcodeCache) Save(qrcode domain.QrcodeMessage) error { | ||
24 | } | 24 | } |
25 | key := lq.keyString(qrcode.Id) | 25 | key := lq.keyString(qrcode.Id) |
26 | bt, _ := json.Marshal(qrcode) | 26 | bt, _ := json.Marshal(qrcode) |
27 | - result := clientRedis.Set(key, string(bt), time.Duration(exp)) | 27 | + result := clientRedis.Set(key, string(bt), time.Duration(exp)*time.Second) |
28 | return result.Err() | 28 | return result.Err() |
29 | } | 29 | } |
30 | 30 |
@@ -204,6 +204,11 @@ type ( | @@ -204,6 +204,11 @@ type ( | ||
204 | //UserPhone string `json:"userPhone"` | 204 | //UserPhone string `json:"userPhone"` |
205 | //Status int `json:"status"` | 205 | //Status int `json:"status"` |
206 | } `json:"cooperationProjectSponsor"` | 206 | } `json:"cooperationProjectSponsor"` |
207 | + CooperationMode struct { | ||
208 | + CooperationModeId int `json:"cooperationModeId,string"` | ||
209 | + CooperationModeName string `json:"cooperationModeName"` | ||
210 | + CooperationModeNumber string `json:"cooperationModeNumber"` | ||
211 | + } `json:"cooperationMode"` //项目模式 | ||
207 | Department struct { | 212 | Department struct { |
208 | DepartmentID string `json:"departmentId"` | 213 | DepartmentID string `json:"departmentId"` |
209 | DepartmentName string `json:"departmentName"` | 214 | DepartmentName string `json:"departmentName"` |
1 | +package controllers | ||
2 | + | ||
3 | +import ( | ||
4 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/auth/command" | ||
5 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/auth/query" | ||
6 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/auth/service" | ||
7 | +) | ||
8 | + | ||
9 | +type AuthController struct { | ||
10 | + baseController | ||
11 | +} | ||
12 | + | ||
13 | +func (controller *AuthController) Login() { | ||
14 | + authService := service.AuthService{} | ||
15 | + loginCmd := &command.LoginCommand{} | ||
16 | + Must(controller.Unmarshal(loginCmd)) | ||
17 | + data, err := authService.AuthLogin(loginCmd) | ||
18 | + controller.Response(data, err) | ||
19 | +} | ||
20 | + | ||
21 | +func (controller *AuthController) LoginPwd() { | ||
22 | + authService := service.AuthService{} | ||
23 | + loginCmd := &command.LoginPwdCommand{} | ||
24 | + Must(controller.Unmarshal(loginCmd)) | ||
25 | + data, err := authService.AuthLoginPwd(loginCmd) | ||
26 | + controller.Response(data, err) | ||
27 | +} | ||
28 | + | ||
29 | +func (controller *AuthController) LoginSms() { | ||
30 | + authService := service.AuthService{} | ||
31 | + loginCmd := &command.LoginSmsCommand{} | ||
32 | + Must(controller.Unmarshal(loginCmd)) | ||
33 | + data, err := authService.AuthLoginSms(loginCmd) | ||
34 | + controller.Response(data, err) | ||
35 | +} | ||
36 | + | ||
37 | +func (controller *AuthController) SendSmsCode() { | ||
38 | + authService := service.AuthService{} | ||
39 | + cmd := &command.SendSmsCodeCommand{} | ||
40 | + err := controller.Unmarshal(cmd) | ||
41 | + if err != nil { | ||
42 | + controller.Response(nil, err) | ||
43 | + return | ||
44 | + } | ||
45 | + err = authService.SendSmsCaptcha(cmd) | ||
46 | + controller.Response(nil, err) | ||
47 | +} | ||
48 | + | ||
49 | +func (controller *AuthController) GetAuthAccessToken() { | ||
50 | + authService := service.AuthService{} | ||
51 | + accessTokenCommand := &command.AccessTokenCommand{} | ||
52 | + err := controller.Unmarshal(accessTokenCommand) | ||
53 | + if err != nil { | ||
54 | + controller.Response(nil, err) | ||
55 | + return | ||
56 | + } | ||
57 | + data, err := authService.GetAuthAccessToken(accessTokenCommand) | ||
58 | + controller.Response(data, err) | ||
59 | +} | ||
60 | + | ||
61 | +func (controller *AuthController) RefreshAuthAccessToken() { | ||
62 | + authService := service.AuthService{} | ||
63 | + refreshTokenCmd := &command.RefreshTokenCommand{} | ||
64 | + err := controller.Unmarshal(refreshTokenCmd) | ||
65 | + if err != nil { | ||
66 | + controller.Response(nil, err) | ||
67 | + return | ||
68 | + } | ||
69 | + data, err := authService.RefreshAuthAccessToken(refreshTokenCmd) | ||
70 | + controller.Response(data, err) | ||
71 | +} | ||
72 | + | ||
73 | +func (controller *AuthController) GetUserInfo() { | ||
74 | + authService := service.AuthService{} | ||
75 | + userInfoCommand := &command.UserInfoCommand{} | ||
76 | + err := controller.Unmarshal(userInfoCommand) | ||
77 | + if err != nil { | ||
78 | + controller.Response(nil, err) | ||
79 | + return | ||
80 | + } | ||
81 | + userInfoCommand.Operator = controller.GetOperator() | ||
82 | + data, err := authService.GetUserInfo(userInfoCommand) | ||
83 | + controller.Response(data, err) | ||
84 | +} | ||
85 | + | ||
86 | +func (controller *AuthController) GetUserMenus() { | ||
87 | + authService := service.AuthService{} | ||
88 | + userMenusCommand := &command.UserMenusCommand{} | ||
89 | + err := controller.Unmarshal(userMenusCommand) | ||
90 | + if err != nil { | ||
91 | + controller.Response(nil, err) | ||
92 | + return | ||
93 | + } | ||
94 | + userMenusCommand.Operator = controller.GetOperator() | ||
95 | + data, err := authService.GetUserMenus(userMenusCommand) | ||
96 | + controller.Response(data, err) | ||
97 | +} | ||
98 | + | ||
99 | +func (controller *AuthController) GetUserOrg() { | ||
100 | + authService := service.AuthService{} | ||
101 | + userOrgCommand := &command.UserOrgCommand{} | ||
102 | + err := controller.Unmarshal(userOrgCommand) | ||
103 | + if err != nil { | ||
104 | + controller.Response(nil, err) | ||
105 | + return | ||
106 | + } | ||
107 | + userOrgCommand.Operator = controller.GetOperator() | ||
108 | + data, err := authService.GetUserOrg(userOrgCommand) | ||
109 | + controller.Response(data, err) | ||
110 | +} | ||
111 | + | ||
112 | +func (controller *AuthController) CompanySignUp() { | ||
113 | + authService := service.AuthService{} | ||
114 | + userOrgCommand := &command.CompanySignUpCommand{} | ||
115 | + err := controller.Unmarshal(userOrgCommand) | ||
116 | + if err != nil { | ||
117 | + controller.Response(nil, err) | ||
118 | + return | ||
119 | + } | ||
120 | + //userOrgCommand.Operator = controller.GetOperator() | ||
121 | + data, err := authService.CompanySignUp(userOrgCommand) | ||
122 | + controller.Response(data, err) | ||
123 | +} | ||
124 | + | ||
125 | +func (controller *AuthController) ResetPassword() { | ||
126 | + authService := service.AuthService{} | ||
127 | + userOrgCommand := &command.ResetPasswordCommand{} | ||
128 | + err := controller.Unmarshal(userOrgCommand) | ||
129 | + if err != nil { | ||
130 | + controller.Response(nil, err) | ||
131 | + return | ||
132 | + } | ||
133 | + //userOrgCommand.Operator = controller.GetOperator() | ||
134 | + data, err := authService.ResetPassword(userOrgCommand) | ||
135 | + controller.Response(data, err) | ||
136 | +} | ||
137 | + | ||
138 | +func (controller *AuthController) OrgSwitch() { | ||
139 | + authService := service.AuthService{} | ||
140 | + cmd := &command.SwitchOrgCommand{} | ||
141 | + err := controller.Unmarshal(cmd) | ||
142 | + if err != nil { | ||
143 | + controller.Response(nil, err) | ||
144 | + return | ||
145 | + } | ||
146 | + cmd.Operator = controller.GetOperator() | ||
147 | + data, err := authService.OrgSwitch(cmd) | ||
148 | + controller.Response(data, err) | ||
149 | +} | ||
150 | + | ||
151 | +func (controller *AuthController) GetCompanyOrgsByUser() { | ||
152 | + authService := service.AuthService{} | ||
153 | + cmd := &query.GetCompanyOrgsByUserQuery{} | ||
154 | + Must(controller.Unmarshal(cmd)) | ||
155 | + | ||
156 | + cmd.Operator = controller.GetOperator() | ||
157 | + data, err := authService.GetCompanyOrgsByUser(cmd) | ||
158 | + controller.Response(data, err) | ||
159 | +} | ||
160 | + | ||
161 | +func (controller *AuthController) GetQrcode() { | ||
162 | + authService := service.AuthService{} | ||
163 | + data, err := authService.GetQrcode() | ||
164 | + controller.Response(data, err) | ||
165 | +} | ||
166 | + | ||
167 | +func (controller *AuthController) QrcodeLoginStatus() { | ||
168 | + authService := service.AuthService{} | ||
169 | + cmd := &query.QrcodeLoginStatusQuery{} | ||
170 | + Must(controller.Unmarshal(cmd)) | ||
171 | + data, err := authService.QrcodeLoginStatus(cmd) | ||
172 | + controller.Response(data, err) | ||
173 | +} | ||
174 | + | ||
175 | +func (controller *AuthController) CheckSmsCode() { | ||
176 | + authService := service.AuthService{} | ||
177 | + cmd := &command.CheckSmsCodeCommand{} | ||
178 | + Must(controller.Unmarshal(cmd)) | ||
179 | + data, err := authService.CheckSmsCode(cmd) | ||
180 | + controller.Response(data, err) | ||
181 | +} |
pkg/port/beego/controllers/baseController.go
0 → 100644
1 | +package controllers | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/linmadan/egglib-go/utils/json" | ||
5 | + "github.com/linmadan/egglib-go/web/beego" | ||
6 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain" | ||
7 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/log" | ||
8 | +) | ||
9 | + | ||
10 | +type baseController struct { | ||
11 | + beego.BaseController | ||
12 | +} | ||
13 | + | ||
14 | +func (controller *baseController) returnPageListData(count int64, data interface{}, err error, pageNumber int) { | ||
15 | + dataMap := map[string]interface{}{ | ||
16 | + "grid": map[string]interface{}{ | ||
17 | + "total": count, | ||
18 | + //"pageNumber": pageNumber, | ||
19 | + "list": data, | ||
20 | + }, | ||
21 | + } | ||
22 | + controller.Response(dataMap, err) | ||
23 | +} | ||
24 | + | ||
25 | +func (controller *baseController) returnListData(count int64, data interface{}, err error) { | ||
26 | + dataMap := map[string]interface{}{ | ||
27 | + "total": count, | ||
28 | + //"pageNumber": pageNumber, | ||
29 | + "list": data, | ||
30 | + } | ||
31 | + controller.Response(dataMap, err) | ||
32 | +} | ||
33 | + | ||
34 | +func (controller *baseController) GetUserId() int64 { | ||
35 | + return 1 | ||
36 | +} | ||
37 | + | ||
38 | +func (controller *baseController) GetCompanyId() int64 { | ||
39 | + return 1 | ||
40 | +} | ||
41 | + | ||
42 | +func (controller *baseController) GetUserBaseId() int64 { | ||
43 | + return 1 | ||
44 | +} | ||
45 | + | ||
46 | +func (controller *baseController) GetOrgId() int64 { | ||
47 | + return 1 | ||
48 | +} | ||
49 | + | ||
50 | +func (controller *baseController) GetOperator() domain.Operator { | ||
51 | + token := controller.Ctx.Input.Header("X-Mmm-Accesstoken") | ||
52 | + loginToken := &domain.LoginToken{} | ||
53 | + err := loginToken.ParseToken(token) | ||
54 | + if err != nil { | ||
55 | + log.Logger.Error(err.Error()) | ||
56 | + } | ||
57 | + op := domain.Operator{ | ||
58 | + UserId: loginToken.UserId, | ||
59 | + CompanyId: loginToken.CompanyId, | ||
60 | + OrgId: loginToken.OrgId, | ||
61 | + UserBaseId: loginToken.UserBaseId, | ||
62 | + Phone: loginToken.Account, | ||
63 | + Token: token, | ||
64 | + } | ||
65 | + // TODO:测试数据后期删除 | ||
66 | + if op.UserId == 0 { | ||
67 | + op.UserId = 9 | ||
68 | + op.CompanyId = 23 | ||
69 | + op.OrgId = 45 | ||
70 | + op.UserBaseId = 5 | ||
71 | + } | ||
72 | + // TODO:打印测试日志 | ||
73 | + log.Logger.Debug("operator " + json.MarshalToString(op)) | ||
74 | + return op | ||
75 | +} | ||
76 | + | ||
77 | +func Must(err error) { | ||
78 | + if err != nil { | ||
79 | + log.Logger.Error(err.Error()) | ||
80 | + } | ||
81 | +} |
pkg/port/beego/routers/auth_router_new.go
0 → 100644
1 | +package routers | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/beego/beego/v2/server/web" | ||
5 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/port/beego/controllers" | ||
6 | +) | ||
7 | + | ||
8 | +func init() { | ||
9 | + //web.Router("/v1/auth/login", &controllers.AuthController{}, "Post:Login") | ||
10 | + web.Router("/v1/auth/login/pwd", &controllers.AuthController{}, "Post:LoginPwd") | ||
11 | + web.Router("/v1/auth/login/sms", &controllers.AuthController{}, "Post:LoginSms") | ||
12 | + web.Router("/v1/auth/login/qrcode", &controllers.AuthController{}, "Post:QrcodeLoginStatus") | ||
13 | + web.Router("/v1/auth/qrcode-init", &controllers.AuthController{}, "Post:GetQrcode") | ||
14 | + web.Router("/v1/auth/sms-code", &controllers.AuthController{}, "Post:SendSmsCode") | ||
15 | + web.Router("/v1/auth/check-sms-code", &controllers.AuthController{}, "Post:CheckSmsCode") | ||
16 | + //web.Router("/v1/auth/access-token", &controllers.AuthController{}, "Post:GetAuthAccessToken") | ||
17 | + web.Router("/v1/auth/refresh-token", &controllers.AuthController{}, "Post:RefreshAuthAccessToken") | ||
18 | + web.Router("/v1/auth/company-sign-up", &controllers.AuthController{}, "Post:CompanySignUp") //公司用户注册 | ||
19 | + web.Router("/v1/auth/reset-password", &controllers.AuthController{}, "Post:ResetPassword") //公司重置密码 | ||
20 | + web.Router("/v1/auth/org-switch", &controllers.AuthController{}, "Post:OrgSwitch") | ||
21 | +} |
1 | -package routers | ||
2 | - | ||
3 | -import ( | ||
4 | - "github.com/beego/beego/v2/server/web" | ||
5 | - "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/port/beego/controllers/mobile_client" | ||
6 | -) | ||
7 | - | ||
8 | -func init() { | ||
9 | - web.Router("/v1/app/auth/smsCode", &mobile_client.UserController{}, "Post:SendSmsCode") | ||
10 | - web.Router("/v1/app/auth/check-phone", &mobile_client.UserController{}, "Post:CheckSmsCode") | ||
11 | - | ||
12 | - web.Router("/v1/app/users/change-password", &mobile_client.UserController{}, "Post:ChangePassword") | ||
13 | - web.Router("/v1/app/users/change-phone", &mobile_client.UserController{}, "Post:ChangePhone") | ||
14 | - web.Router("/v1/app/users/personal", &mobile_client.UserController{}, "Post:UpdateUserInfo") | ||
15 | - web.Router("/v1/app/users/destroy-account", &mobile_client.UserController{}, "Post:DestroyAccount") | ||
16 | -} |
pkg/port/beego/routers/user_router.go
0 → 100644
1 | +package routers | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/beego/beego/v2/server/web" | ||
5 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/port/beego/controllers" | ||
6 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/port/beego/controllers/mobile_client" | ||
7 | +) | ||
8 | + | ||
9 | +func init() { | ||
10 | + | ||
11 | + web.Router("/v1/user/company-orgs", &controllers.AuthController{}, "Post:GetCompanyOrgsByUser") | ||
12 | + web.Router("/v1/user/user-info", &controllers.AuthController{}, "Post:GetUserInfo") | ||
13 | + web.Router("/v1/user/user-menu", &controllers.AuthController{}, "Post:GetUserMenus") | ||
14 | + web.Router("/v1/user/user-orgs", &controllers.AuthController{}, "Post:GetUserOrg") | ||
15 | + | ||
16 | + web.Router("/v1/user/change-password", &mobile_client.UserController{}, "Post:ChangePassword") | ||
17 | + web.Router("/v1/user/change-phone", &mobile_client.UserController{}, "Post:ChangePhone") | ||
18 | + web.Router("/v1/user/personal", &mobile_client.UserController{}, "Post:UpdateUserInfo") | ||
19 | + web.Router("/v1/user/destroy-account", &mobile_client.UserController{}, "Post:DestroyAccount") | ||
20 | +} |
-
请 注册 或 登录 后发表评论