作者 唐旭辉

日常提交存储

@@ -2,10 +2,10 @@ @@ -2,10 +2,10 @@
2 2
3 | 功能 | 完成状态 | 时间 | 接口路径 | 请求方式 | 3 | 功能 | 完成状态 | 时间 | 接口路径 | 请求方式 |
4 | -------------- | ------------------ | --------------- | --------------------------------- | -------- | 4 | -------------- | ------------------ | --------------- | --------------------------------- | -------- |
5 -| 登录 | 进行中 | | | 5 +| 登录获取 token | 进行中 | | /auth/login | post |
6 | 短信验证码 | 未开始 | | | 6 | 短信验证码 | 未开始 | | |
7 -| 获取 token | 未开始 | | |  
8 -| 刷新 token | 未开始 | | | 7 +| 刷新 token | 未开始 | | /v1/auth/refresh_token | post |
  8 +| 切换公司 | 未开始 | | /v1/auth/change_company |
9 | 获取角色列表 | 完成 | | /v1/rbac/:companyid/role | get | 9 | 获取角色列表 | 完成 | | /v1/rbac/:companyid/role | get |
10 | 添加角色 | 完成 | 2019.11.26 | /v1/rbac/role | post | 10 | 添加角色 | 完成 | 2019.11.26 | /v1/rbac/role | post |
11 | 编辑角色 | 完成 | 2019.11.26 | /v1/rbac/role | put | 11 | 编辑角色 | 完成 | 2019.11.26 | /v1/rbac/role | put |
@@ -8,14 +8,18 @@ import ( @@ -8,14 +8,18 @@ import (
8 8
9 //MyConfig 自定义配置选项 9 //MyConfig 自定义配置选项
10 type MyConfig struct { 10 type MyConfig struct {
11 - ConfigName string //配置名称  
12 - SqlConn string //数据库连接  
13 - RedisAddPort string //  
14 - RedisAuth string  
15 - RedisDB int  
16 - LogOutput string  
17 - LogFilename string  
18 - LogLevel string 11 + ConfigName string //配置名称
  12 + SqlConn string //数据库连接
  13 + RedisAddPort string //
  14 + RedisAuth string
  15 + RedisDB int
  16 + LogOutput string
  17 + LogFilename string
  18 + LogLevel string
  19 + UcenterCheckAlt string
  20 + UcenterBaseUrl string
  21 + UcenterSecret string
  22 + UcenterAppKey string
19 } 23 }
20 24
21 //MConfig 25 //MConfig
@@ -31,14 +35,18 @@ func RestMyConfig() *MyConfig { @@ -31,14 +35,18 @@ func RestMyConfig() *MyConfig {
31 mysqlUser, mysqlPassword, mysqlHost, mysqlPort, mysqlDBname) 35 mysqlUser, mysqlPassword, mysqlHost, mysqlPort, mysqlDBname)
32 sqlconn = sqlconn + "?charset=utf8&loc=Asia%2FShanghai" 36 sqlconn = sqlconn + "?charset=utf8&loc=Asia%2FShanghai"
33 MConfig = &MyConfig{ 37 MConfig = &MyConfig{
34 - ConfigName: beego.AppConfig.String("config_name"),  
35 - SqlConn: sqlconn,  
36 - RedisAddPort: beego.AppConfig.String("redis_add_port"),  
37 - RedisAuth: beego.AppConfig.DefaultString("redis_auth", ""),  
38 - RedisDB: beego.AppConfig.DefaultInt("redis_db", 0),  
39 - LogOutput: beego.AppConfig.DefaultString("log_output", "console"),  
40 - LogFilename: beego.AppConfig.DefaultString("log_filename", "./log/ability.log"),  
41 - LogLevel: beego.AppConfig.DefaultString("log_Level", "debug"), 38 + ConfigName: beego.AppConfig.String("config_name"),
  39 + SqlConn: sqlconn,
  40 + RedisAddPort: beego.AppConfig.String("redis_add_port"),
  41 + RedisAuth: beego.AppConfig.DefaultString("redis_auth", ""),
  42 + RedisDB: beego.AppConfig.DefaultInt("redis_db", 0),
  43 + LogOutput: beego.AppConfig.DefaultString("log_output", "console"),
  44 + LogFilename: beego.AppConfig.DefaultString("log_filename", "./log/ability.log"),
  45 + LogLevel: beego.AppConfig.DefaultString("log_Level", "debug"),
  46 + UcenterCheckAlt: beego.AppConfig.String("ucenter_check_alt"),
  47 + UcenterBaseUrl: beego.AppConfig.String("ucenter_base_url"),
  48 + UcenterSecret: beego.AppConfig.String("ucenter_secret"),
  49 + UcenterAppKey: beego.AppConfig.String("ucenter_app_key"),
42 } 50 }
43 return MConfig 51 return MConfig
44 } 52 }
@@ -24,4 +24,12 @@ redis_auth = "" @@ -24,4 +24,12 @@ redis_auth = ""
24 log_output = "file" 24 log_output = "file"
25 log_filename = "${aliyun_logs_access||./log/ability.log}" 25 log_filename = "${aliyun_logs_access||./log/ability.log}"
26 log_level = "${LOG_LEVEL||error}" 26 log_level = "${LOG_LEVEL||error}"
  27 +
  28 +##统一用户中心相关配置
  29 +ucenter_check_alt = "rsF0pL!6DwjBO735"
  30 +ucenter_base_url = "suplus-ucenter-dev.fjmaimaimai.com"
  31 +ucenter_secret = "12412213"
  32 +ucenter_app_key = "111222"
  33 +# 审批流程修改 消息发布
  34 +message_publish = "audit_change"
27 #---自定义配置 结束---- 35 #---自定义配置 结束----
@@ -22,4 +22,9 @@ redis_auth = "" @@ -22,4 +22,9 @@ redis_auth = ""
22 log_output = "file" 22 log_output = "file"
23 log_filename = "${LOG_FILENAME||./log/ability.log}" 23 log_filename = "${LOG_FILENAME||./log/ability.log}"
24 log_level = "${LOG_LEVEL||debug}" 24 log_level = "${LOG_LEVEL||debug}"
  25 +##统一用户中心相关配置
  26 +ucenter_app_key = ""
  27 +ucenter_base_url = ""
  28 +# 审批流程修改 消息发布
  29 +message_publish = "audit_change"
25 #---自定义配置 结束---- 30 #---自定义配置 结束----
@@ -5,6 +5,7 @@ import ( @@ -5,6 +5,7 @@ import (
5 "oppmg/common/log" 5 "oppmg/common/log"
6 "oppmg/protocol" 6 "oppmg/protocol"
7 serveauth "oppmg/services/auth" 7 serveauth "oppmg/services/auth"
  8 + "oppmg/storage/redisdata"
8 ) 9 )
9 10
10 type AuthController struct { 11 type AuthController struct {
@@ -61,5 +62,32 @@ func (c *AuthController) Login() { @@ -61,5 +62,32 @@ func (c *AuthController) Login() {
61 //ChangeCompany 切换公司 62 //ChangeCompany 切换公司
62 //@Router /change_company [post] 63 //@Router /change_company [post]
63 func (c *AuthController) ChangeCompany() { 64 func (c *AuthController) ChangeCompany() {
  65 + var msg *protocol.ResponseMessage
  66 + defer func() {
  67 + c.ResposeJson(msg)
  68 + }()
64 69
  70 + var param protocol.RequestSwapCompany
  71 + if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
  72 + log.Error("json 解析失败", err)
  73 + msg = protocol.BadRequestParam("1")
  74 + return
  75 + }
  76 + userid := c.GetUserId()
  77 + if param.CompanyId <= 0 {
  78 + msg = protocol.BadRequestParam("1")
  79 + return
  80 + }
  81 +
  82 + logintoken, err := serveauth.ChangeLoginToken(userid, param.CompanyId)
  83 + if err != nil {
  84 + msg = protocol.NewReturnResponse(nil, err)
  85 + return
  86 + }
  87 + err = redisdata.SetLoginToken(logintoken, userid)
  88 + if err != nil {
  89 + log.Error("redisdata.SetLoginToken err:%s", err)
  90 + }
  91 + msg = protocol.NewReturnResponse(logintoken, err)
  92 + return
65 } 93 }
@@ -13,7 +13,6 @@ import ( @@ -13,7 +13,6 @@ import (
13 //BaseController 基础 13 //BaseController 基础
14 type BaseController struct { 14 type BaseController struct {
15 beego.Controller 15 beego.Controller
16 - // AppHead protocol.BaseHeader  
17 } 16 }
18 17
19 //Prepare 实现beego.ControllerInterface 的接口 18 //Prepare 实现beego.ControllerInterface 的接口
@@ -25,11 +24,6 @@ func (this *BaseController) Prepare() { @@ -25,11 +24,6 @@ func (this *BaseController) Prepare() {
25 func (this *BaseController) GetAppHead() (appHead protocol.BaseHeader) { 24 func (this *BaseController) GetAppHead() (appHead protocol.BaseHeader) {
26 appHead.AccessToken = this.Ctx.Input.Header(protocol.HeaderAccessToken) 25 appHead.AccessToken = this.Ctx.Input.Header(protocol.HeaderAccessToken)
27 appHead.RefreshToken = this.Ctx.Input.Header(protocol.HeaderRefreshToken) 26 appHead.RefreshToken = this.Ctx.Input.Header(protocol.HeaderRefreshToken)
28 - appHead.UID, _ = strconv.Atoi(this.Ctx.Input.Header(protocol.HeaderUID))  
29 - appHead.UUID = this.Ctx.Input.Header(protocol.HeaderUUID)  
30 - appHead.Timestamp, _ = strconv.Atoi(this.Ctx.Input.Header(protocol.HeaderTimestamp))  
31 - appHead.Devicetype = this.Ctx.Input.Header(protocol.HeaderDevicetype)  
32 - appHead.AppProject = this.Ctx.Input.Header(protocol.HeaderAppproject)  
33 return 27 return
34 28
35 } 29 }
@@ -50,3 +44,15 @@ func (this *BaseController) ResposeJson(msg *protocol.ResponseMessage) { @@ -50,3 +44,15 @@ func (this *BaseController) ResposeJson(msg *protocol.ResponseMessage) {
50 this.Data["json"] = msg 44 this.Data["json"] = msg
51 this.ServeJSON() 45 this.ServeJSON()
52 } 46 }
  47 +
  48 +func (this *BaseController) GetCompanyId() int64 {
  49 + v := this.Ctx.Input.GetData(protocol.HeaderCompanyid)
  50 + companyid, _ := strconv.ParseInt(fmt.Sprint(v), 10, 64)
  51 + return companyid
  52 +}
  53 +
  54 +func (this *BaseController) GetUserId() int64 {
  55 + v := this.Ctx.Input.GetData(protocol.HeaderUserid)
  56 + userid, _ := strconv.ParseInt(fmt.Sprint(v), 10, 64)
  57 + return userid
  58 +}
@@ -76,28 +76,9 @@ var AuthToken = func(ctx *context.Context) { @@ -76,28 +76,9 @@ var AuthToken = func(ctx *context.Context) {
76 return 76 return
77 } 77 }
78 if ok := serveauth.IsJwtErrorExpired(err); ok { 78 if ok := serveauth.IsJwtErrorExpired(err); ok {
79 - //token过期  
80 - mtoken, err = serveauth.ValidJWTToken(refreshToken)  
81 - if err != nil {  
82 - msg = protocol.NewMesage("10024")  
83 - ctx.Output.JSON(msg, false, false)  
84 - return  
85 - }  
86 - storetoken, err = redisdata.GetLoginToken(mtoken.UID)  
87 - if err != nil {  
88 - log.Error("err:%s", err)  
89 - msg = protocol.NewMesage("10024")  
90 - ctx.Output.JSON(msg, false, false)  
91 - return  
92 - }  
93 - if storetoken.AccessToken != accesstoken {  
94 - msg = protocol.NewMesage("10025")  
95 - ctx.Output.JSON(msg, false, false)  
96 - return  
97 - }  
98 - logintoken, _ := serveauth.GenerateAuthToken(mtoken.UID, mtoken.CompanyID)  
99 - serveauth.ResetLoginTokenRedis(logintoken)  
100 - msg = protocol.NewReturnResponse(logintoken, nil) 79 + //token过期,刷新
  80 + logintoken, err := serveauth.RefreshLoginToken(refreshToken)
  81 + msg = protocol.NewReturnResponse(logintoken, err)
101 ctx.Output.JSON(msg, false, false) 82 ctx.Output.JSON(msg, false, false)
102 return 83 return
103 } 84 }
@@ -28,6 +28,16 @@ const ( @@ -28,6 +28,16 @@ const (
28 COMPANY_ENABLE_NO int8 = 2 //无效 28 COMPANY_ENABLE_NO int8 = 2 //无效
29 ) 29 )
30 30
  31 +func (t *Company) IsEnable() bool {
  32 + switch t.Enable {
  33 + case COMPANY_ENABLE_YES:
  34 + return true
  35 + case COMPANY_ENABLE_NO:
  36 + return false
  37 + }
  38 + return false
  39 +}
  40 +
31 func (t *Company) ValidateEnable(v int8) bool { 41 func (t *Company) ValidateEnable(v int8) bool {
32 switch v { 42 switch v {
33 case COMPANY_ENABLE_YES: 43 case COMPANY_ENABLE_YES:
@@ -15,12 +15,37 @@ type UserCompany struct { @@ -15,12 +15,37 @@ type UserCompany struct {
15 CommentTotal int `orm:"column(comment_total)" description:"发表评论总数"` 15 CommentTotal int `orm:"column(comment_total)" description:"发表评论总数"`
16 CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"` 16 CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"`
17 UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"` 17 UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"`
  18 + Enable int8 `orm:"column(enable)"`
  19 + DeleteAt time.Time `orm:"column(delete_at)"`
18 } 20 }
19 21
20 func (t *UserCompany) TableName() string { 22 func (t *UserCompany) TableName() string {
21 return "user_company" 23 return "user_company"
22 } 24 }
23 25
  26 +//用户的公司是否有效
  27 +const (
  28 + USERCOMPANY_ENABLE_YES int8 = 1 //有效
  29 + USERCOMPANY_ENABLE_NO int8 = 2 // 无效
  30 +)
  31 +
  32 +func (t *UserCompany) IsEnable() bool {
  33 + switch t.Enable {
  34 + case USERCOMPANY_ENABLE_YES:
  35 + return true
  36 + case USERCOMPANY_ENABLE_NO:
  37 + return false
  38 + }
  39 + return false
  40 +}
  41 +
  42 +func (t *UserCompany) IsDelete() bool {
  43 + if t.DeleteAt.Unix() > 0 {
  44 + return true
  45 + }
  46 + return false
  47 +}
  48 +
24 func init() { 49 func init() {
25 orm.RegisterModel(new(UserCompany)) 50 orm.RegisterModel(new(UserCompany))
26 } 51 }
@@ -60,3 +85,8 @@ func GetUserCompanyBy(userid int64, companyId int64) (*UserCompany, error) { @@ -60,3 +85,8 @@ func GetUserCompanyBy(userid int64, companyId int64) (*UserCompany, error) {
60 } 85 }
61 return v, nil 86 return v, nil
62 } 87 }
  88 +
  89 +func GetUserCompanyByUser(userid int64) ([]UserCompany, error) {
  90 + datasql := ``
  91 + return nil, nil
  92 +}
@@ -4,12 +4,6 @@ package protocol @@ -4,12 +4,6 @@ package protocol
4 const ( 4 const (
5 HeaderAccessToken string = "x-mmm-accesstoken" 5 HeaderAccessToken string = "x-mmm-accesstoken"
6 HeaderRefreshToken string = "x-mmm-refreshtoken" 6 HeaderRefreshToken string = "x-mmm-refreshtoken"
7 - // HeaderUID string = "x-mmm-uid"  
8 - // HeaderUUID string = "x-mmm-uuid"  
9 - // HeaderTimestamp string = "x-mmm-timestamp"  
10 - // HeaderDevicetype string = "x-mmm-devicetype"  
11 - // HeaderAppproject string = "x-mmm-appproject"  
12 - // HeaderSign string = "x-mmm-sign"  
13 ) 7 )
14 8
15 //用来存储从token中解析出来的内容对应的键名 9 //用来存储从token中解析出来的内容对应的键名
@@ -23,12 +17,6 @@ const ( @@ -23,12 +17,6 @@ const (
23 type BaseHeader struct { 17 type BaseHeader struct {
24 AccessToken string 18 AccessToken string
25 RefreshToken string 19 RefreshToken string
26 - AppProject string  
27 - Devicetype string  
28 - Sign string  
29 - UUID string  
30 - Timestamp int  
31 - UID int  
32 } 20 }
33 21
34 //RequestLogin 登录请求 22 //RequestLogin 登录请求
@@ -51,8 +39,10 @@ type LoginAuthToken struct { @@ -51,8 +39,10 @@ type LoginAuthToken struct {
51 39
52 //RequestSwapCompany 切换公司 40 //RequestSwapCompany 切换公司
53 type RequestSwapCompany struct { 41 type RequestSwapCompany struct {
  42 + CompanyId int64 `json:"company_id"`
54 } 43 }
55 44
56 // ResponseSwapCompany ... 45 // ResponseSwapCompany ...
57 type ResponseSwapCompany struct { 46 type ResponseSwapCompany struct {
  47 + LoginAuthToken
58 } 48 }
@@ -20,4 +20,5 @@ var errmessge ErrorMap = map[string]string{ @@ -20,4 +20,5 @@ var errmessge ErrorMap = map[string]string{
20 "10024": "登录凭证失效", 20 "10024": "登录凭证失效",
21 "10025": "该账号已在其他地方登录", 21 "10025": "该账号已在其他地方登录",
22 "10026": "登录凭证过期", 22 "10026": "登录凭证过期",
  23 + "10027": "无操作权限",
23 } 24 }
@@ -32,26 +32,25 @@ func init() { @@ -32,26 +32,25 @@ func init() {
32 beego.NSRouter("/role_group", &controllers.RbacController{}, "delete:RoleGroupDelete"), 32 beego.NSRouter("/role_group", &controllers.RbacController{}, "delete:RoleGroupDelete"),
33 beego.NSRouter("/:companyid([0-9]+)/role", &controllers.RbacController{}, "get:RoleList"), 33 beego.NSRouter("/:companyid([0-9]+)/role", &controllers.RbacController{}, "get:RoleList"),
34 ), 34 ),
  35 + beego.NSNamespace("/auth",
  36 + beego.NSRouter("/change_company", &controllers.AuthController{}, "post:ChangeCompany"),
  37 + beego.NSRouter("/refresh_token", &controllers.AuthController{}, "post:RefreshToken"),
  38 + ),
35 ) 39 )
36 40
37 nsAuth := beego.NewNamespace("/auth", 41 nsAuth := beego.NewNamespace("/auth",
38 beego.NSBefore(middleware.AllowOption), 42 beego.NSBefore(middleware.AllowOption),
39 beego.NSBefore(middleware.LogRequestData), 43 beego.NSBefore(middleware.LogRequestData),
40 beego.NSRouter("/login", &controllers.AuthController{}, "post:Login"), 44 beego.NSRouter("/login", &controllers.AuthController{}, "post:Login"),
41 - beego.NSNamespace("/token",  
42 - beego.NSBefore(middleware.AuthToken),  
43 - beego.NSRouter("/change_company", &controllers.AuthController{}, "post:ChangeCompany"),  
44 - beego.NSRouter("/refresh_token", &controllers.AuthController{}, "post:RefreshToken"),  
45 - ),  
46 ) 45 )
47 beego.AddNamespace(nsV1) 46 beego.AddNamespace(nsV1)
48 beego.AddNamespace(nsAuth) 47 beego.AddNamespace(nsAuth)
49 } 48 }
50 49
51 //routerPermission 路由对应的权限 50 //routerPermission 路由对应的权限
52 -var routerPermission = map[string]string{  
53 - "get:/v1/company/:companyid/position": "show",  
54 - "post:/v1/company/position": "add",  
55 - "put:/v1/company/position": "edit",  
56 - "delete:/v1/company/position": "delete",  
57 -} 51 +// var routerPermission = map[string]string{
  52 +// "get:/v1/company/:companyid/position": "show",
  53 +// "post:/v1/company/position": "add",
  54 +// "put:/v1/company/position": "edit",
  55 +// "delete:/v1/company/position": "delete",
  56 +// }
@@ -3,12 +3,14 @@ package auth @@ -3,12 +3,14 @@ package auth
3 import ( 3 import (
4 "crypto/sha1" 4 "crypto/sha1"
5 "encoding/hex" 5 "encoding/hex"
  6 + "encoding/json"
6 "fmt" 7 "fmt"
7 "io" 8 "io"
8 "oppmg/common/config" 9 "oppmg/common/config"
9 "oppmg/common/log" 10 "oppmg/common/log"
10 "oppmg/models" 11 "oppmg/models"
11 "oppmg/protocol" 12 "oppmg/protocol"
  13 + "oppmg/services/ucenter"
12 "oppmg/storage/redisdata" 14 "oppmg/storage/redisdata"
13 "strings" 15 "strings"
14 "time" 16 "time"
@@ -155,3 +157,102 @@ func ResetLoginTokenRedis(loginToken protocol.LoginAuthToken) error { @@ -155,3 +157,102 @@ func ResetLoginTokenRedis(loginToken protocol.LoginAuthToken) error {
155 return nil 157 return nil
156 158
157 } 159 }
  160 +
  161 +func ChangeLoginToken(userid, companyid int64) (protocol.LoginAuthToken, error) {
  162 + var (
  163 + logintoken protocol.LoginAuthToken
  164 + err error
  165 + usercompany *models.UserCompany
  166 + companydata *models.Company
  167 + )
  168 +
  169 + usercompany, err = models.GetUserCompanyBy(userid, companyid)
  170 + if err != nil {
  171 + log.Error("GetUserCompanyBy(userid, companyid) err:%s", err)
  172 + return logintoken, protocol.NewErrWithMessage("1")
  173 + }
  174 + if ok := usercompany.IsEnable(); !ok {
  175 + log.Debug("公司禁用此用户")
  176 + return logintoken, protocol.NewErrWithMessage("10027")
  177 + }
  178 + if ok := usercompany.IsDelete(); !ok {
  179 + log.Debug("公司删除此用户")
  180 + return logintoken, protocol.NewErrWithMessage("10027")
  181 + }
  182 + companydata, err = models.GetCompanyById(usercompany.CompanyId)
  183 + if err != nil {
  184 + log.Error("GetCompanyById(%d) err:%s", usercompany.CompanyId, err)
  185 + return logintoken, protocol.NewErrWithMessage("1")
  186 + }
  187 + if ok := companydata.IsEnable(); !ok {
  188 + log.Debug("无效公司")
  189 + return logintoken, protocol.NewErrWithMessage("10027")
  190 + }
  191 + logintoken, err = GenerateAuthToken(userid, companydata.Id)
  192 + if err != nil {
  193 + log.Error("GenerateAuthToken err:%s", err)
  194 + return logintoken, protocol.NewErrWithMessage("1")
  195 + }
  196 + return logintoken, nil
  197 +}
  198 +
  199 +func RefreshLoginToken(refreshtoken string) (protocol.LoginAuthToken, error) {
  200 + var (
  201 + logintoken protocol.LoginAuthToken
  202 + mtoken *MyToken
  203 + err error
  204 + storetoken protocol.LoginAuthToken
  205 + )
  206 + mtoken, err = ValidJWTToken(refreshtoken)
  207 + if err != nil {
  208 + log.Debug("token失效 err:%s", err)
  209 + return logintoken, protocol.NewErrWithMessage("10024")
  210 + }
  211 + storetoken, err = redisdata.GetLoginToken(mtoken.UID)
  212 + if err != nil {
  213 + log.Error("redis err:%s", err)
  214 + return logintoken, protocol.NewErrWithMessage("10024")
  215 + }
  216 + if storetoken.RefreshToken != refreshtoken {
  217 + return logintoken, protocol.NewErrWithMessage("10024")
  218 + }
  219 + logintoken, _ = GenerateAuthToken(mtoken.UID, mtoken.CompanyID)
  220 + ResetLoginTokenRedis(logintoken)
  221 + return logintoken, nil
  222 +}
  223 +
  224 +func LoginAuthByUCenter(account, password string) (protocol.LoginAuthToken, error) {
  225 + var (
  226 + err error
  227 + logintoken protocol.LoginAuthToken
  228 + uclientReturn ucenter.ResponseLogin
  229 + )
  230 +
  231 + _, err := models.GetUserByPhone(account)
  232 + if err != nil {
  233 + log.Debug("GetUserByPhone(%s) err:%s", account, err)
  234 + return logintoken, protocol.NewErrWithMessage("10021")
  235 + }
  236 +
  237 + param := ucenter.RequesLogin{
  238 + Phone: account,
  239 + Password: password,
  240 + }
  241 + uclient := ucenter.NewUCenterClient()
  242 + btBody, err := uclient.Call(param)
  243 + if err != nil {
  244 + log.Error("统一用户中心请求失败 err:%s", err)
  245 + return logintoken, protocol.NewErrWithMessage("1")
  246 + }
  247 + err = json.Unmarshal(btBody, &uclientReturn)
  248 + if err != nil {
  249 + log.Error("解析统一用户中心响应失败 err:%s", err)
  250 + return logintoken, protocol.NewErrWithMessage("1")
  251 + }
  252 + if !(uclientReturn.Code == ucenter.ResponseCode0 &&
  253 + uclientReturn.Msg == ucenter.ResponseMsgOk) {
  254 + return logintoken, protocol.NewErrWithMessage("10021")
  255 + }
  256 +
  257 + return logintoken, err
  258 +}
@@ -323,6 +323,7 @@ func DepartmentDelete(param protocol.RequestDepartmentDelete) error { @@ -323,6 +323,7 @@ func DepartmentDelete(param protocol.RequestDepartmentDelete) error {
323 } 323 }
324 } 324 }
325 o.Commit() 325 o.Commit()
  326 +
326 return nil 327 return nil
327 } 328 }
328 329
@@ -334,7 +335,7 @@ func DepartmentListAll(companyId int64) ([]protocol.ResponseDepartmentInfo, erro @@ -334,7 +335,7 @@ func DepartmentListAll(companyId int64) ([]protocol.ResponseDepartmentInfo, erro
334 err error 335 err error
335 ) 336 )
336 const ( 337 const (
337 - datasql0 string = `SELECT id, company_id,name,parent_id,member,managers ` + 338 + datasql0 string = `SELECT id, company_id,name,parent_id,member,managers,delete_at ` +
338 ` FROM department WHERE company_id = ? AND delete_at = 0` 339 ` FROM department WHERE company_id = ? AND delete_at = 0`
339 ) 340 )
340 err = utils.ExecuteQueryAll(&departmodels, datasql0, companyId) 341 err = utils.ExecuteQueryAll(&departmodels, datasql0, companyId)
@@ -5,6 +5,12 @@ import ( @@ -5,6 +5,12 @@ import (
5 "errors" 5 "errors"
6 ) 6 )
7 7
  8 +//CommResponse 公共响应结构
  9 +type CommResponse struct {
  10 + Code int `json:"code"`
  11 + Msg string `json:"msg"`
  12 +}
  13 +
8 type RequesLogin struct { 14 type RequesLogin struct {
9 Password string `json:"password"` 15 Password string `json:"password"`
10 Phone string `json:"phone"` 16 Phone string `json:"phone"`
@@ -33,6 +39,16 @@ func (r RequesLogin) Valid() error { @@ -33,6 +39,16 @@ func (r RequesLogin) Valid() error {
33 return nil 39 return nil
34 } 40 }
35 41
  42 +type ResponseLogin struct {
  43 + CommResponse
  44 + Data struct {
  45 + Id int64 `json:"id"`
  46 + Phone string `json:"phone"`
  47 + NickName string `json:"nickname"` //昵称
  48 + Avatar string `json:"avatar"` //头像
  49 + } `json:"data"`
  50 +}
  51 +
36 type RequestAddUser struct { 52 type RequestAddUser struct {
37 Phone string `json:"phone"` 53 Phone string `json:"phone"`
38 RegIm bool `json:"regIm"` 54 RegIm bool `json:"regIm"`
@@ -2,11 +2,20 @@ package ucenter @@ -2,11 +2,20 @@ package ucenter
2 2
3 import ( 3 import (
4 "bytes" 4 "bytes"
  5 + "crypto/sha1"
  6 + "fmt"
5 "io/ioutil" 7 "io/ioutil"
6 "net/http" 8 "net/http"
  9 + mconfig "oppmg/common/config"
  10 + "oppmg/common/log"
7 "time" 11 "time"
8 ) 12 )
9 13
  14 +const (
  15 + ResponseCode0 int = 0
  16 + ResponseMsgOk string = "ok"
  17 +)
  18 +
10 type IUCenterParam interface { 19 type IUCenterParam interface {
11 Format() []byte 20 Format() []byte
12 GetPath() (string, string) //返回请求路径path,请求方式mathod 21 GetPath() (string, string) //返回请求路径path,请求方式mathod
@@ -18,13 +27,31 @@ type UCenterClient struct { @@ -18,13 +27,31 @@ type UCenterClient struct {
18 baseUrl string 27 baseUrl string
19 } 28 }
20 29
  30 +func NewUCenterClient() *UCenterClient {
  31 + return &UCenterClient{
  32 + appKey: mconfig.MConfig.UcenterAppKey,
  33 + baseUrl: mconfig.MConfig.UcenterBaseUrl,
  34 + }
  35 +}
  36 +
21 func (client UCenterClient) buildHeader() http.Header { 37 func (client UCenterClient) buildHeader() http.Header {
22 var h = http.Header{} 38 var h = http.Header{}
  39 + nowTime := fmt.Sprint(time.Now().Unix())
  40 + checksum := buildCheckSum(nowTime)
23 h.Set("Content-Type", "application/json") 41 h.Set("Content-Type", "application/json")
24 h.Set("appKey", client.appKey) 42 h.Set("appKey", client.appKey)
  43 + h.Set("nonce", "")
  44 + h.Set("curTime", nowTime)
  45 + h.Set("checkSum", checksum)
25 return h 46 return h
26 } 47 }
27 48
  49 +func buildCheckSum(nowTime string) string {
  50 + str := fmt.Sprintf("%s%s%s", nowTime, mconfig.MConfig.UcenterSecret, mconfig.MConfig.UcenterCheckAlt)
  51 + bt := sha1.Sum([]byte(str))
  52 + return fmt.Sprintf("%x", bt)
  53 +}
  54 +
28 //httpDo post发送json 55 //httpDo post发送json
29 func (client UCenterClient) httpDo(path string, mathod string, posts []byte) ([]byte, error) { 56 func (client UCenterClient) httpDo(path string, mathod string, posts []byte) ([]byte, error) {
30 httpclient := http.Client{ 57 httpclient := http.Client{
@@ -45,7 +72,8 @@ func (client UCenterClient) httpDo(path string, mathod string, posts []byte) ([] @@ -45,7 +72,8 @@ func (client UCenterClient) httpDo(path string, mathod string, posts []byte) ([]
45 if err != nil { 72 if err != nil {
46 return nil, err 73 return nil, err
47 } 74 }
48 - 75 + log.Info("====>Send To UCenter:%s", string(posts))
  76 + log.Info("<====UCenter Return:%s", string(body))
49 return body, nil 77 return body, nil
50 } 78 }
51 79
  1 +package memery
  2 +
  3 +import (
  4 + "sync"
  5 +)
  6 +
  7 +//内存存储
  8 +type StoreAuditChange struct {
  9 + data sync.Map
  10 +}
  11 +
  12 +func (s *StoreAuditChange) SetKey(key, value string) {
  13 + s.data.Store(key, value)
  14 +}
  15 +
  16 +func (s *StoreAuditChange) DeleteKey(key string) {
  17 + s.data.Delete(key)
  18 +}
@@ -56,3 +56,5 @@ func ExistLoginToken(userid int64) bool { @@ -56,3 +56,5 @@ func ExistLoginToken(userid int64) bool {
56 } 56 }
57 return false 57 return false
58 } 58 }
  59 +
  60 +//消息发布订阅