赞
踩
准备工作:小程序和公众号需要绑定在同一开放平台下(这样会有unionid,用来验证是否为同一用户);公众号设置好白名单,网页授权域名(这个要和小程序的api访问域名一致)
步骤:1.准备一个跳转的html页面,放在用网页授权域名可以访问到的地址
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>公众号信息网页授权</title>
- <script src="*****/js/jquery-3.2.0.min.js"></script>
- <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
- <script type="text/javascript">
- let url = window.location.href;
- if(url.includes('code')) {
- $.ajax({
- data: {"code":request('code')},
- type : 'get',
- dataType : 'json',
- url : '接收code获取openid的请求链接',
- success : function(result) {
- }
- })
- if (wx.miniProgram) {
- wx.miniProgram.reLaunch({
- url: `跳转回的小程序页面`,
- success: function() {
- },
- fall: function() {
- }
- })
- }
-
- } else {
- window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=公众号的appid&redirect_uri=当前这个html的访问地址&response_type=code&scope=snsapi_userinfo(这是可以获取用户信息的code,不需要可以使用静默授权snsapi_base)&state=#wechat_redirect"
- }
- function request(paras){
- var url = window.location.href;
- var paraString = url.substring(url.indexOf("?")+1,url.length).split("&");
- var paraObj = {}
- for (i=0; j=paraString[i]; i++){
- paraObj[j.substring(0,j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=")+1,j.length);
- }
- var returnValue = paraObj[paras.toLowerCase()];
- if(typeof(returnValue)=="undefined"){
- return "";
- }else{
- return returnValue;
- }
- }
- </script>
-
- </head>
- <body>
-
- </body>
- </html>
2.写一个接收code获取openid的接口
- /**
- * 公众号授权回调,获取用户信息
- * @param code
- * @param state
- * @param req
- * @throws
- * @return void
- */
- @GetMapping("/savewxopenid")
- public void savewxopenid(String code, String state, HttpServletRequest req) throws Exception {
- String errMsg = "";
- //根据code获取token和openId
- System.out.println("code:"+code+",state:"+state);
- String url = WechatUtilsMP.getAccessTokenRequest(code);
- String result = HttpClientUtil.doGet(url);
- System.out.println("获取网页授权token:"+result);
- logger.info("获取网页授权token:"+result);
- if(StringUtil.isStringEmpty(result)){
- logger.error("获取网页授权token出错:"+result);
- return;
- }
- JSONObject resultJson = JSONObject.fromObject(result);
- String openId = "";
- String unionid = "";
- int subscribe = 0;
- if(resultJson.containsKey("openid")){
- openId = resultJson.getString("openid");
- }
- if(StringUtil.isStringEmpty(openId)){
- logger.error("获取openid出错:"+result);
- return;
- }
- //根据openid获取unionid
- String token = WechatUtilsMP.getAccessTokenRequest();
- String unionUrl = WechatUtilsMP.getUnionIdRequest(openId,token);
- String unionResult = HttpClientUtil.doGet(unionUrl);
- System.out.println("unionResult:"+unionResult);
- JSONObject unionResultJson = JSONObject.fromObject(unionResult);
- if(unionResultJson.containsKey("unionid")){
- unionid = unionResultJson.getString("unionid");
- }
- if(unionResultJson.containsKey("subscribe")){
- subscribe = unionResultJson.getInt("subscribe");
- }
- if(!StringUtil.isStringEmpty(unionid)){
- Member member = memberService.getMemberByUnionid(unionid);
- if(member != null){
- member.setWxOpenId(openId);
- member.setIsFocus((short)subscribe);
- memberService.modifyMember(member);
- }
- }
- }
3.小程序中新增一个web-view页面,url放html的地址
- <template>
- <view>
- <web-view src="html的访问地址"></web-view>
- </view>
- </template>
-
- <script>
- import { mapGetters } from 'vuex'
- import { mapActions } from 'vuex'
- export default {
- components: { },
- data() {
- return {
-
- }
- },
- mounted() {
-
- },
- computed: {
- ...mapGetters(['isAdmin','userinfo']),
- },
- onShow() {
- },
- onLoad(e) {
-
- },
- onBackPress() {
-
- },
- methods: {
- ...mapActions(['setUserInfo']),
- }
- }
- </script>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。