Example #1
0
    def enroll(self, request):
        serializer = self.serializer_class(data=request.data)
        serializer.is_valid(raise_exception=True)
        account = serializer.validated_data['account']
        password = serializer.validated_data['password']
        code = serializer.validated_data['code']
        mm_SMSCode.is_effective(account, code)
        customer = mm_Customer.add(account, password)
        customer_login.login(request, customer.user)
        invitecode = request.query_params.get('invitecode')
        platform = request.META.get(HeadersKey.HTTP_OS, Platform.WEB)
        if invitecode:
            inviter_id = decode_invite_code(invitecode)
            mm_InviteRecord.add_record(inviter_id=inviter_id,
                                       invited_id=customer.id,
                                       platform=platform)
            # 添加邀请返现
            mm_CustomerBonusRecord.add_record(
                customer_id=inviter_id,
                from_customer_id=customer.id,
                action=mm_CustomerBonusRecord.Action_Enroll,
                amount=mm_CustomerBonusRecord.Award_Mapping[
                    mm_CustomerBonusRecord.Action_Enroll],
                desc=mm_CustomerBonusRecord.template_enroll.format(
                    customer.account))
            # 增加积分
            mm_CustomerPoint.add_action(inviter_id,
                                        mm_CustomerPoint.Action_Invite_Enroll)

        data = dict(account=account, id=customer.id, user_id=customer.user.id)
        return Response(data=data, status=status.HTTP_200_OK)
Example #2
0
 def post(self, request):
     """登录"""
     required_params = ['account', 'password']
     Tool.required_params(request, required_params)
     username = request.data.get('account')
     password = request.data.get('password')
     try:
         user = authenticate(self.request,
                             username=username,
                             password=password)
         if user:
             customer_login.login(request, user)
             data = {
                 'id': user.customer.id,
                 'user_id': user.id,
                 'name': user.customer.name,
                 'im_token': user.customer.im_token,
             }
             mm_CustomerPoint.add_action(request.session['customer_id'],
                                         mm_CustomerPoint.Action_Login)
             return Response(Tool.format_data(data))
         else:
             raise LoginException('账号或密码错误')
     except User.DoesNotExist:
         raise LoginException('账号不存在')
Example #3
0
 def post(self, request):
     required_params = ['code', 'account', 'password']
     Tool.required_params(request, required_params)
     code = request.data.get('code')
     account = request.data.get('account')
     password = request.data.get('password')
     mm_SMSCode.is_effective(account, code)
     customer = mm_Customer.add(account, password)
     customer_login.login(request, customer.user)
     data = dict(account=account, id=customer.id, user_id=customer.user.id)
     return Response(Tool.format_data(data), status=status.HTTP_200_OK)
Example #4
0
 def login(self, request):
     login_serializer = CustomerLoginSerilizer(data=request.data)
     login_serializer.is_valid(raise_exception=True)
     try:
         username = login_serializer.validated_data['account']
         password = login_serializer.validated_data['password']
         user = authenticate(request=request, username=username, password=password, is_staff=True)
         if user:
             customer_login.login(request, user)
             serializer = UserSerializer(user)
             return Response(serializer.data)
         else:
             raise LoginException('账号或密码错误')
     except Customer.DoesNotExist:
         raise LoginException('账号不存在')
Example #5
0
 def login_miniprogram(self, request):
     serializer = self.serializer_class(data=request.data)
     serializer.is_valid(raise_exception=True)
     code = serializer.validated_data['code']
     wx_res = requests.get(settings.MinprogramSettings.LOGIN_URL + code)
     ret_json = wx_res.json()
     if 'openid' not in ret_json:
         return Response(data=ret_json, status=status.HTTP_400_BAD_REQUEST)
     openid = ret_json['openid']
     # session_key = ret_json['session_key']
     # unionid = ret_json.get('session_key')
     customer = mm_Customer.get_customer_by_miniprogram(openid)
     customer_login.login(request, customer.user)
     data = {
         'id': customer.id,
         'user_id': customer.user.id,
         'name': customer.name,
         'im_token': customer.im_token,
     }
     mm_CustomerPoint.add_action(request.session['customer_id'],
                                 mm_CustomerPoint.Action_Login)
     return Response(data=data)