forked from JoneXiong/oejia_wx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwx_client.py
82 lines (61 loc) · 2.43 KB
/
wx_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# coding=utf-8
import logging
from wechatpy.client import WeChatClient
from wechatpy.crypto import WeChatCrypto
from odoo import exceptions
from .base import EntryBase
_logger = logging.getLogger(__name__)
WxEnvDict = {}
class WxEntry(EntryBase):
'''
目前仅公众号发送模板消息使用
'''
def __init__(self):
self.client = None
self.wxclient = None
self.crypto_handle = None
self.token = None
super(WxEntry, self).__init__()
def upload_media(self, media_type, media_file):
return self.client.media.upload(media_type, media_file)
def chat_send(self, uuid, msg):
openid = self.get_openid_from_uuid(uuid)
if openid:
self.client.message.send_text(openid, msg)
def send_image(self, uuid, media_id):
openid = self.get_openid_from_uuid(uuid)
if openid:
self.client.message.send_image(openid, media_id)
def send_voice(self, uuid, media_id):
openid = self.get_openid_from_uuid(uuid)
if openid:
self.client.message.send_video(openid, media_id)
def init(self, env):
dbname = env.cr.dbname
global WxEnvDict
if dbname in WxEnvDict:
del WxEnvDict[dbname]
WxEnvDict[dbname] = self
Param = env['ir.config_parameter'].sudo()
self.wx_token = Param.get_param('wx_token') or ''
self.wx_aeskey = Param.get_param('wx_aeskey') or ''
self.wx_appid = Param.get_param('wx_appid') or ''
self.wx_AppSecret = Param.get_param('wx_AppSecret') or ''
self.client = WeChatClient(self.wx_appid, self.wx_AppSecret)
self.wxclient = self.client
try:
if self.wx_aeskey:
self.crypto_handle = WeChatCrypto(self.wx_token, self.wx_aeskey, self.wx_appid)
except:
_logger.error(u'初始化微信公众号客户端实例失败,请在微信对接配置中填写好相关信息!')
try:
users = env['wx.user'].sudo().search([('last_uuid','!=',None)])
for obj in users:
if obj.last_uuid_time:
self.recover_uuid(obj.openid, obj.last_uuid, fields.Datetime.from_string(obj.last_uuid_time))
except:
env.cr.rollback()
import traceback;traceback.print_exc()
print('wx client init: %s %s'%(self.OPENID_UUID, self.UUID_OPENID))
def wxenv(env):
return WxEnvDict[env.cr.dbname]