From 91f77a6957d8e58e2679fb7be95a2e60f2e42e60 Mon Sep 17 00:00:00 2001 From: RainSun Date: Sun, 3 Jan 2021 18:30:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=8C=E7=BB=B4=E7=A0=81?= =?UTF-8?q?=E7=94=9F=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitlab-ci.yml | 29 ++++++++++++++++ Dockerfile | 8 +++++ go.sh | 2 +- lib/__init__.py | 0 lib/allFunction.py | 31 ++++++++++++++++-- lib/db.py | 2 +- lib/utils.py | 15 ++------- logs/acess.log | 0 logs/error.log | 0 qrcode.py => main.py | 23 ++++++------- ...92b9c0d3bd3d539b700bc41.jpg.png => bg.png} | Bin qr/qrcode.html | 5 --- requirements.txt | 15 +++++++++ 13 files changed, 96 insertions(+), 34 deletions(-) create mode 100644 .gitlab-ci.yml create mode 100644 Dockerfile create mode 100644 lib/__init__.py create mode 100644 logs/acess.log create mode 100644 logs/error.log rename qrcode.py => main.py (81%) rename qr/img/{2934349b033b5bb5092b9c0d3bd3d539b700bc41.jpg.png => bg.png} (100%) create mode 100644 requirements.txt diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..722ac89 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,29 @@ +stages: + - clear + - build + - deploy + +clear: + stage: clear + tags: + - dockerbase + script: + - docker stop qrcode + - docker rm qrcode + allow_failure: true + + +build: + stage: build + tags: + - dockerbase + script: + - ls -a + - docker build -t lacus/qrcode . + +deploy: + stage: deploy + tags: + - dockerbase + script: + - docker run -t -d --name qrcode -v /root/yingbo/qrcode/data:/app/data -p 5002:80 lacus/qrcode \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..51f66d7 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3.7-alpine +COPY requirements.txt /app/requirements.txt +WORKDIR /app +RUN apk --update add libxml2-dev libxslt-dev libffi-dev gcc musl-dev libgcc openssl-dev curl +RUN apk add jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev +RUN pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple +COPY . . +CMD sh go.sh && python diff --git a/go.sh b/go.sh index b26db36..bb7dddb 100644 --- a/go.sh +++ b/go.sh @@ -1,3 +1,3 @@ #!/bin/sh echo start -gunicorn qrcode:app -c gunicorn.conf.py \ No newline at end of file +gunicorn main:app -c gunicorn.conf.py \ No newline at end of file diff --git a/lib/__init__.py b/lib/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lib/allFunction.py b/lib/allFunction.py index dae9574..a137bd9 100644 --- a/lib/allFunction.py +++ b/lib/allFunction.py @@ -1,9 +1,11 @@ -from db import findUser, delCode, insertCode -from login import getOpenid -from utils import checkData import re import json import os +import qrcode +from db import findUser, delCode, insertCode +from login import getOpenid +from utils import checkData +from io import BytesIO # 当前文件地址 basedir = os.path.abspath(os.path.dirname(__file__)) @@ -82,3 +84,26 @@ def addQR(request): return False else: return False + +def genQR(text): + # 空内容拦截 + if not text: + return False + qr = qrcode.QRCode( + version=1, + error_correction=qrcode.constants.ERROR_CORRECT_H, + box_size=10, + border=2 + ) + # 传入数据 + qr.add_data(text) + + qr.make(fit=True) + # 生成二维码 + im = qr.make_image() + img = BytesIO() + # 将图片放图片流里面 + im.save(img, format='PNG') + # 返回图片流 + img = img.getvalue() + return img \ No newline at end of file diff --git a/lib/db.py b/lib/db.py index 148a976..c2085d2 100644 --- a/lib/db.py +++ b/lib/db.py @@ -3,7 +3,7 @@ from bson import ObjectId, json_util # 获取数据集 def col(arg: str = None): - conn = MongoClient('mongodb://qrcode:mR6rN4pI0rU6@mongo:27017/qrcode') + conn = MongoClient('mongodb://qrcode:mR6rN4pI0rU6@39.96.28.83:27017/qrcode') return conn.qrcode[arg] # 查找该用户所有信息 diff --git a/lib/utils.py b/lib/utils.py index 907220f..24c9e9b 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -23,7 +23,6 @@ def checkData(data): hrefCount = 0 usernameCount = 0 openIdCount = 0 - timeoutCount = 0 # 可能的参数 # wxp://f2f0e4PCkhToyNDT-zfA-Nn6zoAgPKvK9HUl @@ -141,17 +140,7 @@ def checkData(data): else: node = '' - # 过期时间验证 - timeout = str(data['timeout']) - if(not isKong(timeout)): - if len(timeout) == 13: - timeoutCount = 1 - else: - return False - else: - return False - - if hrefCount >= 2 and usernameCount and openIdCount and timeoutCount: - return {'username': username, 'node': node, 'wxcode': wxcode, 'alcode': alcode, 'qqcode': qqcode, 'openId': openId, 'timeout': timeout, 'create_time': int(time.time()), 'id': randomId()} + if hrefCount >= 2 and usernameCount and openIdCount: + return {'username': username, 'node': node, 'wxcode': wxcode, 'alcode': alcode, 'qqcode': qqcode, 'openId': openId, 'create_time': int(time.time()), 'id': randomId()} else: return False \ No newline at end of file diff --git a/logs/acess.log b/logs/acess.log new file mode 100644 index 0000000..e69de29 diff --git a/logs/error.log b/logs/error.log new file mode 100644 index 0000000..e69de29 diff --git a/qrcode.py b/main.py similarity index 81% rename from qrcode.py rename to main.py index 860efcd..a0cbde3 100644 --- a/qrcode.py +++ b/main.py @@ -1,16 +1,11 @@ +# 将lib里边的文件加到路由中 import sys sys.path.append('./lib') - import os -from flask import Flask, escape, url_for, request, render_template, redirect, abort, send_from_directory -from allFunction import manageLogin, flash, delQR, addQR -from flask_qrcode import QRcode +from flask import Flask, escape, url_for, request, render_template, redirect, abort, send_from_directory, Response +from allFunction import flash, delQR, addQR, genQR, manageLogin app = Flask(__name__) -QRcode(app) - -if __name__ == '__main__': - app.run() # 用户小程序登录 @app.route('/login',methods=['POST']) @@ -51,8 +46,11 @@ def addQRCode(): # 生成二维码 @app.route('/generate',methods=['GET']) def generate(): - text = request.args.get("text") - return qrcode(text) + res = genQR(request.args.get("text")) + if(res): + return Response(res, mimetype='image/png') + else: + abort(400) # 控制小程序是否显示“最佳案例” @app.route('/config', methods=['post']) @@ -83,4 +81,7 @@ def page_not_found(e): @app.errorhandler(410) def page_not_found(e): - return render_template('410.html'), 500 \ No newline at end of file + return render_template('410.html'), 500 + +if __name__ == '__main__': + app.run(host="0.0.0.0", debug=True, port="80") \ No newline at end of file diff --git a/qr/img/2934349b033b5bb5092b9c0d3bd3d539b700bc41.jpg.png b/qr/img/bg.png similarity index 100% rename from qr/img/2934349b033b5bb5092b9c0d3bd3d539b700bc41.jpg.png rename to qr/img/bg.png diff --git a/qr/qrcode.html b/qr/qrcode.html index c6814f9..6be965e 100644 --- a/qr/qrcode.html +++ b/qr/qrcode.html @@ -77,11 +77,6 @@ } catch (e) { } - // 判断时间 - var timeout = parseInt(data.timeout) - timeout = new Date(timeout) - var now = new Date() - if (timeout - now <= 0) window.location.replace("https://qr.lacus.site/qr/410.html"); // 换背景 ele('bg').src = bgs[Math.floor(Math.random() * bgs.length)] setting.id = data._id.$oid diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..fbf16d2 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,15 @@ +certifi==2020.12.5 +chardet==4.0.0 +click==7.1.2 +Flask==1.1.2 +idna==2.10 +itsdangerous==1.1.0 +Jinja2==2.11.2 +MarkupSafe==1.1.1 +Pillow==8.1.0 +pymongo==3.11.2 +qrcode==6.1 +requests==2.25.1 +six==1.15.0 +urllib3==1.26.2 +Werkzeug==1.0.1 \ No newline at end of file