ajax 调用 json-rpc ,参考代码:

function jsonRpc(url, fct_name, params, settings) {
return genericJsonRpc(fct_name, params, function(data) {
return $.ajax(url, _.extend({}, settings, {
url: url,
dataType: 'json',
type: 'POST',
data: JSON.stringify(data, time.date_to_utc),
contentType: 'application/json'


@http.route('/dingtalk/authenticate', type='json', auth='none', methods=["GET", "POST"], csrf=False)
def authenticate(self, code, *args, **kw):
icp = request.env['ir.config_parameter']
access_token = icp.get_param('dingtalk.access_token', default='')
result = user.get_user_id(access_token, code)
if result[0]:
userid = result[1]['userid']

res_users = request.env['hr.employee'].sudo().search([('dingtalk_id', '=', userid)], limit=1)

if res_users:
res_user = res_users[0]
uid = request.session.authenticate(request.session.db, res_user.login, userid)
request.session.dingtalk_user_id = userid

if uid:
values = {'status': 'success', 'dingtalk_user_id': userid, 'uid': uid,
'session_id': request.session.sid}
return json.dumps({'status': 'error', 'errmsg': u'您没有绑定Odoo用户,请联系管理员!'})
_logger.info("values:" + str(values))
return json.dumps(values)
return json.dumps({'status': 'error', 'errmsg': u'您没有绑定Odoo用户,请联系管理员!'})

return json.dumps({'result': 'error', 'errmsg': u'获取钉钉用户信息失败'})

传递sesseion ,保持登录状态,重点是传递X-Openerp-Session-Id :

export function authenticate() {

return new Promise(function (resolve, reject) {
getSessionId().then((res) => {
let Config = {
method: 'POST',
uri: APPHOST + "/dingtalk/authenticate",
type: 'json',
headers: {
'Content-Type': 'application/json',
'X-Openerp-Session-Id': res || '',
body: body_data
authCode().then(function (result) {
const code = result.code
const params = {
code: code
Config.body.params = params || Config.body.params
request(Config, function (error, res) {
if (!error && res.ok) {
const data = res.data
if (data.result) {
} else {



def setup_session(self, httprequest):
# recover or create session

sid = httprequest.args.get('session_id')
explicit_session = True
if not sid:
sid = httprequest.headers.get("X-Openerp-Session-Id")
if not sid:
sid = httprequest.cookies.get('session_id')
explicit_session = False
if sid is None:
httprequest.session = self.session_store.new()
httprequest.session = self.session_store.get(sid)
return explicit_session



@http.route('/web/dataset/search_read', type='json', auth="user")
def search_read(self, model, fields=False, offset=0, limit=False, domain=None, sort=None):
return self.do_search_read(model, fields, offset, limit, domain, sort)

def do_search_read(self, model, fields=False, offset=0, limit=False, domain=None
, sort=None):
""" Performs a search() followed by a read() (if needed) using the
provided search criteria

:param str model: the name of the model to search on
:param fields: a list of the fields to return in the result records
:type fields: [str]
:param int offset: from which index should the results start being returned
:param int limit: the maximum number of records to return
:param list domain: the search domain for the query
:param list sort: sorting directives
:returns: A structure (dict) with two keys: ids (all the ids matching
the (domain, context) pair) and records (paginated records
matching fields selection set)
:rtype: list
Model = request.env[model]

records = Model.search_read(domain, fields,
offset=offset or 0, limit=limit or False, order=sort or False)
if not records:
return {
'length': 0,
'records': []
if limit and len(records) == limit:
length = Model.search_count(domain)
length = len(records) + (offset or 0)
return {
'length': length,
'records': records

@http.route('/web/dataset/load', type='json', auth="user")
def load(self, model, id, fields):
value = {}
r = request.env[model].browse([id]).read()
if r:
value = r[0]
return {'value': value}

def call_common(self, model, method, args, domain_id=None, context_id=None):
return self._call_kw(model, method, args, {})

def _call_kw(self, model, method, args, kwargs):
return call_kw(request.env[model], method, args, kwargs)

@http.route('/web/dataset/call', type='json', auth="user")
def call(self, model, method, args, domain_id=None, context_id=None):
return self._call_kw(model, method, args, {})

@http.route(['/web/dataset/call_kw', '/web/dataset/call_kw/<path:path>'], type='json', auth="user")
def call_kw(self, model, method, args, kwargs, path=None):
return self._call_kw(model, method, args, kwargs)

@http.route('/web/dataset/call_button', type='json', auth="user")
def call_button(self, model, method, args, domain_id=None, context_id=None):
action = self._call_kw(model, method, args, {})
if isinstance(action, dict) and action.get('type') != '':
return clean_action(action)
return False