当前位置:   article > 正文

微信小程序中获取微信公众号授权获得公众号的openId(此方式仅适用于短时间的活动,长期项目还是需要维护基础表)_小程序获取公众号openid

小程序获取公众号openid

准备工作:小程序和公众号需要绑定在同一开放平台下(这样会有unionid,用来验证是否为同一用户);公众号设置好白名单,网页授权域名(这个要和小程序的api访问域名一致)

步骤:1.准备一个跳转的html页面,放在用网页授权域名可以访问到的地址

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>公众号信息网页授权</title>
  6. <script src="*****/js/jquery-3.2.0.min.js"></script>
  7. <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
  8. <script type="text/javascript">
  9. let url = window.location.href;
  10. if(url.includes('code')) {
  11. $.ajax({
  12. data: {"code":request('code')},
  13. type : 'get',
  14. dataType : 'json',
  15. url : '接收code获取openid的请求链接',
  16. success : function(result) {
  17. }
  18. })
  19. if (wx.miniProgram) {
  20. wx.miniProgram.reLaunch({
  21. url: `跳转回的小程序页面`,
  22. success: function() {
  23. },
  24. fall: function() {
  25. }
  26. })
  27. }
  28. } else {
  29. 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"
  30. }
  31. function request(paras){
  32. var url = window.location.href;
  33. var paraString = url.substring(url.indexOf("?")+1,url.length).split("&");
  34. var paraObj = {}
  35. for (i=0; j=paraString[i]; i++){
  36. paraObj[j.substring(0,j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=")+1,j.length);
  37. }
  38. var returnValue = paraObj[paras.toLowerCase()];
  39. if(typeof(returnValue)=="undefined"){
  40. return "";
  41. }else{
  42. return returnValue;
  43. }
  44. }
  45. </script>
  46. </head>
  47. <body>
  48. </body>
  49. </html>

           2.写一个接收code获取openid的接口

  1. /**
  2. * 公众号授权回调,获取用户信息
  3. * @param code
  4. * @param state
  5. * @param req
  6. * @throws
  7. * @return void
  8. */
  9. @GetMapping("/savewxopenid")
  10. public void savewxopenid(String code, String state, HttpServletRequest req) throws Exception {
  11. String errMsg = "";
  12. //根据code获取token和openId
  13. System.out.println("code:"+code+",state:"+state);
  14. String url = WechatUtilsMP.getAccessTokenRequest(code);
  15. String result = HttpClientUtil.doGet(url);
  16. System.out.println("获取网页授权token:"+result);
  17. logger.info("获取网页授权token:"+result);
  18. if(StringUtil.isStringEmpty(result)){
  19. logger.error("获取网页授权token出错:"+result);
  20. return;
  21. }
  22. JSONObject resultJson = JSONObject.fromObject(result);
  23. String openId = "";
  24. String unionid = "";
  25. int subscribe = 0;
  26. if(resultJson.containsKey("openid")){
  27. openId = resultJson.getString("openid");
  28. }
  29. if(StringUtil.isStringEmpty(openId)){
  30. logger.error("获取openid出错:"+result);
  31. return;
  32. }
  33. //根据openid获取unionid
  34. String token = WechatUtilsMP.getAccessTokenRequest();
  35. String unionUrl = WechatUtilsMP.getUnionIdRequest(openId,token);
  36. String unionResult = HttpClientUtil.doGet(unionUrl);
  37. System.out.println("unionResult:"+unionResult);
  38. JSONObject unionResultJson = JSONObject.fromObject(unionResult);
  39. if(unionResultJson.containsKey("unionid")){
  40. unionid = unionResultJson.getString("unionid");
  41. }
  42. if(unionResultJson.containsKey("subscribe")){
  43. subscribe = unionResultJson.getInt("subscribe");
  44. }
  45. if(!StringUtil.isStringEmpty(unionid)){
  46. Member member = memberService.getMemberByUnionid(unionid);
  47. if(member != null){
  48. member.setWxOpenId(openId);
  49. member.setIsFocus((short)subscribe);
  50. memberService.modifyMember(member);
  51. }
  52. }
  53. }

           3.小程序中新增一个web-view页面,url放html的地址

            

  1. <template>
  2. <view>
  3. <web-view src="html的访问地址"></web-view>
  4. </view>
  5. </template>
  6. <script>
  7. import { mapGetters } from 'vuex'
  8. import { mapActions } from 'vuex'
  9. export default {
  10. components: { },
  11. data() {
  12. return {
  13. }
  14. },
  15. mounted() {
  16. },
  17. computed: {
  18. ...mapGetters(['isAdmin','userinfo']),
  19. },
  20. onShow() {
  21. },
  22. onLoad(e) {
  23. },
  24. onBackPress() {
  25. },
  26. methods: {
  27. ...mapActions(['setUserInfo']),
  28. }
  29. }
  30. </script>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/93239?site
推荐阅读
相关标签
  

闽ICP备14008679号