def get(self, request): manager = MarketerModelManager() res = manager.check_phone_exist(self.request.query_params.get('phone')) if res: data = dict(code=PHONE_CODE_STATUS['EXIST'], message='手机已存在') else: data = dict(code=PHONE_CODE_STATUS['CAN_USE'], message='可以使用') return Response(status=status.HTTP_200_OK, data=data)
def wechat_auth_redirect(self, request): code = request.query_params.get('code', '') wechat_auth_handler = WeChantWebAuthHandler(app_id=app_id, app_secret=app_secret) access_token, openid = wechat_auth_handler.gen_access_token(code=code) user_info = wechat_auth_handler.gen_user_info(access_token, openid) if 'openid' not in user_info: user_info['openid'] = openid if not user_info.get('unionid'): return render(request, 'auth_web/auth_marketer_error.html', { 'title': "注册业务员", 'info': "授权异常" }) if MarketerModelManager.check_marketer_exists( unionid=user_info['unionid']): logger.info(f"{user_info['nickname']} 该邀请人已经注册") return render(request, 'auth_web/auth_marketer_error.html', { 'title': "注册业务员", 'info': "该邀请人已经注册" }) # marketer还没有注册 缓存openid, 昵称,缓存头像 _user_info = json.dumps(user_info) RedisUtil.cache_data(user_info['unionid'], _user_info, data_type='wechat_info') return render(request, 'auth_web/auth_marketer.html', { 'title': "注册业务员", 'nickname': user_info['nickname'] })
def get(self, request): user = self.request.user if user: manager = MarketerModelManager() user_info = manager.get_user_info(self.request.user.wechat_unionid) return Response(status=status.HTTP_200_OK, data=user_info) else: return Response(status=status.HTTP_200_OK, data=dict(user_name=None, user_phone=None, user_status='not_register', user_type=None, using_invited_merchants_num=0, reviewing_invited_merchants_num=0, total_bonus=0, user_withdrawable_balance=0, alipay_withdraw_balance=0, wechat_withdraw_balance=0))
def authenticate(self, request): token = request.META.get('Token') or request._request.META.get( 'HTTP_TOKEN') if token is None: raise exceptions.AuthenticationFailed() openid_session_unionid = cache.get(token) if openid_session_unionid is None: raise exceptions.AuthenticationFailed('Invalid token') # return None, None # get user marketer = MarketerModelManager() marketer = marketer.has_unionid(openid_session_unionid['unionid']) if marketer is None: return None, None elif marketer.status == config.SYSTEM_USER_STATUS.DISABLED: raise exceptions.AuthenticationFailed('Disabled User') return marketer, None
def test_check_phone(self): self.factory.create_marketer(phone='18888888888') mamager = MarketerModelManager() res = mamager.check_phone_exist('18888888888') self.assertEqual(res, True)
def validate_phone(self, phone): manager = MarketerModelManager() if manager.check_phone_exist(phone): raise serializers.ValidationError('电话号码已被其他邀请人或业务员绑定') return phone
def _check_user_existed(self): manager = MarketerModelManager() user = manager.has_unionid(self.unionid) return user