- The image generation component is a mature component. Go directly to the Internet to search for a powerfully developed component and quote it in your own framework.
1. Back-end use interface in own system
1. Interface document:
- Interface name
- description
- URL
- Request method
- Incoming parameters
- return value
Interface: Get image verification code
Description: Front-end access, you can get the verification code Image
URL: /api/v1.0/image_codes/<image_code_id>
Request method: GET
incoming parameters:
first name Types of Do you have to Description image_code_id String Yes Verification code number return value:
first name Types of Do you have to Description errno String no error code errmsg String no Error content
2. Back-end interface definition (route definition):
- Adopt REST style
- Call image verification code component to generate image verification code
- Save the image verification code in redis
- Return the image verification code data to the front end in the form of an image
@api.route("/image_codes/<image_code_id>")
def get_image_code(image_code_id):
"""
获取图片验证码
:param image_code_id: 图片的编号
:return: 验证码,验证码图像
"""
# 验证参数
# 业务逻辑处理
# 生成验证码图片
text, image_data = captcha.generate_captcha()
# 保存验证码
try:
redis_store.setex('image_code_%s' % image_code_id, constants.IAMGE_CODE_REDIS_EXPIRES, text)
except Exception as e:
logging.error(e)
return jsonify(errno=RET.DBERR, errmsg='保存图片验证码失败')
# 返回值
response = make_response(image_data)
response.headers['Content-Type'] = 'image/jpg'
return response
3. Cite in the blueprint
- The route must be referenced in the blueprint, otherwise it cannot be called
from flask import Blueprint
api = Blueprint("api_1_0", __name__, url_prefix="/api/v1.0")
from . import demo,verify_code
4. Verification
4.1 Start the background service
4.2 Input in the address bar: http://127.0.0.1:5000/api/v1.0/image_codes/xxxx
- Display the image verification code normally
4.3 Start the redis client, get image_code_xxxx
If the value is consistent with the displayed picture, it is correct