Ejemplo n.º 1
0
def createPids(appid, userId, shareBy, becom):
    """
    创建用户层级记录 # 判断称为分销商是否需要申请
    :return:
    """
    pid = UidPid.objects.filter(uid=shareBy).first()
    # 判断该用户是否有资格分享
    status = 0 if becom == 0 else 1
    if not pid:
        try:
            UidPid.objects.create(appid=appid,
                                  uid=userId,
                                  pid1=shareBy,
                                  status=status,
                                  created=datetime.now())
            return gen_resp(0, msg="userId为{}的用户层级关系创建成功".format(userId))
        except Exception as err:
            log.error("用户层级关系创建失败{}".format(err))
            return gen_resp(10000, msg="用户层级关系创建失败")
    else:
        try:
            UidPid.objects.create(appid=appid,
                                  uid=userId,
                                  pid1=shareBy,
                                  pid2=pid.pid1,
                                  pid3=pid.pid2,
                                  status=status,
                                  created=datetime.now())
            return gen_resp(0, msg="userId为{}的用户层级关系创建成功".format(userId))
        except Exception as err:
            log.error("用户层级关系创建失败{}".format(err))
            return gen_resp(10000, msg="用户层级关系创建失败")
Ejemplo n.º 2
0
def share(sender, **kwargs):
    """
    创建分享记录
    appid, markId, shareBy, userId
    :return:
    """
    # 判断佣金系统是否开启

    appid = kwargs.get('appid')
    userId = kwargs.get('userId')
    shareBy = kwargs.get('shareBy')
    markId = kwargs.get('markId')
    if not (shareBy or markId):
        return gen_resp(0, msg="该用户不是分享注册")
    shop = Shop.objects.filter(appid=appid).first()
    log.info('创建分享记录参数{}shop.useRate{}'.format(kwargs, shop.useRate))
    conf = Conf.objects.filter(appid=appid).first()
    if not conf:
        return gen_resp(20001, msg="appid为{}的分销配置记录不存在".format(conf))

    if shop.useRate and conf.enable != 0:
        # 先判断是否有用户层级  判断是否有分享记录
        if UidPid.objects.filter(uid=userId, status=0).exists():
            return gen_resp(20004, msg="userId为{}的用户层级已存在".format(userId))
        # 判断称为分销商是否需要申请 shareBy是否有分销资格
        sbUP = UidPid.objects.filter(uid=shareBy).first()
        if not sbUP:
            return gen_resp(20003,
                            msg="shareBy为空或者shareBy为{}的记录不存在".format(shareBy))
        if conf.become == 0 and sbUP.status == 0:
            return gen_resp(0, msg="该用户没有分销资格")
        hasRcd = ShareRecord.objects.filter(userId=userId).first()
        if not hasRcd or conf.rule == 1:  # 还没有分享记录 或规则是首次下单
            if not markId or not ShareMark.objects.filter(id=markId).exists():
                return gen_resp(20004,
                                msg="markId为空或者markId为{}的记录不存在".format(markId))
            try:
                ShareRecord.objects.create(markId=markId,
                                           userId=userId,
                                           shareBy=shareBy,
                                           appid=appid)
            except Exception as err:
                log.error("创建分享记录失败{}".format(err))
                return gen_resp(10000, msg="分享记录创建失败")
        # 判断是否是首次进入绑定
        if conf.rule == 0:
            # 先判断是否绑定过!!!上边已做判断(信号在多个接口都会发送,所以要注意避免重复创建数据库记录)
            print("首次进入---createPids")
            createPids(appid, userId, shareBy, conf.become)
        return gen_resp(0, msg="分享记录创建成功")
    else:
        return gen_resp(0, msg="该商户未启用佣金系统")
Ejemplo n.º 3
0
def orderPids(sender, **kwargs):
    """
    下单绑定用户层级
    :return:
    """
    appid = kwargs.get('appid')
    userId = kwargs.get('userId')
    shop = Shop.objects.filter(appid=appid).first()
    conf = Conf.objects.filter(appid=appid).first()
    log.info("下单创建用户层级参数{}useRate{}".format(kwargs, shop.useRate))
    if not conf:
        return gen_resp(20001, msg="appid为{}的分销配置记录不存在".format(conf))

    if shop.useRate and conf.enable != 0 and conf.rule == 1:

        if UidPid.objects.filter(uid=userId, status=0).exists():
            return gen_resp(20003, msg="userId为{}的用户层级已存在".format(userId))

        shrcod = ShareRecord.objects.filter(
            appid=appid, userId=userId).order_by('-created').first()
        if not shrcod:
            return gen_resp(20001, msg="userId为{}的用户分享记录不存在".format(userId))
        shareBy = shrcod.shareBy
        sbUP = UidPid.objects.filter(uid=shareBy).first()
        if not sbUP:
            return gen_resp(20003,
                            msg="shareBy为空或者shareBy为{}的记录不存在".format(shareBy))
        # 要注意!!!这里创建用户层级之前是否需要判断
        # 判断shareBy是否有资格分享
        if conf.become == 0 and sbUP.status == 0:
            return gen_resp(0, msg="该用户没有分销资格")
        createPids(appid, userId, shareBy, conf.become)
        return gen_resp(0, msg="下单创建用户层级成功")
    else:
        return gen_resp(0, msg="该商户未启用佣金系统")
Ejemplo n.º 4
0
    def process_exception(self, request, exception):
        """
        统一处理错误返回
        :param request:
        :param exception:
        :return:
        """

        data = {'code': 10001, 'msg': '未知异常'}

        if isinstance(exception, RespData):
            data = exception.data
        else:
            log.warning(exception)

        return gen_resp(**data)
Ejemplo n.º 5
0
    def wrapper(request, *args):

        start = timezone.now()

        token = request.headers.get('jkbtoken')
        if not token:
            return gen_resp(Code.ILLEGAL)
        try:
            data = jwt.decode(token, verify=False)
            user: User = User.objects.filter(openid=data.get('openid'),
                                             appid=data.get('appid')).first()
            if user is None:
                return gen_resp(msg='用户不存在')
            jwt.decode(token, user.apikey, algorithms=['HS256'])
        except jwt.ExpiredSignatureError:
            return gen_resp(msg='token已经过期')
        except Exception as e:
            log.warning(f'token异常:{e}')
            return gen_resp(msg='token异常')
        request.user = user  # 注意这里的user有别于管理后台的user, 管理后台request.user对应Merc表

        before = ['=' * 40, request.get_raw_uri()]
        if request.content_type in ['application/xml', 'application/json']:
            before.append('body>\n' + request.body.decode())
        elif request.content_type == 'application/x-www-form-urlencoded':
            before.append('form>\n' + str(request.POST.dict()))
        log.info('\n'.join(before))

        try:
            response = func(request, *args)
            if response['Content-Type'] in ['application/json']:
                log.info('\n'.join([
                    'resp>\n' + response.content.decode(),
                    '=' * 40 + ' cost time:' + str(timezone.now() - start)
                ]))
            return response
        except RespData:
            return gen_resp(**RespData.data)
        except Exception as err:
            log.error(err)
            return gen_resp(Code.UNKNOWN)
Ejemplo n.º 6
0
 def get(self, request):
     return gen_resp(10000, 'ashibbbb')
Ejemplo n.º 7
0
    def __call__(self, request):

        # 黑名单处理

        path = str(request.path)

        # 接口关闭csrf检测
        if not path.startswith('/admin'):
            request._dont_enforce_csrf_checks = True

        # 接口token检测
        is_except_path = path.startswith('/admin') or \
            (path.startswith('/openid') and request.method == 'GET') or\
            (path.startswith('/login') and request.method == 'POST') or\
            (path.startswith('/payNoti') and request.method == 'POST') or\
            (path.startswith('/media/') and request.method == 'GET')

        request.APPID = request.headers.get('APPID')
        if not path.startswith('/admin') and not request.APPID and not path.startswith('/payNoti') and not path.startswith('/media/'):  # 除了admin都要检测请求头中是否有APPID + 回调
            return gen_resp(Code.ILLEGAL)

        if not is_except_path:

            start = timezone.now()

            token = request.headers.get('AUTHTOKEN')
            if not token:
                return gen_resp(Code.ILLEGAL)
            try:
                data = jwt.decode(token, verify=False)
                from main.models import User
                user: User = User.objects.filter(id=data.get('uid'), appid=request.APPID).first()
                if user is None:
                    return gen_resp(Code.USER_NOT_FOUND)
                jwt.decode(token, user.apikey, algorithms=['HS256'])
            except jwt.ExpiredSignatureError:
                return gen_resp(Code.TOKEN_EXPIRED)
            except Exception as e:
                log.warning(f'token异常:{e}')
                return gen_resp(Code.TOKEN_EXPIRED)
            request.user = user

            before = ['=' * 40, request.get_raw_uri()]
            if request.content_type in ['application/xml', 'application/json']:
                before.append('body>\n' + request.body.decode())
            elif request.content_type == 'application/x-www-form-urlencoded':
                before.append('form>\n' + str(request.POST.dict()))
            log.info('\n'.join(before))

        response = self.get_response(request)

        if not is_except_path and (response['Content-Type'] in ['application/json']):
            log.info('\n'.join(['\nresp>\n' + response.content.decode(),
                                '=' * 40 + ' cost time:' + str(timezone.now() - start)]))

        # 发送信号
        if path.startswith('/homeCate') or path.startswith('/cate') or path.startswith('/spuDetail'):
            shareBy = request.GET.get('shareBy')
            markId = request.GET.get('markId')
            log.info("shareBy{}markId{}" .format(shareBy, markId))
            signal_first_visit.send(sender=None, appid=request.APPID, markId=markId, userId=request.user.id,
                                    shareBy=shareBy)
            log.info('end---------signal')

        return response
Ejemplo n.º 8
0
def refund(sender, **kwargs):
    """
    消费记录 、记录提现
    :return:
    """
    appid = kwargs.get('appid')
    oid = kwargs.get('oid')
    shop = Shop.objects.filter(appid=appid).first()
    conf = Conf.objects.filter(appid=appid).first()
    if not conf:
        return gen_resp(20001, msg="appid为{}的分销配置记录不存在".format(conf))
    print(kwargs, shop.useRate)
    if shop.useRate and conf.enable != 0:
        order = Order.objects.filter(id=oid).first()
        if not order:
            return gen_resp(20001, msg="oid为{}的订单找不到".format(oid))
        if order.isRefund:
            oSkuIdList = list(
                OrderSku.objects.filter(orderId=oid).values_list('id',
                                                                 flat=True))
            oSkuRe = OrderSkuRefund.objects.filter(orderSkuId__in=oSkuIdList)
            if len(oSkuRe) == len(oSkuIdList):
                # 全部退款
                try:
                    with transaction.atomic():
                        uidFee = UidFee.objects.filter(oid=oid).first()
                        if uidFee:
                            uidFee.delete()
                        cashOut = CashOut.objects.filter(rid=uidFee.id).first()
                        if cashOut:
                            cashOut.delete()
                        return gen_resp(0, msg="消费记录、记录提现删除成功")
                except Exception as err:
                    log.error("消费记录、记录提现删除失败{}".format(err))
                    return gen_resp(10000, msg="消费记录、记录提现删除失败")
            else:
                # 部分退款
                oSkuRe = oSkuRe.aggregate(reFee=Sum('refundFee'))
                reFee = oSkuRe.get('reFee')
                uidFee = UidFee.objects.filter(oid=oid).first()
                if uidFee:
                    # 更新佣金
                    uidFee.money = uidFee.money - reFee
                    rateList = uidFee.rate.split(',')
                    uidFee.fee1 = uidFee.money * Decimal(rateList[0])
                    uidFee.fee2 = uidFee.money * Decimal(
                        rateList[1]) if uidFee.fee2 else uidFee.fee2
                    uidFee.fee3 = uidFee.money * Decimal(
                        rateList[2]) if uidFee.fee3 else uidFee.fee3
                    uidFee.remark = '部分退款'
                    try:
                        uidFee.save()
                        return gen_resp(0, msg="消费记录佣金更新成功")
                    except Exception as err:
                        log.error("消费记录佣金更新失败{}".format(err))
                        return gen_resp(10000, msg="消费记录佣金更新失败")

        else:
            return gen_resp(0, msg="该订单未发生退款")
    else:
        return gen_resp(0, msg="该商户未启用佣金系统")
Ejemplo n.º 9
0
def pay(sender, **kwargs):
    """
    支付成功创建消费记录,记录提现
    :return:
    """
    appid = kwargs.get('appid')
    oid = kwargs.get('oid')
    shop = Shop.objects.filter(appid=appid).first()
    conf = Conf.objects.filter(appid=appid).first()
    log.info("下单创建用户层级参数{}useRate{}".format(kwargs, shop.useRate))

    if not conf:
        return gen_resp(20001, msg="appid为{}的分销配置记录不存在".format(conf))

    if shop.useRate and conf.enable != 0:
        order = Order.objects.filter(id=oid).first()
        if not order:
            return gen_resp(20001, msg="oid为{}的订单不存在".format(oid))
        pids = UidPid.objects.filter(uid=order.userId, status=0).first()
        if not pids:
            return gen_resp(20001,
                            msg="userId为{}的用户层级记录不存在".format(order.userId))

        rate = str(conf.rate1) + ',' + str(conf.rate2) + ',' + str(conf.rate3)
        try:
            with transaction.atomic():
                # 创建消费记录
                uidf = UidFee()
                uidf.appid = order.appid
                uidf.oid = oid
                uidf.uid = order.userId
                uidf.money = order.realFee
                uidf.fee1 = order.realFee * conf.rate1
                uidf.fee2 = order.realFee * conf.rate2 if pids.pid2 else 0
                uidf.fee3 = order.realFee * conf.fee3 if pids.pid3 else 0
                uidf.rate = rate
                # 备注
                uidf.remark = 0
                uidf.save()
                # 创建记录提现
                CashOut.objects.create(appid=order.appid,
                                       rid=uidf.id,
                                       idx=1,
                                       pid=pids.pid1,
                                       status=0)
                if pids.pid2:
                    CashOut.objects.create(appid=order.appid,
                                           rid=uidf.id,
                                           idx=2,
                                           pid=pids.pid2,
                                           status=0)
                if pids.pid3:
                    CashOut.objects.create(appid=order.appid,
                                           rid=uidf.id,
                                           idx=3,
                                           pid=pids.pid3,
                                           status=0)
                return gen_resp(0, msg="消费记录,记录提现创建成功")
        except Exception as err:
            log.error("消费记录创建失败{}".format(err))
            return gen_resp(10000, msg="消费记录创建失败")
    else:
        return gen_resp(0, msg="该商户未启用佣金系统")