让自己的网站在微X里分享时好看有些:记一次败中有成的尝试
<h1 style="color: black; text-align: left; margin-bottom: 10px;">缘由</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">有一天,我看到这篇<span style="color: black;">文案</span>:<a style="color: black;">实践:使个人网站在<span style="color: black;">微X</span>/QQ中被优雅地<span style="color: black;">拜访</span> - 小站背面</a>。我才<span style="color: black;">发掘</span>,个人网站<span style="color: black;">倘若</span><span style="color: black;">无</span><span style="color: black;">尤其</span>在<span style="color: black;">微X</span>那边设置的话,在<span style="color: black;">微X</span>里面分享出来的效果很差,<span style="color: black;">仅有</span>一个链接符号,以及网站的标题。</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">倘若</span>是在浏览器端分享,或许还好一点。<span style="color: black;">例如</span>我用 iPhone 的 Safari 浏览器,分享到<span style="color: black;">微X</span>就可以展示标题、头图和简介。这些应该是网页的<span style="color: black;">有些</span></span></span><span style="color: black;"><span style="color: black;"><meta></span></span> 标签在起<span style="color: black;">功效</span>,<span style="color: black;">详细</span><span style="color: black;">那些</span>就不<span style="color: black;">晓得</span>了,<span style="color: black;">由于</span>我的网站中一堆这种功能的标签。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/96ac3caf87fb46f482194672c5bacf5a~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723777147&x-signature=IKUUZqGRFMXJDlg%2BqAUHVUqsh3M%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">网页分享给联系人的效果,上面是 Safari 分享到<span style="color: black;">微X</span>,下面是<span style="color: black;">微X</span>内分享</p>
</div>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p26-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/72b1b1e5d3b54cf6a163ca991aa51452~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723777147&x-signature=dXXE0e5L0hUIYN%2BJ7JabTFNuxto%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">网页分享到<span style="color: black;">伴侣</span>圈的效果,上面是 Safari 分享到<span style="color: black;">微X</span>,下面是<span style="color: black;">微X</span>内分享</p>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">前面<span style="color: black;">说到</span>的<span style="color: black;">文案</span>中,<span style="color: black;">针对</span><span style="color: black;">微X</span>的这种限制这么描述:</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">微X</span>的链接描述和缩略图<span style="color: black;">并不</span>是自动获取,而是手动定义的。而<span style="color: black;">为何</span><span style="color: black;">咱们</span>普通人分享时<span style="color: black;">无</span>看到<span style="color: black;">关联</span>选项呢?<span style="color: black;">由于</span>这是一个白名单功能,是<span style="color: black;">附庸</span>于<span style="color: black;">微X</span>公众号的一项功能。简言之,你<span style="color: black;">想要</span>自定义卡片样式,你就<span style="color: black;">必须</span>申请一个公众号,并将自己的域名接入该公众号后台。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">雪上加霜的是,这一步<span style="color: black;">需求</span>域名已备案。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">我的域名是备案过的,<span style="color: black;">况且</span>我<span style="color: black;">亦</span>有公众号,我倒是想<span style="color: black;">晓得</span>怎么搞。</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">然则</span>,我找了一下<span style="color: black;">微X</span>公众号的后台,<span style="color: black;">无</span><span style="color: black;">关联</span>的设置。</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">我搜了一下,<span style="color: black;">发掘</span>,原来这个功能用的是<span style="color: black;">微X</span>公众平台的 JS-SDK。文档 里面写的比较<span style="color: black;">仔细</span>,<span style="color: black;">能够</span><span style="color: black;">瞧瞧</span>。</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">我<span style="color: black;">日前</span>的博客是基于 Hexo 的静态博客,<span style="color: black;">运用</span> 安知鱼主题的魔改版本。</span></span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">基本<span style="color: black;">过程</span></h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">微X</span>公众平台的 JS-SDK 中,调用之前<span style="color: black;">必须</span>生成签名。<span style="color: black;">研发</span>者<span style="color: black;">必要</span>在服务器端实现签名的<span style="color: black;">规律</span>。</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">总的<span style="color: black;">来讲</span>,想要为在<span style="color: black;">微X</span>内分享的外链添加<span style="color: black;">照片</span>和简介,分为以下几个<span style="color: black;">过程</span>:</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">1、</span>绑定域名:公众号设置 → 功能设置 → JS 接口安全域名</span></span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p26-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/522018f7c86841bda189def25a481ed1~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723777147&x-signature=jLsWXn0Lh58K%2FnYb0%2B7Cgid67nc%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">绑定域名</p>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">2、</span>设置 IP 白名单:仅支持 IPv4 格式的<span style="color: black;">位置</span>;<span style="color: black;">这儿</span><span style="color: black;">亦</span>能看到 AppID 和 AppSecret(<span style="color: black;">倘若</span>没设置的话生成一下,记下来)</span></span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/f51bb2dce8444ef09f9e474d49d1be02~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723777147&x-signature=uxmmTWYiDBGAmhYGub4yeaictdI%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">3、</span>在页面引入 JS 文件:</span></span><span style="color: black;"><span style="color: black;"><span style="color: black;">http://res.wx.qq.com/open/js/jweixin-1.6.0.js</span></span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">4、</span>在白名单所在网络获取 </span></span><span style="color: black;"><span style="color: black;">access_token</span></span>:</p>GET https:<span style="color: black;">//api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET</span><span style="color: black;">// 正常<span style="color: black;">状况</span>下</span>
{<span style="color: black;">"access_token"</span>:<span style="color: black;">"ACCESS_TOKEN"</span>,<span style="color: black;">"expires_in"</span>:<span style="color: black;">7200</span>}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">5、</span>在白名单所在网络获取 </span></span><span style="color: black;"><span style="color: black;">jsapi_ticket</span></span>:</p><span style="color: black;">GET</span>https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi<span style="color: black;">// 正常<span style="color: black;">状况</span>下</span>
{
<span style="color: black;">"errcode"</span>:<span style="color: black;">0</span>,
<span style="color: black;">"errmsg"</span>:<span style="color: black;">"ok"</span>,
<span style="color: black;">"ticket"</span>:<span style="color: black;">"bxLdikRXVbTPdHSM05e5u5sUoXNKd8-41ZO3MhKoyN5OfkWITDGgnr2fwJ0m9E8NYzWKVZvdVtaUgWvsdshFKA"</span>,
<span style="color: black;">"expires_in"</span>:<span style="color: black;">7200</span>
}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">6、</span><span style="color: black;">经过</span>特定的签名算法生成签名 </span></span><span style="color: black;"><span style="color: black;">signature</span></span>,详见文档。除了前面得到的 <span style="color: black;"><span style="color: black;">jsapi_ticket</span></span>,还要传入秒为单位的时间戳 <span style="color: black;"><span style="color: black;">timestamp</span></span>、随机字符串 <span style="color: black;"><span style="color: black;">noncestr</span></span> 和调用 JS 接口页面的完整 URL <span style="color: black;"><span style="color: black;">url</span></span>(路径要在绑定域名下)。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">7、</span>注入权限验证配置:</span></span></p><span style="color: black;">wx</span><span style="color: black;">.config</span>({
<span style="color: black;">debug</span>: false, <span style="color: black;">// 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,<span style="color: black;">能够</span>在pc端打开,参数信息会<span style="color: black;">经过</span>log打出,仅在pc端时才会打印。</span>
<span style="color: black;">appId</span>: <span style="color: black;">APPID</span>, <span style="color: black;">// 必填,公众号的<span style="color: black;">独一</span>标识</span>
<span style="color: black;">timestamp</span>: TIMESTAMP, <span style="color: black;">// 必填,生成签名的时间戳</span>
<span style="color: black;">nonceStr</span>: <span style="color: black;">NONCESTR</span>, <span style="color: black;">// 必填,生成签名的随机串</span>
<span style="color: black;">signature</span>: <span style="color: black;">SIGNATURE</span>,<span style="color: black;">// 必填,签名</span>
<span style="color: black;">jsApiList</span>: [
<span style="color: black;">updateAppMessageShareData</span>,
<span style="color: black;">updateTimelineShareData</span>
] <span style="color: black;">// 必填,<span style="color: black;">必须</span><span style="color: black;">运用</span>的JS接口列表。上面写了本例中<span style="color: black;">必须</span>的接口</span>
});<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">8、</span>自定义“分享给<span style="color: black;">伴侣</span>”及“分享到 QQ”按钮的分享内容,以及“分享到<span style="color: black;">伴侣</span>圈”及“分享到 QQ 空间”按钮的分享内容:</span></span></p><span style="color: black;">wx</span><span style="color: black;">.ready</span>(function () {<span style="color: black;">//需在用户可能点击分享按钮前就先调用</span>
<span style="color: black;">wx</span><span style="color: black;">.updateAppMessageShareData</span>({ ... })
<span style="color: black;">wx</span><span style="color: black;">.updateTimelineShareData</span>({ ... })
});<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">处理</span>签名的问题</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">研发</span>者<span style="color: black;">必要</span>在服务器端实现签名的<span style="color: black;">规律</span>,<span style="color: black;">况且</span> </span></span><span style="color: black;"><span style="color: black;">access_token</span></span> 和 <span style="color: black;"><span style="color: black;">jsapi_ticket</span></span> 的获取都要在 IP 白名单内进行,并存储,有效期内<span style="color: black;">不该</span>重新请求。<span style="color: black;">因此呢</span>,<span style="color: black;">咱们</span><span style="color: black;">一样</span><span style="color: black;">必须</span>在服务器端获取 <span style="color: black;"><span style="color: black;">access_token</span></span> 和 <span style="color: black;"><span style="color: black;">jsapi_ticket</span></span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">因为</span> Hexo 生成的文件是一堆静态文件,<span style="color: black;">倘若</span>想这么搞就要另起炉灶,专门写一套服务用来生成它们、处理签名。</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">我不太想写,就找有<span style="color: black;">无</span>现成可用的,最后找到了一个<span style="color: black;">运用</span> Express 框架的 JS 项目:<a style="color: black;">「链接」</a>。<span style="color: black;">能够</span>调用它<span style="color: black;">供给</span>的 API,直接得到签名:</span></span></p>GET <span style="color: black;">/api/getWechatJsapiSign/</span>?noncestr=NONCESTR×tamp=TIMESTAMP&url=URL{<span style="color: black;">"signature"</span>: <span style="color: black;">"aaaabbbbbbbbbbbbbb"</span>}<h1 style="color: black; text-align: left; margin-bottom: 10px;">后端 Docker 化</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">因为</span>我几乎所有服务器上的服务都用 Docker 镜像托管,<span style="color: black;">因此</span>我自然就想将它打包为 Docker 镜像,<span style="color: black;">这般</span><span style="color: black;">安排</span>的时候方便<span style="color: black;">有些</span>。</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">因为</span>它<span style="color: black;">必须</span><span style="color: black;">运用</span> Redis 存储 </span></span><span style="color: black;"><span style="color: black;">access_token</span></span> 和 <span style="color: black;"><span style="color: black;">jsapi_ticket</span></span>的信息,<span style="color: black;">因此呢</span>我<span style="color: black;">运用</span> Redis 镜像为<span style="color: black;">基本</span>,安装 Node.js,<span style="color: black;">起步</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">原来的项目由 </span></span><span style="color: black;"><span style="color: black;">config.js</span></span> 写入配置项:</p><span style="color: black;">exports</span>.weixin = {
AppId: <span style="color: black;">wx9999999999</span>,
AppSecret: <span style="color: black;">ad73709c6e0815c999999999999</span>
};
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">我为了<span style="color: black;">安排</span>安全方便,<span style="color: black;">所有</span>改为读取环境变量:</span></span></p><span style="color: black;">exports.weixin</span> = <span style="color: black;">{</span>
<span style="color: black;">AppId</span>: <span style="color: black;">process.env.APP_ID,</span>
<span style="color: black;">AppSecret</span>: <span style="color: black;">process.env.APP_SECRET</span>
<span style="color: black;">};</span><span style="color: black;">ENV</span> <span style="color: black;">APP_ID=wx9999999999</span>
<span style="color: black;">ENV</span> <span style="color: black;">APP_SECRET=ad73709c6e0815c999999999999</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">这般</span>一来,<span style="color: black;">安排</span>时只<span style="color: black;">必须</span>传入环境变量<span style="color: black;">就可</span>:</span></span></p><span style="color: black;">docker</span> <span style="color: black;">run \
... \
-e APP_ID=wx9999999999 \
-e APP_SECRET=ad73709c6e0815c999999999999 \
dingjunyao/wx_jsapi_sign:latest</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">在 Docker 里面<span style="color: black;">起步</span>一个服务容易,<span style="color: black;">然则</span><span style="color: black;">同期</span><span style="color: black;">起步</span>两个服务,并且还要输出日志,就麻烦了。<span style="color: black;">运用</span> dumb-init,便<span style="color: black;">能够</span><span style="color: black;">经过</span>简单的 Shell 脚本,<span style="color: black;">容易</span>实现<span style="color: black;">同期</span>开启多个服务:</span></span></p><span style="color: black;">#!/bin/bash</span>
redis-server &
node app.js<span style="color: black;">CMD</span> <span style="color: black;">[<span style="color: black;">"/usr/bin/dumb-init"</span>, <span style="color: black;">"--"</span>, <span style="color: black;">"bash"</span>, <span style="color: black;">"start.sh"</span>]</span> # <span style="color: black;">start</span><span style="color: black;">.sh</span> 即上面的 <span style="color: black;">Shell</span> 脚本<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">倘若</span>镜像在 Debian 的<span style="color: black;">基本</span>上构建,用 APT <span style="color: black;">就可</span>安装,不<span style="color: black;">必须</span><span style="color: black;">另一</span>添加软件源:</span></span></p>RUN apt-<span style="color: black;">get</span> update -y && \
apt-<span style="color: black;">get</span> install -y dumb-<span style="color: black;">init</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">仔细</span>的更改见我 fork 的项目:<a style="color: black;">GitHub - DingJunyao/wx_jsapi_sign: The Nodejs server for manage Wechat (Wexin) access_token, jsapi ticket and signature generation. 用于管理<span style="color: black;">微X</span>J</a>S API的access_token、ticket和<span style="color: black;">按照</span>参数生成签名</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">。我<span style="color: black;">同期</span>把镜像发布到了 Docker Hub (dingjunyao/wx_jsapi_sign) 和 GitHub Packages(</p>ghcr.io/dingjunyao/wx_jsapi_sign)。
<h1 style="color: black; text-align: left; margin-bottom: 10px;">前端的操作</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">实质</span>上,上面的操作是我后来<span style="color: black;">发掘</span>的。我在 Hexo 主题上折腾了半天,才<span style="color: black;">发掘</span>还要搞后端。</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">我上次大段写前端代码,还是在 2018 年做课设的时候。五年过去了,<span style="color: black;">此刻</span>的前端早已变得亲妈都不认识了。<span style="color: black;">例如</span>我<span style="color: black;">日前</span>的博客项目,除了 Hexo 及附带的<span style="color: black;">各样</span>插件负责管理、渲染、<span style="color: black;">安排</span>等操作外,<span style="color: black;">运用</span>的主题<span style="color: black;">亦</span>采用 pug 模板引擎,以及 stylus 样式引擎。这带来的后果<span style="color: black;">便是</span>,我这种习惯于 HTML + CSS + JS(还是那种最基本的 JS,<span style="color: black;">此刻</span>发展的各项技术我早已跟不上了,哪怕是号<span style="color: black;">叫作</span>入门 ES6 的文档都看不懂) 的外行人,面对<span style="color: black;">此刻</span>的代码,只能摸着石头过河,照着已有的代码,猜测其含义,小修小改。调试则变得<span style="color: black;">反常</span>困难:在浏览器上看到的是渲染后的版本,内容、样式有什么不对的,很大程度上只能靠猜。我<span style="color: black;">通常</span>的做法,<span style="color: black;">便是</span>找到可能是标志性的属性和值,在项目里<span style="color: black;">查询</span>这段文本<span style="color: black;">显现</span>的位置,<span style="color: black;">而后</span>据此修改。</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">我大致的操作如下:</span></span></p>照着主题里面引入配置的方式,照着写<span style="color: black;">微X</span>的配置项和引入它的脚本照着主题里面引用 JS 的<span style="color: black;">办法</span>,引入<span style="color: black;">微X</span>的 JS API复制主题中的一个 JS 文件,找到可能用得上的片段,魔改。简单<span style="color: black;">来讲</span>,<span style="color: black;">便是</span><span style="color: black;">经过</span> fetch <span style="color: black;">拜访</span>签名 API,<span style="color: black;">而后</span><span style="color: black;">按照</span>得到的签名进行<span style="color: black;">微X</span> JS-SDK 的各项操作,<span style="color: black;">包含</span>注入配置、应用 API。<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">我不想大段放代码,有兴趣的<span style="color: black;">能够</span>看 GitHub 上 </p>DingJunyao/hexo-theme-anzhiyu-ding-mod 在 2023-10-17 的提交记录。
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">这种方式极易漏写变量等字符,<span style="color: black;">因此呢</span>我花了两天的时间才改好。</span></span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">处理</span>跨域问题</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">本地<span style="color: black;">安排</span>好后,我初次测试,<span style="color: black;">发掘</span>报跨域<span style="color: black;">关联</span>的错误。简单搜索后,我在后端添加了 </span></span><span style="color: black;"><span style="color: black;">cors</span></span> 包,并<span style="color: black;">运用</span>它。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">实质</span>上,只要在添加路由前执行以下语句就<span style="color: black;">能够</span><span style="color: black;">处理</span>问题了:</span></span></p><span style="color: black;">const</span> cors = <span style="color: black;">require</span>(<span style="color: black;">cors</span>);
app.<span style="color: black;">use</span>(<span style="color: black;">cors</span>());<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">然则</span>我<span style="color: black;">期盼</span>能够指定<span style="color: black;">准许</span>的域,于是这么写:</span></span></p><span style="color: black;">const</span> cors = <span style="color: black;">require</span>(<span style="color: black;">cors</span>)
<span style="color: black;">if</span> (config.whitelist.join(<span style="color: black;">,</span>) === ) {
app.<span style="color: black;">use</span>(<span style="color: black;">cors</span>());
} <span style="color: black;">else</span> {
<span style="color: black;">var</span>corsOptions = {
origin:<span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">(origin, callback)</span> </span>{
console.log(config.whitelist);
<span style="color: black;">if</span> (config.whitelist.indexOf(origin) !== <span style="color: black;">-1</span>) {
callback(<span style="color: black;">null</span>, <span style="color: black;">true</span>)
}<span style="color: black;">else</span> {
callback(<span style="color: black;">new</span> Error(<span style="color: black;">Not allowed by CORS: </span> + origin + <span style="color: black;">not in</span> + config.whitelist))
}
}
};
app.<span style="color: black;">use</span>(<span style="color: black;">cors</span>(<span style="color: black;">corsOptions</span>));
}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">而 </span></span><span style="color: black;"><span style="color: black;">config.whitelist</span></span> <span style="color: black;">一样</span>是从环境变量那里读取:</p><span style="color: black;">exports.whitelist</span> = (process.env.WHITELIST || ).split(<span style="color: black;">,</span>)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">然则</span>它的值,我琢磨了半天,才<span style="color: black;">发掘</span>,<span style="color: black;">倘若</span>不处理的话,除了端口号之外,协议<span style="color: black;">亦</span>要写上:</span></span></p><span style="color: black;">https</span>:<span style="color: black;">//4ading.com</span>
<span style="color: black;">https</span>:<span style="color: black;">//4ading.com,http://localhost:4000</span>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">折腾了两天才<span style="color: black;">发掘</span><span style="color: black;">无</span>权限</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">在浏览器上很难看出来<span style="color: black;">微X</span><span style="color: black;">关联</span>的代码<span style="color: black;">是不是</span>正确运行,<span style="color: black;">必须</span>在<span style="color: black;">微X</span><span style="color: black;">研发</span>者工具里面<span style="color: black;">才可</span>看到。</span></span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/8ad34402c9d5459495b657c10a29b9b0~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723777147&x-signature=htUNfBXdwfpuqnMrBPE5STRclis%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">微X</span><span style="color: black;">研发</span>者工具</p>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">注入权限验证配置的时候,我调试了大半天,总算看到 </span></span><span style="color: black;"><span style="color: black;">config ok</span></span> 的提示了。<span style="color: black;">然则</span>,后续的操作仍然失败。<span style="color: black;">而后</span>我<span style="color: black;">发掘</span>,<span style="color: black;"><span style="color: black;">config ok</span></span> 的时候,返回的 <span style="color: black;"><span style="color: black;">jsApiList</span></span> 列表为空。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/d4cf5abe01f347309a16b597010b0d9d~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723777147&x-signature=3ltB8OicQvGWbJiXsyigUTHr8H0%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">配置成功但无 API 被调用</p>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">我还挺纳闷,把 </span></span><span style="color: black;"><span style="color: black;">config</span></span> 里面的文本改为<span style="color: black;">办法</span>:</p><span style="color: black;">jsApiList</span>:<span style="color: black;">[
wx.updateAppMessageShareData,
wx.updateTimelineShareData
]</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">还是不行。</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">后来我不知<span style="color: black;">为何</span>,到自己的接口权限里面一看:</span></span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/7af638e54ef847faa6ba25d18510db29~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723777147&x-signature=gmTlaq%2BGchvMxjFWYTBjOvjTsQ0%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">接口权限</p>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">这下<span style="color: black;">最终</span>明白了。<span style="color: black;">经过</span><span style="color: black;">微X</span>认证的公众号<span style="color: black;">才可</span>调用这些功能,而注册主体为个人是搞不了认证的。</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">搞了两天,才<span style="color: black;">发掘</span>自己一<span style="color: black;">起始</span>就<span style="color: black;">无</span>权限。</span></span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">败中有成</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">本来到了<span style="color: black;">这儿</span>,我想把代码弃之<span style="color: black;">不消</span>了,<span style="color: black;">不外</span>还是觉得可惜,可是我<span style="color: black;">无</span>能用的号拿来测试效果。</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">所幸,微信的<span style="color: black;">研发</span>者工具中还<span style="color: black;">供给</span>了公众平台测试账号,<span style="color: black;">能够</span>测试<span style="color: black;">必须</span>权限的接口。你<span style="color: black;">必须</span>用自己的<span style="color: black;">微X</span>账号登录、关注测试号,<span style="color: black;">而后</span>用它的 appID 和动态变化的 appSecret 来测试。</span></span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/2e458d200d0e4b94830b1d763e5a75b7~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723777147&x-signature=scNQTIF2wyMTHck5gBlxF%2Fu3nwg%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">测试号管理界面</p>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">我用它测试了一下,可行。</span></span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/2cd751e8418043f0bcc4c2502dd23c8c~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723777147&x-signature=5oi6EwPOjYWeElRIhX4iV9yFzQE%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">测试号下的执行结果</p>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">趁着还有效,我测试了添加代码后,在<span style="color: black;">微X</span>里面分享出来的效果,结果如下:</span></span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/4192a3f23d9746c980f318e3795c78d3~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723777147&x-signature=li8kk3eReUlE85kfs9%2B6ZytV%2Ffw%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">添加代码后,网页分享给联系人的效果</p>
</div>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/53b61b3f0dba4f949abd64bb1a19e029~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723777147&x-signature=%2BGDiK2Gldo51NpyP2XgFnuj9P0s%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">添加代码后,网页分享到<span style="color: black;">伴侣</span>圈的效果</p>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">因此</span>,虽然自己用不了,<span style="color: black;">然则</span>还是写出了可用的代码。</span></span></p>
期待更新、坐等、迫不及待等。 系统提示我验证码错误1500次 \~゛,
页:
[1]