summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore3
-rw-r--r--mini.py129
-rw-r--r--static/index.css37
-rw-r--r--static/ipmch.pngbin0 -> 5097 bytes
-rw-r--r--templates/__init__.py0
-rw-r--r--templates/forma.html31
6 files changed, 200 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..44289f3
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+__pycache__/
+.undodir/
+trash/
diff --git a/mini.py b/mini.py
new file mode 100644
index 0000000..cfd57ce
--- /dev/null
+++ b/mini.py
@@ -0,0 +1,129 @@
+from flask import Flask, request, render_template
+from xml.etree import ElementTree
+from xml.dom import minidom
+import time
+
+app = Flask(__name__)
+
+
+def parse_xml(xml_str: str) -> dict:
+ """function by Fred Yin"""
+ root = ElementTree.fromstring(xml_str)
+ result = {}
+ for child in root:
+ result[child.tag] = child.text
+
+ return result
+
+
+@app.route("/wechat8000", methods=['GET', 'POST'])
+def main_server():
+ print(request)
+ # print xml file from wechat server
+ print("get_data\n", request.get_data())
+ xml_str = request.get_data().decode(encoding="utf-8")
+ # save xml file from wechat server
+ xml_data = parse_xml(xml_str)
+ """parse xml and convert it into dict of wechat info
+ Tags available in xml_data:
+ ToUserName
+ FromUserName
+ CreateTime
+ MsgType (e.g. text)
+ Content (the content sent by wechat user)
+ MsgId
+ """
+
+ if "Event" in xml_data.keys() and xml_data["Event"] == "subscribe":
+ flag = 1
+ elif "Content" in xml_data.keys() and xml_data["Content"] == "建卡":
+ flag = 2
+ else:
+ flag = 3
+
+ # start generating output xml file
+ doc = minidom.Document()
+
+ root = doc.createElement('xml')
+ # generate <xml> root
+ doc.appendChild(root)
+
+ node_2a = doc.createElement('ToUserName')
+ node_2a_content = doc.createCDATASection(xml_data["FromUserName"])
+ node_2a.appendChild(node_2a_content)
+ root.appendChild(node_2a)
+ # generate <ToUserName> i.e. wechat user id
+
+ node_2b = doc.createElement('FromUserName')
+ node_2b_content = doc.createCDATASection(xml_data["ToUserName"])
+ node_2b.appendChild(node_2b_content)
+ root.appendChild(node_2b)
+ # generate <FromUserName> i.e. server id
+
+ node_2c = doc.createElement('CreateTime')
+ node_2c_content = doc.createTextNode(str(int(time.time())))
+ node_2c.appendChild(node_2c_content)
+ root.appendChild(node_2c)
+ # generate <CreateTime> i.e. time stamp in seconds
+
+ node_2d = doc.createElement('MsgType')
+ node_2d_content = doc.createCDATASection("text")
+ node_2d.appendChild(node_2d_content)
+ root.appendChild(node_2d)
+ # generate <MsgType> we only support text now
+
+ if flag == 1:
+ node_2e = doc.createElement('Content')
+ node_2e_content = doc.createCDATASection("""欢迎关注新业态实践团2022公众号"""
+ """您可回复"建卡"二字获得建卡链接""")
+ node_2e.appendChild(node_2e_content)
+ root.appendChild(node_2e)
+ # generate <Content> i.e. the link of form that collect information of users
+ elif flag == 2:
+ node_2e = doc.createElement('Content')
+ node_2e_content = doc.createCDATASection(f"""http://13.250.19.215/form/{xml_data["FromUserName"]}""")
+ node_2e.appendChild(node_2e_content)
+ root.appendChild(node_2e)
+ # generate <Content> i.e. the link of form that collect information of users
+ else:
+ # else just tell wechat server I'm still working
+ return "success"
+
+ xml_str = doc.toprettyxml(indent="\t")
+ xml_str = xml_str.replace("""<?xml version="1.0" ?>\n""", "")
+ """save_path_file = "develop.xml"
+
+ with open(save_path_file, "w") as f:
+ f.write(xml_str)"""
+ # end generating output xml file
+ return xml_str
+
+
+@app.route("/form/<wechat_id>", methods=["GET", "POST"])
+def form(wechat_id):
+ """generate the html layout of form that collect information of users
+ the link of a user is, for example, http://IP:port/form/jsfdfhskjh87493yhut
+ designed personalized to pass user id together with information in form
+ *containing security risk, neglect for now"""
+ dictgan = {
+ 'wechat_id': wechat_id,
+ }
+ if request.method == 'GET':
+ # deploying html template under ./templates/forma.html
+ # pass dictgan to collect user id
+ return render_template("forma.html", data=dictgan)
+
+ if request.method == 'POST':
+ name = request.form.get('name')
+ person_id = request.form.get('person_id')
+ last_menstruation = request.form.get('last_menstruation')
+ wechat_id = request.form.get("wechat_id")
+ print(name, person_id, last_menstruation, wechat_id)
+ # print the information
+ # containing security risk
+ # complete page
+ return '<center><font size=30><b>Done, thank you!</b></font></center>'
+
+
+if __name__ == "__main__":
+ app.run(host='0.0.0.0', port=8000, debug=True)
diff --git a/static/index.css b/static/index.css
new file mode 100644
index 0000000..5d0ef7b
--- /dev/null
+++ b/static/index.css
@@ -0,0 +1,37 @@
+body {
+ font-family: sans-serif;
+}
+
+#title-logo, #title-text {
+ display: inline;
+ vertical-align: middle;
+}
+
+label {
+ display: inline;
+ vertical-align: middle;
+}
+
+input.box {
+ display: inline;
+ vertical-align: middle;
+ min-height: 3em;
+ max-width: 80%;
+ margin: 1em;
+ border: 1px gray solid;
+ border-radius: 0.5em;
+}
+
+input.btn {
+ color: white;
+ background-color: #f79517;
+ font-size: 2em;
+ padding: 0.5em 1em 0.5em 1em;
+ margin: 1em;
+ border-radius: 0.5em;
+ border: none;
+}
+
+input.btn:hover, input.btn:active {
+ filter: drop-shadow(0 0 0.5em gray);
+}
diff --git a/static/ipmch.png b/static/ipmch.png
new file mode 100644
index 0000000..0f1b184
--- /dev/null
+++ b/static/ipmch.png
Binary files differ
diff --git a/templates/__init__.py b/templates/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/templates/__init__.py
diff --git a/templates/forma.html b/templates/forma.html
new file mode 100644
index 0000000..3ff4f77
--- /dev/null
+++ b/templates/forma.html
@@ -0,0 +1,31 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <meta charset="UTF-8">
+ <title>国妇婴微信建卡</title>
+ <link href="/static/index.css" rel="stylesheet" />
+ <link href="/static/ipmch.png" rel="icon" />
+ <meta name="viewport" content="width=device-width, initial-scale=1">
+</head>
+<body>
+<center>
+ <h1>
+ <img id="title-logo" src="/static/ipmch.png" />
+ <div id="title-text">国妇婴微信建卡</div>
+ </h1>
+ <form method="POST" action="">
+ <input class="box" type="hidden" name="wechat_id" value={{ data.wechat_id }}>
+ <label for="name"> 姓名:</label>
+ <input class="box" type="text" name="name">
+ <br>
+ <label for="person_id">证件号:</label>
+ <input class="box" type="text" name="person_id">
+ <br>
+ <label for="last_menstruation">末次月经:</label>
+ <input class="box" type="date" name="last_menstruation">
+ <br>
+ <input class="btn" type="submit" name="submit" value="🕊 提交">
+ </form>
+</center>
+</body>
+</html>