Example #1
0
 def __init__(self, db):
     self.image_article_manager = ImageArticleManager(db)
     self.fans_manager = FansManager(db)
     self.auto_manager = AutoManager(db)
     self.message_manager = MessageManager(db)
     self.card_manager = CardManager(db)
     self.auto_keyword_manager = AutoKeywordManager(db)
Example #2
0
class ShareHandler(BaseHandler):
    analytics_manager = None
    fans_manager = None
    card_manager = None

    def prepare(self):
        self.analytics_manager = AnalyticsManager(self.db)
        self.fans_manager = FansManager(self.db)
        self.card_manager = CardManager(self.db)

    def get(self):
        openid = self.get_argument('openid', None)
        slug = self.get_argument('slug', None)
        aid = self.get_argument('aid', None)
        msg = self.get_argument('msg', None)
        success = 1
        _type = '好友'
        print '%s - %s - %s - %s' % (openid, slug, aid, msg)
        if msg and 'cancel' in msg:
            success = 0
        if msg and 'timeline' in msg:
            _type = '朋友圈'
        self.analytics_manager.save_share_history(openid, slug, success, _type, aid)
        if openid:
            fans = self.fans_manager.get_fans_by_openid(openid)
            if not fans:
                return
            aid = fans.aid
            rule = self.card_manager.get_account_card_rule(aid)
            if not rule or not rule.share:
                return
            card = self.card_manager.get_card_by_aid(aid)
            if not card:
                return
            member = self.card_manager.get_user_card_info(card.cid, openid)
            if not member:
                return
            self.card_manager.new_history(aid, openid, '分享文章', rule.share, member.num)
        self.write('1')
Example #3
0
 def prepare(self):
     self.fans_manager = FansManager(self.db)
     self.card_manager = CardManager(self.db)
     self.account_manager = AccountManager(self.db)
Example #4
0
class CardHandler(BaseHandler):
    """会员卡服务"""

    fans_manager = None
    card_manager = None
    account_manager = None

    def prepare(self):
        self.fans_manager = FansManager(self.db)
        self.card_manager = CardManager(self.db)
        self.account_manager = AccountManager(self.db)

    def get(self, cid):
        """
        用户访问会员卡页面
        1. 如果已经有会员卡,返回信息
        2. 如果没有
            1. 如果会员卡允许自动创建,则自动创建
            2. 如果不允许自动创建,按照要求填写表单
        """
        card = self.card_manager.get_card_by_cid(cid)
        openid = self.get_argument('i', None)
        if not openid or not card:
            raise HTTPError(404)
        fans = self.fans_manager.get_fans_by_openid(openid)
        if not fans:
            pass
        account = self.account_manager.get_account_by_aid(card.aid)
        card_member = self.card_manager.get_user_card_info(cid, openid)
        rule = self.card_manager.get_account_card_rule(card.aid)
        if card_member:
            historys = self.card_manager.get_history_by_openid(card.aid, openid)
            self.render('front/card.html', member=card_member, card=card, account=account, rule=rule, historys=historys)
            return
        if card.register == 1:
            # 自动注册
            card_id = card.id
            num = key_util.generate_digits_starts_with(str(card_id), 7)
            num = str(card_id) + num
            self.card_manager.save_member(card.aid, cid, num, openid, '', '', '')
            card_member = self.card_manager.get_user_card_info(cid, openid)
            self.render('front/card.html', member=card_member, account=account, card=card)
            return
        else:
            # 返回注册卡片页面
            m = self.get_argument('m', None)
            self.render('front/card_reg.html', card=card, openid=openid, account=account, m=m)

    def post(self, *args, **kwargs):
        """提交领取会员卡的表单"""
        result = {'r': 0}
        openid = self.get_argument('openid', None)
        mobile = self.get_argument('mobile', None)
        address = self.get_argument('address', None)
        sex = self.get_argument('sex', None)
        birthday = self.get_argument('birthday', None)
        name = self.get_argument('name', '')
        if not openid:
            result['error'] = '出了点错误,重新注册吧~'
            self.write(result)
            return
        card = self.card_manager.get_card_by_cid(args[0])
        if not card:
            result['error'] = '你来到了一个不存在的页面~'
            self.write(result)
            return
        if card.mobile and not mobile:
            result['error'] = '手机号一定要填哦~'
            self.write(result)
            return
        if card.address and not address:
            result['error'] = '地址一定要填哦~'
            self.write(result)
            return
        if card.sex and not sex:
            result['error'] = '性别一定要选哦~'
            self.write(result)
            return
        if card.birthday and not birthday:
            result['error'] = '生日一定要填哦~'
            self.write(result)
            return
        card_id = card.id
        num = key_util.generate_digits_starts_with(str(card_id), 7)
        num = str(card_id) + num
        self.card_manager.save_member(card.aid, card.cid, num, openid, name, mobile, address, sex, birthday)
        result['r'] = 1
        result['openid'] = openid
        self.write(result)
        return
Example #5
0
 def prepare(self):
     self.analytics_manager = AnalyticsManager(self.db)
     self.fans_manager = FansManager(self.db)
     self.card_manager = CardManager(self.db)
Example #6
0
class CardPointBase(BaseHandler):

    account_manager = None
    card_manager = None
    fans_manager = None
    analytics_manager = None

    TYPE_FOLLOW = '起始积分'
    TYPE_TIME = '满30天累计积分'
    TYPE_SHARE = '分享文章'

    def prepare(self):
        self.account_manager = AccountManager(self.db)
        self.card_manager = CardManager(self.db)
        self.fans_manager = FansManager(self.db)
        self.analytics_manager = AnalyticsManager(self.db)

    def update_account_member_score(self, aid):
        rule = self.card_manager.get_account_card_rule(aid)
        if not rule:
            return
        follow = rule.follow
        times = rule.time
        share = rule.share
        card = self.card_manager.get_card_by_aid(aid)
        if not card:
            return
        members = self.card_manager.list_card_member(aid, card.cid, 0, 0, 10000)
        now = int(time.time())
        for m in members:
            try:
                f = self.fans_manager.get_fans_by_openid(m.openid)
                openid = f.openid
                subscribe_time = f.subscribe_time
                s_time = int(time.mktime(subscribe_time.timetuple()))
                month = int((now - s_time) / (60 * 60 * 24 * 30))
                time_point = times * month
                history = self.card_manager.get_history_by_type(str(f.openid), str(aid), self.TYPE_TIME)
                if not history:
                    self.card_manager.new_history(aid, f.openid, self.TYPE_TIME, time_point, m.num)
                else:
                    self.card_manager.update_history_by_type(f.openid, time_point, self.TYPE_TIME)
                if not self.card_manager.get_history_by_type(f.openid, aid, self.TYPE_FOLLOW):
                    self.card_manager.new_history(aid, f.openid, self.TYPE_FOLLOW, follow, m.num)
                else:
                    self.card_manager.update_history_by_type(f.openid, follow, self.TYPE_FOLLOW)
                share_count = self.analytics_manager.count_share_by_openid(aid, m.openid)
                print share_count
                history = self.card_manager.get_history_by_type(f.openid, aid, self.TYPE_SHARE)
                if share_count and not history:
                    self.card_manager.new_history(aid, openid, self.TYPE_SHARE, int(share_count) * share, m.num)
                if share_count and history:
                    self.card_manager.update_history_by_type(openid, int(share_count) * share, self.TYPE_SHARE)
            except Exception:
                continue
Example #7
0
class Message():
    image_article_manager = None
    fans_manager = None
    auto_manager = None
    message_manager = None
    card_manager = None
    auto_keyword_manager = None

    def __init__(self, db):
        self.image_article_manager = ImageArticleManager(db)
        self.fans_manager = FansManager(db)
        self.auto_manager = AutoManager(db)
        self.message_manager = MessageManager(db)
        self.card_manager = CardManager(db)
        self.auto_keyword_manager = AutoKeywordManager(db)

    def process_message(self, account, message, path):
        """根据消息类型,处理消息"""
        openid = message['FromUserName']
        fans = self.fans_manager.get_fans_by_openid_aid(openid, account.aid)
        if not fans:
            wei_api.get_user_info(account, openid, self._add_single_fan)
        msg_type = message['MsgType'].lower()
        if msg_type == 'text':
            return self._process_text_message(account, message, path)
        if msg_type == 'image':
            return self._process_image_message(account, message, path)
        if msg_type == 'voice':
            return self._process_voice_message(account.aid, message)
        if msg_type == 'video':
            return self._process_video_message(account.aid, message)
        if msg_type == 'location':
            return self._process_location_message(account.aid, message)
        if msg_type == 'link':
            return self._process_link_message(account.aid, message)
        if msg_type == 'event':
            event = message['Event'].lower()
            if event == 'subscribe':
                return self._process_subscribe_event(account, message, path)
            if event == 'unsubscribe':
                return self._process_unsubscribe_event(account, message)
            if event == 'click':
                return self._process_menu_click_event(account, message, path)

    def _add_single_fan(self, user, aid, openid):
        """将关注的用户保存到数据库"""
        if self.fans_manager.get_fans_by_id(openid):
            # 如果用户已经取消关注后再关注
            self.fans_manager.re_subscribe_fans(openid, aid)
        else:
            if not user:
                self.fans_manager.save_single_fans_without_info(aid, openid)
            else:
                self.fans_manager.save_single_fans(user, aid)

    def _process_text_message(self, account, message, path):
        """接受用户发送的文本消息
           1. 保存到数据库
           2. 匹配有没有定义的回复,有则回复
        """
        self.message_manager.receive_text_message(message, account.aid)
        content = message['Content']
        auto = self.auto_keyword_manager.get_auto_by_word(account.aid, content)
        if not auto:
            _list = self.auto_keyword_manager.list_auto_by_wild(account.aid)
            for _l in _list:
                if _l.word in content:
                    auto = _l
        if auto:
            if auto.re_type == 'text':
                return message_util.text_response_to_message(auto.re_content, message, path, account.wei_account)
            elif auto.re_type == 'single':
                image_article = self.image_article_manager.get_image_article_by_id(auto.re_img_art_id)
                return message_util.image_article_group_to_message([image_article], message, path, account.wei_account)
            elif auto.re_type == 'multi':
                image_article_group = self.image_article_manager.get_multi_image_article_by_id(auto.re_img_art_id)
                if not image_article_group:
                    return None
                id_list = [image_article_group.id1, image_article_group.id2, image_article_group.id3,
                           image_article_group.id4,
                           image_article_group.id5]
                id_list = filter(lambda a: a != 0, id_list)
                article_list = []
                for _id in id_list:
                    article_list.append(self.image_article_manager.get_image_article_by_id(_id))
                print article_list
                return message_util.image_article_group_to_message(article_list, message, path, account.wei_account)

    def _process_image_message(self, account, message, path):
        """接收用户发送的图片消息"""
        aid = account.aid
        openid = message['FromUserName']
        self.message_manager.receive_image_message(message, aid)
        auto = self.auto_manager.get_image_auto(aid)
        if auto:
            # 回复验证码
            code = key_util.generate_digits(8)
            if self.auto_manager.has_code(aid, openid):
                content = auto.re_content.split('&&')[2]
            elif self.auto_manager.count_code_response(aid) >= auto.num:
                content = auto.re_content.split('&&')[1]
            else:
                content = auto.re_content.split('&&')[0].replace('#code#', code)
                self.auto_manager.save_code_response(openid, aid, code)
            return message_util.text_response_to_message(content, message, path, account.wei_account)

    def _process_voice_message(self, aid, message):
        """接收用户发送的语音消息"""
        self.message_manager.receive_voice_message(message, aid)

    def _process_video_message(self, aid, message):
        """接收用户发送的视频消息"""
        self.message_manager.receive_video_message(message, aid)

    def _process_location_message(self, aid, message):
        """接收用户发送的位置消息"""
        self.message_manager.receive_location_message(message, aid)

    def _process_link_message(self, aid, message):
        """接收用户发送的链接消息"""
        self.message_manager.receive_link_message(message, aid)

    def _process_subscribe_event(self, account, message, path):
        """
        处理用户关注账号的事件
            1. 将用户信息保存到数据库
            2. 如果有自动回复消息,则回复
        """
        openid = message['FromUserName']
        wei_api.get_user_info(account, openid, self._add_single_fan)
        auto = self.auto_manager.get_follow_auto(account.aid)
        if not auto:
            return
        if auto.type == 'text':
            return message_util.text_response_to_message(auto.re_content, message, path, account.wei_account)
        if auto.type == 'single':
            image_article = self.image_article_manager.get_image_article_by_id(auto.re_img_art_id)
            return message_util.image_article_group_to_message([image_article], message, path, account.wei_account)
        if auto.type == 'multi':
            image_article_group = self.image_article_manager.get_multi_image_article_by_id(auto.re_img_art_id)
            if not image_article_group:
                return None
            id_list = [image_article_group.id1, image_article_group.id2, image_article_group.id3,
                       image_article_group.id4,
                       image_article_group.id5]
            id_list = filter(lambda a: a != 0, id_list)
            article_list = []
            for _id in id_list:
                article_list.append(self.image_article_manager.get_image_article_by_id(_id))
            return message_util.image_article_group_to_message(article_list, message, path, account.wei_account)

    def _process_unsubscribe_event(self, account, message):
        """
        处理用户取消关注账号的事件
        """
        openid = message['FromUserName']
        self.fans_manager.unsubscribe_fans(openid, account.aid)

    def _process_menu_click_event(self, account, message, path):
        """
        处理用户点击自定义菜单的事件
        """
        key = message['EventKey']
        print '_process_menu_click_event : key ------ %s' % key
        auto = self.auto_manager.get_auto_by_key(key)
        if not auto:
            return None
        if auto.type == 'text':
            return message_util.text_response_to_message(auto.re_content, message, path, account.wei_account)
        if auto.type == 'single':
            image_article = self.image_article_manager.get_image_article_by_id(auto.re_img_art_id)
            return message_util.image_article_group_to_message([image_article], message, path, account.wei_account)
        if auto.type == 'card':
            card = self.card_manager.get_card_by_aid(account.aid)
            member = self.card_manager.get_user_card_info(card.cid, message['FromUserName'])
            return message_util.card_response_to_message(card, member, message, path, account.wei_account)
        if auto.type == 'multi':
            image_article_group = self.image_article_manager.get_multi_image_article_by_id(auto.re_img_art_id)
            if not image_article_group:
                return None
            id_list = [image_article_group.id1, image_article_group.id2, image_article_group.id3,
                       image_article_group.id4,
                       image_article_group.id5]
            id_list = filter(lambda a: a != 0, id_list)
            print id_list
            article_list = self.image_article_manager.get_image_article_by_id_list(id_list)
            print 'article list ---------- %s ' % article_list
            return message_util.image_article_group_to_message(article_list, message, path, account.wei_account)