def __init__(self, openid=None): self.openID = openid self.authKey = None self.redis = redis.Redis(db=1) self.usersTable = 'authUsers' self.keysTable = 'authKeys' self.qrAuthUserTable = 'qrAuthUsers'
def post(self, request): rs = redis.Redis() signame = request.data['signame'] conf = rs.get_redis(name='wechat', keys=['jsapi_ticket', 'access_token']) if conf[0] is not None: params = { 'timestamp': int(time.time()), 'noncestr': wc.NONCESTR, 'jsapi_ticket': conf[0], 'url': wc.JSAPI_URLS[signame] } ticket = wechat.Ticket(conf[1]) data = { 'appid': wc.APP_ID, 'timestamp': params['timestamp'], 'noncestr': params['noncestr'], 'signature': ticket.get_signature(params) } return Response({ 'code': '20001', 'data': data }, status=status.HTTP_201_CREATED) return Response( { 'code': '40001', 'msg': 'redis的wechat表不存在,定时刷新access_token功能没有开启' }, status=status.HTTP_400_BAD_REQUEST)
def __init__(self): self.rs = redis.Redis() self.access_token = None self.time_stamp_now = int(time.time()) self.deadline = 15 # access_token 过期时间 15天 self.data = { 'grant_type': cf.GRANT_TYPE, 'client_id': cf.CLIENT_ID, 'client_secret': cf.CLIENT_SECRET }
def authenticate(self, request): try: name = "authToken:" + request.META.get('HTTP_TOKEN') except KeyError: raise exceptions.AuthenticationFailed("认证失败") rs = redis.Redis(db=1) token_obj = rs.get_redis(name=name) if token_obj: return token_obj['user'], request.META.get('HTTP_TOKEN') raise exceptions.AuthenticationFailed("认证失败")
def post(self, request): auth_token = request.data.get('authToken') rs = redis.Redis(db=1) user_data = rs.get_redis(name='authToken:' + auth_token) if len(user_data): return Response({ 'code': '20001', 'data': user_data }, status=status.HTTP_201_CREATED) return Response({ 'code': '40001', 'msg': 'authToken无效' }, status=status.HTTP_400_BAD_REQUEST)
def get(self, request): rs = redis.Redis() access_token = rs.get_redis(name='wechat', key='access_token') serverId = request.data.get('serverId') img_link = wc.API_URL[ 'save_image'] + "?access_token=" + access_token + "&media_id=" + serverId path = "/home/mkfile/upload/" + serverId + ".jpg" req.urlretrieve(img_link, path) return Response( { 'code': '20001', 'data': { 'url': 'http://www.20mk.cn/mkfile/upload/' + serverId + ".jpg" } }, status=status.HTTP_201_CREATED)
def post(self, request): openid = request.data.get('openID') secret_key = request.data.get('secretKey') if openid is None or secret_key is None: return Response({ 'code': '40001', 'msg': '没有身份认证' }, status=status.HTTP_400_BAD_REQUEST) rs = redis.Redis(db=1) secret = rs.get_redis(name='qrAuthUsers', key=openid) if secret == secret_key: data = models.User.objects.get(openid=openid) auth_token = basics.create_token(openid) rs.set_redis(name='authToken:' + auth_token, mapping={ 'id': data.id, 'user': data.username, 'sex': data.sex, 'introduction': data.introduction, 'head_path': data.head_path }, day=5) return Response( { 'code': '20001', 'data': { 'authToken': auth_token } }, status=status.HTTP_201_CREATED) return Response({ 'code': '40001', 'msg': '登录失败' }, status=status.HTTP_400_BAD_REQUEST)
def __match(self, content): rs = redis.Redis() if content == "1000": self.msg.reply_news(pr.INSTRUCTIONS) elif content == "1001" or content == "一千零一": rs.set_redis(name='message', key=self.userName, value='music') self.msg.reply_text(pr.MUSIC_FUNC_START) elif content == "1002" or content == "一千零二": rs.set_redis(name='message', key=self.userName, value='text') self.msg.reply_text(pr.MUSIC_FUNC_STOP) elif content.replace(' ', '').lower() == '@authkey': authkey = auth.AuthKey(self.userName).query_or_create() self.msg.reply_text(authkey) elif content.replace(' ', '').lower() == '@home': self.msg.reply_news(pr.HOME) elif content.replace(' ', '').lower() == '@auth': self.msg.reply_news(pr.AUTH) else: val = rs.get_redis(name='message', key=self.userName) if val is None: mess_type = 'text' else: mess_type = val if mess_type == 'music': Search(content).music_list(self.msg) else: result = self.robot.inter_locution(content) self.msg.reply_text(result.replace('/n', '\n'))