1.建立測試號
直接打開鏈接,微信掃碼登錄即可測試號管理頁面 https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index ,如下圖所示
2.在測試號中的【網頁授權獲取用戶基本信息】中,設置回調地址IP(localhost,127.0.0.1均不能使用,請使用局域網IP或域名,測試號回調地址支持域名和ip,正式公眾號回調地址只支持域名。)
3.下載微信開發者工具,選擇公眾號網頁開發
4.填寫要測試的地址,修改rediect_uri回調地址, 訪問騰訊服務器url規則如下:
https://open.weixin.qq.com/connect/oauth2/authorize?appid=你當前測試號的appid&redirect_uri=你的接口路徑(記得uri碼)&response_type=code&scope=snsapi_userinfo#wechat_redirect
5.訪問以上地址的話,微信服務器會轉發到另一個地址,地址中包含code,可以通過code獲取到用戶的openId(openId是微信用戶唯一憑證)
例如:http://www.baidu.com/spic&code=071kLtzG0DosVc2TypxG0RpwzG0kLtzF&state=
http://第4步中填寫的接口路徑?code=微信服務器返回的code值&state=
注意,每次訪問第4步的地址,微信服務器都會返回不同的code,code只能使用一次,用完就失效了,每個微信用戶的不同code,都指向用戶唯一的oppenId
根據code獲取用戶openId的方法,可以前端來獲取,也可以通過后端來獲取,前端獲取的方法我之前的博客里有寫,這里不再敘述了~~
6.獲取到用戶的唯一憑證openId,然后可以根據openId來判斷用戶登錄和認證的狀態,然后根據業務執行不同的操作,如下圖所示
7.設置微信公眾號的自定義菜單
打開鏈接 https://mp.weixin.qq.com/debug/ ,選擇基礎支持,讓填寫測試號的appID和appsecret來獲取token,如圖所示,然后選擇自定義菜單功能,填入token和用戶自己寫好的菜單欄JSON,來檢驗菜單是否正確,操作如圖所示
{ "button": [ { "name": "查詢", "sub_button": [ { "type": "view", "name": "菜單目錄", "url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=XXXXXXXX&redirect_uri=XXXXXXXX&response_type=code&scope=snsapi_userinfo#wechat_redirect", "sub_button": [ ] }, { "type": "view", "name": "菜單目錄", "url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=XXXXXXXX&redirect_uri=XXXXXXXX&response_type=code&scope=snsapi_userinfo#wechat_redirect", "sub_button": [ ] }, { "type": "view", "name": "菜單目錄", "url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=XXXXXXXX&redirect_uri=XXXXXXXX&response_type=code&scope=snsapi_userinfo#connect_redirect=1#wechat_redirect", "sub_button": [ ] } ] }, { "type": "view", "name": "菜單目錄", "url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=XXXXXXXX&redirect_uri=XXXXXXXX&response_type=code&scope=snsapi_base#connect_redirect=1#wechat_redirect", "sub_button": [ ] }, { "type": "view", "name": "菜單目錄", "url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=XXXXXXXX&redirect_uri=XXXXXXXX&response_type=code&scope=snsapi_base#connect_redirect=1#wechat_redirect", "sub_button": [ ] } ] }
8.其他就是前端項目的搭建(我前端項目采用的Nuxt + Vant框架)、項目內部業務處理了~~
嗯,就醬~~