def join_card_by_user_code(): """ 扫描二维码跳转页面 :return: """ # 由于该页面是一个主入口,逻辑处理需要严谨 # 必须参数,from_open_id: 来源用户,invitation_code:邀请码 from_open_id = request.args.get('from_open_id', None) invitation_code = request.args.get('invitation_code', None) if from_open_id is None or invitation_code is None: return abort(404) # 检查来源用户是否存在 if not BusinessCardModel.has_card(from_open_id): return abort(404) # 检查邀请码???(暂时忽略) pass # 如果是自己扫描自己的二维码,就什么都不处理,跳转到资源社区主页 if g.open_id == from_open_id: return redirect(url_for('web_app.contact_list')) # 用户已经生成名片,发送申请资源请求,跳转到资源社区主页 if BusinessCardModel.has_card(g.open_id): ApplyFriendsModel.apply_friend(g.open_id, from_open_id, u'通过扫描您的海报申请资源对接') return redirect(url_for('web_app.contact_list')) # 用户还没有生成名片,跳转到生成名片页面 return redirect(url_for('web_app.build_card') + "?from_open_id=" + from_open_id + "&invitation_code=" + invitation_code)
def apply_friend(): """ 申请添加好友页面 :return: """ target_open_id = request.args.get('open_id', None) source_open_id = g.open_id if not BusinessCardModel.has_card(target_open_id) or not BusinessCardModel.has_card(source_open_id): return abort(404) return send_file('templates/v1/apply_friend.html', cache_timeout=0)
def get_card_of_a_prople(open_id): """ 获取某位用户的信息 :param open_id: 用户openid :return: """ if not BusinessCardModel.has_card(open_id): return abort(404) card = BusinessCardModel.get_name_card(open_id) card = BusinessCardModel.handle_data_use_in_json(card) if card['redundancy_labels']: card['keys'] = card['redundancy_labels'].split('#') else: card['keys'] = [] if FriendModel.is_friend(g.open_id, open_id) or open_id == g.open_id: card['is_friend'] = 0 else: card['is_friend'] = 1 # 判断是不是自己 if open_id == g.open_id: card['is_me'] = 0 else: card['is_me'] = 1 response = make_response(json.dumps(card)) response.headers['Content-Type'] = 'application/json' return response
def add_friend(cls, source_open_id, target_open_id): """ 添加好友 :param source_open_id: 用户1openid :param target_open_id: 用户2openid :return: boolean """ if cls.is_friend(source_open_id, target_open_id): return False if not BusinessCardModel.has_card(target_open_id): return False if source_open_id == target_open_id: return False sql = "insert into " + cls.get_table_name() + "(source_open_id, target_open_id, status, create_time) " \ "values(%s, %s, 0, now());" db_lib = cls.get_db_lib() db_lib.insert(sql, [source_open_id, target_open_id]) db_lib.insert(sql, [target_open_id, source_open_id]) # 异步通知修改冗余标签 get_update_card_redundancy_labels_dao().delay(source_open_id, target_open_id) return True
def apply_friend(): """ 申请添加好友 :return: """ source_open_id = g.open_id target_open_id = request.form.get('target_open_id', None) remarks = request.form.get('remarks', None) if not BusinessCardModel.has_card( target_open_id) or not BusinessCardModel.has_card(source_open_id): return abort(404) r = ApplyFriendsModel.apply_friend(source_open_id, target_open_id, remarks) response = make_response(json.dumps({"r": r})) response.headers['Content-Type'] = 'application/json' return response
def people(open_id): """ 联系人列表 :param open_id: 用户openid :return: """ if not BusinessCardModel.has_card(open_id): return abort(404) return send_file('templates/v1/people.html', cache_timeout=0)
def generated_name_card(): """ 生成名片接口 @author: yubang :return: str """ open_id = g.open_id name = request.form.get('name', None) company = request.form.get('company', None) industry = request.form.get('industry', None) resources_key = request.form.get('resources_key', None) avatar = request.form.get('avatar', None) role = request.form.get('role', None) invitation_code = request.form.get('invitation_code', None) # 省市 province = request.form.get('province', None) city = request.form.get('city', None) area = request.form.get('area', None) # 检测邀请码 if not BusinessCardModel.check_invitation_code(invitation_code): response = make_response(json.dumps({"code": -5, "msg": u'邀请码不正确!'})) response.headers['Content-Type'] = 'application/json' return response r = BusinessCardModel.make_a_card(open_id, name, company, industry, resources_key, avatar, role, invitation_code, province, city, area) result = dict() if r: result['code'] = 0 result['msg'] = 'ok' else: result['code'] = 0 result['msg'] = u'请勿重复生成名片!' # 处理加入群情况 if 'group_token' in session: try: GroupRecordModel.join_group(g.open_id, session['group_token']) except: pass # 特殊处理扫描海报二维码后生成名片的情况 from_open_id = request.form.get('from_open_id', None) if from_open_id and BusinessCardModel.has_card(from_open_id): ApplyFriendsModel.apply_friend(g.open_id, from_open_id, u'通过扫描您的海报申请资源对接') response = make_response(json.dumps(result)) response.headers['Content-Type'] = 'application/json' return response
def build_card(): """ 生成名片页面 :return: """ if BusinessCardModel.has_card(g.open_id): return redirect(url_for('web_app.update_card')) g.target_url = '/api/v1/generatedNameCard' return send_file('templates/v1/build_card.html', cache_timeout=0)
def update_card(): """ 修改名片页面 :return: """ if not BusinessCardModel.has_card(g.open_id): return redirect(url_for('web_app.build_card')) card = BusinessCardModel.get_name_card(g.open_id) g.target_url = '/api/v1/updateNameCard' return send_file('templates/v1/build_card.html', cache_timeout=0)
def send_message(cls, from_open_id, to_open_id, message, message_type): """ 发送一条消息 :param from_open_id: 发送方openid :param to_open_id: 接收方openid :param message: 消息内容 :param message_type: 消息类型,0为文字 :return: """ # 判断目标用户是否存在 if not BusinessCardModel.has_card(to_open_id): return 0 sql = "insert into " + cls.table_name + "(from_open_id, to_open_id, message_content, message_type, status, " \ "create_time) values(%s, %s, %s, %s, 0, now());" r = cls.db_lib.insert(sql, [from_open_id, to_open_id, message, message_type]) return r
def to_wechat_link(group_token): """ 微信群的链接 :return: """ if not GroupLinksModel.has_link(group_token): return abort(404) session['group_token'] = group_token if BusinessCardModel.has_card(g.open_id): # 判断是否要添加入群 if not GroupRecordModel.has_join_group(g.open_id, group_token): GroupRecordModel.join_group(g.open_id, group_token) log.info(u'%s执行了记录加入群的数据库操作' % g.open_id) log.info(u'%s点击了群链接' % g.open_id) return redirect(url_for('web_app.all_cards', group_token=group_token)) else: return redirect(url_for('web_app.build_card') + "?group_token="+group_token)
def add_friend(cls, source_open_id, target_open_id): """ 添加好友 :param source_open_id: 用户1openid :param target_open_id: 用户2openid :return: boolean """ if cls.is_friend(source_open_id, target_open_id): return False if not BusinessCardModel.has_card(target_open_id): return False if source_open_id == target_open_id: return False sql = "insert into " + cls.get_table_name() + "(source_open_id, target_open_id, status, create_time) " \ "values(%s, %s, 0, now());" db_lib = cls.get_db_lib() db_lib.insert(sql, [source_open_id, target_open_id]) db_lib.insert(sql, [target_open_id, source_open_id]) return True
def poster(open_id): """ 用户海报页面 (注意,该页面不需要登录校验~~~~) :return: """ if not BusinessCardModel.has_card(open_id): return abort(404) card = BusinessCardModel.get_name_card(open_id) if card['redundancy_labels']: card['keys'] = card['redundancy_labels'].split('#') # 只取前5个 if len(card['keys']) > 5: card['keys'] = card['keys'][:5] else: card['keys'] = [] # 生成二维码地址 url = url_for('web_app.join_card_by_user_code', _external=True)+ "?from_open_id=" + card['open_id'] + "&invitation_code=" + card['invitation_code'] card['url'] = urllib.quote(url) return render_template('v1/poster.html', card=card)
def handle(*k, **v): if not BusinessCardModel.has_card(g.open_id): return redirect(url_for('web_app.build_card')) return fn(*k, **v)