def pay(): sessionid = flask.request.values.get('sessionid') money = flask.request.values.get('money') if not sessionid or not money: data = {"code": -1, 'msg': '不能为空'} elif not tools.is_price(money): data = {"code": -1, 'msg': '不能为空'} else: r = tools.get_redis() sessionid_key = r.keys('*%s' % sessionid) if sessionid_key: sessionid = sessionid_key[0] else: return json.dumps({"code": -1, "msg": "请登录"}) user_info = tools.my_redis(sessionid) if user_info: user_info = json.loads(user_info) user_id = user_info.get("user_id") sql = 'select balance from app_myuser where id=%s;' % user_id balance = tools.op_mysql(sql, False).get("balance") money = float(money) if balance >= money: # balance-=money update_sql = "update app_myuser set balance = balance-%s where id = %s" % ( money, user_id) print(update_sql) tools.op_mysql(update_sql) data = {"code": 0, "msg": "支付成功"} else: data = {"code": 0, "msg": "余额不足"} else: data = {"code": -1, "msg": "请登录"} return json.dumps(data, ensure_ascii=False, indent=4)
def pay(): sessionId = request.values.get('sessionId') money = request.values.get('money') if tools.iskey(sessionId): if tools.isdigit(money): username = tools.rc.get(sessionId) sql1 = 'SELECT balance FROM user WHERE username="******"' % (username) balance = tools.op_mysql(sql1, many=0) if balance.get('balance') and float( balance.get('balance') - float(money)) >= 0: sql2 = 'update user set balance=%s-%s where username="******";' % ( float(balance.get("balance")), float(money), username) try: tools.op_mysql(sql2, many=0) return json.dumps({ 'error_code': 0, 'msg': '支付成功' }, ensure_ascii=False) except: return json.dumps({ 'error_code': 1, 'msg': '扣款失败' }, ensure_ascii=False) else: return json.dumps({ 'error_code': 1, 'msg': '余额不足)' }, ensure_ascii=False) else: return json.dumps({ 'error_code': 1, 'msg': '请输入正确金额数值)' }, ensure_ascii=False) else: return json.dumps({ 'error_code': 1, 'msg': '非用户登录,登录不成功!' }, ensure_ascii=False)
def get_bill(): table_list = ['app_myuser', 'czm'] table_name = flask.request.values.get('table_name') limit = flask.request.values.get('limit', 50) if table_name and table_name in table_list: sql = 'select id,username,passwd from %s limit %s' % (table_name, limit) result = tools.op_mysql(sql) data = {'error_code': 0, 'msg': '成功', 'data': result} else: data = {'error_code': -1, 'msg': '没有权限查询该表'} return json.dumps(data, ensure_ascii=False)
def login(): username = flask.request.values.get('username') password = flask.request.values.get('password') if not username or not password: data = {"code": -1, 'msg': '不能为空'} else: # new_password = tools.md5(password) new_password = password sql = 'select id,username,passwd,error_count from app_myuser where username="******";' % ( username) result = tools.op_mysql(sql, False) if result: if result.get('error_count') > 5: data = {"code": -1, 'msg': '账号已经被锁定'} elif new_password == result.get('passwd'): up_sql = 'update app_myuser set error_count=0 where username="******";' % username tools.op_mysql(up_sql) r = tools.get_redis() user_keys = r.keys('%s*' % username) if user_keys: sessionid = user_keys[0].lstrip(username) else: sessionid = tools.get_sessionid(username) user_info = json.dumps({ "user_id": result.get('id'), "username": username }) tools.my_redis(username + sessionid, user_info) data = {"code": 0, "msg": "登录成功", "session_id": sessionid} else: up_sql = 'update app_myuser set error_count=error_count+1 where username="******";' % username tools.op_mysql(up_sql) data = {"code": -1, 'msg': '密码错误'} else: data = {"code": -1, 'msg': '用户不存在'} return json.dumps(data, ensure_ascii=False, indent=4)
import tools, datetime for i in range(3): username = input('username:'******'pwd:').strip() if username == '' or pwd == '': print('不能为空') else: new_password = tools.md5(pwd) sql = 'select username,password,error_count from app_myuser where username="******";' % ( username) result = tools.op_mysql(sql, False) if result: if result.get('error_count') > 5: print('错误次数大于5,账号被冻结') break elif new_password == result.get('password'): print('登录成功 today is %s' % datetime.datetime.today()) up_sql = 'update app_myuser set error_count=0 where username="******";' % username tools.op_mysql(up_sql) break else: up_sql = 'update app_myuser set error_count=error_count+1 where username="******";' % username tools.op_mysql(up_sql) print('密码错误!') else: print('用户不存在')
import tools for i in range(3): username = input('username:'******'pwd:').strip() cpwd = input('cpwd:').strip() if username == '' or pwd == '' or cpwd == '': print('不能为空') elif pwd != cpwd: print('两次输入的密码不一致') else: sql = 'select username from app_myuser where username="******";' % username if tools.op_mysql(sql): print('已被注册过') else: new_password = tools.md5(pwd) insert_sql = 'insert into app_myuser (username,password) values ("%s","%s");' % ( username, new_password) tools.op_mysql(insert_sql) print('注册成功!') break