update
This commit is contained in:
parent
824b73741e
commit
f2fdc85c80
@ -68,7 +68,7 @@ async def wechat_phone_login(
|
|||||||
|
|
||||||
# # 获取企业微信的 userid
|
# # 获取企业微信的 userid
|
||||||
wecom_client = WecomClient()
|
wecom_client = WecomClient()
|
||||||
wecom_info = await wecom_client.miniprogram_to_userid(request.login_code)
|
wecom_info = await wecom_client.miniprogram_to_userid(openid=openid)
|
||||||
print(f"获取到的企业微信用户信息: {wecom_info}")
|
print(f"获取到的企业微信用户信息: {wecom_info}")
|
||||||
|
|
||||||
wecom_userid = None
|
wecom_userid = None
|
||||||
|
|||||||
@ -34,37 +34,42 @@ class WecomClient:
|
|||||||
logging.error(f"获取access_token失败: {str(e)}")
|
logging.error(f"获取access_token失败: {str(e)}")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
async def miniprogram_to_userid(self, code: str) -> Optional[Dict]:
|
async def miniprogram_to_userid(self, openid: str) -> Optional[Dict]:
|
||||||
"""
|
"""
|
||||||
小程序code转换为企业微信userid
|
转换小程序身份到企业微信
|
||||||
注意:这里使用code而不是openid
|
可以使用code或openid
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
|
# 获取provider_token
|
||||||
|
provider_token = await self.get_provider_token()
|
||||||
|
|
||||||
|
# 构建请求参数
|
||||||
url = "https://qyapi.weixin.qq.com/cgi-bin/miniprogram/jscode2session"
|
url = "https://qyapi.weixin.qq.com/cgi-bin/miniprogram/jscode2session"
|
||||||
params = {
|
params = {
|
||||||
"access_token": await self.get_access_token(),
|
"access_token": await self.get_access_token(),
|
||||||
"js_code": code,
|
"appid": settings.WECHAT_APPID,
|
||||||
"grant_type": "authorization_code"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# 根据传入参数类型选择
|
||||||
|
params["openid"] = openid
|
||||||
|
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
async with session.get(url, params=params) as response:
|
async with session.get(url, params=params) as response:
|
||||||
result = await response.json()
|
result = await response.json()
|
||||||
print(result)
|
logging.info(f"转换结果: {result}")
|
||||||
|
|
||||||
if result.get("errcode") == 0:
|
if result.get("errcode") == 0:
|
||||||
data = {
|
return {
|
||||||
"userid": result.get("userid"),
|
"userid": result.get("userid"), # 如果是企业成员,返回userid
|
||||||
"pending_id": result.get("pending_id"),
|
"pending_id": result.get("pending_id"), # 如果是外部联系人,返回pending_id
|
||||||
|
"openid": result.get("openid"),
|
||||||
"session_key": result.get("session_key")
|
"session_key": result.get("session_key")
|
||||||
}
|
}
|
||||||
return data
|
|
||||||
else:
|
else:
|
||||||
logging.error(f"code转换失败: {result}")
|
logging.error(f"身份转换失败: {result}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"code转换失败: {str(e)}")
|
logging.error(f"身份转换失败: {str(e)}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
async def get_user_info(self, userid: str) -> Optional[Dict]:
|
async def get_user_info(self, userid: str) -> Optional[Dict]:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user