import flask, json from lib.tools import op_mysql, md5, saveRedis,table_exists,getRedis server = flask.Flask(__name__) @server.route('/table_data') def get_table_data(): table_list = ['app_myuser', 'dsk_test', 'app_student', 'app_product'] table_name = flask.request.args.get('table_name') limit = flask.request.args.get('limit', '10') # 默认给10条数据 if table_name not in table_list: return json.dumps({'msg': '没有获取这个表的权限重新输入'}, ensure_ascii=False) # 没有实现校验表是否存在 if not table_name: return json.dumps({'msg': 'table_name是必填字段'}, ensure_ascii=False) if limit.isdigit(): sql = 'select * from %s limit %s;' % (table_name, limit) else: return json.dumps({'msg': 'limit传入一个整数'}, ensure_ascii=False) result = op_mysql(sql) return json.dumps(result, ensure_ascii=False) @server.route('/add_mem', methods=['post']) def add_mem(): username = flask.request.json.get('username') password = flask.request.json.get('password') if username and password: sql = 'select * from app_myuser where username="%s";' % username if op_mysql(sql): data = {'msg': '用户名已存在'} else: new_password = md5(password) insert_sql = 'insert into app_myuser(username,passwd) value("%s","%s")' % (username, new_password) op_mysql(insert_sql) data = {'msg': '添加成功'} else: return json.dumps({'msg': '必填项参数未填,请查看接口文档'}, ensure_ascii=False) return json.dumps(data, ensure_ascii=False) @server.route("/login", methods=['post']) def login(): username = flask.request.json.get('username').strip() password = flask.request.json.get('password').strip() if username and password: sql = "select password,error_count from zf_user where username = '%s'" % username res = op_mysql(sql) print("res===",res) if res: if res.get('error_count') > 2: data = {'msg': '用户已经被锁定'} else: new_passwd = md5(password) if res.get('password') == new_passwd: saveRedis(md5(username), "session:%s" % username) # 保存到redis data = {"session": md5(username), "code": 0, "msg": '登录成功'} else: print('密码输入错误!') count = res.get('error_count') + 1 sql2 = 'update zf_user set error_count = %s where username = "%s";' % ( count, username ) op_mysql(sql2) else: data = {'msg': '用户不存在'} return json.dumps(data, ensure_ascii=False) # 2、添加qq群信息的接口, #add_mem # qq_mem表 :id,qq,nick、join_time,gender,card # qq号是唯一的 # 入参: # qq,nick、join_time,gender,card,session # 创建数据表SQL语句 table_sql = """CREATE TABLE qq_mem_dongxl ( id int(20) NOT NULL AUTO_INCREMENT PRIMARY KEY, qq varchar(20) NOT NULL unique , nick varchar(200) NOT NULL, join_time date , gender varchar(10), card varchar(100))""" @server.route('/add_qq', methods=['post']) def add_qq(): username=flask.request.args.get("username") # 判断session是否存在 values = getRedis("session:%s" % username) if not values: data = {'msg': '没有权限请重新登录', "code": -1} else: # 判断表是否存在 result = table_exists("qq_mem_dongxl") if result == 0: # 表不存在,创建表 op_mysql(table_sql) # 像表中插入数据 qq = flask.request.json.get('qq').strip() nick = flask.request.json.get('nick').strip() join_time = flask.request.json.get('join_time').strip() gender = flask.request.json.get('gender').strip() card = flask.request.json.get('card').strip() insert_sql = 'insert into qq_mem_dongxl (qq,nick,join_time,gender,card) value("%s","%s","%s","%s","%s")' \ % (qq, nick, join_time, gender, card) sql = 'select * from qq_mem_dongxl where qq="%s";' % qq if op_mysql(sql): data = {'msg': 'QQ号已存在'} else: op_mysql(insert_sql) data = {'msg': '添加成功', "code": 0} return json.dumps(data, ensure_ascii=False)