feat: 存储wxid与openId绑定
This commit is contained in:
Binary file not shown.
Binary file not shown.
BIN
kunpeng.db
BIN
kunpeng.db
Binary file not shown.
22
main.py
22
main.py
@@ -48,15 +48,31 @@ async def root():
|
|||||||
|
|
||||||
# 微信登录
|
# 微信登录
|
||||||
@app.get("/login")
|
@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)
|
print('code--',code)
|
||||||
|
new_user={}
|
||||||
payload = {'appid': 'wxa55815dd741a540d', 'secret': '82ce78719cc6b6e4a8ff738704d5b6cc','js_code':code,'grant_type':'authorization_code'}
|
payload = {'appid': 'wxa55815dd741a540d', 'secret': '82ce78719cc6b6e4a8ff738704d5b6cc','js_code':code,'grant_type':'authorization_code'}
|
||||||
res = requests.get('https://api.weixin.qq.com/sns/jscode2session', params=payload)
|
res = requests.get('https://api.weixin.qq.com/sns/jscode2session', params=payload)
|
||||||
r=res.json()
|
r=res.json()
|
||||||
# 返回参数 session_key/unionid/errmsg/openid/errcode
|
# 返回参数 session_key/unionid/errmsg/openid/errcode
|
||||||
print('r----',r)
|
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']
|
# "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)
|
# crud.create_user(db=SessionLocal(),user=userClass)
|
||||||
|
|
||||||
# 保存好友信息
|
# 保存好友信息
|
||||||
utils.saveWxidInfo(account_wxid, wxid)
|
# utils.saveWxidInfo(account_wxid, wxid)
|
||||||
# 发送小程序
|
# 发送小程序
|
||||||
utils.sendminiProgram(data, account_wxid, wxid, planetXml)
|
utils.sendminiProgram(data, account_wxid, wxid, planetXml)
|
||||||
# if msg in ['腾讯云']:
|
# if msg in ['腾讯云']:
|
||||||
|
|||||||
8
utils.py
8
utils.py
@@ -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)
|
allFriend = WXBotService.get_address_list(account_wxid)
|
||||||
for friendItem in allFriend['data']["contactList"]:
|
for friendItem in allFriend['data']["contactList"]:
|
||||||
if friendItem['wxid'] == wxid:
|
if friendItem['wxid'] == wxid:
|
||||||
|
# 存储小程序openid
|
||||||
|
if openId:
|
||||||
|
friendItem['openId'] = openId
|
||||||
friendItemObj = models.User(**friendItem)
|
friendItemObj = models.User(**friendItem)
|
||||||
db_user = crud.get_user_by_wxid(db=SessionLocal(), wxid=wxid)
|
db_user = crud.get_user_by_wxid(db=SessionLocal(), wxid=wxid)
|
||||||
if db_user:
|
if db_user:
|
||||||
print("wxid already registered")
|
print("wxid already registered")
|
||||||
# return db_user
|
# return db_user
|
||||||
else:
|
else:
|
||||||
crud.create_user(db=SessionLocal(), user=friendItemObj)
|
new_user=crud.create_user(db=SessionLocal(), user=friendItemObj)
|
||||||
|
return new_user
|
||||||
|
|
||||||
|
|
||||||
# 发送小程序模版,建议远程更新
|
# 发送小程序模版,建议远程更新
|
||||||
|
|||||||
Reference in New Issue
Block a user