def reg(): # 参数验证 if not request.json: code = '100003' msg = '请求数据格式错误(非JSON格式)' return jsonify({"code": code, "msg": msg, "data": {}}) if 'name' not in request.json: code = '100004' msg = 'name参数缺失' return jsonify({"code": code, "msg": msg, "data": {}}) if 'password' not in request.json: code = '100004' msg = 'password参数缺失' return jsonify({"code": code, "msg": msg, "data": {}}) name = request.json.get('name') password = request.json.get('password') u = User() status = u.addUser(name, password) if status is True: code = '100000' msg = '成功' session_id = str(uuid.uuid1()) session['session_id'] = True response = make_response( jsonify({ "code": code, "msg": msg, "data": { "name": name, "password": md5(password) } })) response.set_cookie('SESSION_ID', session_id, expires=120) return response elif status == -1: code = '100001' msg = '失败,用户已存在' else: code = '100002' msg = '失败,添加用户失败' return jsonify({ "code": code, "msg": msg, "data": { "name": name, "password": md5(password) } })
def checkUser(self, name, password): global data user = list(filter(lambda user: user.get('name') == name, data)) if user: return '验证成功' if md5(password) == user[0].get( 'password') else '用户名或密码错误' else: return '用户不存在'
def modifyUser(self, name, password): global data user = list(filter(lambda user: user.get('name') == name, data)) if not user: return '用户不存在' else: id = int(data[-1].get('id')) + 1 user[0].update({"id": id, "name": name, "password": md5(password)}) with open(DB_FILE, 'w') as f: json.dump(data, f) with open(DB_FILE, encoding='utf-8') as f: data = json.load(f) user = list(filter(lambda user: user.get('name') == name, data)) if user: return '修改成功' if md5(password) == user[0].get( 'password') else '修改失败,密码错误' else: return '修改失败,用户不存在'
def modifyUser(self, name, password): if not self.getUserByName(name): return -1 else: self.cur.execute( "update user set name='%s', password='******' where name='%s'" % (name, md5(password), name)) self.conn.commit() return self.checkUser(name, password)
def addUser(self, name, password): if self.getUserByName(name): return -1 else: self.cur.execute( "insert into user (name, password) values ('%s', '%s')" % (name, md5(password))) self.conn.commit() return self.checkUser(name, password)
def modifyUser(self, name, password): if not self.getUserByName(name): return -1 else: id = int(self.data[-1].get('id')) + 1 self.getUserByName(name).update({ "id": id, "name": name, "password": md5(password) }) with open(self.path, 'w') as f: json.dump(self.data, f) self.load() return self.checkUser(name, password)
def updateUser(): # 参数验证 if not request.json: code = '100003' msg = '请求数据格式错误(非JSON格式)' return jsonify({"code": code, "msg": msg, "data": {}}) if 'name' not in request.json: code = '100004' msg = 'name参数缺失' return jsonify({"code": code, "msg": msg, "data": {}}) if 'password' not in request.json: code = '100004' msg = 'password参数缺失' return jsonify({"code": code, "msg": msg, "data": {}}) token = request.values.get('token') if not token or len(token) != 32: return jsonify({"code": '100007', "msg": '鉴权失败', "data": None}) else: u = User() name = request.json.get('name') password = request.json.get('password') if u.updateUser(name, password): code = '100000' msg = '成功' else: code = '100007' msg = '更新用户信息失败' return jsonify({ "code": code, "msg": msg, "data": { "name": name, "passwod": md5(password) } })
def checkUser(self, name, password): if self.getUserByName(name): return True if md5(password) == self.getUserByName(name).get( 'password') else False else: return None
json.dump(self.data, f) self.load() return self.checkUser(name, password) def updateUser(self, name, password): if self.getUserByName(name): return self.modifyUser(name, password) else: return self.addUser(name, password) def delUser(self, name): user = self.getUserByName(name) if not user: return None else: self.data.pop(self.data.index(user)) with open(self.path, 'w') as f: json.dump(self.data, f) self.load() return False if self.getUserByName(name) else True if __name__ == '__main__': u = User("user_data.json") print(u.getAll()) print(u.getUserById(1)) print(u.getUserByName("张三")) print(md5('123456')) print(u.checkUser("张三", '123456')) print(u.addUser("李四", '234567')) print(u.modifyUser("李四", '134567'))
from flask_restful import Api from flask_restful import Resource from sign import checkSign from sign import md5 # import sys # sys.path.append("/usr/local/lib/python3.6/site-packages") # from flask_cors import * cf = configparser.ConfigParser() conf_path = os.path.join(os.path.dirname(__file__), "db.conf") cf.read(os.path.join(os.path.dirname(__file__), "db.conf")) app = Flask(__name__) app.secret_key = md5("user") api = Api(app) # CORS(app, supports_credentials=True) if cf.get("DEFAULT", 'redis') == 'on': app.config['SESSION_TYPE'] = 'redis' app.config['SESSION_REDIS'] = redis.Redis( host=cf.get("REDIS", 'redis_server'), port=int(cf.get("REDIS", 'redis_port'))) Session(app) redis_db1 = redis.Redis(host=cf.get("REDIS", 'redis_server'), port=int(cf.get("REDIS", 'redis_port')), db=1) if cf.get('DEFAULT', 'mysql') == 'on': from user_model_mysql import User