def alter_pwd(): """ 用户修改密码 :return: """ global alter_email_code req_json = request.json user_name = req_json['user_name'] new_password = req_json['new_password'] # re_password = req_json['re_password'] email = req_json['email'] user_email_code = req_json['email_code'] if user_name and new_password and email and user_email_code: if not UserModelDao.query_user(1, user_name=user_name): # 用户名不存在 response_data = response[20201] # elif new_password != re_password: # 两次输入密码不一致 # response_data = response[20304] elif user_email_code != alter_email_code: # 邮箱验证码错误 response_data = response[20305] else: # 修改成功 UserModelDao.alter_user_pwd(user_name, new_password) response_data = response[200] else: # 缺少参数 response_data = response[20101] return json.dumps(response_data, ensure_ascii=False)
def alter_send_email(): """ 修改密码时,开启另一个线程发送邮箱验证码 :return: """ global alter_email_code email = request.json['email'] user_name = request.json['user_name'] if email and user_name: if not dao.validate_email(email): # 邮箱格式错误 response_data = response[20307] elif not UserModelDao.query_user(4, user_name=user_name, email=email): # 用户名和邮箱不匹配 response_data = response[20308] else: alter_email_code = dao.thread_send_email(email) # 发送成功 response_data = response[200] else: # 缺少参数 response_data = response[20101] return json.dumps(response_data, ensure_ascii=False)
def register(self, data): if data['project_code'] == PROJECT_CODE: msg = UserModelDao.add_user(data['user_name'], data['user_pwd']) else: msg = 'project_code is wrong' print(msg) ServerDao.publish_register_msg(self.client, data['user_name'], msg)
def get_user_data(): page_size = 10 page = 1 all_user = UserModelDao.query_user(0) all_list = [i for i in all_user] data = all_list[(page - 1) * page_size:page * page_size] user_name = [] user_pwd = [] state = [] email = [] for i in data: user_name.append(i.user_name) user_pwd.append(i.user_pwd) state.append(i.user_state) email.append(i.email) print(user_name, user_pwd, email, state) return json.dumps({ 'code': 200, 'msg': 'Success', 'data': { 'user_name': user_name, 'user_pwd': user_pwd, 'email': email } })
def register_send_email(): """ 注册时,开启另一个线程发送邮箱验证码 :return: """ global register_email_code email = request.json["email"] # 获取用户输入的邮箱 if email: if not dao.validate_email(email): # 邮箱格式错误 response_data = response[20307] elif UserModelDao.query_user(2, email=email): # 邮箱已被使用 response_data = response[20306] else: register_email_code = dao.thread_send_email(email) print(register_email_code) # 发送成功 response_data = response[200] else: # 缺少参数 response_data = response[20101] return json.dumps(response_data, ensure_ascii=False)
def delete_manager(): """ 删除管理员 :return: """ user_name = request.json['user_name'] if user_name: UserModelDao.set_group_id(user_name, 1) # 删除成功 response_data = response[200] else: # 缺乏参数 response_data = response[20101] return json.dumps(response_data, ensure_ascii=False)
def set_user_state(): """ 设置用户状态,禁用或者恢复用户 :return: """ user_name = request.json["user_name"] user_state = request.json["user_state"] if user_name and user_state is not None: UserModelDao.set_user_state(user_name, user_state) # 设置成功 response_data = response[200] else: # 参数缺乏 response_data = response[20101] return json.dumps(response_data, ensure_ascii=False)
def login(self, data): msg = UserModelDao.check_user_and_user_pwd(data['user_name'], data['user_pwd']) print(msg) token = LibDao.set_user_name_token(data['user_name']) if msg == 'Correct' else None # topic使用用户名,即对某个用户定点发送 # t = Thread(target=ServerDao.publish_login_msg, args=(data['user_name'], msg, token, )) # t.start() return_topic = data['return_topic'] ServerDao.publish_login_msg(self.client, return_topic, data['user_name'], msg, token)
def get_user_name_from_id(user_id): """ 由id得name :param user_id: :return: """ user = UserModelDao.find_by_user_id(user_id) if not (user and user.user_name): return None return user.user_name
def is_user_root(user_id): """ 查看一个user是否root用户 :param user_id: :return: """ user = UserModelDao.find_by_user_id(user_id) if not (user and user.group_id == 3): return False return True
def _handle(*k, **v): token = request.headers.get('token') user_id = dao.get_user_id_from_token(token) user = UserModelDao.find_by_user_id(user_id) if not (user and user.group_id): # 不存在 return json.dumps(response[20201]) url = request.path print(url) if not GroupPowerModelDao.check_group_permission(user.group_id, url): return json.dumps(response[20203]) # 无权限 return func(*k, **v)
def register(): """ 用户注册功能 :return: """ global register_email_code data = request.json # 获取表单数据 name = data["user_name"] pwd = data["password"] # check_pwd = data["check_password"] email = data["email"] user_email_code = data["email_code"] if name and pwd and email and user_email_code: # and check_pwd: if not re.search(u'^[_a-zA-Z0-9\u4e00-\u9fa5]+$', name): # 用户名格式出错 response_data = response[20302] elif UserModelDao.query_user(1, user_name=name): # 用户名已存在 response_data = response[20301] elif len(pwd) < 6: # 密码长度太短 response_data = response[20303] # elif check_pwd != pwd: # 两次密码输入不一致 # response_data = response[20304] elif user_email_code != register_email_code: # 邮箱验证码错误 response_data = response[20305] else: # 插入新用户 UserModelDao.add_user(name, pwd, email) response_data = response[200] else: # 缺少参数 response_data = response[20101] return json.dumps(response_data, ensure_ascii=False)
def publish_latest_note(return_topic, note_id): """ 将最新发布的信息发送给所有订阅的客户端 :param return_topic: 标识房间 :param note_id: :return: """ client = mqtt.Client() client.connect(HOST, PORT, 60) model = ChatNotesModelDao.query_one_note(note_id) user_name = UserModelDao.get_name_by_user_id(model.user_id) data = [user_name, model.message, model.time] publish_data = {'latest_note': data} client.publish(return_topic, str(publish_data).encode(), 1) print('publish one note to ', return_topic, ' successfully') client.loop()
def get_user_data(): page_size = request.json['page_size'] page = request.json['page'] group_id = request.json['group_id'] all_user_list = UserModelDao.query_user(3, group_id=group_id) all_user_list = [i for i in all_user_list] user_list = all_user_list[(page - 1) * page_size:page * page_size] user_data = [] for user in user_list: user_data.append({ 'user_name': user.user_name, 'user_pwd': user.user_pwd, 'user_state': user.user_state, 'email': user.email }) data = {'count': len(all_user_list), 'user_data': user_data} return json.dumps({'code': 200, 'msg': 'Success', 'data': data})
def get_user_mount(): """ 得到用户数量 :return: """ all_user_list = UserModelDao.query_user(0) data = {'root': 0, 'manager': 0, 'user': 0} for user_model in all_user_list: if user_model.group_id == 1: data['user'] += 1 elif user_model.group_id == 2: data['manager'] += 1 elif user_model.group_id == 3: data['root'] += 1 response_data = {'data': data} response_data.update(response[200]) return json.dumps(response_data)
def chat(self, data): token = data['token'] return_topic = data['return_topic'] if not LibDao.if_token_valid(token): token_user = LibDao.get_user_name_from_token(token) if token_user: # 能获取到名字就发送 ServerDao.publish_invalid_msg(self.client, return_topic) return room_name = data['room_name'] user_name = LibDao.get_user_name_from_token(token) room_id = ChatRoomsModelDao.get_id_by_name(room_name) user_id = UserModelDao.get_id_by_user_name(user_name) message = data['msg'] time = data['time'] note_id = ChatNotesModelDao.add_note(room_id, user_id, message, time) if note_id: ServerDao.publish_latest_note(room_name, note_id)
def check_user_able_access_url(user_id, url): """ 判断用户是否可以访问某url :param user_id: 用户id :param url: api地址 :return: """ # 检查用户所属用户组 user = UserModelDao.find_by_user_id(user_id) if not (user and user.group_id): return False # # 检查用户组是否存在 # if not UserGroupModelDao.find_by_id(user.group_id): # return False # 检查权限 return GroupPowerModelDao.check_group_permission(user.group_id, url)
def publish_room_all_notes(return_topic, room_name): """ 给客户端该房间的所有历史记录 :param return_topic: 标识一个客户端 :param room_name: :return: """ client = mqtt.Client() client.connect(HOST, PORT, 60) room_id = ChatRoomsModelDao.get_id_by_name(room_name) model_list = ChatNotesModelDao.query_notes(room_id) data = [] for model in model_list: user_name = UserModelDao.get_name_by_user_id(model.user_id) one_data = [user_name, model.message, model.time] data.append(one_data) publish_data = {'all_notes': data} client.publish(return_topic, str(publish_data).encode(), 1) print('publish all notes to ', return_topic, ' successfully') client.loop()
def user_login(): """ 获取api调用token :return: """ req_json = request.json user_name = req_json["user_name"] user_pwd = req_json["user_pwd"] if not (user_name and user_pwd): # 参数缺失 return json.dumps(response[20101]) user_id = UserModelDao.check_user_and_user_pwd(user_name, user_pwd) if not root_dao.is_user_root(user_id): # 校验不通过 return json.dumps(response[20201]) if user_id == -1 else json.dumps( response[20202]) token = dao.get_one_token() dao.set_user_id_token(token, user_id) response_data = {"token": token, 'user_id': user_id} response_data.update(response[200]) return json.dumps(response_data)
def return_ui_config(): columns = UserModelDao.get_ui_config() return json.dumps({'code': 200, 'msg': 'Success', 'data': columns})
import os import csv import pathlib import time import requests from lxml import etree create_all_table() # 用户 GroupModelDao.add_group('普通用户') GroupModelDao.add_group('管理员') GroupModelDao.add_group('超级管理员') # 添加一个超级用户 UserModelDao.add_user('SuperVisitor', 'veg_root#2019', '*****@*****.**') UserModelDao.set_group_id('SuperVisitor', 3) # 模型信息 PredictModelModelDao.add_model('bp', 'bp神经网络') PredictModelModelDao.add_model('lstm', 'lstm神经网络') PredictModelModelDao.add_model('arima', 'arima时间序列') # 用户权限表 GroupPowerModelDao.add_one_power(1, '/user/register') GroupPowerModelDao.add_one_power(1, '/user/login') GroupPowerModelDao.add_one_power(1, '/user/alter_pwd') GroupPowerModelDao.add_one_power(1, '/user/register/send_email') GroupPowerModelDao.add_one_power(1, '/user/vegetable/k_line') GroupPowerModelDao.add_one_power(1, '/user/alter_pwd/send_email') GroupPowerModelDao.add_one_power(1, '/user/vegetable/information')