def club_user_get_session_id_view(request): """ 商家用户使用 code 去换取 sessionid URL[POST]: /weixin/club/get_session_id/ """ post_data = get_data_from_request(request) code = post_data.get('code') if code: user, session_key = WxminiAuthManager.sync_wx_mini_user_info_for_club( code) else: return json_http_error('缺少参数') user_info = get_user_info_by_user_id_db(user.id) club_user_info = get_club_user_info_by_user_info(user_info) wx_mini_request_login(request, user, session_key) if not club_user_info.exists(): return json_http_success({"sessionid": request.session.session_key}) if club_user_info.count() > 1: return json_http_error(u'多用户商户错误') return json_http_success({ 'club_id': club_user_info[0].club.id, "sessionid": request.session.session_key })
def get_next_explore_post_view(request): """ 获取信息流的下一个: 1. 信息流包含: 普通信息(足迹, 求助帖) + 优惠券信息 2. [3, 9, 20, 30] 这四个位置是优惠券, 如果没有足够的普通信息, 都填充优惠券就可以了 3. 0~10km、10~30km、30~100km、100km 及以上 GET: /footprint/next_explore_post/ 一共有四种返回类型: text, image_text, coupon_template, help """ lon = float(request.GET.get('lon', 0)) lat = float(request.GET.get('lat', 0)) user_info = get_user_info_by_user_id_db(request.user.id) current_date = datetime_to_str(datetime.datetime.now(), date_format=FORMAT_DATE_WITHOUT_SEPARATOR) next_explore_times = int( ExploreSurplusTimesManager.get_times(current_date, user_info.id)) + 1 if next_explore_times > ExploreSurplusTimesManager.EXPLORE_DAY_LIMIT: return json_http_error(u'今天的次数已经用完了') # 增加一次探索次数 ExploreSurplusTimesManager.add_times(current_date, user_info.id) # 随机返回一张优惠券, 但是, 注意位置 (获取不到优惠券, 返回用户足迹吧) if next_explore_times in [3, 9, 20, 30]: coupon_template = get_next_explore_template(next_explore_times) if coupon_template: # 返回的信息里面带有 type, 前端识别不同的 type 去展示 return json_http_success( build_coupon_template_info(coupon_template)) else: footprint = get_next_explore_footprint(next_explore_times, lon, lat, request.user) if footprint: return json_http_success( build_footprint_info_for_explore(request.user, footprint)) return json_http_error(u'没有更多的探索啦') else: # 如果不是优惠券的位置, 返回足迹 footprint = get_next_explore_footprint(next_explore_times, lon, lat, request.user) if footprint: return json_http_success( build_footprint_info_for_explore(request.user, footprint)) # 如果获取不到足迹, 再尝试返回优惠券 coupon_template = get_next_explore_template(next_explore_times, ignore_index=True) if coupon_template: return json_http_success( build_coupon_template_info(coupon_template)) # 如果优惠券也获取不到 return json_http_error(u'没有更多的探索啦')
def get_club_detail_view(request): """ 商户查看自己的信息 /commercial/club_detail/ """ return json_http_success( build_club_detail_info(int(request.GET['club_id'])))
def login_and_get_session_id_view(request): """ 使用小程序的登录然后返回session_id,目前支持两种登录方式: 1、使用微信的code后台服务器验证方式 2、使用春雨的用户名和账户验证 URL[POST]: /weixin/get_session_id/ :param request: {code, encryptedData, iv} """ # 使用code方式进行登录 post_data = get_data_from_request(request) code = post_data.get('code') encrypted_data = post_data.get('encryptedData') iv = post_data.get('iv') if code: user, session_key = WxminiAuthManager.sync_wx_mini_user_info( code, encrypted_data, iv) # 使用form表单携带账户和密码进行登录 else: return json_http_error('缺少参数') # 用户登录返回session信息 if not user: return json_http_error('invalid user') wx_mini_request_login(request, user, session_key) return json_http_success({"sessionid": request.session.session_key})
def post_content_view(request): """ 发送信息 URL[POST]: /chat/post_content/ :param request: conversation_id, content_type, content """ data = get_data_from_request(request) receiver_id = int(data.get('receiver_id')) conversation_id = data.get('conversation_id') if not receiver_id and not conversation_id: return json_http_error('参数错误') conversation_id = conversation_id or get_conversation_id_by_user_ids( [receiver_id, request.user.id]) content = data['content_json'] if content['type'] == 'image': if not is_image_valid(content['url']): return json_http_error('请文明发言!') elif content['type'] == 'text': if not is_content_valid(content['text']): return json_http_error('请文明发言!') content_str = json.dumps(content) chat_record = create_chat_record_db(conversation_id, content_str, request.user.id) # 发推送、更新badge、 ConversationMessageManager.add_message(receiver_id, request.user.id, conversation_id, content) return json_http_success()
def get_upload_token_view(request): """ 获取图片上传的token :param request: :return: """ return json_http_success({'token': get_upload_token()})
def get_nearest_activity_view(request): """ 获取最近的活动的地址,100km以内吧 :param request: :return: {'lat', 'lon'} """ lat = float(request.GET.get('lat', 0)) lon = float(request.GET.get('lon', 0)) if not (lat and lon): return json_http_error('参数错误') members = activity_location_container.get_members_within_radius( lon, lat, 100, GeoUnitEnum.KM, sort=GeoSortEnum.ASC) if not members: return json_http_success() location = activity_location_container.get_position(members[0])[0] return json_http_success({'lat': location[1], 'lon': location[0]})
def club_user_login_view(request): """ 商家用户登录 由于不需要商户的 "其他信息", 所以, 前端值传递 code 也是可以的 URL[POST]: /weixin/club/login/ """ post_data = get_data_from_request(request) account = post_data['account'] password = post_data['password'] code = post_data.get('code') if code: user, session_key = WxminiAuthManager.sync_wx_mini_user_info_for_club( code) # 使用form表单携带账户和密码进行登录 else: return json_http_error('缺少参数') # 如果之前这个用户登录过踪踪用户端, 那么, 这个 UserInfo 表对象也是有的 user_info = get_user_info_by_user_id_db(user.id) user_extra_info = json.loads(user_info.extra_info) user_extra_info.update({'is_club': 1}) user_info.extra_info = json.dumps(user_extra_info) user_info.save() club_user_info = club_user_login(account, password, user_info) if not club_user_info: return json_http_error(u'用户名或密码错误') wx_mini_request_login(request, user, session_key) return json_http_success({ 'club_id': club_user_info.club.id, "sessionid": request.session.session_key })
def activity_detail_view(request): """ 获取活动详细信息 URL[GET]: /commercial/get_activity_detail/ :return: { top_image, title, club_name, avatar, telephone, introduction, image_list, detail, address, time_detail, description, total_quota, participants: [{user_id, avatar}] } """ activity_id = request.GET['activity_id'] activity = get_commercial_activity_by_id_db(activity_id) if not activity: return json_http_error('id错误') result = build_activity_detail(activity, request.user.id) return json_http_success(result)
def set_my_profile_view(request): """ 设置我的资料接口 URL[POST]: /user_info/my_profile/edit/ :param request: sex, avatar, location, nickname, birthday, signature, wechat_no, show_wechat_no :return: """ post_data = get_data_from_request(request) sex = post_data.get('sex') avatar = post_data.get('avatar') location = post_data.get('location') nickname = post_data.get('nickname') if nickname and not is_content_valid(nickname): return json_http_error(u'nickname 不合法') if avatar and not is_image_valid(avatar): return json_http_error(u'avatar 不合法') birthday = post_data.get('birthday') wechat_no = post_data.get('wechat_no') show_wechat_no = post_data.get('show_wechat_no') if show_wechat_no is not None: show_wechat_no = bool(int(show_wechat_no)) signature = post_data.get('signature') if birthday: birthday = str_to_datetime(birthday) user_info = update_my_profile_db(request.user, sex, avatar, location, nickname, wechat_no, show_wechat_no, signature, birthday) return json_http_success() if user_info else json_http_error()
def get_club_info_view(request): """ 获取俱乐部信息 URL[GET]: /commercial/get_club_info/ :param request: :return: { club_info: { id, name, avatar, address, } activities_info: [ { activity_id, distance, title, created_time, activity_time, images_list, total_quota, participants: [ { user_id, avatar }, ] }, ] } """ club_id = int(request.GET['club_id']) club = get_club_by_id_db(club_id) if not club: return json_http_error('id错误') club_info = build_club_info(club) return json_http_success(club_info)
def explore_say_hello_view(request): """ 探索模式下的打招呼按钮 URL[POST]: /chat/explore_say_hello/ """ data = get_data_from_request(request) receiver_id = int(data.get('receiver_id')) # 消息接收者 id # 需要先根据 user_id 和 receiver_id 确定会话是否已经建立了 user_ids = [int(receiver_id), request.user.id] user_ids.sort(key=lambda peer_id: int(peer_id)) content = { 'type': 'text', 'text': [{"node": "text", "text": u"我来打个招呼~"}] } content_str = json.dumps(content) # 尝试获取会话信息 chat_info = get_conversation_info_by_user_ids(user_ids[0], user_ids[1]) # 如果获取不到, 则主动创建一个 if not chat_info: conversation_id = get_conversation_id_by_user_ids([user_ids[0], user_ids[1]]) chat_info, _ = get_or_create_conversation_info(conversation_id, user_ids[0], user_ids[1]) # 创建一条会话记录 chat_record = create_chat_record_db(chat_info.conversation_id, content_str, request.user.id) # 发推送、更新badge ConversationMessageManager.add_message(receiver_id, request.user.id, chat_info.conversation_id, content) return json_http_success()
def post_footprint_view(request): """ 发布踪踪动态 URL[POST]: /footprint/create/ :param request: :return: """ post_data = get_data_from_request(request) latitude = post_data.get('lat') longitude = post_data.get('lon') location = post_data.get('location') content = post_data['content'] if content and not is_content_valid(content): return json_http_error('请注意用词') image_list = post_data['image_list'] if isinstance(image_list, str): image_list = json.loads(image_list) for image in image_list: logging.info('{}{}'.format(image, type(image))) if not is_image_valid(image): return json_http_error('请文明发言') hide = bool(int(post_data.get('hide', 0))) footprint = create_footprint_db(request.user, content, latitude, longitude, location, image_list, hide) if latitude and longitude: add_user_location(footprint.id, longitude, latitude) return json_http_success()
def get_user_coupon_list_view(request): """ 获取用户优惠券列表信息 /footprint/user_coupon_list/ """ return json_http_success( {'coupons': build_user_coupon_list_info(request.user)})
def get_user_coupon_info_view(request): """ 获取单张用户优惠券信息 /footprint/user_coupon_info/ """ coupon_id = int(request.GET.get('coupon_id', 0)) return json_http_success( build_user_coupon_info(UserCoupon.objects.get(id=coupon_id)))
def get_nearby_clubs_view(request): """ 获取用户 "附近的" 商家信息, 按照距离倒排 URL[GET]: /commercial/nearby_clubs/ """ lon = float(request.GET.get('lon', 0)) lat = float(request.GET.get('lat', 0)) return json_http_success({'clubs': get_nearby_clubs_info(lon, lat)})
def get_top_banner_view(request): """ 获取顶部banner广告信息 :return: { title, avatar, activity_id, } """ banner = get_top_banner_db() result = {} if not banner else build_top_banner(banner) return json_http_success(result)
def club_consume_user_coupon_info_view(request): """ 商户消耗用户优惠券详情 URL[GET]: /commercial/consume_user_coupon_info/ """ club_id = int(request.GET['club_id']) consume_result = build_club_consume_user_coupon_info(club_id) if not consume_result: return json_http_error(u'找不到商家信息') return json_http_success(consume_result)
def acquire_coupon_template_view(request): """ 用户主动领取优惠券模板 POST: /footprint/acquire_coupon/ """ user = request.user post_data = get_data_from_request(request) template = get_coupon_template_by_id_db(post_data['template_id']) return json_http_success({'id': acquire_new_coupon(user, template)})
def add_favor_view(request): """ 点赞view,点击两次的话就取消了 URL[POST]: /footprint/favor/ :param request: footprint id :return favor_num: 总的点赞数 """ post_data = get_data_from_request(request) footprint_id = post_data['footprint_id'] favor_num = add_favor_db(footprint_id, FlowType.FOOTPRINT, request.user.id) return json_http_success({'favor_num': favor_num})
def get_explore_banner_view(request): """ 获取顶部 explore banner 信息 URL[GET]: /commercial/get_explore_banner/ :return: { 'title': banner.title, 'description': banner.description, 'image': banner.image } """ return json_http_success(build_explore_banner(get_explore_banner_db()))
def favor_activity_view(request): """ 给活动点赞 URL[POST]: /commercial/favor_activity/ :param request: :return: """ post_data = get_data_from_request(request) activity_id = post_data['activity_id'] favor_num = add_favor_db(activity_id, FlowType.ACTIVITY, request.user.id) return json_http_success({'favor_num': favor_num})
def club_update_detail_view(request): """ 商户更新自己的头像信息 /commercial/update_detail/ """ post_data = get_data_from_request(request) avatar_url = post_data['avatar'] club_id = post_data['club_id'] club_update_detail(avatar_url, club_id) return json_http_success({})
def get_user_brief_profile_view(request): """ 获取用户小窗口简介 1.用户个人资料 2.用户最新一条票圈 URL[GET]: /user_info/get_user_info/ :param request: user_id """ user_id = request.GET['user_id'] if not user_id: return json_http_error('') return json_http_success({'user_info': get_user_brief_profile(user_id)})
def get_user_coupon_info_for_charge_off_view(request): """ 展示用户优惠券的信息 --> 用来核销的商户查看, 所以, 只会传递 coupon_code URL[GET]: /commercial/user_coupon_info_for_charge_off/ """ club_id = int(request.GET['club_id']) coupon_code = request.GET['coupon_code'] coupon_info = build_user_coupon_info_for_charge_off(club_id, coupon_code) if not coupon_info: return json_http_error(u'优惠券码错误') return json_http_success(coupon_info)
def club_activity_confirm_info_view(request): """ 商户活动预约确认/未确认信息详情 URL[GET]: /commercial/activity_confirm_info/ """ club_id = int(request.GET['club_id']) is_confirm = bool(int(request.GET['is_confirm'])) # 0-未确认; 1-已确认 confirm_info = build_club_activity_confirm_info(club_id, is_confirm) if not confirm_info: return json_http_error(u'找不到商家信息') return json_http_success(confirm_info)
def club_confirm_activity_participant_view(request): """ 商户确认用户的活动预约 URL[POST]: /commercial/confirm_activity_participant/ """ post_data = get_data_from_request(request) activity_participant_id = post_data['activity_participant_id'] confirm_info = club_confirm_activity_participant(activity_participant_id) if confirm_info: return json_http_error(confirm_info) return json_http_success({})
def user_delete_footprint_view(request): """ 用户删除自己发表的足迹记录 /footprint/user_delete/ """ user_id = int(request.GET.get('user_id', 0)) or request.user.id footprint_id = int(request.GET.get('footprint_id', 0)) footprint = get_footprint_by_id_db(footprint_id) if not footprint or footprint.user_id != user_id: return json_http_success({}) footprint.is_deleted = True footprint.save() # 用户删除足迹时, 清掉缓存信息 remove_user_location(footprint_id) try: TotalFlow.objects.get(flow_id=footprint_id, flow_type=FlowType.FOOTPRINT).delete() except Exception as e: print(e) return json_http_success({})
def help_post_pop_up_view(request): """ 用户首次进入帮助贴的弹窗 /footprint/user_help_post_pop_up/ """ user_info = get_user_info_by_user_id_db(request.user.id) user_info_extra = json.loads(user_info.extra_info) if 'first_time_help_post' not in user_info_extra: user_info_extra['first_time_help_post'] = True user_info.extra_info = json.dumps(user_info_extra) user_info.save() return json_http_success({ "title": u'【掘地求胜】求助帖说明', "image": u'', "description": u'欢迎来到【掘地求胜】活动,在这里,如何您有中意的商家,' u'您可以向周围的小哥哥/小姐姐发送求助,索要商家优惠券。\n您只要选择商家后,点击发布TA就能看到啦~' }) return json_http_success({})
def get_footprint_detail_view(request): """ /footprint/detail/ 获取痕迹详情 包含: 1.footprint详情 2.评论: 距离 3.评论的点赞数 :param request: :return: """ footprint_id = request.GET.get('footprint_id') footprint = get_footprint_by_id_db(footprint_id) footprint_detail = build_footprint_detail(footprint, request.user) return json_http_success(footprint_detail)