feat: 存储wxid与openId绑定

This commit is contained in:
hackrobot
2024-03-24 23:16:32 +08:00
parent e40688816d
commit e0e0e7eb57
5 changed files with 25 additions and 5 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

22
main.py
View File

@@ -48,15 +48,31 @@ async def root():
# 微信登录
@app.get("/login")
def read_users(code, db: Session = Depends(get_db)):
def read_users(code,wxid, db: Session = Depends(get_db)):
print('code--',code)
new_user={}
payload = {'appid': 'wxa55815dd741a540d', 'secret': '82ce78719cc6b6e4a8ff738704d5b6cc','js_code':code,'grant_type':'authorization_code'}
res = requests.get('https://api.weixin.qq.com/sns/jscode2session', params=payload)
r=res.json()
# 返回参数 session_key/unionid/errmsg/openid/errcode
print('r----',r)
openId=r['openid']
print('openId----',openId)
db_user = crud.get_user_by_openId(db, openId=openId)
print('db_user----',db_user)
if db_user:
# 返回用户完整数据
new_user= db_user
else:
if wxid:
# 机器人id
account_wxid='wxid_sfvsru738v3122' #异度空间
# 新增用户
new_user=utils.saveWxidInfo(account_wxid, wxid,openId)
# "errmsg":r['errmsg'],,"errcode":r['errcode']
return {"session_key":r['session_key'],"unionid":r['unionid'],"openid":r['openid']}
# return {"session_key":r['session_key'],"unionid":r['unionid'],"openid":r['openid']}
return new_user
@@ -221,7 +237,7 @@ def kp(event_data):
# crud.create_user(db=SessionLocal(),user=userClass)
# 保存好友信息
utils.saveWxidInfo(account_wxid, wxid)
# utils.saveWxidInfo(account_wxid, wxid)
# 发送小程序
utils.sendminiProgram(data, account_wxid, wxid, planetXml)
# if msg in ['腾讯云']:

View File

@@ -25,17 +25,21 @@ from pydantic import BaseModel
# 保存好友信息
def saveWxidInfo(account_wxid, wxid):
def saveWxidInfo(account_wxid, wxid,openId=None):
allFriend = WXBotService.get_address_list(account_wxid)
for friendItem in allFriend['data']["contactList"]:
if friendItem['wxid'] == wxid:
# 存储小程序openid
if openId:
friendItem['openId'] = openId
friendItemObj = models.User(**friendItem)
db_user = crud.get_user_by_wxid(db=SessionLocal(), wxid=wxid)
if db_user:
print("wxid already registered")
# return db_user
else:
crud.create_user(db=SessionLocal(), user=friendItemObj)
new_user=crud.create_user(db=SessionLocal(), user=friendItemObj)
return new_user
# 发送小程序模版,建议远程更新