前端发送的链接地址也简单,如: https://xxx.xxxx.cn/ 没有多余参数,起初试的复杂的链接参数比较多,为了判断是不是发送的链接地址问题,测试了简单一点的,最后还是不行!
前端代码:
// 1. 获取当前页面URL(需去掉#及其后面部分)
const url = window.location.href.split('#')[0];
const postData = {
url: url //
};
fetch('/api/wechat/config', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(postData)
}).then(response => response.json())
.then(config => {
console.log(encodeURIComponent(url));
wx.config({
debug: false, /
appId: config.appId,
timestamp: config.timestamp,
nonceStr: config.nonceStr,
signature: config.signature,
jsApiList: config.jsApiList
});
wx.ready(() => {
// 分享给朋友
let wxTitleValue = $("#xxxx").val().trim();
wx.updateAppMessageShareData({
title: wxTitleValue,
desc: 'xxxxxxxxxxxxxxx!',
link: url, // 分享链接
imgUrl: 'https://xxxxx/logo.png',
success: () => alert('分享配置成功'),
fail: (err) => {
alert('分享配置失败:', res)
console.error("分享配置失败:", err);
}
});
wx.checkJsApi({
jsApiList: ['updateAppMessageShareData'],
success: (res) => {
console.log('接口权限检查结果:', res.checkResult);
}
});
// 分享到朋友圈
wx.updateTimelineShareData({
title:wxTitleValue,
link: window.location.href,
imgUrl: 'https://xxxxx/logo.png',
success: () => console.log('朋友圈分享配置成功'),
fail: (err) => {
console.error("分享配置失败:", err);
}
});
});
// 错误处理
wx.error(res => {
alert('微信配置失败:'+ res.toString())
console.error('微信配置失败:', res);
});
})
.catch(error => {
console.error('获取配置失败:', error);
});
.net后端代码:
1、参数接受方法
[HttpPost("config")]
public async Task GetConfig([FromBody] UrlRequest request)
{
if (request == null || string.IsNullOrEmpty(request.Url))
{
return BadRequest("url 参数是必需的");
}
var config = await _weChatService.GetConfigAsync(request.Url);
return Ok(config);
}
2、签名获取方法:
public async Task> GetConfigAsync(string url)
{
var ticket = await GetJsApiTicketAsync();
var noncestr = Guid.NewGuid().ToString("N").Substring(0, 16);
var timestamp = DateTimeOffset.Now.ToUnixTimeSeconds().ToString();
var signature = GenerateSignature(ticket, noncestr, timestamp, url);
return new Dictionary
{
{ "appId", _configuration["WeChat:AppId"] },
{ "timestamp", timestamp },
{ "nonceStr", noncestr },
{ "signature", signature },
{ "jsApiList", "['updateAppMessageShareData','updateTimelineShareData']" }
};
}
3、signature加密算法:
private string GenerateSignature(string jsapiTicket, string nonceStr, string timestamp, string url)
{
// 1. 按字典序拼接参数
string stringToSign = $"jsapi_ticket={jsapiTicket}&noncestr={nonceStr}×tamp={timestamp}&url={url}";
_logger.LogInformation($"signature加密前字符串为: {stringToSign}");
// 2. SHA1加密
using (SHA1 sha1 = SHA1.Create())
{
byte[] bytes = Encoding.UTF8.GetBytes(stringToSign);
byte[] hashBytes = sha1.ComputeHash(bytes);
return BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
}
}
报错信息:{"errMsg":"config:fail"}、{"retCode":-1"errMsg":"updateAppMessageShareData:the permission value is offline verifying"}

你好,报错提示什么呢?