WxworkAuth
是一个灵活的企业微信认证工具类,提供可自由搭配的用户信息验证和登录方法。相比路由守卫 WxworkAuthGuard
的强制性限制,WxworkAuth
允许在页面中灵活调用,实现页面先加载、再执行用户信息同步和注册的流程。
文件位置: fmode-ng/core.ts
constructor
或生命周期钩子中调用userid
作为用户名,后6位作为密码自动注册和登录import { WxworkAuth } from 'fmode-ng/core';
// 初始化
const wxAuth = new WxworkAuth({
cid: 'cDL6R1hgSi', // 公司帐套ID
appId: 'crm' // 应用ID,可选,默认为 'crm'
});
import { Component } from '@angular/core';
import { WxworkAuth } from 'fmode-ng/core';
@Component({
selector: 'app-my-page',
templateUrl: './my-page.component.html'
})
export class MyPageComponent {
wxAuth: WxworkAuth;
userInfo: any;
constructor() {
this.wxAuth = new WxworkAuth({ cid: 'cDL6R1hgSi' });
// 页面加载后执行用户认证
this.initAuth();
}
async initAuth() {
try {
// 方案1: 一站式认证和登录
const { userInfo, profile, user } = await this.wxAuth.authenticateAndLogin();
console.log('用户信息:', userInfo);
console.log('Profile ID:', profile.id);
console.log('登录用户:', user?.get('username'));
this.userInfo = userInfo;
} catch (error) {
console.error('认证失败:', error);
}
}
}
constructor(options: {
cid: string; // 公司帐套ID,必填
appId?: string; // 应用ID,可选,默认为 'crm'
})
示例:
const wxAuth = new WxworkAuth({ cid: 'cDL6R1hgSi' });
getUserInfo(code?: string): Promise<any>
获取企业微信用户信息(不登录)
参数:
code
(可选): 授权码,如果不提供则尝试从缓存或URL获取返回值: 企业微信用户信息对象
{
userid?: string; // 企业员工ID
external_userid?: string; // 外部联系人ID
openid?: string; // 微信OpenID
name?: string; // 姓名
mobile?: string; // 手机号
email?: string; // 邮箱
avatar?: string; // 头像URL
gender?: number; // 性别 (1:男, 2:女)
// ... 其他字段
}
使用场景: 只需要获取用户信息,不需要登录系统
示例:
const userInfo = await wxAuth.getUserInfo();
console.log('用户ID:', userInfo.userid);
console.log('姓名:', userInfo.name);
console.log('手机:', userInfo.mobile);
syncUserInfo(userInfo?: any): Promise<any>
同步用户信息到 Profile
/UserSocial
表(不登录)
参数:
userInfo
(可选): 用户信息对象,如果不提供则自动调用 getUserInfo()
获取返回值: Profile
或 UserSocial
Parse Object
使用场景: 只需要同步用户信息到数据库,不需要登录系统
示例:
// 方式1: 自动获取用户信息并同步
const profile = await wxAuth.syncUserInfo();
console.log('同步成功,Profile ID:', profile.id);
// 方式2: 使用已有的用户信息
const userInfo = await wxAuth.getUserInfo();
const profile = await wxAuth.syncUserInfo(userInfo);
autoLogin(userInfo?: any): Promise<FmodeUser | null>
自动登录或注册用户(使用 userid
和后6位作为密码)
参数:
userInfo
(可选): 用户信息对象,如果不提供则自动获取返回值: FmodeUser
对象或 null
登录逻辑:
userid
或 external_userid
或 openid
作为用户名user
指针到 Profile
/UserSocial
使用场景: 需要用户登录系统才能访问功能
示例:
const user = await wxAuth.autoLogin();
if (user) {
console.log('登录成功,用户名:', user.get('username'));
} else {
console.log('登录失败');
}
authenticateAndLogin(code?: string): Promise<{ userInfo, profile, user }>
一站式方法:获取用户信息、同步、并自动登录/注册
参数:
code
(可选): 授权码返回值:
{
userInfo: any; // 企业微信用户信息
profile: any; // Profile 或 UserSocial 对象
user: FmodeUser | null; // 登录的用户对象
}
执行流程:
Profile
/UserSocial
表使用场景: 最常用的场景,一次调用完成所有认证流程
示例:
const { userInfo, profile, user } = await wxAuth.authenticateAndLogin();
console.log('用户信息:', userInfo);
console.log('Profile ID:', profile.id);
console.log('登录用户:', user?.get('username'));
oauth(scope?, renew?): Promise<string | null>
触发 OAuth 授权流程
参数:
scope
(可选): 授权类型
'snsapi_base'
: 静默授权(默认)'snsapi_privateinfo'
: 敏感信息授权renew
(可选): 是否强制重新授权(清除缓存),默认 false
返回值: 授权码或 null
(如果跳转)
使用场景: 需要手动触发授权流程
示例:
// 静默授权
await wxAuth.oauth('snsapi_base');
// 敏感信息授权
await wxAuth.oauth('snsapi_privateinfo');
// 强制重新授权
await wxAuth.oauth('snsapi_base', true);
loginPC(): Promise<string>
PC端登录(弹出扫码面板)
返回值: 授权码
使用场景: PC端扫码登录
示例:
const code = await wxAuth.loginPC();
console.log('获得授权码:', code);
getSDK(): WxworkSDK
获取 WxworkSDK
实例(用于高级操作)
返回值: WxworkSDK
实例
使用场景: 需要使用 SDK 的其他高级功能
示例:
const sdk = wxAuth.getSDK();
// 获取当前聊天对象
const { GroupChat, Contact } = await sdk.getCurrentChatObject();
// 同步群聊信息
const group = await sdk.syncGroupChat(groupInfo);
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html'
})
export class DashboardComponent implements OnInit {
wxAuth: WxworkAuth;
currentUser: any;
constructor() {
this.wxAuth = new WxworkAuth({ cid: 'cDL6R1hgSi' });
}
async ngOnInit() {
// 页面加载后自动认证和登录
const { user } = await this.wxAuth.authenticateAndLogin();
this.currentUser = user;
if (!user) {
// 处理登录失败的情况
console.error('用户登录失败');
}
}
}
@Component({
selector: 'app-profile-preview',
templateUrl: './profile-preview.component.html'
})
export class ProfilePreviewComponent implements OnInit {
wxAuth: WxworkAuth;
userInfo: any;
constructor() {
this.wxAuth = new WxworkAuth({ cid: 'cDL6R1hgSi' });
}
async ngOnInit() {
// 只获取用户信息,不登录
this.userInfo = await this.wxAuth.getUserInfo();
console.log('用户名:', this.userInfo.name);
}
}
@Component({
selector: 'app-onboarding',
templateUrl: './onboarding.component.html'
})
export class OnboardingComponent {
wxAuth: WxworkAuth;
step = 1;
userInfo: any;
profile: any;
constructor() {
this.wxAuth = new WxworkAuth({ cid: 'cDL6R1hgSi' });
}
async step1_getUserInfo() {
this.userInfo = await this.wxAuth.getUserInfo();
this.step = 2;
}
async step2_syncProfile() {
this.profile = await this.wxAuth.syncUserInfo(this.userInfo);
this.step = 3;
}
async step3_login() {
const user = await this.wxAuth.autoLogin(this.userInfo);
if (user) {
console.log('登录成功!');
// 跳转到主页
}
}
}
import { FmodeParse } from 'projects/fmode-ng/src/lib/core/parse';
const Parse = FmodeParse.with("nova");
async function ensureAuthenticated() {
const currentUser = Parse.User.current();
if (currentUser) {
console.log('用户已登录:', currentUser.get('username'));
return currentUser;
}
// 未登录,执行认证
const wxAuth = new WxworkAuth({ cid: 'cDL6R1hgSi' });
const { user } = await wxAuth.authenticateAndLogin();
return user;
}
WxworkAuthGuard
(路由守卫)特点:
限制:
使用方式:
const routes: Routes = [
{
path: 'protected',
component: ProtectedComponent,
canActivate: [WxworkAuthGuard]
}
];
WxworkAuth
(认证工具类)特点:
constructor
或生命周期钩子中灵活调用优势:
使用方式:
// 在组件中直接调用
const wxAuth = new WxworkAuth({ cid: 'cDL6R1hgSi' });
await wxAuth.authenticateAndLogin();
创建一个 Angular Service 来统一管理认证逻辑:
import { Injectable } from '@angular/core';
import { WxworkAuth } from 'fmode-ng/core';
@Injectable({
providedIn: 'root'
})
export class AuthService {
private wxAuth: WxworkAuth;
constructor() {
// 从环境变量或配置中获取 cid
this.wxAuth = new WxworkAuth({ cid: 'cDL6R1hgSi' });
}
async authenticate() {
return await this.wxAuth.authenticateAndLogin();
}
async getUserInfo() {
return await this.wxAuth.getUserInfo();
}
}
在组件中使用:
@Component({
selector: 'app-my-page',
templateUrl: './my-page.component.html'
})
export class MyPageComponent implements OnInit {
constructor(private authService: AuthService) {}
async ngOnInit() {
const { user } = await this.authService.authenticate();
console.log('当前用户:', user?.get('username'));
}
}
async initAuth() {
try {
const { user } = await this.wxAuth.authenticateAndLogin();
if (!user) {
// 认证成功但登录失败
this.showMessage('登录失败,请稍后再试');
return;
}
// 登录成功
this.onLoginSuccess(user);
} catch (error: any) {
// 认证失败
console.error('认证错误:', error);
if (error.message?.includes('无法获取用户信息')) {
this.showMessage('无法获取用户信息,请检查网络');
} else {
this.showMessage('认证失败,请重试');
}
}
}
使用环境变量管理不同环境的配置:
// environment.ts
export const environment = {
production: false,
wxwork: {
cid: 'cDL6R1hgSi',
appId: 'crm'
}
};
// 使用
import { environment } from 'src/environments/environment';
const wxAuth = new WxworkAuth({
cid: environment.wxwork.cid,
appId: environment.wxwork.appId
});
cid
在 WxworkSDK.companyMap
中有对应的配置try-catch
捕获getUserInfo()
会优先使用 localStorage 缓存,传入 code
可强制刷新userid
后6位作为密码,确保 userid
长度 ≥ 6Parse.User
需要转换为 FmodeUser
类型:user as FmodeUser
fmode-ng/core.ts
projects/fmode-ng/src/lib/social/wxwork/wxwork-auth.guard.ts
projects/fmode-ng/src/lib/core/social/wxwork/wxwork.sdk.ts
projects/fmode-ng/src/lib/core/social/wxwork/wxwork.corp.ts
如遇问题,请检查:
corpId
, agentId
, suiteId
)cid
是否在 companyMap
中存在