def task_update_unique_visitor(mama_id, openid, appkey, click_time):

    if XiaoluMama.objects.filter(pk=mama_id).count() <= 0:
        return

    nick, img = '', ''
    unionid = get_unionid_by_openid(openid, appkey)
    if unionid:
        from shopapp.weixin.models import WeixinUserInfo
        userinfo = WeixinUserInfo.objects.filter(unionid=unionid).first()
        if userinfo:
            nick, img = userinfo.nick, userinfo.thumbnail
    else:
        # if no unionid exists, then use openid
        unionid = openid

    date_field = click_time.date()
    uni_key = gen_uniquevisitor_unikey(openid, date_field)

    try:
        visitor = UniqueVisitor(mama_id=mama_id,
                                visitor_unionid=unionid,
                                visitor_nick=nick,
                                visitor_img=img,
                                uni_key=uni_key,
                                date_field=date_field)
        visitor.save()
    except IntegrityError:
        logger.warn(
            "IntegrityError - UniqueVisitor | mama_id: %s, uni_key: %s" %
            (mama_id, uni_key))
        pass
Exemplo n.º 2
0
def get_strade_wxid_iter(strade):
    """ 获取特卖订单微信openid,unionid """
    buyer_openid = strade.get_buyer_openid()
    ordertime = strade.pay_time
    wx_unionid = get_unionid_by_openid(buyer_openid, settings.WX_PUB_APPID)
    if not wx_unionid:
        wx_unionid = strade.receiver_mobile or str(strade.buyer_id)
    xd_unoins = WeixinUnionID.objects.filter(unionid=wx_unionid, app_key=settings.WEIXIN_APPID)  # 小店openid
    xd_openid = wx_unionid
    if xd_unoins.count() > 0:
        xd_openid = xd_unoins[0].openid

    isinxiaolumms = XiaoluMama.objects.filter(openid=wx_unionid,
                                              charge_status=XiaoluMama.CHARGED,
                                              charge_time__lte=ordertime)
    if isinxiaolumms.count() > 0:
        return isinxiaolumms[0], xd_openid, wx_unionid
    return None, xd_openid, wx_unionid
Exemplo n.º 3
0
    def get(self, request, linkid):
        click_url = request.get_full_path()
        if not self.is_from_weixin(request):
            share_url = WEB_SHARE_URL.format(site_url=settings.M_SITE_URL,
                                             mm_linkid=linkid,
                                             ufrom='web')
            logger.info({
                'action': 'ClickChannelLogView',
                'desc': 'not from weixin',
                'mm_linkid': linkid,
                'redirect_url': share_url,
                'click_url': click_url,
                'created': datetime.datetime.now(),
            })
            return redirect(share_url)
        self.set_appid_and_secret(settings.WX_PUB_APPID,
                                  settings.WX_PUB_APPSECRET)
        openid, unionid = self.get_openid_and_unionid(request)
        if not valid_openid(openid):
            redirect_url = self.get_wxauth_redirct_url(request)
            return redirect(redirect_url)
        click_time = datetime.datetime.now()
        ctasks.task_Create_Click_Record.delay(linkid, openid, unionid,
                                              click_time,
                                              settings.WX_PUB_APPID, click_url)

        if not valid_openid(unionid):
            unionid = get_unionid_by_openid(openid, settings.WX_PUB_APPID)
        xlmms = XiaoluMama.objects.filter(openid=unionid)
        if xlmms.exists():
            share_url = WEB_SHARE_URL.format(site_url=settings.M_SITE_URL,
                                             mm_linkid=xlmms[0].id,
                                             ufrom='wx')
        else:
            share_url = settings.M_SITE_URL
        response = redirect(share_url)
        self.set_cookie_openid_and_unionid(response, openid, unionid)
        return response
Exemplo n.º 4
0
    def get(self, request, linkid):
        from django_statsd.clients import statsd

        statsd.incr('xiaolumm.weixin_click')
        content = request.GET
        next_page = content.get('next', None)
        click_url = request.get_full_path()
        # print 'next_page:', next_page
        # logger.error('next_page %s-path:%s' % (next_page, content))
        click_time = datetime.datetime.now()
        if not self.is_from_weixin(request):
            share_url = get_share_url(next_page=next_page,
                                      mm_linkid=linkid,
                                      ufrom='web')
            response = redirect(share_url)
            response.set_cookie('mm_linkid', linkid, max_age=86400)
            if XiaoluSwitch.is_switch_open(10):
                logger.info({
                    'action': 'ClickLogView',
                    'code': 1,
                    'mm_linkid': linkid,
                    'redirect_url': share_url,
                    'click_url': click_url,
                    'created': datetime.datetime.now(),
                })
            return response

        self.set_appid_and_secret(settings.WX_PUB_APPID,
                                  settings.WX_PUB_APPSECRET)
        openid, unionid = self.get_openid_and_unionid(request)
        if not valid_openid(openid):
            redirect_url = self.get_wxauth_redirct_url(request)
            return redirect(redirect_url)

        ctasks.task_Create_Click_Record.delay(linkid, openid, unionid,
                                              click_time,
                                              settings.WX_PUB_APPID, click_url)

        if not valid_openid(unionid):
            unionid = get_unionid_by_openid(openid, settings.WX_PUB_APPID)

        # 2017-2-27 正式的小鹿妈妈为精英妈妈及以上
        if int(linkid) <= 0:
            xlmms = XiaoluMama.objects.filter(
                openid=unionid,
                status=XiaoluMama.EFFECT,
                charge_status=XiaoluMama.CHARGED,
                last_renew_type__gte=XiaoluMama.ELITE)
            linkid = xlmms.exists() and xlmms[0].id or linkid

        if int(linkid) <= 0 and XiaoluSwitch.is_switch_open(10):
            logger.info({
                'action': 'ClickLogView',
                'code': 2,
                'unionid': unionid,
                'mm_linkid': linkid,
                'http_referer': request.META.get('HTTP_REFERER'),
                'click_url': click_url,
                'cookies': request.COOKIES,
                'created': datetime.datetime.now(),
            })

        # 2017-2-27 如果打开链接的人是个你的铺子妈妈,以前是会用你的铺子妈妈id替换next里面的mamaid,现在不这样做,还是使用原来的linkid
        # share_url = get_share_url(next_page=next_page, mm_linkid=mm_linkid, ufrom='wx')
        share_url = get_share_url(next_page=next_page,
                                  mm_linkid=linkid,
                                  ufrom='wx')
        response = redirect(share_url)
        self.set_cookie_openid_and_unionid(response, openid, unionid)
        response.set_cookie('mm_linkid', linkid, max_age=86400)
        return response
Exemplo n.º 5
0
def tongji_wxorder(sender, obj, **kwargs):
    """ 统计微信小店订单订单提成信息 """

    today = datetime.date.today()
    target_time = obj.order_create_time.date()
    if target_time > today:
        target_time = today

    ordertime = obj.order_create_time
    order_stat_from = ordertime - datetime.timedelta(days=CLICK_VALID_DAYS)
    mm_order_amount = obj.order_total_price
    mm_rebeta_amount = 0
    mm_order_rebeta = 0
    buyer_mobile = obj.receiver_mobile
    wx_unionid = get_unionid_by_openid(obj.buyer_openid, settings.WEIXIN_APPID)
    isinxiaolumm = XiaoluMama.objects.filter(openid=wx_unionid,
                                             charge_status=XiaoluMama.CHARGED,
                                             charge_time__lte=ordertime)

    if isinxiaolumm.count() > 0:
        xiaolumm = isinxiaolumm[0]
        # 计算小鹿妈妈订单返利
        mm_rebeta_amount = xiaolumm.get_Mama_Trade_Amount(obj)
        mm_order_rebeta = xiaolumm.get_Mama_Trade_Rebeta(obj)
        tongjiorder, state = StatisticsShopping.objects.get_or_create(linkid=xiaolumm.id,
                                                                      wxorderid=str(obj.order_id))
        tongjiorder.linkname = xiaolumm.weikefu
        tongjiorder.openid = buyer_mobile
        tongjiorder.wxordernick = obj.buyer_nick
        tongjiorder.wxorderamount = mm_order_amount
        tongjiorder.rebetamount = mm_rebeta_amount
        tongjiorder.shoptime = obj.order_create_time
        tongjiorder.tichengcount = mm_order_rebeta
        tongjiorder.save()

        daytongji, state = StatisticsShoppingByDay.objects.get_or_create(linkid=xiaolumm.id,
                                                                         tongjidate=target_time)
        if state:
            daytongji.buyercount = 1
            daytongji.linkname = xiaolumm.weikefu
            daytongji.ordernumcount = 1
            daytongji.orderamountcount = mm_order_amount
            daytongji.todayamountcount = mm_order_rebeta
            daytongji.save()
        return

    mm_clicks = Clicks.objects.filter(click_time__range=(order_stat_from, ordertime)).filter(
        openid=obj.buyer_openid).order_by('-click_time')
    if mm_clicks.count() > 0:
        mm_linkid = get_xlmm_linkid(mm_clicks)
        mm_order_rebeta = obj.order_total_price

        xiaolu_mmset = XiaoluMama.objects.filter(id=mm_linkid)
        if xiaolu_mmset.count() > 0:
            xiaolu_mm = xiaolu_mmset[0]
            # 计算小鹿妈妈订单返利
            mm_rebeta_amount = xiaolu_mm.get_Mama_Trade_Amount(obj)
            mm_order_rebeta = xiaolu_mm.get_Mama_Trade_Rebeta(obj)
            tongjiorder, state = StatisticsShopping.objects.get_or_create(linkid=mm_linkid,
                                                                          wxorderid=str(obj.order_id))
            tongjiorder.linkname = xiaolu_mm.weikefu
            tongjiorder.openid = buyer_mobile
            tongjiorder.wxordernick = obj.buyer_nick
            tongjiorder.wxorderamount = mm_order_amount
            tongjiorder.rebetamount = mm_rebeta_amount
            tongjiorder.shoptime = obj.order_create_time
            tongjiorder.tichengcount = mm_order_rebeta
            tongjiorder.save()

            daytongji, state = StatisticsShoppingByDay.objects.get_or_create(linkid=mm_linkid,
                                                                             tongjidate=target_time)
            if state:
                daytongji.buyercount = 1
                daytongji.linkname = xiaolu_mm.weikefu
                daytongji.ordernumcount = 1
                daytongji.orderamountcount = mm_order_amount
                daytongji.todayamountcount = mm_order_rebeta
                daytongji.save()

        else:
            StatisticsShopping(linkid=0,
                               openid=buyer_mobile,
                               wxorderid=str(obj.order_id),
                               wxorderamount=mm_order_amount,
                               shoptime=obj.order_create_time,
                               tichengcount=mm_order_rebeta).save()

    else:
        tongjiorder, state = StatisticsShopping.objects.get_or_create(linkid=0, wxorderid=str(obj.order_id))
        tongjiorder.openid = buyer_mobile
        tongjiorder.wxorderamount = mm_order_amount
        tongjiorder.shoptime = obj.order_create_time
        tongjiorder.tichengcount = mm_order_rebeta
        tongjiorder.save()