Exemplo n.º 1
0
 def get_jsapi_ticket(self):
     """获取JS_SDK权限签名的jsapi_ticket"""
     access_token = self._token_cache.get_cache(self._token_cache.KEY_ACCESS_TOKEN)
     if access_token:
         url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi' % access_token
         r = requests.get(url)
         logger.info('【微信JS-SDK】获取JS_SDK权限签名的jsapi_ticket的Response[' + str(r.status_code) + ']')
         if r.status_code == 200:
             res = r.text
             logger.info('【微信JS-SDK】获取JS_SDK权限签名的jsapi_ticket>>>>' + res)
             d = json.loads(res)
             errcode = d['errcode']
             if errcode == 0:
                 jsapi_ticket = d['ticket']
                 # 添加至redis中
                 self._token_cache.set_access_cache(self._token_cache.KEY_JSAPI_TICKET, jsapi_ticket)
             else:
                 logger.info('【微信JS-SDK】获取JS_SDK权限签名的jsapi_ticket>>>>errcode[' + errcode + ']')
                 logger.info('【微信JS-SDK】request jsapi_ticket error, will retry get_jsapi_ticket() method after 10s')
                 tornado.ioloop.IOLoop.instance().call_later(10, self.get_jsapi_ticket)
         else:
             logger.info('【微信JS-SDK】request jsapi_ticket error, will retry get_jsapi_ticket() method after 10s')
             tornado.ioloop.IOLoop.instance().call_later(10, self.get_jsapi_ticket)
     else:
         logger.error('【微信JS-SDK】获取JS_SDK权限签名的jsapi_ticket时,access_token获取失败, will retry get_access_token() method after 10s')
         tornado.ioloop.IOLoop.instance().call_later(10, self.get_access_token)
Exemplo n.º 2
0
 def excute(self):
     """执行定时器任务"""
     logger.info('【获取微信全局唯一票据access_token】>>>执行定时器任务')
     tornado.ioloop.IOLoop.instance().call_later(0, self.get_access_token)
     tornado.ioloop.IOLoop.instance().call_later(0,
                                                 self.get_wd_access_token)
     tornado.ioloop.PeriodicCallback(
         self.get_access_token, self._expire_time_access_token).start()
     tornado.ioloop.PeriodicCallback(
         self.get_wd_access_token, self._expire_time_access_token).start()
Exemplo n.º 3
0
 def get_access_token(self):
     """获取微信全局唯一票据access_token"""
     url = WxConfig.config_get_access_token_url
     r = requests.get(url)
     logger.info('【获取微信全局唯一票据access_token】Response[' + str(r.status_code) +
                 ']')
     if r.status_code == 200:
         res = r.text
         logger.info('【获取微信全局唯一票据access_token】>>>' + res)
         d = json.loads(res)
         if 'access_token' in d.keys():
             access_token = d['access_token']
             # 添加至redis中
             self._token_cache.set_access_cache(
                 self._token_cache.KEY_ACCESS_TOKEN, access_token)
             # 获取JS_SDK权限签名的jsapi_ticket
             self.get_jsapi_ticket()
             return access_token
         elif 'errcode' in d.keys():
             errcode = d['errcode']
             logger.info(
                 '【获取微信全局唯一票据access_token-SDK】errcode[' + errcode +
                 '] , will retry get_access_token() method after 10s')
             tornado.ioloop.IOLoop.instance().call_later(
                 10, self.get_access_token)
     else:
         logger.error('【获取微信全局唯一票据access_token】request access_token error' +
                      ',will retry get_access_token() method after 10s')
         tornado.ioloop.IOLoop.instance().call_later(
             10, self.get_access_token)
Exemplo n.º 4
0
 def get_wd_access_token(self):
     """获取微店全局唯一票据access_token"""
     url = WxConfig.config_wd_get_access_token_url
     r = requests.get(url)
     logger.info('【获取微店全局唯一票据access_token】Response[' + str(r.status_code) +
                 ']')
     if r.status_code == 200:
         res = r.text
         logger.info('【获取微店全局唯一票据access_token】>>>' + res)
         d = json.loads(res)
         if d['status']['status_code'] == 0:
             access_token = d['result']['access_token']
             self._token_cache.set_access_cache(
                 self._token_cache.KEY_WD_ACCESS_TOKEN, access_token)
             return access_token
         else:
             errorcode = d['status']['status_code']
             logger.info(
                 '【获取微店全局唯一票据access_token-SDK】errcode[' + errcode +
                 '] , will retry get_access_token() method after 10s')
             tornado.ioloop.IOLoop.instance().call_later(
                 10, self.get_wd_access_token)
Exemplo n.º 5
0
 def on_response(self, response):
     try:
         CreateTime = int(time.time())
         if response.error:
             out = self.reply_text(self._fddrom_name, self._to_name,
                                   CreateTime,
                                   WxConfig.HTTP_RESPONSE_ERROR_COPYWRITE)
             self.write(out)
         else:
             CreateTime = int(time.time())
             res_json = json.loads(response.body)
             if res_json["status"]["status_code"] != 0 or res_json[
                     "result"]["status_ori"] == 10:
                 out = self.reply_text(self._from_name, self._to_name,
                                       CreateTime,
                                       WxConfig.PART_IN_FAILURE_COPYWRITE)
                 self.write(out)
                 logger.info("==========非法订单以及请求==========")
                 logger.info(res_json)
                 return
             self.send_service_message_text(
                 WxConfig.PART_IN_SUCCESS_COPYWRITE)
             name = res_json["result"]["buyer_info"]["name"]
             exit_media_id = self._media_cache.get_cache(self._order_id)
             if exit_media_id is not None:
                 out = self.reply_image(self._from_name, self._to_name,
                                        CreateTime, exit_media_id)
                 self.write(out)
             else:
                 token = self._token_cache.get_cache(
                     self._token_cache.KEY_ACCESS_TOKEN)
                 rawImagePath = self.get_random_path()
                 playload_image = {'access_token': token, 'type': 'image'}
                 logger.info("【新创建图片】" + rawImagePath)
                 namefont = ImageFont.truetype(self.get_font_path(), 20)
                 idFont = ImageFont.truetype(self.get_font_path(), 12)
                 im = Image.open(rawImagePath)
                 draw = ImageDraw.Draw(im)
                 draw.text((340, 363),
                           name[0:9],
                           fill=(0, 0, 0),
                           font=namefont)
                 draw.text((490, 980),
                           self._order_id,
                           fill=(165, 165, 165),
                           font=idFont)
                 newPath = self.workpath + "/core/product/" + self._order_id + '.jpeg'
                 im.save(newPath)
                 data = {'media': open(newPath, 'rb')}
                 r = requests.post(
                     url=
                     'http://file.api.weixin.qq.com/cgi-bin/media/upload',
                     params=playload_image,
                     files=data)
                 image_json = json.loads(r.text)
                 media_id = image_json["media_id"]
                 self._media_cache.set_cache(self._order_id, media_id)
                 out = self.reply_image(self._from_name, self._to_name,
                                        CreateTime, media_id)
                 self.write(out)
     except Exception as e:
         logger.error(str(e))
     finally:
         self.finish()