@custouch/jssdk
    Preparing search index...

    @custouch/jssdk

    // 在入口文件出初始化(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/cdp
    • cloud -> 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 校验员工访问权限
    • 拒绝访问时会自动执行已知动作,例如 Toast 或跳转表单/会员表单

    如果需要手动调试或自定义串联流程,仍可以直接调用以下协议层 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 和 mail
    • 短信授权配置 enabled 为 true 时,必须在用户明确勾选后,依次使用勾选文案、协议标题和协议正文构造完整授权快照,各部分以一个普通空格连接,并通过 consentText 提交;协议正文中的 HTML 保持接口返回的原文
    • 短信授权配置 enabled 为 false 时,不传 smsRecipientAuthorization
    • 邮件验证码请求不传 smsRecipientAuthorization
    • 在 cdpAccess.checkAccess() 自动登录模式下,SDK 会在手机号发码前获取、展示并提交短信授权,业务页通常不需要自己管理登录弹层
    • 顶层导出为 namespace: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}/proxy
    • cloud -> 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 会合并 CurrentPageParams
    • 开启 trackOriginOpenId 时会补 originOpenId
    • 会合并已缓存的 UTM/source 参数,例如 utm_source、utm_medium、utm_campaign
    • m_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
    }

    字段说明:

    • Resource 事件的 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.href
    • uaId、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 source
    • UTM 字段同理:data.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.title
    • duration 传字符串
    • path 建议使用 location.pathname + location.search
    import {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>