示例#1
0
 def post(self):
     sp_id = self.current_user.supplier_id
     cate_id = self.get_argument('cate_id', '')
     # 查出默认的分组ID
     default_cate_id = self.db.get('select id from member_category where sp_id=%s and name="未分组"', sp_id).id
     # 微信服务器分组放到0
     if self.current_user.sp_props.wx_type == 'service' and self.current_user.sp_props.wx_verified == '1':
         members = self.db.query('select wx_id from member where sp_id=%s and category_id=%s', sp_id, cate_id)
         app_id = self.current_user.sp_props.app_id
         app_secret = self.current_user.sp_props.app_secret
         for mem in members:
             body = {"openid": mem.wx_id, "to_groupid": 0}
             wx = Weixin(db=self.db, sp_id=sp_id, method='groups/members/update', body=json_dumps(body))
             wx.set_app_info(app_id, app_secret)
             res = yield wx.fetch()
             wx.parse_response(res.body)
             if not wx.is_ok():
                 logging.error('wx error: %s' % wx.error)
     # 把要删除组的会员合并入默认分组
     self.db.execute('update member set category_id=%s where sp_id=%s and category_id=%s',
                     default_cate_id, sp_id, cate_id)
     # 删除分组
     self.db.execute('update member_category set deleted=1 where id=%s', cate_id)
     # 返回
     self.set_header('Content-Type', 'application/json; charset=UTF-8')
     self.write(json_dumps({'ok': True}))
示例#2
0
 def post(self):
     self.set_header('Content-Type', 'application/json; charset=UTF-8')
     sp_id = self.current_user.supplier_id
     cate_name = self.get_argument('name', '').encode('utf-8')
     # 检查是否重复
     exist = self.db.query('select * from member_category where sp_id=%s and name=%s and deleted=0', sp_id, cate_name)
     # 重复组名,返回
     if exist:
         self.write(json_dumps({'ok': False, 'error': '分组名称已经存在'}))
         return
     # 更新微信分组
     if self.current_user.sp_props.wx_type == 'service' and self.current_user.sp_props.wx_verified == '1':
         body = json_dumps({"group": {"name": cate_name}})
         wx = Weixin(db=self.db, sp_id=sp_id, method='groups/create', body=body)
         wx.set_app_info(self.current_user.sp_props.app_id, self.current_user.sp_props.app_secret)
         res = yield wx.fetch()
         wx.parse_response(res.body)
         if wx.is_ok():
             # 更新本地数据库
             last_id = self.db.execute('insert into member_category (sp_id, name, deleted, wx_grp_id) '
                                       'values (%s, %s, 0, %s)', sp_id, cate_name, wx.message.group.id)
             self.write(json_dumps({'ok': True, 'cate_id': last_id, 'cate_name': cate_name}))
         else:
             logging.error('wx error: %s' % wx.error)
             self.write(json_dumps({'ok': False, 'error': '添加失败,%s' % wx.error}))
     else:
         # 更新本地数据库
         last_id = self.db.execute('insert into member_category (sp_id, name, deleted) '
                                   'values (%s, %s, 0)', sp_id, cate_name)
         self.write(json_dumps({'ok': True, 'cate_id': last_id, 'cate_name': cate_name}))
示例#3
0
 def post(self):
     sp_id = self.current_user.supplier_id
     cate_id = self.get_argument('cate_id', '')  # 新cate id
     mem_id = self.get_argument('mem_id', '')
     try:
         map(int, mem_id.split(','))
     except:
         raise HTTPError(500)
     # 更新微信服务器
     if self.current_user.sp_props.wx_type == 'service' and self.current_user.sp_props.wx_verified == '1':
         members = self.db.query('select wx_id from member where sp_id=%s and id in (' + mem_id + ')', sp_id)
         wx_cate_id = self.db.get('select wx_grp_id from member_category where id=%s', cate_id).wx_grp_id
         app_id = self.current_user.sp_props.app_id
         app_secret = self.current_user.sp_props.app_secret
         for mem in members:
             body = {"openid": mem.wx_id, "to_groupid": int(wx_cate_id)}
             wx = Weixin(db=self.db, sp_id=sp_id, method='groups/members/update', body=json_dumps(body))
             wx.set_app_info(app_id, app_secret)
             res = yield wx.fetch()
             wx.parse_response(res.body)
             if not wx.is_ok():
                 logging.error('wx error: %s' % wx.error)
     # 更新本地信息
     if mem_id and cate_id:
         sql = 'update member set category_id=%s where sp_id=%s and id in (' + mem_id + ')'
         self.db.execute(sql, cate_id, sp_id)
示例#4
0
    def post(self):
        params = dict([(name, self.get_argument(name).encode('utf-8'))
                       for name in self.request.arguments
                       if name.startswith('s_menu_item')
                       or name.startswith('m_menu_title')])

        main_pos_list = []
        main_content_list = []
        menu_pos_list = []
        menu_content_list = []
        for name, content in params.iteritems():
            # 只有那些有值的父菜单才能有机会插进数据库
            # 循环找到正确的顺序位置,数据库能准确按顺序插入
            if name.startswith('m_menu_title') and content.split('--')[0]:
                name_list = name.split('--')
                main_temp = (name_list[1], content)
                if not main_pos_list:
                    main_pos_list.append(name_list[1])
                    main_content_list.append(main_temp)
                else:
                    is_insert = False
                    for index, number in enumerate(main_pos_list):
                        if name_list[1] < number:
                            main_pos_list.insert(index, name_list[1])
                            main_content_list.insert(index, main_temp)
                            is_insert = True
                            break
                    if not is_insert:
                        main_pos_list.append(name_list[1])
                        main_content_list.append(main_temp)

            elif name.startswith('s_menu_item'):
                # 如果二级菜单名为空,则取消增加该行
                is_name = content.split('--')[0]
                if not is_name.strip():
                    continue

                menu_list = name.split('--')
                position = int(menu_list[1]) * 10 + int(menu_list[2])
                content_temp = (menu_list[1], content)
                if not menu_pos_list:
                    menu_pos_list.append(position)
                    menu_content_list.append(content_temp)
                else:
                    is_insert = False
                    for index, number in enumerate(menu_pos_list):
                        if position < number:
                            menu_pos_list.insert(index, position)
                            menu_content_list.insert(index, content_temp)
                            is_insert = True
                            break
                    if not is_insert:
                        menu_pos_list.append(position)
                        menu_content_list.append(content_temp)
            else:
                pass

        # 以下代码用做后端检验数据的正确性
        # is_true = False
        # for item in menu_content_list:
        #
        #     pass
        # if not is_true:
        #     self.redirect(self.reverse_url('menu'))

        sp_id = self.current_user.supplier_id
        self.db.execute(
            'delete from wx_menu where sp_id = %s and parent_id is not null',
            sp_id)
        self.db.execute('delete from wx_menu where sp_id = %s ', sp_id)

        menu_data = {'button': []}
        for main_item in main_content_list:
            print main_content_list
            m_list = main_item[1].split('--')
            if len(m_list) == 3:
                if m_list[1] == '5':
                    md5_content = hashlib.md5(m_list[2]).hexdigest()
                    m_list[2] = md5_content[:12] + m_list[2]
                self.db.execute(
                    'insert into wx_menu(sp_id, name, action_type, action) values(%s, %s, %s, %s)',
                    sp_id, m_list[0], m_list[1], m_list[2])

                if m_list[1] == '2':
                    # view
                    m_button = {
                        'type': 'view',
                        'name': m_list[0],
                        'url': m_list[2]
                    }
                elif m_list[1] == '5':
                    m_button = {
                        'type':
                        'click',
                        'name':
                        m_list[0],
                        'key':
                        action_type_wx_menu[int(m_list[1])] + ':' +
                        md5_content[:12]
                    }
                else:
                    # click
                    m_button = {
                        'type':
                        'click',
                        'name':
                        m_list[0],
                        'key':
                        action_type_wx_menu[int(m_list[1])] + ':' + m_list[2]
                    }

                menu_data['button'].append(m_button)

            elif len(m_list) == 1:
                pid = self.db.execute(
                    'insert into wx_menu(sp_id, name) values(%s, %s)', sp_id,
                    m_list[0])
                sub_buttons = []
                menu_data['button'].append({
                    'name': m_list[0],
                    'sub_button': sub_buttons
                })
                for menu_item in menu_content_list:
                    if menu_item[0] == main_item[0]:
                        me_list = menu_item[1].split('--')
                        if me_list[1] == '5':
                            md5_content = hashlib.md5(me_list[2]).hexdigest()
                            me_list[2] = md5_content[:12] + me_list[2]

                        self.db.execute(
                            'insert into wx_menu(sp_id, name, action_type, action, parent_id) '
                            'values(%s, %s, %s, %s, %s)', sp_id, me_list[0],
                            me_list[1], me_list[2], pid)

                        if me_list[1] == '5':
                            sub_button = {
                                'type':
                                'click',
                                'name':
                                me_list[0],
                                'key':
                                action_type_wx_menu[int(me_list[1])] + ':' +
                                md5_content[:12]
                            }
                        elif me_list[1] != '2':
                            # click
                            sub_button = {
                                'type':
                                'click',
                                'name':
                                me_list[0],
                                'key':
                                action_type_wx_menu[int(me_list[1])] + ':' +
                                me_list[2]
                            }
                        else:
                            # view
                            sub_button = {
                                'type': 'view',
                                'name': me_list[0],
                                'url': me_list[2]
                            }

                        sub_buttons.append(sub_button)
            else:
                pass

        # 如果一级菜单不是直接点击,且它没有对应的二级菜单,则删除
        main_menu = self.db.query(
            'select id from wx_menu where sp_id = %s and parent_id is null '
            'and (action_type = " " or action_type is null)', sp_id)
        for item in main_menu:
            if not self.db.query(
                    'select id from wx_menu where sp_id = %s and parent_id = %s',
                    sp_id, item.id):
                self.db.execute(
                    'delete from wx_menu where id = %s and sp_id = %s',
                    item.id, sp_id)

        #如果类型为图文信息或者图文信息组的时候,且图文信息没有值就删除
        check_type = [3, 4, 7]
        self.db.execute(
            'delete from wx_menu where sp_id = %%s and action_type in (%s) and action = "null"'
            % ','.join(['%s'] * len(check_type)), sp_id, *check_type)
        wx = Weixin(db=self.db,
                    sp_id=sp_id,
                    method="menu/create",
                    body=json_dumps(menu_data))
        app_id = self.current_user.sp_props.app_id
        app_secret = self.current_user.sp_props.app_secret
        wx.set_app_info(app_id, app_secret)
        result = yield wx()
        wx.parse_response(result.body)

        logging.info(json_dumps(menu_data))
        if wx.is_ok():
            logging.info('push menu to weixin sucessfully: %s', wx.message)
        else:
            logging.error('push menu to weixin fail: %s', wx.message)
        self.redirect(self.reverse_url('weixin.menu.show'))
示例#5
0
    def event(self, sp_id, msg, mem, sp_props):
        msg_time = datetime.fromtimestamp(int(msg.findtext('./CreateTime')))
        event = msg.findtext('./Event').lower()
        wx_id = mem.wx_id
        # 取消订阅,更新数据库
        if event == 'unsubscribe':
            logging.info('user %s un-sub', wx_id)
            self.db.execute('update member set wx_unfollow_at=%s where id=%s',
                            msg_time, mem.id)  # 更新取消时间
            self.write('')  # 退订,什么都不返回
            return
        if event == 'subscribe':
            logging.info('user %s subscribe', wx_id)
            # 查是否存在用户
            if mem:
                # 如果是认证的服务号,去抓取一下用户信息
                if sp_props.wx_type == 'service' and sp_props.wx_verified == '1':
                    app_id = sp_props.app_id
                    app_secret = sp_props.app_secret
                    info = Weixin(method='user/info',
                                  db=self.db,
                                  sp_id=sp_id,
                                  body='')
                    info.set_app_info(app_id, app_secret)
                    response = info.sync_fetch(openid=wx_id, lang='zh_CN')
                    info.parse_response(response)
                    if info.is_ok():
                        nickname = info.message.nickname
                        # 暂时数据库未升级至可以存储emoji字符,故使用以下代码,升级数据库后可删除
                        import re
                        try:
                            # UCS-4
                            highpoints = re.compile(u'[\U00010000-\U0010ffff]')
                        except re.error:
                            # UCS-2
                            highpoints = re.compile(
                                u'[\uD800-\uDBFF][\uDC00-\uDFFF]')
                        nickname = highpoints.sub(u'', nickname)
                        sex = {0: '未知', 1: '男', 2: '女'}.get(info.message.sex)
                        follow_at = datetime.fromtimestamp(
                            info.message.subscribe_time)
                        head_img = info.message.headimgurl
                        self.db.execute(
                            'update member set wx_name = %s, gender = %s, last_active=NOW(), max_msg_id=0,'
                            'head_img = %s, wx_follow_at = %s, country = %s, province = %s, city = %s '
                            'where wx_id = %s and sp_id = %s', nickname, sex,
                            head_img, follow_at, info.message.country,
                            info.message.province, info.message.city, wx_id,
                            sp_id)
                        logging.info('user %s fetch info successfully', wx_id)
                    else:
                        self.db.execute(
                            'update member set wx_follow_at=%s, last_active=NOW(), '
                            'max_msg_id=0 where id=%s', msg_time, mem.id)
                        logging.info('user %s fetch info failed', wx_id)
                else:
                    self.db.execute(
                        'update member set wx_follow_at=%s, last_active=NOW(), '
                        'max_msg_id=0 where id=%s', msg_time, mem.id)

            # 推送新关注消息
            app_msg = self.db.get(
                'select wam.* '
                'from supplier_property sp join wx_app_msg wam on sp.value = wam.id '
                'where sp.sp_id = %s and name="wx_sub_msg" ', mem.sp_id)
            if app_msg:
                response = Template(wx_response['news']).generate(
                    to_user=mem.wx_id,
                    from_user=sp_props.wx_id,
                    time=int(time.time()),
                    items=[
                        PropDict(
                            title=app_msg.title,
                            description=app_msg.summary,
                            picurl=img_url(app_msg.cover),
                            url='http://%s.quanfx.com/app_msg/%s?wx_id=%s' %
                            (sp_id, app_msg.id, mem.wx_id))
                    ],
                )
                self.write(response)
                return
            else:
                self.write('')
                return
        if event == 'click':
            event_key = msg.findtext('./EventKey')
            action_type, action = event_key.split(':', 1)
            if action_type == 'website':
                cover = self.db.get(
                    'select * from supplier_property where sp_id=%s and name="wx_site_cover"',
                    sp_id)
                cover = json.loads(cover.value, object_hook=json_hook)
                response = Template(wx_response['news']).generate(
                    to_user=mem.wx_id,
                    from_user=sp_props.wx_id,
                    time=int(time.time()),
                    items=[
                        PropDict(title=cover.title,
                                 description=cover.desc,
                                 picurl=img_url(cover.pic),
                                 url='http://%s.quanfx.com/?wx_id=%s' %
                                 (sp_id, mem.wx_id))
                    ],
                )
                self.write(response)
                return
            elif action_type == 'app_msg':
                app_msg = self.db.get(
                    'select * from wx_app_msg where id=%s and deleted=0',
                    action)
                if app_msg:
                    response = Template(wx_response['news']).generate(
                        to_user=mem.wx_id,
                        from_user=sp_props.wx_id,
                        time=int(time.time()),
                        items=[
                            PropDict(
                                title=app_msg.title,
                                description=app_msg.summary,
                                picurl=img_url(app_msg.cover),
                                url='http://%s.quanfx.com/app_msg/%s?wx_id=%s'
                                % (sp_id, app_msg.id, mem.wx_id))
                        ],
                    )
                    self.write(response)
                    return
                else:
                    self.write('')
                    return
            elif action_type == 'app_msg_group':
                app_msg_group = self.db.get(
                    'select * from wx_app_msg_gp where deleted=0 and id=%s',
                    action)
                if app_msg_group:
                    app_msgs = self.db.query(
                        'select * from wx_app_msg where deleted=0 and id in ('
                        + app_msg_group.app_msgs + ')')
                    response = Template(wx_response['news']).generate(
                        to_user=mem.wx_id,
                        from_user=sp_props.wx_id,
                        time=int(time.time()),
                        items=[
                            PropDict(
                                title=app_msg.title,
                                description=app_msg.summary,
                                picurl=img_url(app_msg.cover),
                                url='http://%s.quanfx.com/app_msg/%s?wx_id=%s'
                                % (sp_id, app_msg.id, mem.wx_id))
                            for app_msg in app_msgs
                        ],
                    )
                    self.write(response)
                    return
                self.write('')
                return
            elif action_type == 'text':
                sms = self.db.get(
                    'select * from wx_menu where action like %s and sp_id = %s limit 1',
                    '%' + action + '%', sp_id)
                if sms:
                    response = Template(wx_response['text']).generate(
                        to_user=mem.wx_id,
                        from_user=sp_props.wx_id,
                        time=int(time.time()),
                        content=sms.action[12:])
                    self.write(response)
                    return
                self.write('')
                return
            elif action_type == 'member':
                # 取会员卡封面
                cover = self.db.get(
                    'select * from supplier_property where sp_id=%s and name="wx_mem_cover"',
                    sp_id)
                if not cover:
                    # 没有会员卡封面,取官网封面
                    cover = self.db.get(
                        'select * from supplier_property where sp_id=%s and name="wx_site_cover"',
                        sp_id)
                cover = json.loads(cover.value, object_hook=json_hook)
                response = Template(wx_response['news']).generate(
                    to_user=mem.wx_id,
                    from_user=sp_props.wx_id,
                    time=int(time.time()),
                    items=[
                        PropDict(title=cover.title,
                                 description=cover.desc,
                                 picurl=img_url(cover.pic),
                                 url='http://%s.quanfx.com/member?wx_id=%s' %
                                 (sp_id, mem.wx_id))
                    ],
                )
                self.write(response)
                return
            elif action_type == 'book':
                cover = self.db.get(
                    'select * from wx_booking_setting where sp_id=%s and id = %s',
                    sp_id, action)
                logging.info(cover)
                if cover:
                    info = json.loads(cover.info, object_hook=json_hook)
                    response = Template(wx_response['news']).generate(
                        to_user=mem.wx_id,
                        from_user=sp_props.wx_id,
                        time=int(time.time()),
                        items=[
                            PropDict(
                                title=info.title,
                                description=info.desc,
                                picurl=img_url(info.pic),
                                url='http://%s.quanfx.com/book/%s?wx_id=%s' %
                                (sp_id, cover.id, mem.wx_id))
                        ],
                    )
                    self.write(response)
                    return
                else:
                    self.write('')
                    return
            elif action_type == 'mall':
                cover = self.db.get(
                    'select * from supplier_property where sp_id=%s and name="wx_mall_cover"',
                    sp_id)
                if not cover:
                    # 没有商城封面,取官网封面
                    cover = self.db.get(
                        'select * from supplier_property where sp_id=%s and name="wx_site_cover"',
                        sp_id)
                cover = json.loads(cover.value, object_hook=json_hook)
                response = Template(wx_response['news']).generate(
                    to_user=mem.wx_id,
                    from_user=sp_props.wx_id,
                    time=int(time.time()),
                    items=[
                        PropDict(
                            title=cover.title,
                            description=cover.desc,
                            picurl=img_url(cover.pic),
                            url='http://%s.quanfx.com/mall/goods?wx_id=%s' %
                            (sp_id, mem.wx_id))
                    ],
                )
                self.write(response)
                return
            elif action_type == 'activity':
                cover = self.db.get(
                    'select * from supplier_property where sp_id = %s and name="wx_activity_cover"',
                    sp_id)
                if not cover:
                    # 没有商城封面,取官网封面
                    cover = self.db.get(
                        'select * from supplier_property where sp_id=%s and name="wx_site_cover"',
                        sp_id)
                cover = json.loads(cover.value, object_hook=json_hook)
                response = Template(wx_response['news']).generate(
                    to_user=mem.wx_id,
                    from_user=sp_props.wx_id,
                    time=int(time.time()),
                    items=[
                        PropDict(
                            title=cover.title,
                            description=cover.desc,
                            picurl=img_url(cover.pic),
                            url='http://%s.quanfx.com/activity/list?wx_id=%s' %
                            (sp_id, mem.wx_id))
                    ],
                )
                self.write(response)
                return
示例#6
0
    def event(self, sp_id, msg, mem, sp_props):
        msg_time = datetime.fromtimestamp(int(msg.findtext('./CreateTime')))
        event = msg.findtext('./Event').lower()
        wx_id = mem.wx_id
        # 取消订阅,更新数据库
        if event == 'unsubscribe':
            logging.info('user %s un-sub', wx_id)
            self.db.execute('update member set wx_unfollow_at=%s where id=%s', msg_time, mem.id)  # 更新取消时间
            self.write('')  # 退订,什么都不返回
            return
        if event == 'subscribe':
            logging.info('user %s subscribe', wx_id)
            # 查是否存在用户
            if mem:
                # 如果是认证的服务号,去抓取一下用户信息
                if sp_props.wx_type == 'service' and sp_props.wx_verified == '1':
                    app_id = sp_props.app_id
                    app_secret = sp_props.app_secret
                    info = Weixin(method='user/info', db=self.db, sp_id=sp_id, body='')
                    info.set_app_info(app_id, app_secret)
                    response = info.sync_fetch(openid=wx_id, lang='zh_CN')
                    info.parse_response(response)
                    if info.is_ok():
                        nickname = info.message.nickname
                        # 暂时数据库未升级至可以存储emoji字符,故使用以下代码,升级数据库后可删除
                        import re
                        try:
                            # UCS-4
                            highpoints = re.compile(u'[\U00010000-\U0010ffff]')
                        except re.error:
                            # UCS-2
                            highpoints = re.compile(u'[\uD800-\uDBFF][\uDC00-\uDFFF]')
                        nickname = highpoints.sub(u'', nickname)
                        sex = {0: '未知', 1: '男', 2: '女'}.get(info.message.sex)
                        follow_at = datetime.fromtimestamp(info.message.subscribe_time)
                        head_img = info.message.headimgurl
                        self.db.execute('update member set wx_name = %s, gender = %s, last_active=NOW(), max_msg_id=0,'
                                        'head_img = %s, wx_follow_at = %s, country = %s, province = %s, city = %s '
                                        'where wx_id = %s and sp_id = %s',
                                        nickname, sex, head_img, follow_at, info.message.country,
                                        info.message.province, info.message.city,
                                        wx_id, sp_id)
                        logging.info('user %s fetch info successfully', wx_id)
                    else:
                        self.db.execute('update member set wx_follow_at=%s, last_active=NOW(), '
                                        'max_msg_id=0 where id=%s', msg_time, mem.id)
                        logging.info('user %s fetch info failed', wx_id)
                else:
                    self.db.execute('update member set wx_follow_at=%s, last_active=NOW(), '
                                    'max_msg_id=0 where id=%s', msg_time, mem.id)

            # 推送新关注消息
            app_msg = self.db.get('select wam.* '
                                  'from supplier_property sp join wx_app_msg wam on sp.value = wam.id '
                                  'where sp.sp_id = %s and name="wx_sub_msg" ', mem.sp_id)
            if app_msg:
                response = Template(wx_response['news']).generate(
                    to_user=mem.wx_id,
                    from_user=sp_props.wx_id,
                    time=int(time.time()),
                    items=[PropDict(
                        title=app_msg.title,
                        description=app_msg.summary,
                        picurl=img_url(app_msg.cover),
                        url='http://%s.quanfx.com/app_msg/%s?wx_id=%s' % (sp_id, app_msg.id, mem.wx_id)
                    )],
                )
                self.write(response)
                return
            else:
                self.write('')
                return
        if event == 'click':
            event_key = msg.findtext('./EventKey')
            action_type, action = event_key.split(':', 1)
            if action_type == 'website':
                cover = self.db.get('select * from supplier_property where sp_id=%s and name="wx_site_cover"',  sp_id)
                cover = json.loads(cover.value, object_hook=json_hook)
                response = Template(wx_response['news']).generate(
                    to_user=mem.wx_id,
                    from_user=sp_props.wx_id,
                    time=int(time.time()),
                    items=[PropDict(
                        title=cover.title,
                        description=cover.desc,
                        picurl=img_url(cover.pic),
                        url='http://%s.quanfx.com/?wx_id=%s' % (sp_id, mem.wx_id)
                    )],
                )
                self.write(response)
                return
            elif action_type == 'app_msg':
                app_msg = self.db.get('select * from wx_app_msg where id=%s and deleted=0', action)
                if app_msg:
                    response = Template(wx_response['news']).generate(
                        to_user=mem.wx_id,
                        from_user=sp_props.wx_id,
                        time=int(time.time()),
                        items=[PropDict(
                            title=app_msg.title,
                            description=app_msg.summary,
                            picurl=img_url(app_msg.cover),
                            url='http://%s.quanfx.com/app_msg/%s?wx_id=%s' % (sp_id, app_msg.id, mem.wx_id)
                        )],
                    )
                    self.write(response)
                    return
                else:
                    self.write('')
                    return
            elif action_type == 'app_msg_group':
                app_msg_group = self.db.get('select * from wx_app_msg_gp where deleted=0 and id=%s', action)
                if app_msg_group:
                    app_msgs = self.db.query('select * from wx_app_msg where deleted=0 and id in ('
                                             + app_msg_group.app_msgs + ')')
                    response = Template(wx_response['news']).generate(
                        to_user=mem.wx_id,
                        from_user=sp_props.wx_id,
                        time=int(time.time()),
                        items=[PropDict(
                            title=app_msg.title,
                            description=app_msg.summary,
                            picurl=img_url(app_msg.cover),
                            url='http://%s.quanfx.com/app_msg/%s?wx_id=%s' % (sp_id, app_msg.id, mem.wx_id)
                        ) for app_msg in app_msgs],
                    )
                    self.write(response)
                    return
                self.write('')
                return
            elif action_type == 'text':
                sms = self.db.get('select * from wx_menu where action like %s and sp_id = %s limit 1', '%' + action + '%', sp_id)
                if sms:
                    response = Template(wx_response['text']).generate(
                        to_user=mem.wx_id,
                        from_user=sp_props.wx_id,
                        time=int(time.time()),
                        content=sms.action[12:]
                    )
                    self.write(response)
                    return
                self.write('')
                return
            elif action_type == 'member':
                # 取会员卡封面
                cover = self.db.get('select * from supplier_property where sp_id=%s and name="wx_mem_cover"',  sp_id)
                if not cover:
                    # 没有会员卡封面,取官网封面
                    cover = self.db.get('select * from supplier_property where sp_id=%s and name="wx_site_cover"',  sp_id)
                cover = json.loads(cover.value, object_hook=json_hook)
                response = Template(wx_response['news']).generate(
                    to_user=mem.wx_id,
                    from_user=sp_props.wx_id,
                    time=int(time.time()),
                    items=[PropDict(
                        title=cover.title,
                        description=cover.desc,
                        picurl=img_url(cover.pic),
                        url='http://%s.quanfx.com/member?wx_id=%s' % (sp_id, mem.wx_id)
                    )],
                )
                self.write(response)
                return
            elif action_type == 'book':
                cover = self.db.get('select * from wx_booking_setting where sp_id=%s and id = %s', sp_id, action)
                logging.info(cover)
                if cover:
                    info = json.loads(cover.info, object_hook=json_hook)
                    response = Template(wx_response['news']).generate(
                        to_user=mem.wx_id,
                        from_user=sp_props.wx_id,
                        time=int(time.time()),
                        items=[PropDict(
                            title=info.title,
                            description=info.desc,
                            picurl=img_url(info.pic),
                            url='http://%s.quanfx.com/book/%s?wx_id=%s' % (sp_id, cover.id, mem.wx_id)
                        )],
                    )
                    self.write(response)
                    return
                else:
                    self.write('')
                    return
            elif action_type == 'mall':
                cover = self.db.get('select * from supplier_property where sp_id=%s and name="wx_mall_cover"', sp_id)
                if not cover:
                    # 没有商城封面,取官网封面
                    cover = self.db.get('select * from supplier_property where sp_id=%s and name="wx_site_cover"', sp_id)
                cover = json.loads(cover.value, object_hook=json_hook)
                response = Template(wx_response['news']).generate(
                    to_user=mem.wx_id,
                    from_user=sp_props.wx_id,
                    time=int(time.time()),
                    items=[PropDict(
                        title=cover.title,
                        description=cover.desc,
                        picurl=img_url(cover.pic),
                        url='http://%s.quanfx.com/mall/goods?wx_id=%s' % (sp_id, mem.wx_id)
                    )],
                )
                self.write(response)
                return
            elif action_type == 'activity':
                cover = self.db.get('select * from supplier_property where sp_id = %s and name="wx_activity_cover"',
                                    sp_id)
                if not cover:
                    # 没有商城封面,取官网封面
                    cover = self.db.get('select * from supplier_property where sp_id=%s and name="wx_site_cover"', sp_id)
                cover = json.loads(cover.value, object_hook=json_hook)
                response = Template(wx_response['news']).generate(
                    to_user=mem.wx_id,
                    from_user=sp_props.wx_id,
                    time=int(time.time()),
                    items=[PropDict(
                        title=cover.title,
                        description=cover.desc,
                        picurl=img_url(cover.pic),
                        url='http://%s.quanfx.com/activity/list?wx_id=%s' % (sp_id, mem.wx_id)
                    )],
                )
                self.write(response)
                return
示例#7
0
def main_loop():
    redis = common.redis_client()
    db = common.db_client()
    args = {'db': db, 'body': ''}

    while common.running():
        sp_id = redis.brpop('q:wx:mem:update')[1]

        args['sp_id'] = sp_id
        app_id = db.get(
            'select * from supplier_property where name="app_id" and sp_id = %s',
            sp_id)['value']
        app_secret = db.get(
            'select * from supplier_property where name="app_secret" and sp_id = %s',
            sp_id)['value']

        wx = Weixin(method='user/get', **args)
        wx.set_app_info(app_id, app_secret)

        info = Weixin(method='user/info', **args)
        info.set_app_info(app_id, app_secret)

        next_openid = ''
        while True:
            result = yield wx(next_openid=next_openid)
            wx.parse_response(result.body)

            if wx.is_ok() and 'data' in wx.message.keys():
                count = wx.message.count
                total = wx.message.total
                customs = wx.message.data.openid
                for custom in customs:
                    response = yield info(openid=custom, lang='zh_CN')
                    info.parse_response(response.body)
                    if info.is_ok():
                        nickname = info.message.nickname

                        # 暂时数据库未升级至可以存储emoji字符,故使用以下代码,升级数据库后可删除
                        import re
                        try:
                            # UCS-4
                            highpoints = re.compile(u'[\U00010000-\U0010ffff]')
                        except re.error:
                            # UCS-2
                            highpoints = re.compile(
                                u'[\uD800-\uDBFF][\uDC00-\uDFFF]')
                        nickname = highpoints.sub(u'', nickname)
                        # todo =========================未升级前的替代方案===================

                        sex = {0: '未知', 1: '男', 2: '女'}.get(info.message.sex)
                        follow_at = datetime.fromtimestamp(
                            info.message.subscribe_time)
                        openid = info.message.openid
                        head_img = info.message.headimgurl

                        mid = db.query(
                            'select * from member where wx_id = %s and sp_id = %s',
                            custom, sp_id)

                        if mid:
                            db.execute(
                                'update member set wx_name = %s, gender = %s,'
                                'head_img = %s, wx_follow_at = %s, country = %s, province = %s, city = %s '
                                'where wx_id = %s and sp_id = %s', nickname,
                                sex, head_img, follow_at, info.message.country,
                                info.message.province, info.message.city,
                                custom, sp_id)
                        else:
                            cid = db.query(
                                'select * from member_category where sp_id = %s and name = "未分组" ',
                                sp_id)[0].id
                            db.execute(
                                'insert into member set sp_id = %s, wx_id = %s, wx_name = %s, gender = %s,'
                                'head_img = %s, wx_follow_at = %s, created_at = NOW(),'
                                'category_id = %s, country = %s, province = %s, city = %s, '
                                'source = "微信导入"', sp_id, openid, nickname,
                                sex, head_img, follow_at, cid,
                                info.message.country, info.message.province,
                                info.message.city)
                        logging.info('会员 %s 更新信息成功', custom.encode('utf-8'))
                    else:
                        logging.error('获取 %s 信息失败。失败原因 %s',
                                      custom.encode('utf-8'),
                                      error_list.get(info.error_code, ''))

                if count < 10000 or count == total:
                    break
                else:
                    next_openid = wx.message.next_openid
            else:
                logging.error('获取关注者失败.商户 %s, 错误原因 %s', sp_id,
                              error_list.get(wx.error_code, ''))
                break
示例#8
0
def main_loop():
    redis = common.redis_client()
    db = common.db_client()
    args = {
        'db': db,
        'body': ''
    }

    while common.running():
        sp_id = redis.brpop('q:wx:mem:update')[1]

        args['sp_id'] = sp_id
        app_id = db.get('select * from supplier_property where name="app_id" and sp_id = %s', sp_id)['value']
        app_secret = db.get('select * from supplier_property where name="app_secret" and sp_id = %s', sp_id)['value']

        wx = Weixin(method='user/get', **args)
        wx.set_app_info(app_id, app_secret)

        info = Weixin(method='user/info', **args)
        info.set_app_info(app_id, app_secret)

        next_openid = ''
        while True:
            result = yield wx(next_openid=next_openid)
            wx.parse_response(result.body)

            if wx.is_ok() and 'data' in wx.message.keys():
                count = wx.message.count
                total = wx.message.total
                customs = wx.message.data.openid
                for custom in customs:
                    response = yield info(openid=custom, lang='zh_CN')
                    info.parse_response(response.body)
                    if info.is_ok():
                        nickname = info.message.nickname

                        # 暂时数据库未升级至可以存储emoji字符,故使用以下代码,升级数据库后可删除
                        import re
                        try:
                            # UCS-4
                            highpoints = re.compile(u'[\U00010000-\U0010ffff]')
                        except re.error:
                            # UCS-2
                            highpoints = re.compile(u'[\uD800-\uDBFF][\uDC00-\uDFFF]')
                        nickname = highpoints.sub(u'', nickname)
                        # todo =========================未升级前的替代方案===================

                        sex = {0: '未知', 1: '男', 2: '女'}.get(info.message.sex)
                        follow_at = datetime.fromtimestamp(info.message.subscribe_time)
                        openid = info.message.openid
                        head_img = info.message.headimgurl

                        mid = db.query('select * from member where wx_id = %s and sp_id = %s',
                                       custom, sp_id)

                        if mid:
                            db.execute('update member set wx_name = %s, gender = %s,'
                                       'head_img = %s, wx_follow_at = %s, country = %s, province = %s, city = %s '
                                       'where wx_id = %s and sp_id = %s',
                                       nickname, sex, head_img, follow_at, info.message.country,
                                       info.message.province, info.message.city,
                                       custom, sp_id)
                        else:
                            cid = db.query('select * from member_category where sp_id = %s and name = "未分组" ',
                                           sp_id)[0].id
                            db.execute('insert into member set sp_id = %s, wx_id = %s, wx_name = %s, gender = %s,'
                                       'head_img = %s, wx_follow_at = %s, created_at = NOW(),'
                                       'category_id = %s, country = %s, province = %s, city = %s, '
                                       'source = "微信导入"',
                                       sp_id, openid, nickname, sex, head_img, follow_at, cid, info.message.country,
                                       info.message.province, info.message.city)
                        logging.info('会员 %s 更新信息成功', custom.encode('utf-8'))
                    else:
                        logging.error('获取 %s 信息失败。失败原因 %s', custom.encode('utf-8'), error_list.get(info.error_code, ''))

                if count < 10000 or count == total:
                    break
                else:
                    next_openid = wx.message.next_openid
            else:
                logging.error('获取关注者失败.商户 %s, 错误原因 %s', sp_id, error_list.get(wx.error_code, ''))
                break
示例#9
0
    def post(self):
        params = dict([(name, self.get_argument(name).encode('utf-8')) for name in self.request.arguments
                       if name.startswith('s_menu_item') or name.startswith('m_menu_title')])

        main_pos_list = []
        main_content_list = []
        menu_pos_list = []
        menu_content_list = []
        for name, content in params.iteritems():
            # 只有那些有值的父菜单才能有机会插进数据库
            # 循环找到正确的顺序位置,数据库能准确按顺序插入
            if name.startswith('m_menu_title') and content.split('--')[0]:
                name_list = name.split('--')
                main_temp = (name_list[1], content)
                if not main_pos_list:
                    main_pos_list.append(name_list[1])
                    main_content_list.append(main_temp)
                else:
                    is_insert = False
                    for index, number in enumerate(main_pos_list):
                        if name_list[1] < number:
                            main_pos_list.insert(index, name_list[1])
                            main_content_list.insert(index, main_temp)
                            is_insert = True
                            break
                    if not is_insert:
                        main_pos_list.append(name_list[1])
                        main_content_list.append(main_temp)

            elif name.startswith('s_menu_item'):
                # 如果二级菜单名为空,则取消增加该行
                is_name = content.split('--')[0]
                if not is_name.strip():
                    continue

                menu_list = name.split('--')
                position = int(menu_list[1]) * 10 + int(menu_list[2])
                content_temp = (menu_list[1], content)
                if not menu_pos_list:
                    menu_pos_list.append(position)
                    menu_content_list.append(content_temp)
                else:
                    is_insert = False
                    for index, number in enumerate(menu_pos_list):
                        if position < number:
                            menu_pos_list.insert(index, position)
                            menu_content_list.insert(index, content_temp)
                            is_insert = True
                            break
                    if not is_insert:
                        menu_pos_list.append(position)
                        menu_content_list.append(content_temp)
            else:
                pass

        # 以下代码用做后端检验数据的正确性
        # is_true = False
        # for item in menu_content_list:
        #
        #     pass
        # if not is_true:
        #     self.redirect(self.reverse_url('menu'))

        sp_id = self.current_user.supplier_id
        self.db.execute('delete from wx_menu where sp_id = %s and parent_id is not null', sp_id)
        self.db.execute('delete from wx_menu where sp_id = %s ', sp_id)

        menu_data = {'button': []}
        for main_item in main_content_list:
            print main_content_list
            m_list = main_item[1].split('--')
            if len(m_list) == 3:
                if m_list[1] == '5':
                    md5_content = hashlib.md5(m_list[2]).hexdigest()
                    m_list[2] = md5_content[:12] + m_list[2]
                self.db.execute('insert into wx_menu(sp_id, name, action_type, action) values(%s, %s, %s, %s)',
                                sp_id, m_list[0], m_list[1], m_list[2])

                if m_list[1] == '2':
                    # view
                    m_button = {
                        'type': 'view',
                        'name': m_list[0],
                        'url': m_list[2]
                    }
                elif m_list[1] == '5':
                    m_button = {
                        'type': 'click',
                        'name': m_list[0],
                        'key': action_type_wx_menu[int(m_list[1])] + ':' + md5_content[:12]
                    }
                else:
                    # click
                    m_button = {
                        'type': 'click',
                        'name': m_list[0],
                        'key': action_type_wx_menu[int(m_list[1])] + ':' + m_list[2]
                    }

                menu_data['button'].append(m_button)

            elif len(m_list) == 1:
                pid = self.db.execute('insert into wx_menu(sp_id, name) values(%s, %s)', sp_id, m_list[0])
                sub_buttons = []
                menu_data['button'].append({'name': m_list[0], 'sub_button': sub_buttons})
                for menu_item in menu_content_list:
                    if menu_item[0] == main_item[0]:
                        me_list = menu_item[1].split('--')
                        if me_list[1] == '5':
                            md5_content = hashlib.md5(me_list[2]).hexdigest()
                            me_list[2] = md5_content[:12] + me_list[2]

                        self.db.execute('insert into wx_menu(sp_id, name, action_type, action, parent_id) '
                                        'values(%s, %s, %s, %s, %s)', sp_id, me_list[0], me_list[1], me_list[2], pid)

                        if me_list[1] == '5':
                            sub_button = {
                                'type': 'click',
                                'name': me_list[0],
                                'key': action_type_wx_menu[int(me_list[1])] + ':' + md5_content[:12]
                            }
                        elif me_list[1] != '2':
                            # click
                            sub_button = {
                                'type': 'click',
                                'name': me_list[0],
                                'key': action_type_wx_menu[int(me_list[1])] + ':' + me_list[2]
                            }
                        else:
                            # view
                            sub_button = {
                                'type': 'view',
                                'name': me_list[0],
                                'url': me_list[2]
                            }

                        sub_buttons.append(sub_button)
            else:
                pass

        # 如果一级菜单不是直接点击,且它没有对应的二级菜单,则删除
        main_menu = self.db.query('select id from wx_menu where sp_id = %s and parent_id is null '
                                  'and (action_type = " " or action_type is null)', sp_id)
        for item in main_menu:
            if not self.db.query('select id from wx_menu where sp_id = %s and parent_id = %s', sp_id, item.id):
                self.db.execute('delete from wx_menu where id = %s and sp_id = %s', item.id, sp_id)

        #如果类型为图文信息或者图文信息组的时候,且图文信息没有值就删除
        check_type = [3, 4, 7]
        self.db.execute('delete from wx_menu where sp_id = %%s and action_type in (%s) and action = "null"' %
                        ','.join(['%s'] * len(check_type)), sp_id, *check_type)
        wx = Weixin(db=self.db, sp_id=sp_id, method="menu/create", body=json_dumps(menu_data))
        app_id = self.current_user.sp_props.app_id
        app_secret = self.current_user.sp_props.app_secret
        wx.set_app_info(app_id, app_secret)
        result = yield wx()
        wx.parse_response(result.body)

        logging.info(json_dumps(menu_data))
        if wx.is_ok():
            logging.info('push menu to weixin sucessfully: %s', wx.message)
        else:
            logging.error('push menu to weixin fail: %s', wx.message)
        self.redirect(self.reverse_url('weixin.menu.show'))