評判一下注冊的步驟
注冊模塊
1. 用戶在前端輸入手機號 點擊獲取驗證碼
2. 后端獲取到手機號並發送驗證碼
3. 用戶接受到驗證碼, 輸入並登錄或注冊
4. 后端完成登錄或注冊操作, 授權用戶登錄系統
https://blog.csdn.net/qq_22034353/article/details/90640981
P22
1. 下載 wheezy.captcha的第三方開發包
書寫視圖函數
根據第三方包類下面生成的實例,直接拿來用 (可以看到上面類的initlization方法 return 返回三個參數, 也就是我們實例返回的三個參數 name, text, image_data
from . import auth
from app.utils.captch.captcha import captcha
# GET 127.0.0.1/api/v1.0/image_codes/<image_code_id>
@auth.route('/image_codes/<image_code_id>')
def get_image_code(image_code_id):
"""
獲取圖片驗證碼
: params iamge_code_id: 圖片驗證碼編號
:return: 驗證碼圖片
"""
# 接受參數
# 檢驗參數
# 業務邏輯處理
# 生成圖片驗證碼
# 名字, 真實文本, 圖片數據
name, text, image_data = captcha.generate_captcha()
# 將驗證碼真實值與編號保存到redis, 並設置有效期
# 1. redis 有哪些數據類型?
# "key": xxx
#2. 可以存一條記錄嗎? 把所有驗證碼都放入key= "image_codes" 中, 以列表的形式嗎? 為什么不行?
#3. 那我們以什么形式存儲?
# 4. 哈希類型怎么存取的?
# 返回圖片
# 返回值
pass
# 生成6位隨機數 而不是生成0-6位隨機數
>>> '%06d' % random.randint(0, 999999)
'692782'
# 判斷圖片驗證碼是否過期
獲取短信驗證碼的接口
@auth.route("/sms_codes/<re(r'1[34578]\d{9}'):mobile>")
def get_sms_code(mobile):
"""獲取短信驗證碼"""
# 1.獲取參數
# 2. 校驗參數
# 3. 業務邏輯處理
# 從redis中取出真實的圖片驗證碼
# 判斷圖片驗證碼是否過期
# 與用戶填寫的值對比
# 判斷手機號存在不,
try:
user = User.query.filter_by(mobile=mobile).first()
except Exception as e:
current_user.logger.error(e)
else:
if user is not None:
return jsonify("手機號已存在")
# 不存在則生成短信驗證碼
sms_code = "%06d" % random.randint(0, 999999)
# 保存真實的短信驗證碼
try:
# 返回值
5.如何生成0-6位隨機數?使用什么模塊? 如何生成6位隨機數?
答案:
-
字符串 列表, 哈希 set
-
可以, 不以列表的形式, 因為我們要存入驗證碼和其對應的id,兩者之間有關系, 而列表只能存放字符串,沒有對應關系 例: [ {}]
-
image_codes: {"編號1":"真實文本", "":""}
-
與python對象的字典類型類似{'hash': 'value'}形式???? (還沒學習redis, 有待確定)
-
使用random模塊的randint方法
# 0- 6位隨機數 >>>random.randint(0, 999999) # 6位隨機數 >>>%06d % random.randint(0, 999999)