def get_area_info(): # 查询redis try: response_json = redis_store.get('area_info') except Exception as e: logging.error(e) else: if response_json is not None: response_json = json.loads(response_json) return jsonify(errno=RET.OK, errmsg='ok', data=response_json['data']) # return response_json,200,{'Content-Type': 'application/json'} try: area_li = Area.query.all() except Exception as e: logging.error(e) return jsonify(errno=RET.DBERR, errmsg='数据库异常') area_dict_li = [] for area in area_li: area_dict_li.append(area.to_dict()) # 保存列表数据到redis中,将数据转成字符串 response_dict = dict(data=area_dict_li) # response_dict = dict(data=area_dict_li,errno=RET.OK, errmsg='ok') response_json = json.dumps(response_dict) try: redis_store.setex('area_info', constants.AREA_INFO_REDIS_CACHE_EXPIRES, response_json) except Exception as e: logging.error(e) return jsonify(errno=RET.OK, errmsg='ok', data=area_dict_li)
def get_house_detail(house_id): """ 获取房屋信息 :param house_id: ,房屋的id :return: 房屋的详细信息 """ # 当前用户的id防止自己刷单 user_id = session.get('user_id', '-1') # 校验参数 if not house_id: return jsonify(errno=RET.PARAMERR, errmsg='参数错误') # 从缓存中查询数据,首页的轮播图要优化,不能让用户频繁访问数据库 try: result = redis_store.get('house_info_%s' % house_id) except Exception as e: logging.error(e) result = None if result: # print('"errno":%s,"errmsg":"ok","data":{"house":%s,"user_id":%s}' % (RET.OK, result.decode(), user_id), 200, { # 'Content-Type': 'application/json'}) return '{"errno":%s,"errmsg":"ok","data":{"house":%s,"user_id":%s}}' % ( RET.OK, result.decode(), user_id), 200, { 'Content-Type': 'application/json' } # # print(result.decode(),200, {'Content-Type': 'application/json'}) # # return result.decode(),200, {'Content-Type': 'application/json'} # 查询数据库 try: house = House.query.get(house_id) except Exception as e: logging.error(e) return jsonify(errno=RET.DBERR, errmsg='查询数据库失败') # print(house) # <House 5>不用循环了,因为直接返回的是对象 if not house: return jsonify(errno=RET.NODATA, errmsg='房屋不存在') house_data = house.to_full_dict() # house_data.update(errno=RET.OK, errmsg='ok', user_id=user_id) # 存入redis中 json_house = json.dumps(house_data) try: redis_store.setex('house_info_%s' % house_id, constants.HOUSE_DETAIL_REDIS_EXPIRE, json_house) except Exception as e: logging.error(e) # print('*' * 20) # print(user_id, house_data) # print('%' * 20) return jsonify(errno=RET.OK, errmsg='ok', data={ 'house': house_data, 'user_id': user_id })
def login(): # 登录接受参数 request_dict = request.get_json() mobile = request_dict.get('mobile') password = request_dict.get('password') # 校验参数 if not all([mobile, password]): return jsonify(errno=RET.PARAMERR, errmsg='参数不完整') if not re.match(r'1[345678]\d{9}', mobile): return jsonify(errno=RET.PARAMERR, errmsg='手机格式错误') # 业务逻辑处理 # 判断错误次数是否超过限制,如果超过限制则直接返回通过ip查 # redis 用户ip user_ip = request.remote_addr try: access_nums = redis_store.get('access_nums_%s' % user_ip) except Exception as e: logging.error(e) else: if access_nums is not None and int( access_nums) >= constants.LOGIN_ERROR_MAX_TIMES: return jsonify(RET.REQERR, errmsg='错误次数太多请稍后重试') # 从数据库中查询手机号是否存在 try: user = User.query.filter_by(mobile=mobile).first() except Exception as e: logging.error(e) return jsonify(errno=RET.DBERR, errmsg='获取用户信息失败') # 验证密码 if user is None or not user.check_pwd_hash(password): try: pl = redis_store.pipeline() pl.incr('access_nums_%s' % user_ip) pl.expire('access_nums_%s' % user_ip, constants.LOGIN_ERROR_FORBID_TIME) pl.execute() except Exception as e: logging.error(e) return jsonify(errno=RET.DATAERR, errmsg='账号密码不匹配') # 保存登录状态 session['name'] = user.name session['mobile'] = user.mobile session['user_id'] = user.id # 返回 return jsonify(errno=RET.OK, errmsg='登录成功')
def get_house_index(): ''' 获取首页房屋信息 :return: 排序后的房屋信息 ''' # 查询数据库之前先查询缓存 try: result = redis_store.get('house_page_data') except Exception as e: logging.error(e) result = None if result: return result.decode(), 200, {'Content-Type': 'application/json'} else: # 查询数据库,返回房屋信息最多的5条数据 try: houses = House.query.order_by(House.order_count.desc()).limit( constants.HOME_PAGE_MAX_NUMS).all() except Exception as e: logging.error(e) return jsonify(errno=RET.DBERR, errmsg='获取数据失败') if not houses: return jsonify(errno=RET.NODATA, errmsg='没有数据') houses_li = [] for house in houses: houses_li.append(house.to_basic_dict()) # 如果缓存中没有数据先加载缓存中去 house_dict = dict(errno=RET.OK, errmsg='ok', data=houses_li) json_houses = json.dumps(house_dict) try: redis_store.setex('house_page_data', constants.HOME_PAGE_DATA_REDIS_EXPIRES, json_houses) except Exception as e: logging.error(e) return json_houses, 200, {'Content-Type': 'application/json'}
def register(): """ 注册 :param 手机号 短信验证码 密码 确认密码 :return: """ request_dict = request.get_json() mobile = request_dict.get('mobile') sms_code = request_dict.get('sms_code') password = request_dict.get('password') password2 = request_dict.get('password2') # 验证 if not all([mobile, sms_code, password, password2]): return jsonify(errno=RET.PARAMERR, errmsg='参数不完整') # 判断手机号的格式 if not re.match(r'1[345678]\d{9}', mobile): return jsonify(errno=RET.PARAMERR, errmsg='手机号格式错误') if password != password2: return jsonify(errno=RET.PARAMERR, errmsg='两次密码不一致') # 业务逻辑 # 从redis中取出短信验证码 try: real_sms_code = redis_store.get('sms_code_%s' % mobile) except Exception as e: logging.error(e) return jsonify(errno=RET.DBERR, errmsg='获取短信验证码异常') # 判断短信验证是否过期 if real_sms_code is None: return jsonify(errno=RET.NODATA, errmsg='短信验证码失效') # 删除redis中的短信验证码 try: redis_store.delete('sms_code_%s' % mobile) except Exception as e: logging.error(e) # 判断用户填写的短信验证码的正确性 real_sms_code = real_sms_code.decode() if real_sms_code != sms_code: return jsonify(errno=RET.DATAERR, errmsg='短信验证码错误') # 手机号是否被注册过 ,为了节省数据库的开销把手机号设置成唯一值,当手机号重复时,无法插入数据库 # try: # user = User.query.filter_by(mobile=mobile).first() # except Exception as e: # logging.error(e) # else: # if user is not None: # # 表示手机号已经被注册了,已经存在在数据库当中了 # return jsonify(errno=RET.DATAEXIST, errmsg='手机号已经存在') # 保存数据 user = User(name=mobile, mobile=mobile) # user.pwd_hash(password) 改变它为装饰器 user.password = password try: db.session.add(user) db.session.commit() except Exception as e: db.session.rollback() logging.error(e) return jsonify(errno=RET.DATAEXIST, errmsg='手机号已经存在') except Exception as e: db.session.rollback() # 为了防止数据库中存在垃圾数据所以要回滚 logging.error(e) return jsonify(errno=RET.DBERR, errmsg='插入数据库异常') # 保存登录状态到session中 session['name'] = mobile session['mobile'] = mobile session['user_id'] = user.id # 返回结果 return jsonify(errno=RET.OK, errmsg='注册成功')
def get_sms_code(mobile): '''获取短信验证码''' # 获取图片验证码参数 image_code = request.args.get('image_code') # 图片验证码 image_code_id = request.args.get('image_code_id') # uuid # 校验图片验证码参数 if not all([image_code, image_code_id]): return jsonify(errno=RET.PARAMERR, errmsg='参数不完整') # 业务逻辑,从redis取出图片验证码 try: real_image_code = redis_store.get('image_code_%s' % image_code_id) except Exception as e: logging.error(e) return jsonify(errno=RET.DBERR, errmsg='redis数据库异常') # 判断图片验证码是否过期 if real_image_code is None: return jsonify(errno=RET.NODATA, errmsg='图片验证码失效') # 没有过期删除redis中的图片验证码 try: redis_store.delete('image_code_%s' % image_code_id) except Exception as e: logging.error(e) # print(real_image_code) b'TKNE' # 与用户填写的图片验证码做对比 real_image_code = real_image_code.decode() if real_image_code.lower() != image_code.lower(): return jsonify(errno=RET.DATAERR, errmsg='图片验证码错误') # 判断手机号是否是重复发送的代码,优化 try: send_flag = redis_store.get('send_sms_code%s' % mobile) except Exception as e: logging.error(e) else: if send_flag is not None: return jsonify(errno=RET.REQERR, errmsg='请求过于频繁') # 判断手机号是否存在 try: user = User.query.filter_by(mobile=mobile).first() except Exception as e: logging.error(e) else: if user is not None: # 表示手机号已经被注册了,已经存在在数据库当中了 return jsonify(errno=RET.DATAEXIST, errmsg='手机号已经存在') # 生成短信验证码 sms_code = '%06d' % random.randint(0, 999999) # 保存真实的短息验证码到redis中 try: pl = redis_store.pipeline() # redis_store.setex('sms_code_%s' % mobile, constants.SMS_CODE_REDIS_WXPIRES, sms_code) pl.setex('sms_code_%s' % mobile, constants.SMS_CODE_REDIS_WXPIRES, sms_code) # 保存发送给这个手机号的记录 # redis_store.setex('send_sms_code%s' % mobile, constants.SEND_SMS_CODE_EXPIRES, 1) pl.setex('send_sms_code%s' % mobile, constants.SEND_SMS_CODE_EXPIRES, 1) pl.execute() except Exception as e: logging.error(e) return jsonify(errno=RET.DATAERR, errmsg='保存短信验证码异常') # 发短信,返回值将它优化为celery分布式系统,避免前端反应延迟 # try: # ccp = CCP() # result = ccp.send_message(mobile, (sms_code, int(constants.SMS_CODE_REDIS_WXPIRES / 60))) # except Exception as e: # logging.error(e) # return jsonify(errno=RET.THIRDERR, errmsg='发送异常') # from inside_frame.tasks.task_sms import send_sms # 异步发送的时候不能直接调用写的方法,一定要加上delay from inside_frame.tasks.sms.tasks import send_sms send_sms.delay(mobile, (sms_code, int(constants.SMS_CODE_REDIS_WXPIRES / 60))) # if result == 0: return jsonify(errno=RET.OK, errmsg='发送成功')