def admin_modify_info(): """ 管理员修改他人信息 :return: """ db = Database() token = request.form['token'] user = db.get({'token': token, 'group': 0}, 'user') if not user: return jsonify({'code': 0, 'msg': 'unexpected user'}) # 失败返回 username = request.form['username'] snum = request.form['snum'] exist = db.get({'username': username}, 'user') if exist: if exist['Snum'] != snum: return jsonify({'code': -1, 'msg': 'username is already exist'}) phone = request.form['phone'] group = request.form['group'] res = db.update({'Snum': snum}, { 'username': username, 'phone': phone, 'group': group }, 'user') if res: return jsonify({'code': 1, 'msg': 'success'}) return jsonify({'code': -2, 'msg': 'fail'})
def register(): db = Database() snum = request.form['snum'] username = request.form['username'] password = request.form['password'] repeat_by_username = db.get({'Snum': username}, 'user') if repeat_by_username: if repeat_by_username['Snum'] != snum: return jsonify({'code': -2, 'msg': 'repeat Snum'}) face = request.form['face'] # base64转图片 imgdata = base64.b64decode(face) filename = random_char() + ".bmp" file = open(FILE_PATH + "/face/" + filename, 'wb') file.write(imgdata) file.close() user = db.get({'Snum': snum}, 'user') if user: repeat = db.get({'username': username}, 'user') if repeat: return jsonify({'code': -2, 'msg': 'repeat username'}) flag = db.update({'Snum': snum}, { 'username': username, 'password': generate_password(password), 'face': filename, 'group': 1 }, 'user') if flag: return jsonify({'code': 1, 'msg': 'success'}) return jsonify({'code': 0, 'msg': 'unknown error'}) return jsonify({'code': -1, 'msg': 'user not found'})
def get_all_student_info(): """ 获取所有学生的基本信息 :return: """ token = request.values.get('token') db = Database() user = db.get({'token': token, 'group': 0}, 'user') if user: data = db.get({}, 'user') if data: for it in data: it.pop("password") it.pop("token") it.pop("ID") it.pop("face") tp = it['group'] if tp == 1: tp = "学生" elif tp == 0: tp = "管理员" it.update({'group': tp}) return jsonify({'code': 1, 'msg': 'success', 'data': data}) return jsonify({'code': 0, 'msg': 'unexpected user'}) # 失败返回
def get_comment(): """ 获取评论 :return: """ token = request.values.get('token') db = Database() user = db.get({'token': token}, 'user') if user: article_id = request.values.get('article_id') comments = db.get({ 'father': db.MYSQL_NULL, 'article_id': article_id }, 'comment_info', 0) + db.get({ 'father': '', 'article_id': article_id }, 'comment_info', 0) for item in comments: children = db.get({'father': item['ID']}, 'comment_info', 0) for child in children: child.update({'time': child['time'].strftime("%Y-%m-%d")}) item.update({ 'children': children, 'time': item['time'].strftime("%Y-%m-%d") }) return jsonify({'code': 1, 'msg': 'success', 'data': comments}) return jsonify({'code': 0, 'msg': 'permission denied'})
def get_article_tag(): """ 获取文章标签 :return: """ token = request.values.get('token') db = Database() user = db.get({'token': token}, 'user') if user: article_id = request.values.get('article_id') tags = db.get({'article_id': article_id}, 'article_tag', 0) return jsonify({'code': 1, 'msg': 'success', 'data': tags}) return jsonify({'code': 0, 'msg': 'permission denied'})
def face_check(): username = request.form['username'] # 传入图片的base64编码,不包含图片头,如data:image/jpg;base64 img1 = "" img2 = request.form['face'] # 获取用户的人脸照片,转换为base64编码 db = Database() user = db.get({'username': username}, 'user') if not user: user = db.get({'Snum': username}, 'user') if user: with open(FILE_PATH + "/face/" + user['face'], 'rb') as f: base64_data = base64.b64encode(f.read()) img1 = base64_data.decode() else: return jsonify({'code': -4, 'msg': 'user not exist'}) request_url = "https://aip.baidubce.com/rest/2.0/face/v3/match" params = json.dumps([{ "image": img1, "image_type": "BASE64", "face_type": "LIVE", "quality_control": "LOW" }, { "image": img2, "image_type": "BASE64", "face_type": "LIVE", "quality_control": "LOW" }]) params = bytes(params, encoding="utf8") request_url = request_url + "?access_token=" + ACCESS_TOKEN rq = urllib.request.Request(url=request_url, data=params) rq.add_header('Content-Type', 'application/json') response = urllib.request.urlopen(rq) content = response.read() if content: content = str(content, encoding="utf8") content = json.loads(content) errcode = content['error_code'] if errcode == 100 or errcode == 110 or errcode == 111: return jsonify({'code': 0, 'msg': 'access token was invalid'}) elif errcode == 18: return jsonify({'code': -1, 'msg': 'QPS limit'}) elif errcode == 0: similarity = content['result']['score'] return jsonify({'code': 1, 'msg': 'success', 'data': similarity}) return jsonify({'code': -3, 'msg': 'fail', 'data': content}) return jsonify({'code': -2, 'msg': 'fail', 'data': content})
def add_new_student(): snum = request.form['snum'] token = request.form['token'] db = Database() admin = db.get({'token': token, 'group': 0}, 'user') if admin: exist = db.get({'Snum': snum, 'group': 1}, 'user') if exist: return jsonify({"code": -2, 'msg': "user exist"}) result = db.insert({'Snum': snum, 'group': 1}, 'user') if result: return jsonify({"code": 1, 'msg': "success"}) return jsonify({"code": 0, 'msg': "error"}) return jsonify({"code": -1, 'msg': "permission denied"})
def modify_info(): """ 修改用户自身的信息 :return: """ token = request.form['token'] db = Database() user = db.get({'token': token}, 'user') if user: password = request.form['password'] phone = request.form['phone'] face = request.form['face'] # base64转图片 imgdata = base64.b64decode(face) filename = random_char() + ".bmp" # 改成绝对路径 file = open(FILE_PATH + "/face/" + filename, 'wb') file.write(imgdata) file.close() res = db.update({'token': token}, { 'password': generate_password(password), 'phone': phone, 'face': filename }, 'user') return jsonify({'code': 1, 'msg': 'success'}) return jsonify({'code': 0, 'msg': 'unexpected user'}) # 失败返回
def add_article_tag(): """ 添加文章标签 :return: """ token = request.form['token'] db = Database() user = db.get({'token': token, 'group': 0}, 'user') if user: article_id = request.form['article_id'] name = request.form['name'] tag_type = request.form['tag_type'] icon = request.form['icon'] description = request.form['description'] flag = db.insert( { 'article_id': article_id, 'name': name, 'description': description, 'icon': icon, 'tag_type': tag_type }, 'article_tag') if flag: return jsonify({'code': 1, 'msg': 'success'}) return jsonify({'code': -1, 'msg': 'insert error'}) return jsonify({'code': 0, 'msg': 'permission denied'})
def change_article(): """ 修改文章 :return: """ token = request.form['token'] db = Database() user = db.get({'token': token, 'group': 0}, 'user') if user: article_id = request.form['article_id'] content = request.form['content'] title = request.form['title'] tag = request.form['tag'] flag = db.new_update({'ID': article_id}, { 'content': content, 'title': title, 'tag': tag, 'changer': user['ID'] }, 'article') if flag: db.insert( { 'article_id': article_id, 'user_id': user['ID'], 'type': 1, 'content': content }, 'history') return jsonify({'code': 1, 'msg': 'success'}) return jsonify({'code': -1, 'msg': 'unknown error'}) return jsonify({'code': 0, 'msg': 'permission denied'})
def add_article(): """ 添加文章 :return: """ token = request.form['token'] db = Database() user = db.get({'token': token, 'group': 0}, 'user') if user: content = request.form['content'] title = request.form['title'] tag = request.form['tag'] flag = db.insert( { 'content': content, 'title': title, 'tag': tag, 'author': user['ID'] }, 'article') if flag: article_id = db.sql('select last_insert_id();') db.insert( { 'article_id': article_id[0]['last_insert_id()'], 'user_id': user['ID'], 'type': 0, 'content': content }, 'history') return jsonify({'code': 1, 'msg': 'success'}) return jsonify({'code': -1, 'msg': 'unknown error'}) return jsonify({'code': 0, 'msg': 'permission denied'})
def get_tag_list(): """ 获取tag列表 :return: """ token = request.values.get('token') db = Database() user = db.get({'token': token}, 'user') if user: tag_id = request.values.get('tag_id') tag = db.get({'ID': tag_id}, 'tag') if tag: tag_list = [tag['ID']] get_father_tag(tag, tag_list) return jsonify({'code': 1, 'msg': 'success', 'data': tag_list}) return jsonify({'code': -1, 'msg': 'unknown tag'}) return jsonify({'code': 0, 'msg': 'permission denied'})
def check_snum(): snum = request.form['snum'] db = Database() user = db.get({'Snum': snum}, 'user') if user: if user['username'] and user['password'] and user['face']: return jsonify({'code': -1, 'msg': 'already exist'}) return jsonify({'code': 1, 'msg': 'success'}) return jsonify({'code': 0, 'msg': 'user not found'})
def get_articles(): """ 获取tag下的文章 :return: """ token = request.values.get('token') db = Database() user = db.get({'token': token}, 'user') if user: tag_id = request.values.get('tag_id') articles = db.get({'tag': tag_id}, 'article', 0) for article in articles: article.update({ 'time': article['time'].strftime("%Y-%m-%d"), 'author': get_user(article['author']), 'changer': get_user(article['changer']) }) return jsonify({'code': 1, 'msg': 'success', 'data': articles}) return jsonify({'code': 0, 'msg': 'permission denied'})
def get_tag_tree(): """ 获取tag树 :return: """ token = request.values.get('token') db = Database() user = db.get({'token': token}, 'user') if user: tags = db.get({'father': db.MYSQL_NULL}, 'tag', 0) tags1 = db.get({'father': ''}, 'tag', 0) tags = tags + tags1 for tag in tags: tag.update({ 'children': get_tag_child(tag) + in_get_articles(tag), 'type': 0 }) return jsonify({'code': 1, 'msg': 'success', 'data': tags}) return jsonify({'code': 0, 'msg': 'permission denied'})
def get_article(): """ 获取文章 :return: """ token = request.values.get('token') db = Database() user = db.get({'token': token}, 'user') if user: article_id = request.values.get('article_id') article = db.get({'ID': article_id}, 'article') if article: article.update({ 'time': article['time'].strftime("%Y-%m-%d"), 'author': get_user(article['author']), 'changer': get_user(article['changer']) }) return jsonify({'code': 1, 'msg': 'success', 'data': article}) return jsonify({'code': -1, 'msg': 'unknown article'}) return jsonify({'code': 0, 'msg': 'permission denied'})
def new_token(): """ 获取一个不重复的token :return: token """ db = Database() token = random_char() check = db.get({'token': token}, 'user') # 检查token是否可用 if check: return new_token() # 递归调用 return token
def change_head(): token = request.form['token'] db = Database() user = db.get({'token': token}, 'user') if user: head = request.form['head'] flag = db.update({'token': token}, {'head': head}, 'user') if flag: return jsonify({'code': 1, 'msg': 'success'}) return jsonify({'code': -1, 'msg': 'cannot change head'}) return jsonify({'code': 0, 'msg': 'user not found'})
def get_user(id): """ 获取用户名称 :param id: :return: """ db = Database() user = db.get({'ID': id}, 'user') if user: return user['username'] return ''
def get_history(): """ 获取修改记录历史 :return: """ token = request.values.get('token') db = Database() user = db.get({'token': token}, 'user') if user: article_id = request.values.get('article_id') articles = db.get({'article_id': article_id}, 'history', 0) for article in articles: article.update({ 'username': get_user(article['user_id']), 'date': article['date'].strftime("%Y-%m-%d %H:%M:%S") }) return jsonify({'code': 1, 'msg': 'success', 'data': articles}) return jsonify({'code': 0, 'msg': 'permission denied'})
def get_father_tag(tag, tag_list): """ 获取某个tag的上级tag :return: """ db = Database() if tag['father']: father = db.get({'ID': tag['father']}, 'tag') if father: tag_list.insert(0, father['ID']) get_father_tag(father, tag_list)
def get_history_article(): """ 获取文章的历史信息 :return: """ token = request.values.get('token') db = Database() user = db.get({'token': token}, 'user') if user: article_id = request.values.get('history_id') article = db.get({'id': article_id}, 'history') if article: article.update({ 'username': get_user(article['user_id']), 'date': article['date'].strftime("%Y-%m-%d %H:%M:%S") }) return jsonify({'code': 1, 'msg': 'success', 'data': article}) return jsonify({'code': -1, 'msg': 'cannot find article'}) return jsonify({'code': 0, 'msg': 'permission denied'})
def login(): """ 登录 :return: """ username = request.form['username'] password = request.form['password'] way = 'username' db = Database() user = db.get( { 'username': username, 'password': generate_password(password) }, 'user') if not user: user = db.get( { 'Snum': username, 'password': generate_password(password) }, 'user') way = 'Snum' if user: result = db.update( { way: username, 'password': generate_password(password) }, {'token': new_token()}, 'user') # 更新token return jsonify({ 'code': 1, 'msg': 'success', 'data': { 'token': result['token'], 'username': result['username'], 'id': result['ID'], 'group': result['group'] } }) return jsonify({'code': 0, 'msg': 'unexpected user'}) # 失败返回
def get_tag_child(tag): """ 获取tag的子tag :param tag: tag :return: children """ db = Database() tags = db.get({'father': tag['ID']}, 'tag', 0) for item in tags: item.update({ 'children': get_tag_child(item) + in_get_articles(item), 'type': 0 }) return tags
def check_account(): """ 人脸检测之前的账号密码核对 :return: """ username = request.form['username'] password = request.form['password'] db = Database() user = db.get( { 'username': username, 'password': generate_password(password) }, 'user') if not user: user = db.get( { 'Snum': username, 'password': generate_password(password) }, 'user') if user: return jsonify({'code': 1, 'msg': 'success'}) return jsonify({'code': 0, 'msg': 'unexpected user'}) # 失败返回
def get_basic_info(): token = request.form['token'] db = Database() user = db.get({'token': token}, 'user') if user: data = { 'username': user['username'], 'group': user['group'], 'snum': user['Snum'], 'desc': user['desc'], 'head': user['head'], 'phone': user['phone'] } return jsonify({'code': 1, 'msg': 'success', 'data': data}) return jsonify({'code': 0, 'msg': 'user not found'})
def delete_tag(): """ 清除tag :return: """ token = request.form['token'] db = Database() user = db.get({'token': token, 'group': 0}, 'user') if user: tag_id = request.form['tag_id'] flag = db.delete({'ID': tag_id}, 'tag') if flag: return jsonify({'code': 1, 'msg': 'success'}) return jsonify({'code': -1, 'msg': 'unknown error'}) return jsonify({'code': 0, 'msg': 'permission denied'})
def in_get_articles(tag): """ 获取tag下的文章 :param tag: :return: """ db = Database() articles = db.get({'tag': tag['ID']}, 'article', 0) data = [] for item in articles: data.append({ 'name': item['title'], 'ID': item['ID'], 'children': [], 'type': 1 }) return data
def change_password(): """ 修改密码 :return: """ token = request.form['token'] db = Database() user = db.get({'token': token}, 'user') if user: password = request.form['password'] flag = db.update( { 'password': generate_password(password), 'token': token }, 'user') if flag: return jsonify({'code': 1, 'msg': 'success'}) return jsonify({'code': -1, 'msg': 'unknown error'}) return jsonify({'code': 0, 'msg': 'permission denied'})
def change_tag(): """ 修改tag :return: """ token = request.form['token'] db = Database() user = db.get({'token': token, 'group': 0}, 'user') if user: name = request.form['name'] father = request.form['father'] tag_id = request.form['tag_id'] flag = db.update({'ID': tag_id}, { 'name': name, 'father': father }, 'tag') if flag: return jsonify({'code': 1, 'msg': 'success'}) return jsonify({'code': -1, 'msg': 'unknown error'}) return jsonify({'code': 0, 'msg': 'permission denied'})