生命在折旧 發表於 2022-7-22 22:39:00

【Golang】golang开发微信公众号网页授权功能

<h2>基本流程</h2>
<p>微信公众号服务号的网页授权功能开发,主要是通过js跳转到一个微信提供的url</p>
<p>然后微信会弹出获取昵称头像的按钮</p>
<p>允许获取后,会回跳到我们的网址上,并且带着一个code参数</p>
<p>&nbsp;</p>
<p>我们拿到code参数,调用接口获取到获取到昵称头像、以及openid。这样就拿到了微信客户的主要信息</p>
<p>我们数据库会存储一个对应关系,微信openid对应的我们用户的唯一标识,这样就能直接登录到系统了。</p>
<p>&nbsp;</p>
<h2>实际案例</h2>
<p>比如我的唯一在线客服系统,客服人员点击模板消息的时候,就是访问以下网址</p>
<p>http://127.0.0.1:8081/wechatTransfer?ent_id=xxxxxx</p>
<p>这个页面什么也没干,就是把ent_id下客户的微信公众号APP_ID以及配置的跳转HOST拼接到下面的url,然后直接跳转</p>
<p>这里注意一下,我们自己的回跳的地址,如果是带着参数的,需要urlencode编码一下</p>
<div class="cnblogs_code">
<pre>                <span style="color: rgba(0, 0, 255, 1)">var</span> url=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">https://open.weixin.qq.com/connect/oauth2/authorize?appid=</span><span style="color: rgba(128, 0, 0, 1)">"</span>+<span style="color: rgba(0, 0, 0, 1)">APP_ID
                  </span>+<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">&amp;redirect_uri=</span><span style="color: rgba(128, 0, 0, 1)">"</span>+<span style="color: rgba(0, 0, 0, 1)">HOST
                  </span>+<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">%2FwechatKefu%3Fent_id%3D</span><span style="color: rgba(128, 0, 0, 1)">"</span>+<span style="color: rgba(0, 0, 0, 1)">ENT_ID
                  </span>+<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">%26kefu_name%3D</span><span style="color: rgba(128, 0, 0, 1)">"</span>+<span style="color: rgba(0, 0, 0, 1)">KEFU_NAME
                  </span>+<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">&amp;response_type=code&amp;scope=snsapi_userinfo#wechat_redirect</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
                document.location.href</span>=url;</pre>
</div>
<p>&nbsp;</p>
<p>用户点击允许获取信息后,会带着code回跳到</p>
<p>http://127.0.0.1:8081/wechatKefu?ent_id=xxxx&amp;kefu_name=xxxxx&amp;<strong>code=xxxxxxx</strong></p>
<p>&nbsp;在页面中通过code获取微信openid和头像昵称</p>
<p>主要的逻辑代码如下,这里面的appId appSecret等信息需要使用自己的</p>
<p>引入的包</p>
<div class="cnblogs_code">
<pre>    <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">github.com/silenceper/wechat/v2</span><span style="color: rgba(128, 0, 0, 1)">"</span>
    <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">github.com/silenceper/wechat/v2/cache</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
    offConfig </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">github.com/silenceper/wechat/v2/officialaccount/config</span><span style="color: rgba(128, 0, 0, 1)">"</span>
    <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">github.com/silenceper/wechat/v2/officialaccount/oauth</span><span style="color: rgba(128, 0, 0, 1)">"</span></pre>
</div>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取微信用户信息</span>
func GetWechatUserInfo(weixinCode, entId <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">) (oauth.UserInfo, error) {
    </span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)"> userinfo oauth.UserInfo


    cfg :</span>= &amp;<span style="color: rgba(0, 0, 0, 1)">offConfig.Config{
      AppID:   APP_ID,
      AppSecret: AppSecret,
      Token:   Token,
      Cache: memory,
    }
    wc :</span>=<span style="color: rgba(0, 0, 0, 1)"> wechat.NewWechat()
    officialAccount :</span>=<span style="color: rgba(0, 0, 0, 1)"> wc.GetOfficialAccount(cfg)
    oauth :</span>=<span style="color: rgba(0, 0, 0, 1)"> officialAccount.GetOauth()
    accessToken, err :</span>=<span style="color: rgba(0, 0, 0, 1)"> oauth.GetUserAccessToken(weixinCode)
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> err !=<span style="color: rgba(0, 0, 0, 1)"> nil {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> userinfo, err
    }
    userinfo, err </span>= oauth.GetUserInfo(accessToken.AccessToken, accessToken.OpenID, <span style="color: rgba(128, 0, 0, 1)">""</span><span style="color: rgba(0, 0, 0, 1)">)
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> err !=<span style="color: rgba(0, 0, 0, 1)"> nil {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> userinfo, err
    }
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> userinfo, nil
}</span></pre>
</div>
<p>拿到openId ,查出来绑定的用户,生成好对应的token信息直接跳转到自己的后台</p>

</div>
<div id="MySignature" role="contentinfo">
    <h3>十年开发经验程序员,离职全心创业中,历时三年开发出的产品《唯一客服系统》</h3>
<div> 一款基于Golang+Vue开发的在线客服系统,软件著作权编号:2021SR1462600。一套可私有化部署的网站在线客服系统,编译后的二进制文件可直接使用无需搭开发环境,下载zip解压即可,仅依赖MySQL数据库,是一个开箱即用的全渠道在线客服系统,致力于帮助广大开发者/公司快速部署整合私有化客服功能。 </div>
<div> 开源地址:唯一客服(开源学习版) </div>
<div> 官网地址:唯一客服官网 </div><br><br>
来源:https://www.cnblogs.com/taoshihan/p/16506672.html
頁: [1]
查看完整版本: 【Golang】golang开发微信公众号网页授权功能