def get_area_info(): """获取城区信息""" # 查缓存 try: resp_json = redis_store.get("area_info") except Exception as e: current_app.logger.error(e) else: if resp_json is not None: current_app.logger.info("redis have area info") return resp_json, 200, {"Content-Type": "application/json"} # 查数据库,转换为json,并保存入redis try: area_li = Area.query.all() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="数据库异常") area_dict_li = [] for area in area_li: area_dict_li.append(area.to_dict()) res_dict = dict(errno=RET.OK, errmsg="OK", data=area_dict_li) resp_json = json.dumps(res_dict) try: redis_store.setex("area_info", settings.AREA_INFO_REDIS_CACHE_EXPIRES, resp_json) except Exception as e: current_app.logger.error(e) return resp_json, 200, {'Content-Type': "application/json"}
def register(): """ 注册 :param 手机号、短信验证码、密码、确认密码 :return: json 提示前端是否注册成功 """ req_dict = request.get_json() mobile = req_dict.get("mobile") sms_code = req_dict.get("sms_code") password = req_dict.get("password") confirm_password = req_dict.get("password2") # 校验 if not all([mobile, sms_code, password, confirm_password]): return jsonify(errno=RET.PARAMERR, errmsg="参数不完整") if not re.match(r'1[3578]\d{9}', mobile): return jsonify(errno=RET.PARAMERR, errmsg="手机号格式错误") if password != confirm_password: return jsonify(errno=RET.PARAMERR, errmsg="两次密码不一致") try: real_sms_code = redis_store.get("sms_code_%s" % mobile) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="服务器错误") # 读取短信验证码失败 if real_sms_code is None: return jsonify(errno=RET.NODATA, errmsg="验证码失效") try: redis_store.delete("sms_code_%s" % mobile) except Exception as e: current_app.logger.error(e) if str(real_sms_code, encoding='utf-8') != sms_code: return jsonify(errno=RET.DATAERR, errmsg="短信验证码错误") user = User(name=mobile, mobile=mobile) user.password = password try: db.session.add(user) db.session.commit() except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="手机号已存在") session['name'] = mobile session['mobile'] = mobile session['user_id'] = user.id return jsonify(errno=RET.OK, errmsg="注册成功")
def login(): """ 用户登录 :return: """ req_dict = request.get_json() mobile = req_dict.get("mobile") password = req_dict.get("passwd") if not all([mobile, password]): return jsonify(errno=RET.PARAMERR, errmsg="参数不完整") if not re.match(r'1[3578]\d{9}', mobile): return jsonify(errno=RET.PARAMERR, errmsg="手机格式不正确") # 判断是否超出错误次数限制 user_ip = request.remote_addr try: access_num = redis_store.get("access_num_%s" % user_ip) except Exception as e: current_app.logger.error(e) else: if access_num is not None and int( access_num) > settings.LOGIN_ERROR_MAX_TIMES: return jsonify(errno=RET.REQERR, errmsg="操作频繁,请稍后再试") try: user = User.query.filter_by(mobile=mobile).first() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="获取用户信息失败") if user is None or not user.check_password(password): try: if user_ip != "127.0.0.1": redis_store.incr("access_num_%s" % user_ip) redis_store.expire("access_num_%s" % user_ip, settings.LOGIN_ERROR_FORBID_TIME) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DATAERR, errmsg="账号或密码错误") session["mobile"] = user.mobile session["name"] = user.name session["user_id"] = user.id return jsonify(errno=RET.OK, errmsg="登录成功")
def get_house_index(): """主页轮播图图片展示""" try: ret = redis_store.get("home_page_data") except Exception as e: current_app.logger.error(e) ret = None if ret: current_app.logger.info("redis have house_index data") return '{"errno":0, "errmsg":"OK", "data":%s}' % str( ret, encoding="utf8"), 200, { "Content-Type": "application/json" } try: houses = House.query.order_by(House.order_count.desc()).limit( settings.HOME_PAGE_MAX_HOUSES) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="查询数据失败") if not houses: return jsonify(errno=RET.NODATA, errmsg="无数据") houses_list = [] for house in houses: if not house.index_image_url: continue houses_list.append(house.to_basic_dict()) json_house = json.dumps(houses_list) try: redis_store.setex("home_page_data", settings.HOUES_LIST_PAGE_REDIS_CACHE_EXPIRES, json_house) except Exception as e: current_app.logger.error(e) return '{"errno":0, "errmsg":"OK", "data":%s}' % json_house, 200, { "Content-Type": "application/json" }
def get_house_detail(house_id): """获取房屋的详细信息""" user_id = session.get("user_id") if not house_id: return jsonify(errno=RET.PARAMERR, errmsg="参数缺失") try: ret = redis_store.get("house_info_%s" % house_id) except Exception as e: current_app.logget.error(e) ret = None if ret: data = {"user_id": user_id, "house_data": str(ret, encoding="utf8")} current_app.logger.info("redis have house_data") return jsonify(errno=RET.OK, errmsg="OK", data=data) try: house = House.query.get(house_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="无有效数据") if not house: return jsonify(errno=RET.NODATA, errmsg="房屋不存在") try: house_data = house.to_full_dict() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DATAERR, errmsg="数据出错") json_house = json.dumps(house_data) try: redis_store.setex("house_info_%s" % house_id, settings.HOUSE_DETAIL_REDIS_EXPIRE_SECOND, json_house) except Exception as e: current_app.logger.error(e) data = {"user_id": user_id, "house_data": json_house} return jsonify(errno=RET.OK, errmsg="OK", data=data)
def send_sms(): """ 发送短信验证码,并保存到redis,判断图片验证码的有效性,并删除原有图片验证码 url: /api/smscode/?mobile=xx&code=xx&codeId=xx :param :return: """ image_code = request.args.get("code") image_code_id = request.args.get("codeId") mobile = request.args.get("mobile") if not all([image_code_id, image_code, mobile]): return jsonify(errno=RET.PARAMERR, errmsg="参数不完整") # 判断图片验证码 try: real_image_code = redis_store.get("image_code_%s" % image_code_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="redis数据库异常") if real_image_code is None: return jsonify(errno=RET.NODATA, errmsg="图片验证码失效") if real_image_code.lower() != real_image_code.lower(): return jsonify(errno=RET.DATAERR, errmsg="图片验证码错误") try: redis_store.delete("image_code_%s" % image_code_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="redis数据库异常") # 判断手机号 if not re.match(r'1[3458]\d{9}', str(mobile)): return jsonify(errno=RET.PARAMERR, errmsg="手机号格式错误") try: user = User.query.filter_by(mobile=mobile).first() except Exception as e: current_app.logger.error(e) else: if user is not None: return jsonify(error=RET.DATAEXIST, errmsg="手机号已存在") sms_code = "%06d" % random.randint(0, 99999) try: redis_store.setex("sms_code_%s" % mobile, settings.SMS_CODE_REDIS_EXPIRES, sms_code) redis_store.setex("send_sms_code_%s" % mobile, settings.SEND_SMS_CODE_INTERVAL, 1) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="保存短信验证码异常") # 非异步发送短信 try: sms = SendSMS() result = sms.send_template_sms(mobile, [sms_code, int(settings.SEND_SMS_CODE_INTERVAL/60)], 1) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg="发送异常") if result == 0: return jsonify(errno=RET.OK, errmsg="发送成功") else: return jsonify(errno=RET.THIRDERR, errmsg="发送失败")