Example #1
0
    def get(self, request, event_id, *args, **kwargs):
        # 1. check whether event_id is valid
        activity_entry = get_activity_entry(event_id)
        if not activity_entry:
            return Response({"error": "wrong event id"})

        customer = get_customer(request)
        if not customer or not customer.mobile:
            return Response({"bind": False})

        unionid = customer.unionid
        openid = get_openid_by_unionid(unionid, settings.WX_PUB_APPID)

        application = get_application(event_id, unionid, customer.mobile)
        if not application:
            key = 'apply'
        else:
            key = 'activate'

        # logger.warn("AppJoinView: customer=%s, event_id=%s, key=%s, openid=%s" % (customer.nick, event_id, key, openid))

        html = activity_entry.get_html(key)
        response = redirect(html)
        self.set_cookie_openid_and_unionid(response, openid, unionid)

        return response
Example #2
0
 def get_openid_and_unoinid_by_appkey(self, appkey):
     if not self.unionid.strip():
         return ('', '')
     from shopapp.weixin import options
     openid = options.get_openid_by_unionid(self.unionid, appkey)
     if not openid and appkey == settings.WX_PUB_APPID:
         return self.openid, self.unionid
     return openid, self.unionid
Example #3
0
    def get(self, request):
        """
        mama_id: 推荐人的专属id
        """
        content = request.GET
        mama_id = content.get('mama_id')
        mama_id = re.match("\d+", mama_id).group()

        deposite_url = "/m/register/deposite/?mama_id={0}".format(mama_id)
        # 加上装饰器之后已经登陆并注册状态(customer unionid)
        # 必须注册之后才可以成为小鹿代理  这里使用特卖公众账号授权
        self.set_appid_and_secret(settings.WX_PUB_APPID,
                                  settings.WX_PUB_APPSECRET)
        # 获取 openid 和 unionid
        # openid, unionid = self.get_openid_and_unionid(request)
        customer = Customer.objects.get(user=request.user)
        unionid = customer.unionid
        openid = get_openid_by_unionid(unionid, settings.WX_PUB_APPID)

        logger.info('mama register:%s,%s,%s' % (customer, openid, unionid))
        # if not valid_openid(openid) or not valid_openid(unionid):
        #     redirect_url = self.get_snsuserinfo_redirct_url(request)
        #     return redirect(redirect_url)
        customer_mobile = customer.mobile
        referal = XiaoluMama.objects.filter(id=mama_id).first()
        referal_from = referal.mobile if referal else ''  # 如果推荐人存在则
        if customer_mobile:  # 如果用户存在手机号码
            xlmm = XiaoluMama.objects.filter(openid=unionid).first()
            if xlmm:  # 存在则保存当前登陆用户的手机号码到当前小鹿妈妈的手机号字段
                xlmm.mobile = customer_mobile
                xlmm.save(update_fields=['mobile'])
            else:  # 否则创建当前用户的小鹿妈妈账号 并且是填写资料后状态
                XiaoluMama.objects.create(mobile=customer_mobile,
                                          referal_from=referal_from,
                                          progress=XiaoluMama.PROFILE,
                                          openid=unionid)

        try:
            xiaolumm = XiaoluMama.objects.get(openid=unionid)
        except XiaoluMama.DoesNotExist:
            response = Response({
                'openid': openid,
                'unionid': unionid,
                'xiaolumm': None,
                "mama_id": mama_id
            })
            self.set_cookie_openid_and_unionid(response, openid, unionid)
            return response

        except Exception, exc:
            logger.error(exc.message, exc_info=True)
            raise exc
Example #4
0
    def get(self, request, event_id, *args, **kwargs):
        content = request.GET
        from_customer = request.COOKIES.get("from_customer", "")
        mobile = request.COOKIES.get("mobile")
        imei = content.get('mobileSNCode') or ''  # 用户设备号
        openid, unionid = self.get_cookie_openid_and_unoinid(request)

        # 1. check whether event_id is valid
        activity_entry = get_activity_entry(event_id)
        if not activity_entry:
            return Response({"rcode": 1, "msg": "wrong event id"})

        customer = get_customer(request)
        if customer:
            openid = get_openid_by_unionid(customer.unionid,
                                           settings.WX_PUB_APPID)
            mobile = customer.mobile

        applied = False
        application_count = 0
        if openid:
            applications = XLSampleApply.objects.filter(user_openid=openid,
                                                        event_id=event_id)
            application_count = applications.count()
        elif mobile:
            applications = XLSampleApply.objects.filter(mobile=mobile,
                                                        event_id=event_id)
            application_count = applications.count()
        if application_count > 0:
            try:
                application = applications[0]
                if imei:
                    application.event_imei = '{0}_{1}'.format(event_id, imei)
                    application.save()
            except Exception, exc:
                logger.warn(exc.message)
            applied = True
Example #5
0
    def get(self, request, active_id, *args, **kwargs):

        customer = get_object_or_404(Customer, user=request.user.id)
        unionid = customer.unionid

        self.set_appid_and_secret(settings.WX_PUB_APPID, settings.WX_PUB_APPSECRET)
        if not unionid:
            user_infos = self.get_auth_userinfo(request)
            unionid = user_infos.get('unionid')
            openid = user_infos.get('openid')
            if not self.valid_openid(unionid):
                redirect_url = self.get_snsuserinfo_redirct_url(request)
                return redirect(redirect_url)
        else:
            openid = get_openid_by_unionid(unionid, settings.WX_PUB_APPID)
            user_infos = {
                'openid': openid,
                'unionid': customer.unionid,
                'nickname': customer.nick,
                'headimgurl': customer.thumbnail,
            }

        referal_id = request.GET.get('referal_id')
        referal_dress = None
        if referal_id:
            if not referal_id.isdigit():
                raise Http404('404')
            referal_dress = get_object_or_404(MamaDressResult, id=referal_id)

        mama_dress, state = MamaDressResult.objects.get_or_create(user_unionid=unionid)
        if state:
            mama_dress.openid = user_infos.get('openid')
            mama_dress.referal_from = referal_dress and referal_dress.user_unionid or ''
            mama_dress.mama_headimg = user_infos.get('headimgurl') or ''
            mama_dress.mama_nick = user_infos.get('nickname') or ''
            mama_dress.save()

        replay = request.GET.get('replay', '')
        if mama_dress.is_finished():
            if not replay:
                response = redirect(reverse('dress_result'))
                self.set_cookie_openid_and_unionid(response, openid, unionid)
                return response
            else:
                mama_dress.replay()

        question_id = 1
        question = self.get_question(question_id)
        self.template_name = self.template_name.format(active_id)

        dress_age, dress_star = self.get_dress_age_and_star(mama_dress)
        response = Response({
            'dress_id': mama_dress.id,
            'active_id': active_id,
            'question_id': question_id,
            'question': question,
            'pre_question_id': question_id,
            'post_question_id': question_id + 1,
            'score_string': '',
            'share_params': self.render_share_params(
                request,
                mama_dress=mama_dress,
                dress_age=dress_age,
                dress_star=dress_star
            ),
        })

        self.set_cookie_openid_and_unionid(response, openid, unionid)
        return response
Example #6
0
 def get_openid_and_unionid_by_customer(self, request):
     customer = get_customer(request)
     if not customer:
         return '', ''
     openid = get_openid_by_unionid(customer.unionid, settings.WX_PUB_APPID)
     return openid, customer.unionid
Example #7
0
def get_wxopenid(sale_trade, customer):
    wx_unionid = customer.unionid
    xd_openid = get_openid_by_unionid(wx_unionid, settings.WX_PUB_APPID)
    return xd_openid, wx_unionid
Example #8
0
    def create_Saletrade(self,
                         form,
                         address,
                         customer,
                         order_type=SaleTrade.SALE_ORDER):
        """ 创建特卖订单方法 """
        tuuid = form.get('uuid')
        if not self.is_valid_uuid(tuuid):
            raise exceptions.APIException(u'订单UUID异常')
        sale_trade, state = SaleTrade.objects.get_or_create(
            tid=tuuid, buyer_id=customer.id)

        if sale_trade.status not in (SaleTrade.WAIT_BUYER_PAY,
                                     SaleTrade.TRADE_NO_CREATE_PAY):
            raise exceptions.APIException(u'订单不可支付')

        params = {'channel': form.get('channel')}
        if address:
            params.update({
                'receiver_name': address.receiver_name,
                'receiver_state': address.receiver_state,
                'receiver_city': address.receiver_city,
                'receiver_district': address.receiver_district,
                'receiver_address': address.receiver_address,
                'receiver_zip': address.receiver_zip,
                'receiver_phone': address.receiver_phone,
                'receiver_mobile': address.receiver_mobile,
            })

        if state:
            cart_ids = [
                i for i in form.get('cart_ids', '').split(',') if i.isdigit()
            ]
            cart_qs = ShoppingCart.objects.filter(id__in=cart_ids,
                                                  buyer_id=customer.id)

            is_boutique = False
            for cart in cart_qs:
                mp = cart.get_modelproduct()
                if mp and mp.is_boutique:
                    is_boutique = True
                if mp and mp.is_boutique_coupon:
                    order_type = SaleTrade.ELECTRONIC_GOODS_ORDER

            from shopapp.weixin.options import get_openid_by_unionid
            openid = get_openid_by_unionid(customer.unionid,
                                           settings.WX_PUB_APPID)
            params.update({
                'is_boutique': is_boutique,
                'order_type': order_type,
                'buyer_nick': customer.nick,
                'buyer_message': form.get('buyer_message', ''),
                'payment': float(form.get('payment')),
                'pay_cash': float(form.get('payment')),
                'total_fee': float(form.get('total_fee')),
                'post_fee': float(form.get('post_fee')),
                'discount_fee': float(form.get('discount_fee')),
                'charge': '',
                'status': SaleTrade.WAIT_BUYER_PAY,
                'openid': openid,
                'extras_info': {
                    'mm_linkid': form.get('mm_linkid', '0'),
                    'ufrom': form.get('ufrom', ''),
                    'wallet_renew_deposit': form.get('wallet_renew_deposit',
                                                     '')
                }
            })
        for k, v in params.iteritems():
            hasattr(sale_trade, k) and setattr(sale_trade, k, v)
        sale_trade.save()
        return sale_trade, state