def get_image_code(): """ :return: """ # 1.接受参数 image_code_id = request.args.get('imageCodeID') # 2.校验参数 if not image_code_id: abort(400) # 缺少参数 # 3.生成图片及验证码 name, text, image = captcha.generate_captcha() try: redis_store.set("imageID:" + image_code_id, text, constants.IMAGE_CODE_REDIS_EXPIRES) except Exception as e: logging.error(e) abort(500) response = make_response(image) response.headers["Content-Type"] = "image/jpg" return response
def send_msg_code(): # 获取参数 print('#######################') phone = request.form.get('phone') print(phone) # 校验参数是否为空 if not all([phone]): return jsonify(phone=phone, msg='有参数为空') # 校验手机号码是否合法 if not re.match(r'1[3456789]\d{9}', phone): return jsonify(msg='手机号码不合法') # 生成并存储短信验证码 msg_code = '%04d' % random.randint(0, 9999) print(msg_code) # 参数是key,value,过期时间 try: redis_store.set('msg_%s' % phone, msg_code, constant.PHONE_CODE_REDIS_EXPIRES) except Exception as e: logging.error(e) return jsonify(msg='验证码保存错误') logging.debug(msg_code) # 发送短信验证码 ccp = CCP() ret = ccp.send_template_sms(phone, [msg_code, constant.PHONE_CODE_REDIS_EXPIRES/60], '1') print('手机验证码发送结果', ret) if ret == -1: return '发送失败' # 响应 return jsonify(msg=constant.RET_OK)
def send_sms_code(): # 1.获取数据 print(request) json_dict = request.json print(json_dict) phone_number = json_dict.get('mobile') client_image_code = json_dict.get("image_code") image_code_id = json_dict.get("image_code_id") # 2.检验数据 if not all([phone_number, client_image_code, image_code_id]): return jsonify(errno=response_code.RET.PARAMERR, errmsg="缺少必传参数") if not re.match("^1[3-8]\d{9}$", phone_number): return jsonify(errno=response_code.RET.PARAMERR, errmsg="电话号码有误") try: server_image_code = redis_store.get("imageID:" + image_code_id) except Exception as e: logging.error(e) return jsonify(errno=response_code.RET.DBERR, errmsg='查询图片验证码失败') if not server_image_code: return jsonify(errno=response_code.RET.NODATA, errmsg='图片验证码不存在') if client_image_code.lower() != server_image_code.lower(): return jsonify(errno=response_code.RET.PARAMERR, errmsg='验证码输入有误') sms_code = "%06d" % random.randint(0, 999999) # print(sms_code) logging.debug(sms_code) # 调用阿里云接口发送验证码 __business_id = uuid.uuid1() code_json = "{'code':" + sms_code + "}" res = json.loads( send_sms(__business_id, phone_number, "hcf1425", "SMS_145595939", code_json).decode()) status = res.get('Code') # print(res) if status != "OK": return jsonify(errno=response_code.RET.PARAMERR, errmsg='短信发送失败') try: redis_store.set("SMS:" + phone_number, sms_code, constants.SMS_CODE_REDIS_EXPIRES) except Exception as e: logging.error(e) return jsonify(errno=response_code.RET.DATAERR, errmsg='存储短信失败') # 8.返回短信验证码发送的结果 return jsonify(errno=response_code.RET.OK, errmsg='发送短信成功')
def cat_detail(): # 这个是详情页,只需要获取car_id car_id = request.args.get('car_id') car_obj = Car.query.get(car_id) # 检验数据是否为空 if not car_obj: return jsonify(errcode=constant.RET_CAR_NOTFOUND, errmsg='car_id出错') # todo 当用户登录的情况下,存储car_id到redis数据库8中,类型用列表,有个g变量存了user_id user_id = session.get('user_id') if user_id: try: history_key = 'history_%s' % user_id # 可能插入重复,所以要删除原来的再加入 redis_store.lrem(history_key, 0, car_id) # 进行存储,左插入,获取使用lrange redis_store.lpush(history_key, car_id) except Exception as e: print(e) return jsonify(errcode=constant.RET_REDIS_INSERT_ERR, errmsg='redis插入错误') print(request.args) print(car_obj) # todo 获取到了这辆车,然后展示六个表内容和图片,to_detail_dict() # todo 防止频繁访问mysql,存进redis redis_key = 'mysql-redis_%s' % car_id # 存进redis的key名 redis_data = redis_store.get(redis_key) if redis_data: # 注意bytes转码 ret_dict = redis_data.decode() else: # 字典存进redis要序列化 try: ret_dict = json.dumps(car_obj.to_detail_dict()) redis_store.set(redis_key, ret_dict, constant.CAR_INFO_REDIS_EXPIRES) except Exception as e: print(e) return jsonify(errcode=constant.RET_REDIS_INSERT_ERR, errmsg='redis插入错误') # 返回信息,和data ret_dict = json.loads(ret_dict) return jsonify(msg='ok', data=ret_dict)
def post(self, address): """ :param address: 搜索城市 post的数据 info_id:选择的地址ID "info_id: 50488" search_month:搜索的月份 "search_month: 10" search_year: 搜索的年份 "search_year: 2018" search_datetime: 搜索的日期,当需要查询某一天的具体时间的时候才传入这个字段的数据,其他时候不传,该数据的月份年份需与 search_month,search_year相同 "search_datetime: 10/16/2018" :return: 如果有search_datetime这个字段, 则返回当日可用时间:["09:00 AM", "12:00 PM", "03:00 PM"] 如果没有,则返回当月可选日期 ['10/11/2018', '10/13/2018', '10/16/2018', '10/17/2018', '10/20/2018', '10/29/2018', '11/01/2018', '11/05/2018', '11/08/2018', '11/15/2018', '11/22/2018', '11/29/2018'] """ args = parser.parse_args() info_id = args.get('info_id') search_month = args.get('search_month') search_year = args.get('search_year') search_datetime = args.get('search_datetime') search_data = dict( zip(['id', 'month', 'year', 'datetime'], [info_id, search_month, search_year, search_datetime])) redis_key = address + info_id + "_" + search_month+ "_" + search_year if not search_datetime \ else address + info_id + "_" + search_datetime rv = redis_store.get(redis_key) code = 200 if not rv: try: rv = self.sp.doSearchData(address, search_data) redis_store.set(redis_key, rv) redis_store.expire(redis_key, 1296000) #设置过期时间为15天 except Exception as e: print(e) code = 500 rv = [] else: rv = eval(rv) return jsonify({"code": code, "data": rv})
def get(self, address): openId = request.args["openId"] user = User.query.filter_by(openId=openId).first() if user is None: return [] rv = redis_store.get(address) if not rv: try: results = self.sp.doSearchData(address) redis_store.set(address, results) redis_store.expire(address, 1296000) #设置过期时间为15天 rv = {address: results} rv["code"] = 200 # user.sharedNum = (user.sharedNum - 1) if user.sharedNum > 1 else 0 # db.session.add(user) # db.session.commit() except Exception as e: print(e) rv = {"code": 500, "msg": "Get Address Info List Fail!"} else: rv = {address: eval(rv)} rv["code"] = 200 return jsonify(rv)
def connect(): user_id = session.get("user_id") if user_id is None: return False sid = request.sid redis_store.set(user_id, sid)