企业微信内部应用开发 WECOM-JSSDK 调用扫一扫

import * as ww from '@wecom/jssdk'
import {type CommonResult, ScanQRCodeType} from '@wecom/jssdk'
import {useQwConfigStore} from "@/store/qwConfig";
import {onMounted} from "vue";
import {getAppJsapiTicket, getEntJsapiTicket} from "@/api/qw";
import {getNanoId} from "@/utils/randomUtils";
import useCrypt from "@/hooks/useCrypt";

export default function () {
    const qwConfigStore = useQwConfigStore()
    const {sha1Encrypt} = useCrypt()
    let entJsapiTicket = ''
    let appJsapiTicket = ''

    onMounted(async () => {
        console.log('useQw.ts 挂载完毕')
        // 获取ticket
        getEntJsapiTicket().then(({data}) => {
            entJsapiTicket = data;
        })
        getAppJsapiTicket().then(({data}) => {
            appJsapiTicket = data;
        })
    })


    function register() {
        ww.register({
            corpId: qwConfigStore.corpId,       // 必填,当前用户企业所属企业ID
            agentId: qwConfigStore.agentId,                  // 必填,当前应用的AgentID
            jsApiList: ['scanQRCode'], // 必填,需要使用的JSAPI列表
            getConfigSignature,                // 必填,根据url生成企业签名的回调函数
            getAgentConfigSignature            // 必填,根据url生成应用签名的回调函数
        })
    }

    async function getConfigSignature(url: string) {
        // 生成方法参考 https://developer.work.weixin.qq.com/document/14924
        let timestamp = Date.now();
        let nonceStr = getNanoId();
        let signature = getSignature(entJsapiTicket, nonceStr, timestamp, url);
        return {timestamp, nonceStr, signature}
    }

    async function getAgentConfigSignature(url: string) {
        // 根据 url 生成应用签名,生成方法同上,但需要使用应用的 jsapi_ticket
        let timestamp = Date.now();
        let nonceStr = getNanoId();
        let signature = getSignature(appJsapiTicket, nonceStr, timestamp, url);
        return {timestamp, nonceStr, signature}
    }

    function getSignature(ticket: string, nonceStr: string, timestamp: number, url: string) {
        let str = `jsapi_ticket=${ticket}&noncestr=${nonceStr}&timestamp=${timestamp}&url=${url}`
        return sha1Encrypt(str);
    }

    async function scanQRCode() {
        console.log('scanQRCode')
        register()

        // @ts-ignore
        let res: CommonResult & { resultStr?: string } = {};
        await ww.scanQRCode({
                needResult: true, //扫描结果是否由企业微信处理
                scanType: [ScanQRCodeType.qrCode],
                success(result) {
                    // 成功回调,result.errMsg 固定格式为“方法名:ok”
                    if (result.errCode != 0) {
                        alert(`扫描成功,出现未知错误,联系开发人员进行处理 result:${JSON.stringify(result)}`)
                    }
                    res = result;
                },
                fail(result) {
                    alert(`扫描失败,出现未知错误,联系开发人员进行处理  result:${JSON.stringify(result)} `)
                },
                cancel(result) {
                    // alert(`cancel 回调`)
                },
                complete(result) {
                    // alert(`complete 回调`)
                }
            }
        )
        return res;
    }

    return {scanQRCode}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值