def show_user_info(): uid = request.args.get('userId') peer_id = request.args.get('peerId') show_id = uid if peer_id: show_id = peer_id user = get_user_with_cache(show_id) if not user: return make_response_error(501, 'user not found') data = { 'user_id': user.id, 'nickname': user.nickname, 'birthday': user.birthday, 'gender': user.gender, 'avatar': user.avatar, 'height': user.height, 'sexual': user.sexual, 'education': user.education, 'salary': user.salary, 'emotion': user.emotion } if show_id == uid: data['phone'] = user.phone return make_response_ok(data)
def send_sms(): phone = request.form.get('phone') m = re.match(pattern_phone, phone) if not m: return make_response_error(300, 'phone number format error.') # 这里需要修改为对接短信服务 code = '97532' key = f'{phone}-{code}' r.set(key, code, 60) return make_response_ok({'phone': phone, 'code': code})
def list_nearby_user(): latlng = request.args.get('latlng', '') logging.info(f'remote ip : {request.remote_addr}') location = latlng.split(',') if len(location) <= 1: return make_response_error(502, 'lat,lng error') lat, lng = location data = {'longitude': lng, 'latitude': lat} return make_response_ok(data)
def update_user(): uid = request.form.get('userId', '') nickname = request.form.get('nickname') if not check_nickname(nickname): return make_response_error( 505, 'the length of nickname must larger than 5 and less thad 8') email = request.form.get('email') gender = request.form.get('gender', default=0, type=int) if gender > 2 or gender < 0: gender = 0 sexual = request.form.get('sexual', default=0, type=int) if sexual > 2 or sexual < 0: sexual = 0 height = request.form.get('height', type=int) education = request.form.get('education', type=int) emotion = request.form.get('emotion', type=int) salary = request.form.get('salary', type=int) birthday = request.form.get('birthday', type=str) avatar = request.form.get('avatar', type=str) if birthday: birthday = datetime.strptime(birthday, "%Y-%m-%d") user: UserInfo = get_user_with_cache(uid) user.nickname = nickname user.emotion = emotion user.email = email user.gender = gender user.sexual = sexual user.height = height user.education = education user.salary = salary user.birthday = birthday user.avatar = avatar db.session.commit() return make_response_ok(data={"data": user.id})
def login(): phone = request.form.get('phone') code = request.form.get('code') key = f'{phone}-{code}' sms_code = r.get(key) if sms_code: sms_code = sms_code.decode() if code != sms_code: return make_response_error(503, 'sms code error') auth_info = UserAuth.query.filter_by(open_id=phone).first() if not auth_info: auth_info = register_by_phone(phone) else: auth_info = login_by_phone(auth_info) data = { 'token': auth_info.token, 'expire_time': auth_info.expired_time.strftime("%Y-%m-%d %H:%M:%S"), 'user_id': auth_info.user_basic.id } r.set(f'auth_info_{auth_info.user_id}', str(data)) return make_response_ok(data)
def internal_error(error): db.session.rollback() return make_response_error(500, error.description)
def not_found_error(error): return make_response_error(404, error.description)