作者 唐旭辉

权限设置调整

@@ -67,7 +67,10 @@ func (c *AuthController) Login() { @@ -67,7 +67,10 @@ func (c *AuthController) Login() {
67 if err != nil { 67 if err != nil {
68 log.Error("token 信息记录redis失败") 68 log.Error("token 信息记录redis失败")
69 } 69 }
70 - msg = protocol.NewReturnResponse(logintoken, nil) 70 + data := map[string]interface{}{
  71 + "access": logintoken,
  72 + }
  73 + msg = protocol.NewReturnResponse(data, nil)
71 return 74 return
72 } 75 }
73 76
@@ -113,6 +116,22 @@ func (c *AuthController) Me() { @@ -113,6 +116,22 @@ func (c *AuthController) Me() {
113 userid := c.GetUserId() 116 userid := c.GetUserId()
114 companyid := c.GetCompanyId() 117 companyid := c.GetCompanyId()
115 userinfo, err := serveauth.UserBaseInfo(userid, companyid) 118 userinfo, err := serveauth.UserBaseInfo(userid, companyid)
116 - msg = protocol.NewReturnResponse(userinfo, err) 119 + if err != nil {
  120 + log.Error("%s", err)
  121 + }
  122 + menus, err := serveauth.GetUserHasMenu(userid, companyid)
  123 + if err != nil {
  124 + log.Error("%s", err)
  125 + }
  126 + companys, err := serveauth.UserHasCompanys(userid)
  127 + if err != nil {
  128 + log.Error("%s", err)
  129 + }
  130 + data := map[string]interface{}{
  131 + "user": userinfo,
  132 + "menus": menus,
  133 + "companys": companys,
  134 + }
  135 + msg = protocol.NewReturnResponse(data, nil)
117 return 136 return
118 } 137 }
@@ -55,7 +55,7 @@ type ResponseMeInfo struct { @@ -55,7 +55,7 @@ type ResponseMeInfo struct {
55 Companyid int64 `json:"company_id"` 55 Companyid int64 `json:"company_id"`
56 Companyname string `json:"company_name"` 56 Companyname string `json:"company_name"`
57 Logo string `json:"logo"` 57 Logo string `json:"logo"`
58 - Companys []MeCompany `json:"companys"` 58 + Companys []MeCompany `json:"-"`
59 // Menu 59 // Menu
60 } 60 }
61 61
@@ -3,13 +3,11 @@ package auth @@ -3,13 +3,11 @@ package auth
3 import ( 3 import (
4 "crypto/sha1" 4 "crypto/sha1"
5 "encoding/hex" 5 "encoding/hex"
6 - "encoding/json"  
7 "fmt" 6 "fmt"
8 "io" 7 "io"
9 "oppmg/common/log" 8 "oppmg/common/log"
10 "oppmg/models" 9 "oppmg/models"
11 "oppmg/protocol" 10 "oppmg/protocol"
12 - "oppmg/services/ucenter"  
13 "oppmg/storage/redisdata" 11 "oppmg/storage/redisdata"
14 "oppmg/utils" 12 "oppmg/utils"
15 "strings" 13 "strings"
@@ -201,9 +199,15 @@ func LoginAuthByUCenter(account, password string) (protocol.LoginAuthToken, erro @@ -201,9 +199,15 @@ func LoginAuthByUCenter(account, password string) (protocol.LoginAuthToken, erro
201 log.Debug("no company") 199 log.Debug("no company")
202 return logintoken, protocol.NewErrWithMessage("10022") 200 return logintoken, protocol.NewErrWithMessage("10022")
203 } 201 }
  202 + //获取上一次登录的公司
  203 + uAuth, err := models.GetUserAuthByUser(userdata.Id)
  204 + if err == nil {
  205 + companyid = uAuth.CurrentCompanyId
  206 + } else {
204 companyid = companys[0].Id 207 companyid = companys[0].Id
  208 + }
205 // var uclientReturn *ucenter.ResponseLogin 209 // var uclientReturn *ucenter.ResponseLogin
206 - // uclientReturn, err = requestUCenterLogin(account, password) 210 + // uclientReturn, err = ucenter.RequestUCenterLogin(account, password)
207 // if err != nil { 211 // if err != nil {
208 // return logintoken, protocol.NewErrWithMessage("10021") 212 // return logintoken, protocol.NewErrWithMessage("10021")
209 // } 213 // }
@@ -223,31 +227,6 @@ func LoginAuthByUCenter(account, password string) (protocol.LoginAuthToken, erro @@ -223,31 +227,6 @@ func LoginAuthByUCenter(account, password string) (protocol.LoginAuthToken, erro
223 return logintoken, err 227 return logintoken, err
224 } 228 }
225 229
226 -func requestUCenterLogin(account, password string) (*ucenter.ResponseLogin, error) {  
227 - var uclientReturn *ucenter.ResponseLogin  
228 - param := ucenter.RequesLogin{  
229 - Type: 1,  
230 - Phone: account,  
231 - Password: password,  
232 - }  
233 - uclient := ucenter.NewUCenterClient()  
234 - btBody, err := uclient.Call(param)  
235 - if err != nil {  
236 - log.Error("统一用户中心请求失败 err:%s", err)  
237 - return nil, protocol.NewErrWithMessage("1")  
238 - }  
239 - err = json.Unmarshal(btBody, &uclientReturn)  
240 - if err != nil {  
241 - log.Error("解析统一用户中心响应失败 err:%s", err)  
242 - return nil, protocol.NewErrWithMessage("1")  
243 - }  
244 - if !(uclientReturn.Code == ucenter.ResponseCode0 &&  
245 - uclientReturn.Msg == ucenter.ResponseMsgOk) {  
246 - return nil, protocol.NewErrWithMessage("10021")  
247 - }  
248 - return uclientReturn, nil  
249 -}  
250 -  
251 type companybase struct { 230 type companybase struct {
252 Id int64 `orm:"column(id)"` 231 Id int64 `orm:"column(id)"`
253 Name string `orm:"coumn(name)"` 232 Name string `orm:"coumn(name)"`
@@ -288,7 +267,7 @@ func UserBaseInfo(userid, companyid int64) (protocol.ResponseMeInfo, error) { @@ -288,7 +267,7 @@ func UserBaseInfo(userid, companyid int64) (protocol.ResponseMeInfo, error) {
288 var ( 267 var (
289 err error 268 err error
290 userinfo *models.User 269 userinfo *models.User
291 - companylist []companybase 270 +
292 meInfo protocol.ResponseMeInfo 271 meInfo protocol.ResponseMeInfo
293 currentCompany *models.Company 272 currentCompany *models.Company
294 ) 273 )
@@ -302,11 +281,7 @@ func UserBaseInfo(userid, companyid int64) (protocol.ResponseMeInfo, error) { @@ -302,11 +281,7 @@ func UserBaseInfo(userid, companyid int64) (protocol.ResponseMeInfo, error) {
302 log.Error("GetCompanyById(%d) err:%s", companyid, err) 281 log.Error("GetCompanyById(%d) err:%s", companyid, err)
303 return meInfo, protocol.NewErrWithMessage("1", err) 282 return meInfo, protocol.NewErrWithMessage("1", err)
304 } 283 }
305 - companylist, err = getUserCompanyReal(userid)  
306 - if err != nil {  
307 - log.Error("getUserCompanyReal(%d) err:%s", userid, err)  
308 - return meInfo, protocol.NewErrWithMessage("1", err)  
309 - } 284 +
310 meInfo = protocol.ResponseMeInfo{ 285 meInfo = protocol.ResponseMeInfo{
311 NickName: userinfo.NickName, 286 NickName: userinfo.NickName,
312 Icon: userinfo.Icon, 287 Icon: userinfo.Icon,
@@ -314,12 +289,42 @@ func UserBaseInfo(userid, companyid int64) (protocol.ResponseMeInfo, error) { @@ -314,12 +289,42 @@ func UserBaseInfo(userid, companyid int64) (protocol.ResponseMeInfo, error) {
314 Companyname: currentCompany.Name, 289 Companyname: currentCompany.Name,
315 Logo: currentCompany.Logo, 290 Logo: currentCompany.Logo,
316 } 291 }
  292 +
  293 + return meInfo, nil
  294 +}
  295 +
  296 +func UserHasCompanys(userid int64) ([]protocol.MeCompany, error) {
  297 + var (
  298 + companylist []companybase
  299 + err error
  300 + mecompanys []protocol.MeCompany
  301 + )
  302 + companylist, err = getUserCompanyReal(userid)
  303 + if err != nil {
  304 + log.Error("getUserCompanyReal(%d) err:%s", userid, err)
  305 + return nil, protocol.NewErrWithMessage("1", err)
  306 + }
317 for _, v := range companylist { 307 for _, v := range companylist {
318 t := protocol.MeCompany{ 308 t := protocol.MeCompany{
319 Id: v.Id, 309 Id: v.Id,
320 Name: v.Name, 310 Name: v.Name,
321 } 311 }
322 - meInfo.Companys = append(meInfo.Companys, t) 312 + mecompanys = append(mecompanys, t)
323 } 313 }
324 - return meInfo, nil 314 + return mecompanys, nil
  315 +}
  316 +
  317 +func GetUserHasMenu(userid, companyid int64) ([]protocol.PermissionItem, error) {
  318 + const datasql string = `SELECT id,name,icon,parent_id,senior_status,sort,code
  319 + FROM menu WHERE enabled=1 ORDER BY sort `
  320 + var (
  321 + list []protocol.PermissionItem
  322 + err error
  323 + )
  324 + err = utils.ExecuteQueryAll(&list, datasql)
  325 + if err != nil {
  326 + log.Error("EXECUTE SQL err:%s", err)
  327 + return nil, protocol.NewErrWithMessage("1")
  328 + }
  329 + return list, nil
325 } 330 }
@@ -27,19 +27,19 @@ func UserAdd(param protocol.RequestUserAdd) error { @@ -27,19 +27,19 @@ func UserAdd(param protocol.RequestUserAdd) error {
27 err = validCompanyRole(param.CompanyId, param.Roles) 27 err = validCompanyRole(param.CompanyId, param.Roles)
28 if err != nil { 28 if err != nil {
29 log.Error(err.Error()) 29 log.Error(err.Error())
30 - return protocol.NewErrWithMessage("10031") 30 + return err
31 } 31 }
32 //校验部门 32 //校验部门
33 err = validCompanyDepart(param.CompanyId, param.Departments) 33 err = validCompanyDepart(param.CompanyId, param.Departments)
34 if err != nil { 34 if err != nil {
35 log.Error(err.Error()) 35 log.Error(err.Error())
36 - return protocol.NewErrWithMessage("10032") 36 + return err
37 } 37 }
38 //校验职位 38 //校验职位
39 err = validCompanyPosition(param.CompanyId, param.Positions) 39 err = validCompanyPosition(param.CompanyId, param.Positions)
40 if err != nil { 40 if err != nil {
41 log.Error(err.Error()) 41 log.Error(err.Error())
42 - return protocol.NewErrWithMessage("10033") 42 + return err
43 } 43 }
44 userm = &models.User{ 44 userm = &models.User{
45 NickName: param.Name, 45 NickName: param.Name,
@@ -344,12 +344,6 @@ func UserEdit(param protocol.RequestUserEdit) error { @@ -344,12 +344,6 @@ func UserEdit(param protocol.RequestUserEdit) error {
344 } 344 }
345 o := orm.NewOrm() 345 o := orm.NewOrm()
346 o.Begin() 346 o.Begin()
347 - // err = registUser(userm, param.CompanyId, o)  
348 - // if err != nil {  
349 - // o.Rollback()  
350 - // log.Error("registUser err:%s", err)  
351 - // return protocol.NewErrWithMessage("1")  
352 - // }  
353 // TODO 用户数据更新 347 // TODO 用户数据更新
354 //添加角色 348 //添加角色
355 err = editUserRole(userm.Id, param.CompanyId, param.Roles, o) 349 err = editUserRole(userm.Id, param.CompanyId, param.Roles, o)
@@ -154,5 +154,4 @@ func RoleMenuEdit(companyid int64, roleId int64, menuids []int64) error { @@ -154,5 +154,4 @@ func RoleMenuEdit(companyid int64, roleId int64, menuids []int64) error {
154 } 154 }
155 o.Commit() 155 o.Commit()
156 return nil 156 return nil
157 -  
158 } 157 }
  1 +package ucenter
  2 +
  3 +import (
  4 + "encoding/json"
  5 + "oppmg/common/log"
  6 + "oppmg/protocol"
  7 +)
  8 +
  9 +func RequestUCenterLogin(account, password string) (*ResponseLogin, error) {
  10 + var uclientReturn *ResponseLogin
  11 + param := RequesLogin{
  12 + Type: 1,
  13 + Phone: account,
  14 + Password: password,
  15 + }
  16 + uclient := NewUCenterClient()
  17 + btBody, err := uclient.Call(param)
  18 + if err != nil {
  19 + log.Error("统一用户中心请求失败 err:%s", err)
  20 + return nil, protocol.NewErrWithMessage("1")
  21 + }
  22 + err = json.Unmarshal(btBody, &uclientReturn)
  23 + if err != nil {
  24 + log.Error("解析统一用户中心响应失败 err:%s", err)
  25 + return nil, protocol.NewErrWithMessage("1")
  26 + }
  27 + if !(uclientReturn.Code == ResponseCode0 &&
  28 + uclientReturn.Msg == ResponseMsgOk) {
  29 + return nil, protocol.NewErrWithMessage("10021")
  30 + }
  31 + return uclientReturn, nil
  32 +}