新闻首页模块
一、注册相关接口
(一)图片验证码
1、获取前端生成的UUID编码
image_code_id = generate()前端调用该方法生成UUID编号,并发送给服务器
由于这是一个imp标签所以服务器可以request.args.get()获取到这个编码
2、调用captcha(图灵测试)扩展生成图片验证码
对获取到的参数进行验证,判断是否存在
存在则:name,text,image = captcha.generate_captcha()调用captcha生成图片验证码
3、以前端获取的UUID为键,captcha生成的text为值,存储到Redis数据库中
使用Redis数据库redis_store.setex(imageCodeId,time,text)将数据进行保存
4、使用flask中的make_response将图片返回给前端页面
response = make_response(image)
5、修改前端相应报头并返回
response. headers[‘Content-Type’] = ‘image/jpg’
return….
(二)发送手机短信
1、根据接口文档指定请求方式,以及确定需要接受的参数
2、对接受的参数进行校验
(1)确认参数的完整性
if not all([mobile,image_code,image_code_id])
return…
(2)确认手机号是否符合规范(采用正则的方式验证)
if not re.match(r‘1[3456789]\d{9}’,mobile)
return…
(3)确认验证码是否正确,从Redis数据库中取出之前保存的text值进行比对,为防止二次使用验证码,取出后删除数据库中数据
real_image_code = redis_store.get(image_code_id)
redis_store. delete(…)
if not real_image_code.lower() != image_code.lower()
return…
3、参数校验完成后判断用户是否已经注册
查询MySQL数据库中是否存在该用户
user = User.query.filter_by(mobile=mobile).first()
if user 表示用户存在已经注册
return…
4、生成6位随机数,并以用户手机号位键,随机数位值将数据存储到Redis数据库
sms_code = ‘%06d’ % random.randint(0,999999)
redis_store.setex(mobile,time,sms_code)
5、调用第三方云通讯接口发送生成的6位随机数给用户手机,
生成6位随机数:sms_code = ‘%06d’ % random.randint(0,999999)
调用云通讯发送短信:ccp = sms.CCP()
result = ccp.send_template_sms(mobile,[sms_code,time / 60],1)
如果result == 0 表示发送成功,else发送失败
(三)注册按钮
1、根据接口文档指定请求方式,以及确定需要接受的参数
2、对接受的参数进行校验
(1)确认参数的完整性
if not all([mobile,sms_code,password])
return…
(2)确认手机号是否符合规范(采用正则的方式验证)
if not re.match(r‘1[3456789]\d{9}’,mobile)
return…
(3)确认手机验证码是否正确,从Redis数据库中取出之前保存的sms_code值进行比对,为防止二次使用,取出后删除数据库中数据
real_sms_code = redis_store.get(mobile)
由于之前设置的是有时效的数据,因此需要判断是否还存在该sms_code
redis_store.delete(…)
if not real_sms_code != str(sms_code)
return…
3、将用户的数据存储到MySQL数据库
user = User()
user.moblie = mobile
user.password = password(加密存储)
user.nike_name = mobile(给用户默认设置一个昵称)
db.session.add(user)
db.session,commit()
4、为实现状态保持,将用户的注册的信息存储到Reids数据库中,并提示注册完成
session[‘user_id’] = user.id
session['mobile'] = user.mobile
session['nick_name'] = mobile
二、登录相关接口
1、根据接口文档确定需要接受的参数以及请求方式
2、校验参数的完整性
if not all([mobile,password])
3、判断手机号码是否符合规则
if not re.match(r‘1[3456789]\d{9}’,mobile)
return…
4、根据手机号码进行MySQL的数据查找
user = User.query.filter_by(mobile=mobile).first()
5、判断是否存在用户或则密码是否正确
if user is None or not user.check_password(password)(调用加密密码的检查匹对方法)
return
6、保存用户的登录信息并记录用户的最后登录时间
session[‘user_id’] = user.id
session['mobile'] = mobile
session['nick_name'] = user.nick_name
user.last_login = datetime.now()
7、记录的用户登录时间需要提交到数据库中
db.session.commit()
8、返回提示用户登录成功
三、退出相关接口
退出即删除用户的登录信息
session.pop(‘user_id’)
session.pop('mobile')
session.pop(‘nick_name’)
此处建议使用pop()方法,不建议使用clear()方法
四、主页相关接口
采用模板的方式,所以需要导入render_template
1、确认用户是否登录在线
user = g.user //此处是定义的装饰器
2、展示点击排行按点击量进行排序查找,且根据前端的设计是显示的六条信息
news_list = News.query.order_by(News.clicks.desc()).limit(6)
3、判断点击排行数据查找结果是否存在
if not news_list:
return…
4、定义一个列表容器保存查询结果
news_dict_list = []
5、遍历所有的查询对象并添加到列表容器中,并调用模型类中to.dict()的方法将之转换成字符串
for news in news_list:
news_dict_list.append(news.to_dict())
6、展示新闻分类数据需搜索所有分类信息
categories = Category.query.all()
7、判断分类数据是否存在
if not categories:
return
8、定义一个列表容器保存查询结果
category_list = []
9、遍历所有的查询对象并添加到列表容器中,并调用模型类中to.dict()的方法将之转换成字典
for category in categories:
category_list.append(category.to_dict())
10、定义一个字典保存所有数据,并将之传给模板进行数据填充
data = {
"user":user.to_dict() if user else None,//表示如果用户未登录的情况下也是可以访问主页面的
"news_dict_list":news_dict_list,
"category_list":category_list
}
return render_template(‘模板’,data=data)
五、首页新闻数据列表接口
1、根据接口文档定义路由,请求方式以及需要哪些参数等(备注:ajax/get请求)
2、接受所需要参数并进行校验(cid,page,per_page)
为实现友好的交互,即使后端没传过来数据,在首页也是需要展示新闻的,所以我们默认会给其定义一个参数
cid = request.args.get(‘cid’,‘1’) è分类id
page = request.args.get('page','1')
per_page = request.args.get('per_page','10')
3、校验参数,并将数字参数转换成整型值(为了和数据库进行查找)
try:
cid,page,per_page = int(cid),int(page),int(per_page)
except Exception as e:
return