// 在入口文件出初始化(main.ts)
import {initBase,initWxKit,initWwKit} from '@custouch/jssdk'
/** -----以下初始化方法请按需加载------ */
// 定义全局配置、初始化caKit,定义在最前面
initBase({
appId: window.env.APP_ID,
uaId: window.env.UA_ID
//切换为生产环境
// mode:'production'
//切换api为cloud
// apiEnv:'cloud'
})
// 定义微信配置、初始化wxKit
initWxKit({ defaultAccess: { notWechatLimit: 'userinfo', wechatLimit: 'userinfo' }, })
// 定义企微配置、初始化wwKit
initWwKit({ corpId: window.env.CORP_ID, agentId: window.env.AGENT_ID })
initBase({
//切换为cloud api
apiEnv:'cloud'
// 手动切换为生产环境(慎重)
// env:'production'
})
当页面需要按 CDP 访问权限配置控制资源访问时,需要在 initBase 中补充 cdpTenantId 和 cdpTeamId。
CDP 请求地址会根据全局 apiEnv 自动切换:
beta -> https://open.beta.custouch.com/platform/cdpcloud -> https://open.cloud.custouch.com/platform/cdp当页面需要通过同域代理保留 CDP 登录态时,可通过 cdpProxyBaseUrl 指定包含租户和团队 ID 的完整代理地址。未配置时仍使用上述默认地址。
import { initBase, initWxKit, cdpAccess } from '@custouch/jssdk'
initBase({
appId: window.env.APP_ID,
uaId: window.env.UA_ID,
apiEnv: 'beta',
cdpTenantId: window.env.TENANT_ID,
cdpTeamId: window.env.TEAM_ID,
cdpProxyBaseUrl: `${window.location.origin}/neo/cdp/platform/cdp/${window.env.TENANT_ID}/${window.env.TEAM_ID}`
})
initWxKit({})
const accessResult = await cdpAccess.checkAccess({
resourceType: 'LandingPage',
resourceId: 'page-1'
})
if (!accessResult.success) {
return
}
cdpAccess.checkAccess() 会按管理端配置执行访问控制:
loginStrategy 建立 CDP 联系人身份,再调用 CDP evaluate 接口staff/checkStaffAccess 校验员工访问权限如果需要手动调试或自定义串联流程,仍可以直接调用以下协议层 API:
// 获取短信接收方授权配置
const smsAuthorizationSetting = await cdpAuth.getRecipientAuthorizationSetting('sms')
// 发送验证码
const { verificationId } = await cdpAuth.sendVerificationCode({
type: 'sms',
phone: '13800138000',
areaCode: '86',
...(smsAuthorizationSetting.enabled
? {
smsRecipientAuthorization: {
agreed: true,
// 按勾选文案、协议标题、协议正文的顺序保存完整授权快照
consentText: `${smsAuthorizationSetting.consentText} ${smsAuthorizationSetting.protocolTitle} ${smsAuthorizationSetting.protocolContent}`
}
}
: {})
})
// 手机验证码登录
await cdpAuth.loginByPhoneCode({
phone: '13800138000',
code: '123456',
verificationId
})
// 查询当前 CDP 会话
const session = await cdpAuth.me()
// 统一的签到码业务请求
await signCode.view({ projectId: 1 })
await signCode.query({ projectId: 1, key: 'Phone', value: '13800138000' })
await signCode.sign({ projectId: 1, detail: { Phone: '13800138000' } })
await signCode.getSignInfo({ projectId: 1, onlySign: true })
await signCode.checkSignStatus({ projectId: 1 })
说明:
signCode 统一使用微信代理 certificate-repo 模块cdpAuth 使用 Cookie Session,接口请求默认带 credentials: 'include'cdpAuth.getRecipientAuthorizationSetting() 的渠道参数支持 sms 和 mailenabled 为 true 时,必须在用户明确勾选后,依次使用勾选文案、协议标题和协议正文构造完整授权快照,各部分以一个普通空格连接,并通过 consentText 提交;协议正文中的 HTML 保持接口返回的原文enabled 为 false 时,不传 smsRecipientAuthorizationsmsRecipientAuthorizationcdpAccess.checkAccess() 自动登录模式下,SDK 会在手机号发码前获取、展示并提交短信授权,业务页通常不需要自己管理登录弹层cdpAccess.*、cdpAuth.*、signCode.*Resource 和 ContentHub 模块需要向 CDP 上报行为时,需要先在 initBase 中配置 apiEnv、cdpTenantId 和 cdpTeamId。
import { initBase, cdpProxy } from '@custouch/jssdk'
initBase({
apiEnv: 'beta',
cdpTenantId: window.env.TENANT_ID,
cdpTeamId: window.env.TEAM_ID
})
CDP Action 请求会根据 apiEnv 走对应的 CDP Proxy 地址:
beta -> https://open.beta.custouch.com/platform/cdp/{tenantId}/{teamId}/proxycloud -> https://open.cloud.custouch.com/platform/cdp/{tenantId}/{teamId}/proxy接口入口:
await cdpProxy.actionView({
module: 'resource',
target: 'documentview',
data: {
title: '资源标题',
hashId: 'resource-hash-id',
path: location.pathname + location.search
}
})
请求结构:
interface CdpActionView {
documentview: { hashId: string }
downloadview: { hashId: string }
shareview: { hashId: string }
mailDoc: { hashId: string; mail: string }
videoview: { hashId: string }
'pre-videoview': { hashId: string }
videorecord: { hashId: string; duration: string }
article_view: { articleId: string }
material_view: {
contentHubId: string
contentType: number
sourceId: string
}
}
type CdpActionData = CdpActionView[keyof CdpActionView] &
Partial<BaseActionData> & {
source?: string
utm_source?: string
utm_medium?: string
utm_campaign?: string
utm_term?: string
utm_content?: string
m_ref?: string
}
type CdpProxyActionRequest = {
module: string
target: string
data: CdpActionData
}
module 和 target 使用字符串,data 直接传入 CdpActionView 中对应事件的数据对象。
cdpProxy.actionView() 会和 actionView() 一样补齐基础上报信息:
uaId 来自 initBase({ uaId })path 默认使用 window.location.href,如果 data 中显式传入 path 则使用传入值title、cms、tagIds、routeName 会合并 CurrentPageParamstrackOriginOpenId 时会补 originOpenIdutm_source、utm_medium、utm_campaignm_ref 默认按 data.m_ref -> sessionStorage['m_ref'] -> 当前链接 m_ref 的顺序取值cdpProxy.actionView() 和 actionView() 一样要求存在 uaId 和 title。title 可以在 data 中传入,也可以提前写入 CurrentPageParams.title。
当前支持的 target:
| module | target | 说明 |
|---|---|---|
resource |
documentview |
文档浏览 |
resource |
downloadview |
文档下载 |
resource |
shareview |
文档分享 |
resource |
mailDoc |
文档发送邮件 |
resource |
videoview |
视频浏览 |
resource |
pre-videoview |
视频预览 |
resource |
videorecord |
视频播放记录 |
contenthub |
article_view |
自定义图文浏览 |
contenthub |
material_view |
内容聚合素材浏览 |
data 的事件字段由 CdpActionView 定义,所有事件还可以传入以下公共字段:
type CdpActionCommonData = {
title?: string
path?: string
uaId?: string
cms?: string
tagIds?: string
originOpenId?: string
source?: string
utm_source?: string
utm_medium?: string
utm_campaign?: string
utm_term?: string
utm_content?: string
m_ref?: string
}
字段说明:
hashId 必传mailDoc 的 mail 必传videorecord 的 duration 必传,且必须是字符串article_view 的 articleId 必传material_view 的 contentHubId、contentType 和 sourceId 必传,contentType 保持接口定义的数字类型title 必传,或通过 CurrentPageParams.title 提供path 建议显式传 location.pathname + location.search;不传时会按 actionView() 基础逻辑使用 window.location.hrefuaId、cms、tagIds、originOpenId 属于基础上报信息,一般由 SDK 自动补齐source、utm_source、utm_medium、utm_campaign、utm_term、utm_content 可按需传入m_ref 可显式传入;未传时 SDK 依次从 sessionStorage 和当前链接补齐,三处均无非空值时不上报该字段Source / UTM 传参规则:
data 内的显式字段传入 source 和 UTM 信息path 的 query 中data.source -> path query sourcedata.utm_source -> path query utm_source,data.utm_medium -> path query utm_medium,data.utm_campaign -> path query utm_campaign,data.utm_term -> path query utm_term,data.utm_content -> path query utm_content文档浏览:
await cdpProxy.actionView({
module: 'resource',
target: 'documentview',
data: {
title: '文档标题',
hashId: 'document-hash-id',
path: location.pathname + location.search,
source: 'wechat',
utm_source: 'campaign-source'
}
})
文档发送邮件:
await cdpProxy.actionView({
module: 'resource',
target: 'mailDoc',
data: {
title: '文档标题',
hashId: 'document-hash-id',
path: location.pathname + location.search,
mail: 'user@example.com'
}
})
视频播放记录:
await cdpProxy.actionView({
module: 'resource',
target: 'videorecord',
data: {
title: '视频标题',
hashId: 'video-hash-id',
path: location.pathname + location.search,
duration: '120'
}
})
自定义图文浏览:
await cdpProxy.actionView({
module: 'contenthub',
target: 'article_view',
data: {
title: '文章标题',
articleId: 'article-id'
}
})
内容聚合素材浏览:
await cdpProxy.actionView({
module: 'contenthub',
target: 'material_view',
data: {
title: '文章标题',
contentHubId: 'content-hub-id',
contentType: 7,
sourceId: 'article-id'
}
})
注意事项:
data 直接传入对应事件的输入对象module 和 target 均为字符串,不在类型中建立两者与 data 的判别联合title 必须存在,可以来自 data.title 或 CurrentPageParams.titleduration 传字符串path 建议使用 location.pathname + location.searchimport {wxKit,wwKit} from '@custouch/jssdk'
// 微信环境登录后获取openid
wxKit.openId
// 企业微信登录后获取memberid
wwKit.memberId
// 获取授权信息配置登录后获取微信用户信息
checkAccess().then(async ()=>{
const userinfo = await wxKit.userInfo()
})
// 获取授权信息配置登录后获取企业微信用户信息
wwKit.login().then(async ()=>{
const userinfo = await wwKit.userInfo()
})
this.caKit.RequestCollect({title:'需要上报的标题'})
// false为不自动初始化jsconfig
const wxKit = initWxKit({ ... }, false)
/**
* 手动调用,载入自定义config并隐藏所有菜单
* hideAllMenu:true为隐藏功能菜单
* 注意这种情况下需要关闭自动初始化jsconfig,见上方代码
* */
wxKit.initConfig({wx.WxConfig}, { hideAllMenu: true })
// 判断sdk是否加载完成
await wxKit.sdkCompleted
// wx.ready
wxKit.sdk.ready(()=>{
})
# 获取typescript支持
yarn add @wecom/jssdk -D
// wx.onMenuShareAppMessage
wwKit.sdk.onMenuShareAppMessage({
...
})
import {FetchRequest} from '@custouch/jssdk'
const request = new FetchRequest({
...
})
request.get(...)
request.post(...)
调用showToast需要载入,在企业/微信当前页扫码登录会用到
import '@custouch/jssdk/dist/style.css'
<script src="https://oss.cloud.custouch.com/static/v1/jssdk/0.4.17-5.js"></script>
<script>
// 通过window.jssdk暴露接口
const {initBase} = window.jssdk
</script>