# -*- coding = utf-8 -*-
# @Time : 2021/4/29 15:13
# @File :adminmodel.py
# @Software:PyCharm
from flask import Flask,request,jsonify,session
import re
from admin import admin_blue
# from blog import user,guanli
from werkzeug.security import generate_password_hash,check_password_hash
from peewee import *
db = MySQLDatabase('msg',host ='127.0.0.1',user='root',passwd='9953001')
db.connect()
class BaseModel(Model):
class Meta:
database = db # 每一个继承BaseModel类的子类都是连接db表
class user(BaseModel):
mobile = CharField()
password = CharField()
email = CharField()
class guanli(BaseModel):
mobilegl = CharField()
passwordgl = CharField()
emailgl = CharField()
user.create_table()
guanli.create_table()
def enPassWord(password):#将明密码转化为hash码
return generate_password_hash(password)#返回转换的hash码
def checkPassWord(enpassword,password):#第一参数是从数据查询出来的hash值,第二参数是需要检验的密码
return check_password_hash(enpassword,password)#如果匹配返回true
# app = Flask(__name__)
# app.secret_key = 'please-generate-a-random-secret_key'
#
# app.register_blueprint(admin_blue)
@admin_blue.route('/registergl',methods=['POST'])
def registergl():
req_dict = request.get_json()
mobilegl = req_dict.get('mobilegl')
passwordgl = req_dict.get('passwordgl')
password2gl = req_dict.get('password2gl')
emailgl = req_dict.get("emailgl")
if not all([mobilegl, passwordgl, password2gl,emailgl]):
return jsonify(errmsg='请求参数不完整')
# 判断手机号格式
if not re.match(r"1[34578]\d{9}", mobilegl):
return jsonify(errmsg='手机号格式不正确')
if not re.match(r"[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-zA-Z0-9]+", emailgl):
return jsonify(errmsg='邮箱格式不正确')
if passwordgl != password2gl:
return jsonify(errmsg='两次密码不一致')
passwordgl= enPassWord(passwordgl)
n = guanli.get_or_none(guanli.mobilegl == mobilegl,emailgl==emailgl)
if n == None:
guanli.create(mobilegl=mobilegl, passwordgl=passwordgl,emailgl=emailgl)
return jsonify(errmsg='注册成功')
else:
return jsonify(errmsg='已注册过,请登录')
@admin_blue.route('/logingl', methods=['POST'])
def logingl():
"""登陆
传递参数: 手机号、密码,
格式为:json
"""
# 获取参数
req_dict = request.get_json()
mobilegl = req_dict.get('mobilegl')
passwordgl = req_dict.get('passwordgl')
# 验证参数的完整性
if not all([mobilegl, passwordgl]):
return jsonify(errmsg='参数不完整')
# 验证手机号的格式
if not re.match(r'1[34578]\d{9}', mobilegl):
return jsonify(errmsg='手机号格式错误')
guanlis=guanli.get_or_none(guanli.mobilegl==mobilegl)
if guanlis==None:
return jsonify(errmsg='未注册')
else:
if checkPassWord(guanlis.passwordgl,passwordgl)==True:
session['mobilegl'] = mobilegl
session['passwordgl'] = passwordgl
return jsonify(errmsg='登录成功')
elif checkPassWord(guanlis.passwordgl,passwordgl)==False:
return jsonify(errmsg='密码错误')
@admin_blue.route('/indexgl', methods=['GET', 'POST'])
def indexgl():
"""登录主页"""
# 尝试从session中获取用户的名字
mobilegl = session.get('mobilegl')
# 如果session中的name存在,则表示已经登陆,否则未登录
if mobilegl is None:
return jsonify(errmsg='false')
else:
i=user.select()
dict=[]
for j in i:
dict.append({"id":j.id,"mobile": j.mobile,"password":j.password})
return jsonify(dict)
req_dict = request.get_json()
idgl= req_dict.get('idgl')
user.delete().where(user.id == idgl).execute()
return jsonify(errmsg="删除成功")
@admin_blue.route('/indexgl', methods=['DELETE'])
def logoutgl():
"""退出"""
# 清除session数据
session.clear()
return jsonify(errmsg='ok')
@admin_blue.route('/change-passwordgl', methods=['GET', 'POST'])
def change_passwordgl():
mobilegl = session.get('mobilegl')
if mobilegl is None:
return jsonify(errmsg='未登录,无法修改密码')
else:
t = guanli.get(guanli.mobilegl==mobilegl)
req_dict = request.get_json()
repasswordgl = req_dict.get('repasswordgl')
newpasswordgl= req_dict.get('newpasswordgl')
if checkPassWord(t.passwordgl,repasswordgl)==True:
newpasswordgl = enPassWord(newpasswordgl)
guanli.update(passwordgl=newpasswordgl).where(guanli.mobilegl ==mobilegl).execute()
return jsonify(errmsg='修改成功!')
else:
return jsonify(errmsg='密码验证错误')
# if __name__ == '__main__':
# app.run(debug=True)