要哈嘻之前的部分
[有偿]wx.config 提示 invalid signature?有偿解决哈, 欢迎私聊 前端代码 // main.ts import { createApp } from "vue"; import "./style.css"; import App from "./App.vue"; import * as VueRouter from "vue-router"; import routes from "./router"; import { initWx } from "./lib/wx"; const router = VueRouter.createRouter({ // history: VueRouter.createWebHashHistory(), history: VueRouter.createWebHistory("/web/mz-public/"), routes, }); router.afterEach(() => { initWx(location.href); }); createApp(App).use(router).mount("#app"); // wx.ts export async function initWx(url: string) { const res = await axios.post(`${HOST}/signature`, { url }); const { signature, timestamp, noncestr, accessToken, jsApiTicket } = res.data; const config = { appId: APPID, timestamp, debug: true, jsApiList: ["openLocation"], nonceStr: noncestr, signature, }; console.log(JSON.stringify({ ...config, url, accessToken, jsApiTicket })); wx.config(config); } 后端代码 const cache = { access_token: { value: "", outtime: 0 }, jsapi_ticket: { value: "", outtime: 0 }, }; async function getAccessToken() { if (cache.access_token.value && cache.access_token.outtime > new Date().getTime()) { console.log("access_token ok"); return cache.access_token.value; } const result = await axios.get( `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${APPID}&secret=${APPSECRET}` ); cache.access_token.value = result.data.access_token; cache.access_token.outtime = new Date().getTime() + EXPIRE_TIME; return result.data.access_token; } async function getJsApiTicket(access_token: string) { if (cache.jsapi_ticket.value && cache.jsapi_ticket.outtime > new Date().getTime()) { console.log("jsapi_ticket ok"); return cache.jsapi_ticket.value; } const result = await axios.get( `https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=${access_token}&type=jsapi` ); cache.jsapi_ticket.value = result.data.ticket; cache.jsapi_ticket.outtime = new Date().getTime() + EXPIRE_TIME; return result.data.ticket; } function generateSign(params: { jsapi_ticket: string; url: string; timestamp: string }) { const { jsapi_ticket, url, timestamp } = params; const msg = [ `jsapi_ticket=${jsapi_ticket}`, `url=${url}`, `timestamp=${timestamp}`, `noncestr=${NONCESTR}`, ].sort((a, b) => { return a < b ? -1 : 1; }); console.log("msg"); return crypto.createHash("sha1").update(msg.join("&")).digest("hex"); } export async function getSign(url: string) { console.log(cache); const accessToken = await getAccessToken(); const jsApiTicket = await getJsApiTicket(accessToken); const timestamp = new Date().getTime().toString().slice(0, 10); const signature = generateSign({ jsapi_ticket: jsApiTicket, url, timestamp }); return { signature, timestamp, noncestr: NONCESTR, accessToken, jsApiTicket }; } 验证器验证过 signature是正确的, 怀疑url有问题, 但试了两种都不行 [图片]
2023-10-30