def login(): if request.method == 'GET': return render_template(THEMES + 'users/login.html') else: username = request.form['username'] password = request.form['password'] password = common.hashPwd(password) res = usersModels.users.checkpassword(username, password, request.remote_addr) if res["code"]: model = usersModels.users() # 实例化一个对象,将查询结果逐一添加给对象的属性 model.id = res["msg"].id model.username = res["msg"].username model.avatar = res["msg"].avatar model.nickname = res["msg"].nickname model.score = res["msg"].score if res["msg"].group: model.group = authorModels.authGroup.find_by_id( res["msg"].group).title else: model.group = "普通会员" login_user(model) return json.dumps({"code": 0, "msg": "登陆成功!"}) else: return json.dumps({"code": 1, "msg": res["msg"]})
def register(): username = request.form['username'] password = request.form['password'] nickname = request.form['nickname'] if len(password) < 6: return json.dumps({"code": 1, "msg": "密码格式错误"}) password = common.hashPwd(password) res = usersModels.users.check_username(username) if res: return json.dumps({"code": 1, "msg": "用户名已存在"}) else: # 初始化role 并插入数据库 role = usersModels.users( username=username, password=password, nickname=nickname, email='', description='', avatar="/static/uploads/avatar/{}.png".format(random.randint( 1, 10)), sex=3, login_num=0, score=0, group=0, status=1, register_ip=request.remote_addr, birthday='0001-01-01 00:00:00', reg_time=time.strftime('%Y-%m-%d %H:%M:%S'), update_time=time.strftime('%Y-%m-%d %H:%M:%S')) MysqlDB.session.add(role) MysqlDB.session.flush() MysqlDB.session.commit() return json.dumps({"code": 0, "msg": "注册成功!"})