沒有需求就沒有開發 需求在小程式内直接可以關注公衆号 且如果已經關注則隐藏關注按鈕;找了許多沒有特别完整的 索性就把自己的做法給整理一下 步驟大緻如下
1. 小程式關聯公衆号:
1.1 路徑:小程式背景——設定——關注公衆号

1.2 關聯以後就可以直接在小程式内使用“<official-account></official-account>”元件了 對應參考連結:https://developers.weixin.qq.com/miniprogram/dev/component/official-account.html
2. 擷取使用者是否關注公衆号辨別
2.1 實作:借用了小程式“web-view”元件來實作 注意需要先去小程式開發設定——業務域名 添加公衆号設定的網頁授權域名才可以通路;對應參考連結:https://developers.weixin.qq.com/miniprogram/dev/component/web-view.html
2.1.1 小程式設定跳轉路徑 擷取code:URL必須是上面設定好的域名下的路徑
<web-view src="https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=URL&response_type=code&scope=snsapi_base#wechat_redirect"></web-view>
2.1.2 通過code換取公衆号openid和網頁授權access_token (就是正常擷取公衆号openid流程 參考連結:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html)
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
2.1.3 擷取通用接口access_token (參考連結:https://developers.weixin.qq.com/doc/offiaccount/WeChat_Invoice/Nontax_Bill/API_list.html#1.1%20%E8%8E%B7%E5%8F%96access_token)注意:2.1.2擷取到的網頁授權access_token是無法用于後續請求的
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
2.1.4 通過通用接口access_token和公衆号openid擷取使用者基本資訊(即是否關注公衆号辨別)參考連結:https://developers.weixin.qq.com/doc/offiaccount/User_Management/Get_users_basic_information_UnionID.html
https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
建議将擷取到的公衆号openid與小程式openid對應關系 儲存進表;供後續調用
3. 利用web-view的JSSDK接口攜參跳轉回小程式
//subscribe即為傳回的是否關注公衆号辨別
<script>
wx.miniProgram.switchTab({url: '/pages/index/index?subscribe='+subscribe})
</script>