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 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 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 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 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'})
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 change_article_tag(): """ 修改文章标签 :return: """ token = request.form['token'] db = Database() user = db.get({'token': token, 'group': 0}, 'user') if user: tag_id = request.form['tag_id'] name = request.form['name'] tag_type = request.form['tag_type'] icon = request.form['icon'] description = request.form['description'] flag = db.update({'id': tag_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': 'update error'}) return jsonify({'code': 0, 'msg': 'permission denied'})