123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- # -*- coding: utf-8 -*-
- import json
- from odoo import http
- from odoo.http import request
- from odoo import fields
- from .. import defs
- from .base import BaseController
- from .tools import get_wx_session_info, get_wx_user_info
- from weixin import WeixinMpAPI
- # from werobot.client import Client2, ClientException2
- from .client import Client2, ClientException2
- from openerp import exceptions
- import logging
- _logger = logging.getLogger(__name__)
- class WxappMessage(http.Controller, BaseController):
- @http.route('/<string:sub_domain>/check', auth='public', methods=['GET'])
- def check_message_setting(self, sub_domain, **kwargs): # 已经没用了?
- try:
- config = request.env['wxapp.config'].sudo()
- app_id = config.get_config('app_id', sub_domain)
- secret = config.get_config('secret', sub_domain)
- message_token = config.get_config('message_token', sub_domain)
- if not app_id or not secret or not message_token:
- return self.res_err(404)
- # message_token = '5598d9jr1spbyikc3xdh2fnouj3ubs7c'
- api = WeixinMpAPI(appid=app_id, app_secret=secret, **kwargs)
- api.mp_token = message_token # 这个类应该并不是用于小程序消息的,所以这里需要专门赋值
- success = api.validate_signature()
- if success:
- return api.echostr
- return self.res_err(-1)
- except Exception as e:
- _logger.exception(e)
- return self.res_err(-1, e.name)
- @http.route('/<string:sub_domain>/send-message', auth='public', methods=['POST'], csrf=False)
- def send_message(self, sub_domain, **kwargs): # 已经没用了?
- try:
- config = request.env['wxapp.config'].sudo()
- app_id = config.get_config('app_id', sub_domain)
- secret = config.get_config('secret', sub_domain)
- client = Client2(app_id, secret)
- encrypt_type = request.params.get('encrypt_type', 'raw')
- if encrypt_type == 'raw':
- dic = json.loads(request.httprequest.data)
- client.user_token = dic.get('token', '')
- client.page = dic.get('page', '')
- client.emphasis_keyword = dic.get('emphasis_keyword', '')
- client.template_id = dic.get('template_id', '')
- client.data = dic.get('data', '')
- else:
- raise exceptions.UserError(u'未处理的请求类型')
- access_token = request.env(user=1)['wxapp.access_token'].search([
- ('token', '=', client.user_token),
- ])
- if not access_token:
- return self.res_err(10000)
- client.send_text_message(access_token.open_id)
- except Exception as e:
- raise exceptions.UserError(u'发送失败 %s' % e)
- return
|