diff --git a/__pycache__/config.cpython-310.pyc b/__pycache__/config.cpython-310.pyc index 3b8704a..9af80f9 100644 Binary files a/__pycache__/config.cpython-310.pyc and b/__pycache__/config.cpython-310.pyc differ diff --git a/__pycache__/main.cpython-310.pyc b/__pycache__/main.cpython-310.pyc index 16fbb7f..cbc0604 100644 Binary files a/__pycache__/main.cpython-310.pyc and b/__pycache__/main.cpython-310.pyc differ diff --git a/config.py b/config.py index 4225599..7daf4a1 100644 --- a/config.py +++ b/config.py @@ -3,3 +3,11 @@ import os work_path = os.getcwd() wxbot_url = "http://127.0.0.1:2022/KP" # api地址 SECRET_KEY = '鉴权秘钥' # 鉴权秘钥 + +# 异度空间 wxid_sfvsru738v3122 +# 异度星球 wxid_haqoszjheyuz22 +account_wxid="wxid_sfvsru738v3122" +chatroomId='49187487591@chatroom' #异度星球 + +appid= 'wxa55815dd741a540d', +secret='82ce78719cc6b6e4a8ff738704d5b6cc', diff --git a/main.py b/main.py index b48e3d9..b4dee6f 100644 --- a/main.py +++ b/main.py @@ -26,6 +26,7 @@ import xml.dom.minidom from pydantic import BaseModel import utils import requests +import config models.Base.metadata.create_all(bind=engine) @@ -46,35 +47,66 @@ def get_db(): async def root(): return {"message": "Hello World"} + # 微信登录 @app.get("/login") -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() +def read_users(code, wxid, db: Session = Depends(get_db)): + print('code--', code) + new_user = {} + # 异度星球-小程序 + payload = { + 'appid': config.appid, + 'secret': config.secret, + 'js_code': code, + 'grant_type': 'authorization_code' + } + res = requests.get(config.tokenUrl, params=payload) + r = res.json() # 返回参数 session_key/unionid/errmsg/openid/errcode - print('r----',r) - openId=r['openid'] - print('openId----',openId) + print('r----', r) + openId = r['openid'] + print('openId----', openId) db_user = crud.get_user_by_openId(db, openId=openId) - print('db_user----',db_user) - + print('db_user----', db_user) + if db_user: # 返回用户完整数据 - new_user= db_user + new_user = db_user else: if wxid: # 机器人id - account_wxid='wxid_sfvsru738v3122' #异度空间 + account_wxid = config.account_wxid # 新增用户 - new_user=utils.saveWxidInfo(account_wxid, wxid,openId) + 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 new_user +# 加入微信群 +@app.get("/joingroup") +# 群-异度星球 49187487591@chatroom chatroom +def read_users(wxid, db: Session = Depends(get_db)): + # debug + # chatroomId = '49187487591@chatroom' + # 判断是否在群中 + inGroup = False + groupList = WXBotService.get_group_members_list( + robot_wxid=config.account_wxid, from_wxid=config.chatroomId) + print('groupList', groupList) + for groupItem in groupList['data']: + if groupItem['wxid'] == wxid: + inGroup = True + if inGroup: + return {"code": 200, "success": "fail", "message": "已经在群聊中"} + else: + WXBotService.add_group_members_40_more( + robot_wxid=config.account_wxid, + from_wxid=config.chatroomId, #异度星球群 + new_wxid=wxid) + print('wxid--', wxid) + return {"code": 200, "success": "ok", "message": "群聊邀请已发送"} + # 创建用户 @app.post("/users") @@ -135,7 +167,6 @@ def hellopost(requestData: dict): return json.dumps({"Code": 0}) - def kp(event_data): # unicode [\\uxxxxx] 转 emoji 表情 event_data = json.loads( @@ -230,7 +261,8 @@ def kp(event_data): kp_log = f'{type_str} 文本消息 内容:{msg}' if msg in ['加群']: # 将好友信息=>保存到数据库 - newFriendDict = WXBotService.get_friend_info(account_wxid, wxid) + newFriendDict = WXBotService.get_friend_info( + account_wxid, wxid) # print('newFriendDict[data]---',newFriendDict['data']) userClass = models.User(**(newFriendDict['data'])) # print('userClass====',type(userClass)) @@ -238,11 +270,19 @@ def kp(event_data): # 保存好友信息 # utils.saveWxidInfo(account_wxid, wxid) + + # 直接邀请加群>40 + # WXBotService.add_group_members_40_more(robot_wxid='wxid_sfvsru738v3122', + # from_wxid='49187487591@chatroom', + # new_wxid='wxid_4413224132412') + + # 发送小程序 utils.sendminiProgram(data, account_wxid, wxid, planetXml) + # if msg in ['腾讯云']: # # 发送小程序 - # utils.sendminiProgram(data, account_wxid, wxid, tengxunyunXml) + # utils.sendminiProgram(data, account_wxid, wxid, tengxunyunXml) elif msg_type == 3: # 3 图片消息 kp_log = f'{type_str} 图片消息 内容:{filepath}' @@ -318,7 +358,6 @@ def kp(event_data): f'pagepath:{xml_appmsg.findtext("weappinfo/pagepath")}' print("---------------------------kp_log", msg) - elif xml_type == '57': # 引用消息 kp_log = f'{type_str} 引用消息 内容:{msg}' elif xml_type == '2000': # 2000 转账消息