76 lines
2.5 KiB
Python
76 lines
2.5 KiB
Python
from fastapi import Depends, FastAPI, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
|
|
# from . import crud, models, schemas
|
|
import crud, models, schemas
|
|
from database import SessionLocal, engine
|
|
# from .database import SessionLocal, engine
|
|
|
|
import json
|
|
import os
|
|
import threading
|
|
import time
|
|
from pydantic import BaseModel
|
|
from flask import request, Flask
|
|
try:
|
|
import xml.etree.cElementTree as ET
|
|
except ImportError:
|
|
import xml.etree.ElementTree as ET
|
|
|
|
import WXBotService
|
|
from planet import planetXml
|
|
from lxml import etree
|
|
import xml.dom.minidom
|
|
from pydantic import BaseModel
|
|
|
|
|
|
# 保存好友信息
|
|
def saveWxidInfo(account_wxid, wxid,openId=None,h5openid=None):
|
|
print('wxid',wxid)
|
|
print('openId',openId)
|
|
print('h5openid',h5openid)
|
|
|
|
allFriend = WXBotService.get_address_list(account_wxid)
|
|
for friendItem in allFriend['data']["contactList"]:
|
|
if friendItem['wxid'] == wxid:
|
|
# 存储小程序openid
|
|
if openId:
|
|
friendItem['openId'] = openId
|
|
if h5openid:
|
|
friendItem['h5openid'] = h5openid
|
|
friendItemObj = models.User(**friendItem)
|
|
db_user = crud.get_user_by_wxid(db=SessionLocal(), wxid=wxid)
|
|
if db_user:
|
|
print("wxid already registered")
|
|
print("开始更新数据表")
|
|
crud.update_user_field(db=SessionLocal(),wxid=wxid,openId=openId,h5openid=h5openid)
|
|
# 更新数据
|
|
# return db_user
|
|
else:
|
|
new_user=crud.create_user(db=SessionLocal(), user=friendItemObj)
|
|
return new_user
|
|
|
|
|
|
# 发送小程序模版,建议远程更新
|
|
def sendminiProgram(data, account_wxid, wxid, msgXml):
|
|
# 发送小程序
|
|
if data.get('isSender') != 1:
|
|
root = ET.fromstring(msgXml) #xml-字符串形式
|
|
|
|
#打开xml文档
|
|
# current_dir = os.path.dirname(os.path.abspath(__file__))
|
|
# print('current_dir+/planet.xml',current_dir+'\planet.xml')
|
|
# tree = ET.parse(current_dir+'\planet.xml')
|
|
# root = tree.getroot()
|
|
|
|
# 微信好友的wxid
|
|
# wxid='wxid_w2imh7ki5s4q12'
|
|
for pagepath in root.iter('pagepath'):
|
|
print(pagepath.text) # 打印名字
|
|
pagepath.text = pagepath.text + f'?wxid={wxid}'
|
|
print('修改path', pagepath.text) # 打印名字
|
|
|
|
# 修改后的xml转成string , method='xml'
|
|
msgXml = ET.tostring(root, encoding='utf-8').decode('utf-8')
|
|
WXBotService.send_xml_message(account_wxid, wxid, msgXml)
|