From e5c10d41696e0a22482ce1d628862411754141b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B9=8F=E5=AE=87?= Date: Sat, 15 Mar 2025 10:19:29 +0800 Subject: [PATCH] 1 --- yuthon_wechat/__manifest__.py | 6 ++ yuthon_wechat/controllers/main.py | 24 ++++++- yuthon_wechat/data/auth_oauth_provider.xml | 2 + .../oauth_provider/wecom_oauth_provider.js | 68 +++++++++++++++++++ 4 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 yuthon_wechat/static/src/frontend/oauth_provider/wecom_oauth_provider.js diff --git a/yuthon_wechat/__manifest__.py b/yuthon_wechat/__manifest__.py index 055734ac6..7f6906b6e 100644 --- a/yuthon_wechat/__manifest__.py +++ b/yuthon_wechat/__manifest__.py @@ -27,6 +27,12 @@ 'wizard/wecom_api_sync_wizard.xml', 'views/wx_work_views.xml', ], +"assets": { + + "web.assets_frontend": [ + "yuthon_wechat/static/src/js/frontend/**/*", + ], + }, 'application': True, 'installable': True, 'auto_install': False, diff --git a/yuthon_wechat/controllers/main.py b/yuthon_wechat/controllers/main.py index 49b4da3a2..b32ec7b16 100644 --- a/yuthon_wechat/controllers/main.py +++ b/yuthon_wechat/controllers/main.py @@ -1,12 +1,32 @@ # -*- coding: utf-8 -*- -# Part of Odoo. See LICENSE file for full copyright and licensing details. +# !参考 \addons\auth_oauth\controllers\main.py -from odoo import http +import base64 +import functools +import json +import logging +import os + +import werkzeug.urls +import werkzeug.utils +from werkzeug.exceptions import BadRequest + +from odoo import api, http, SUPERUSER_ID, _ +from odoo.exceptions import AccessDenied +from odoo.http import request, Response +from odoo import registry as registry_get +from odoo.tools.misc import clean_context + +from odoo.addons.auth_signup.controllers.main import AuthSignupHome as Home # type: ignore +from odoo.addons.auth_oauth.controllers.main import OAuthController # type: ignore +from odoo.addons.web.controllers.utils import ensure_db, _get_login_redirect_url # type: ignore +from odoo.addons.wecom_api.api.wecom_abstract_api import ApiException # type: ignore class Home(http.Controller): @http.route('/WW_verify_fC7AX158Ffby8Ed1.txt', type='http', auth="public") def WW_verify_UP42BAWNwtWK60RH(self): return 'fC7AX158Ffby8Ed1' + \ No newline at end of file diff --git a/yuthon_wechat/data/auth_oauth_provider.xml b/yuthon_wechat/data/auth_oauth_provider.xml index e9590b8f5..bea12e550 100644 --- a/yuthon_wechat/data/auth_oauth_provider.xml +++ b/yuthon_wechat/data/auth_oauth_provider.xml @@ -5,6 +5,7 @@ Wechat Corp 企业微信扫码登录 corpid agentid corpsecret + fa fa-fw fa-sign-in text-primary wecom_qr_login https://open.work.weixin.qq.com/wwopen/sso/qrConnect https://qyapi.weixin.qq.com/cgi-bin/gettoken https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo @@ -14,6 +15,7 @@ Wechat Corp2 企业微信直接登录 corpid agentid corpsecret + fa fa-fw fa-sign-in text-primary wecom_quick_login https://open.weixin.qq.com/connect/oauth2/authorize https://qyapi.weixin.qq.com/cgi-bin/gettoken https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo diff --git a/yuthon_wechat/static/src/frontend/oauth_provider/wecom_oauth_provider.js b/yuthon_wechat/static/src/frontend/oauth_provider/wecom_oauth_provider.js new file mode 100644 index 000000000..a3ad3e8ff --- /dev/null +++ b/yuthon_wechat/static/src/frontend/oauth_provider/wecom_oauth_provider.js @@ -0,0 +1,68 @@ +/** @odoo-module **/ + +import { browser } from "@web/core/browser/browser"; +import { _t } from "@web/core/l10n/translation"; +import { markup } from "@odoo/owl"; +import { sprintf } from '@web/core/utils/strings'; +import { session } from "@web/session"; +import { WecomSsoLoginDialog } from "../wecom_sso_login_dialog/wecom_sso_login_dialog"; + +import publicWidget from "@web/legacy/js/public/public_widget"; + +publicWidget.registry.WecomAuthProviders = publicWidget.Widget.extend({ + selector: 'div.o_login_auth', + events: { + // 'click a': '_onClick', + }, + jsLibs: [ + // document.location.protocol + "//wwcdn.weixin.qq.com/node/open/js/wecom-jssdk-2.0.2.js", + ], + init: function (parent, options) { + this.rpc = this.bindService("rpc"); + this.orm = this.bindService("orm"); + this.dialog = this.bindService("dialog"); + + return this._super.apply(this, arguments); + }, + start: function () { + var def = this._super.apply(this, arguments); + this.formEl = this.el.closest('form'); + + this.isWecomClient = this.checkWecomClient(); + + this.initAuthLinkElement(); + + return def; + }, + checkWecomClient: function () { + /* + mozilla/5.0 (windows nt 10.0; wow64) applewebkit/537.36 (khtml, like gecko) chrome/129.0.6668.101 safari/537.36 language/zh colorscheme/light wxwork/4.1.33 (micromessenger/6.2) windowswechat mailplugin_electron wemail embeddisk wwmver/3.26.16.708 + */ + const ua = navigator.userAgent.toLowerCase(); + if (ua.match(/WxWork/i) === "wxwork") { + return true; + } else { + return false; + } + }, + initAuthLinkElement: function () { + const self = this; + self.el.querySelectorAll('a').forEach(function (linkElement) { + let iconElement = linkElement.firstElementChild; + if (self.isWecomClient && iconElement.classList.contains('wecom_quick_login')) { + // 企业微信客户端自动登录到Odoo + self._wecomClientAutoLogin(linkElement); + } + }); + }, + _wecomClientAutoLogin(linkElement) { + + // https://open.weixin.qq.com/connect/oauth2/authorize? + // appid=wwc955c117b738b052&agentid=1000115 + // &redirect_uri=http://82.156.218.102:8057/auth_oauth/signin& + // state={"d":+"2531310",+"p":+5,+"r":+"http%3A%2F%2F82.156.218.102%3A8057%2Fweb"}&response_type=code&scope=snsapi_base#wechat_redirect + let urlString = linkElement.getAttribute('href'); + + window.location.href = urlString; + }, +}) \ No newline at end of file