web前端实现微信签名 调用wx提供的API

本文详细介绍了在前端实现微信配置签名的步骤,包括生成UUID、时间戳、ASCII排序以及使用SHA1加密的方法。通过获取access_token和jsapi_ticket,结合其他必要参数,完成微信API的配置,最终成功调用微信接口如选择图片功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

一、用到的方法封装

二、获取微信配置的相关数据完成签名

三、经过我的一波疯狂骚操作,完成了前端的签名操作,顺利进入到了wx配置,那么接下来就开始wx api的走秀吧!!!!


、用到的方法封装

(1)生成UUI

// 生成一个随机的UUID作为动态值
export const UUID = (len?: number, radix?: number) => {
    const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
    const uuid: any[] = [];
    let i: any = 0;
    radix = radix || chars.length;
    if (len) {
        for (i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)];
    } else {
        let r;
        // 中间用特殊字符链接
        uuid[8] = uuid[13] = uuid[18] = uuid[23] = '';
        uuid[14] = '4';
        // 长度不长于32位
        for (i = 0; i < 32; i++) {
            if (!uuid[i]) {
                r = 0 | (Math.random() * 16);
                uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r];
            }
        }
    }
    return uuid.join('');
};

(2)生成时间戳或时间

const formatNumber = (n: any) => {
    n = n.toString();
    return n[1] ? n : `0${n}`;
};
export const formatTime = (date: { getFullYear: () => any; getMonth: () => number; getDate: () => any; getHours: () => any; getMinutes: () => any; getSeconds: () => any }, type: any) => {
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const hour = date.getHours();
    const minute = date.getMinutes();
    const second = date.getSeconds();

    switch (type) {
        case 'YY':
            return `${[ye
前端实现Web分享到微信的核心是调用微信JS-SDK提供的接口,获取分享参数并生成分享链接。以下是一个使用Vue.js框架实现Web分享到微信的示例代码: ```html <template> <div> <button @click="shareToWechat">分享到微信</button> </div> </template> <script> import wx from 'weixin-js-sdk' export default { methods: { async shareToWechat() { const url = window.location.href.split('#')[0] // 分享的网址 const title = '分享标题' // 分享的标题 const description = '分享描述' // 分享的描述 const imageUrl = 'https://example.com/share-image.jpg' // 分享的图片链接 const response = await fetch('/api/wechat-signature?url=' + encodeURIComponent(url)) const { appId, timestamp, nonceStr, signature } = await response.json() wx.config({ debug: false, appId, timestamp, nonceStr, signature, jsApiList: ['updateAppMessageShareData', 'updateTimelineShareData'] }) wx.ready(() => { wx.updateAppMessageShareData({ title, desc: description, link: this.generateShareLink(url, title, description, imageUrl), imgUrl: imageUrl, success: () => { console.log('分享到微信好友成功') }, cancel: () => { console.log('取消分享到微信好友') } }) wx.updateTimelineShareData({ title, link: this.generateShareLink(url, title, description, imageUrl), imgUrl: imageUrl, success: () => { console.log('分享到微信朋友圈成功') }, cancel: () => { console.log('取消分享到微信朋友圈') } }) }) }, generateShareLink(url, title, description, imageUrl) { const encodedUrl = encodeURIComponent(url) const encodedTitle = encodeURIComponent(title) const encodedDescription = encodeURIComponent(description) const encodedImageUrl = encodeURIComponent(imageUrl) return `http://example.com/wechat-share.html?url=${encodedUrl}&title=${encodedTitle}&desc=${encodedDescription}&imgUrl=${encodedImageUrl}` } } } </script> ``` 这段代码中,我们使用了Vue.js框架和weixin-js-sdk库。在点击“分享到微信”按钮时,我们首先向后端API请求获取微信签名,然后在前端JS代码中调用wx.config函数配置微信JS-SDK的参数。在配置完成后,我们使用wx.updateAppMessageShareData和wx.updateTimelineShareData函数来设置分享到微信好友和微信朋友圈的参数,并在分享成功或取消分享时触发回调函数。最后,我们使用generateShareLink函数来生成分享链接,该链接包含了分享的网址、标题、描述和图片链接等参数,供微信客户端识别并显示分享内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值