def auth_of_wechat(self, code): """ 微信认证 """ response_info = {} url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&grant_type=authorization_code".format( config.WECHAT_APP_ID, config.WECHAT_APP_SECRET, code) try: response = requests.get(url) response_info = response.json() except Exception: send_try_except() return response_info
def query_user_info(self, open_id, access_token, refresh_token): """ 获取用户的信息 """ response_info = {} url = "https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}".format( access_token, open_id) try: response = requests.get(url) response_info = response.json() if 'errcode' in response_info: return None except Exception: send_try_except() return response_info
def finish(self, chunk=None, code=None, msg=None): """ 请求结果finish """ if chunk is None and code is None: chunk = dict(code=-102, msg="内部异常") # 上报异常 send_try_except(self.request) if isinstance(chunk, dict): if 'code' not in chunk: chunk.update(code=0) if 'msg' not in chunk: chunk.update(msg='ok') if code: chunk.update(code=code) if msg: chunk.update(msg=msg) self.response = chunk # 日志上报 self.send_to_gray_log() super(BaseHandler, self).finish(chunk)
def upload_head_url(self, user_id, imgdata): """头像上传保存本地,并上传七牛""" rootpath = config.USERIMG_ROOT_PATH or os.path.dirname(__file__) # 分10个文件夹存储 seq = user_id % 10 + 10000 imgpath = os.path.abspath(os.path.join(rootpath, "head_url", str(seq))) if not os.path.exists(imgpath): os.makedirs(imgpath) os.chmod(imgpath, stat.S_IRWXG) strio = BytesIO(imgdata) img = Image.open(strio) filename = "wwd_head_{0}.jpg".format(user_id) path = os.path.join(imgpath, filename) try: img.save(path, quality=100) except: img = img.convert('RGB') img.save(path, quality=100) sizes = (160, 80) head_val = "ww{0}".format(int(time.time())) hash_value, key_value = "", "" for size in sizes: imgname = "wwd_head_{0}_{1}.jpg".format(user_id, size) path = os.path.join(imgpath, imgname) thum_size = (size, size) thum = img.resize(thum_size, Image.BILINEAR) thum.save(path, quality=100) bucket_name = "head-img" upload_key = "head_url/{seq}/wwd_head_{user_id}_{size}_{head_val}.jpg".format( user_id=user_id, size=size, head_val=head_val, seq=seq) hash_value, key_value = qiniu_tools.upload_file( bucket_name, upload_key, path) if all([hash_value, key_value]): self.update_user_val(user_id, head_val) return True else: send_try_except() return False
def run(self): """ 执行 """ job = self.beanstalk.reserve() data = json.loads(job.body) user_id = int(data['user_id']) if not user_id: return job.delete() if 'type' not in data.keys() or 'worktype' not in data.keys(): return job.delete() raw_data = data['data'] try: head_url = common.my_str(raw_data['head_url']) if head_url == "": return job.delete() imgdata = requests.get(head_url).content self.user_handle.upload_head_url(user_id, imgdata) except Exception: send_try_except() return job.delete()