企业微信8.22之后根据用户id获取信息已停用,修改登录认证

pull/814/head
code huan 2022-08-28 17:56:46 +08:00
parent 5a9d097344
commit b9510a7057
2 changed files with 256 additions and 187 deletions

View File

@ -406,7 +406,10 @@ func (c *AccountController) WorkWeixinLoginCallback() {
contact_access_token, ok := workweixin.GetAccessToken(true)
if ok {
logs.Warning("contact_access_token: ", contact_access_token)
user_info, err_msg, ok := workweixin.RequestUserInfo(contact_access_token, user_id)
// 获取用户信息
//user_info, err_msg, ok := workweixin.RequestUserInfo(contact_access_token, user_id)
// 获取用户id 列表
user_info, err_msg, ok := workweixin.GetUserListId(contact_access_token, user_id)
if ok {
// [-------所有字段-Debug----------
// user_info.UserId
@ -502,7 +505,7 @@ func (c *AccountController) WorkWeixinLoginCallback() {
// WorkWeixinLoginBind 用户企业微信登录-绑定
func (c *AccountController) WorkWeixinLoginBind() {
if user_info, ok := c.GetSession(SessionUserInfoKey).(workweixin.WorkWeixinUserInfo); ok && len(user_info.UserId) > 0 {
if user_info, ok := c.GetSession(SessionUserInfoKey).(workweixin.WorkWeixinDeptUserInfo); ok && len(user_info.UserId) > 0 {
req_account := c.GetString("account")
req_password := c.GetString("password")
if req_account == "" || req_password == "" {
@ -517,29 +520,30 @@ func (c *AccountController) WorkWeixinLoginBind() {
ormer := orm.NewOrm()
o, err := ormer.Begin()
if err != nil {
logs.Error("开启事时出错 -> ", err)
c.JsonResult(500, "开启事时出错: ", err.Error())
logs.Error("开启事时出错 -> ", err)
c.JsonResult(500, "开启事时出错: ", err.Error())
}
if err := account.AddBind(ormer); err != nil {
o.Rollback()
c.JsonResult(500, "绑定失败,数据库错误: "+err.Error())
} else {
// 绑定成功之后修改用户信息
member.LastLoginTime = time.Now()
member.RealName = user_info.Name
member.Avatar = user_info.Avatar
//member.RealName = user_info.Name
//member.Avatar = user_info.Avatar
if len(member.Avatar) < 1 {
member.Avatar = conf.GetDefaultAvatar()
}
member.Email = user_info.Email
member.Phone = user_info.Mobile
//member.Email = user_info.Email
//member.Phone = user_info.Mobile
if _, err := ormer.Update(member, "last_login_time", "real_name", "avatar", "email", "phone"); err != nil {
o.Rollback()
logs.Error("保存用户信息失败=>", err)
c.JsonResult(500, "绑定失败,现有账户信息更新失败: "+err.Error())
} else {
if err := o.Commit(); err != nil {
logs.Error("提交事物时出错 -> ", err)
c.JsonResult(500, "提交事物时出错: ", err.Error())
logs.Error("开启事务时出错 -> ", err)
c.JsonResult(500, "开启事务时出错: ", err.Error())
} else {
c.DelSession(SessionUserInfoKey)
c.SetMember(*member)
@ -588,8 +592,8 @@ func (c *AccountController) WorkWeixinLoginIgnore() {
ormer := orm.NewOrm()
o, err := ormer.Begin()
if err != nil {
logs.Error("开启事时出错 -> ", err)
c.JsonResult(500, "开启事时出错: ", err.Error())
logs.Error("开启事时出错 -> ", err)
c.JsonResult(500, "开启事时出错: ", err.Error())
}
member.Account = user_info.UserId
@ -630,8 +634,8 @@ func (c *AccountController) WorkWeixinLoginIgnore() {
c.JsonResult(500, "注册失败,数据库错误: "+err.Error())
} else {
if err := o.Commit(); err != nil {
logs.Error("提交事时出错 -> ", err)
c.JsonResult(500, "提交事时出错: ", err.Error())
logs.Error("提交事时出错 -> ", err)
c.JsonResult(500, "提交事时出错: ", err.Error())
} else {
member.LastLoginTime = time.Now()
_ = member.Update("last_login_time")

View File

@ -38,6 +38,15 @@ type UserIdResponse struct {
DeviceId string `json:"DeviceId"` // 设备号
}
// 获取成员ID列表-请求响应结构
type UserListIdResponse struct {
ErrCode int `json:"errcode"`
ErrMsg string `json:"errmsg"`
NextCursor string `json:"next_cursor"` // 分页游标,下次请求时填写以获取之后分页的记录
DeptUser []WorkWeixinDeptUserInfo `json:"dept_user"` // 用户-部门关系列表
}
// 获取用户信息-请求响应结构
type UserInfoResponse struct {
ErrCode int `json:"errcode"`
@ -63,6 +72,12 @@ type AccessTokenCache struct {
UpdateTime time.Time `json:"update_time"`
}
// 企业微信用户信息-结构
type WorkWeixinDeptUserInfo struct {
UserId string `json:"UserId"` // 企业成员UserID
Department int `json:"department"` // 成员所属部门id列表
}
// 企业微信用户信息-结构
type WorkWeixinUserInfo struct {
UserId string `json:"UserId"` // 企业成员UserID
@ -178,7 +193,11 @@ func RequestUserId(access_token string, code string) (user_id string, ok bool) {
return uir.UserId, true
}
// 获取用户详细信息-请求
/*
-
202281510 - - IP
url:https://developer.work.weixin.qq.com/document/path/96079
*/
func RequestUserInfo(contact_access_token string, userid string) (user_info WorkWeixinUserInfo, error_msg string, ok bool) {
url := "https://qyapi.weixin.qq.com/cgi-bin/user/get"
req := httplib.Get(url)
@ -220,3 +239,49 @@ func RequestUserInfo(contact_access_token string, userid string) (user_info Work
}
return info, "", true
}
/*
ID
*/
func GetUserListId(contact_access_token string, userid string) (user_info WorkWeixinDeptUserInfo, error_msg string, ok bool) {
url := "https://qyapi.weixin.qq.com/cgi-bin/user/list_id"
req := httplib.Get(url)
req.Param("access_token", contact_access_token) // 通讯录应用调用接口凭证
req.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: false})
req.AddFilters(httpFilter)
respStr, err := req.String()
_ = respStr
var info WorkWeixinDeptUserInfo
if err != nil {
logs.Error(err)
return info, "请求失败", false
} else {
logs.Debug(respStr)
}
// 返回响应
var uir UserListIdResponse
//获取用户信息失败: 请求数据结果错误
err = req.ToJSON(&uir)
if err != nil {
logs.Error(err)
return info, "请求数据结果错误", false
}
if uir.ErrCode != 0 {
return info, uir.ErrMsg, false
}
// 判断userid 中是否还有当前用户id
for i := 0; i < len(uir.DeptUser); i++ {
if uir.DeptUser[i].UserId == userid {
info = WorkWeixinDeptUserInfo{
UserId: uir.DeptUser[i].UserId,
}
} else {
return info, uir.ErrMsg, false
}
}
return info, "", true
}