首页 > 其他分享 >公众号登录获取才code和openid,线上调试

公众号登录获取才code和openid,线上调试

时间:2024-03-12 10:24:38浏览次数:25  
标签:openid code console log res var 调试

1.实现静默登录获取code,在method方法里面定义函数,在onLoad里面调用getCode方法

getCode() { // 静默授权
                console.log(111111111111)
                var local = 'http://kaifa4.xinglian.work/user/#/pages/login/index' //当前页面的地址
                var appid = 'wx573536af9afe2c60' //公众号里有自己查
                this.code = this.getUrlCode().code // 截取code    code只能用一次
                console.log('code111', this.code)
                // 判断地址栏参数有无code,如果没有code,页面地址就跳转到微信提供的获取code的链接
                if (this.code == null || this.code == '') {
                    console.log(111)
                    location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid +
                        "&redirect_uri=" +
                        encodeURIComponent(local) + "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect"
                    this.code = this.getUrlCode().code
                } else {
                    // 获取code后自己的业务逻辑
                    console.log(222)
                    this.getOpenid()
                }

            },
            getOpenid() {
                let data = {
                    code: this.code
                }
                openid(data).then(res => {
                    console.log(res.data)
                    if (res.msg == "success") {
                        this.form.mp_openid = res.data.mp_openid;
                        console.log('this.form.mp_openid', this.form.mp_openid)
                    }
                })
            },
            // 截取url中的code方法
            getUrlCode() {
                var url = location.search

                var theRequest = new Object()
                if (url.indexOf("?") != -1) {
                    var str = url.substr(1)
                    var strs = str.split("&")
                    for (var i = 0; i < strs.length; i++) {
                        theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1])
                    }
                }
                console.log(theRequest, '111')
                return theRequest
            },

2.需要注意:调试到线上调试才能获取code,或者配置外网内射到外网

在微信开发者工具运行

 输入路径

 

公众号打包和h5类似,配置域名点击发行

 

 

标签:openid,code,console,log,res,var,调试
From: https://www.cnblogs.com/hs20011205/p/18067713

相关文章

  • LeetCode - 高频SQL50题(基础版)部分题解(上)
    1581.进店却未进行过交易的顾客原题:https://leetcode.cn/problems/customer-who-visited-but-did-not-make-any-transactions/题意:有一些顾客可能光顾了购物中心但没有进行交易。请你编写一个解决方案,来查找这些顾客的ID(customer_id),以及他们只光顾不交易的次数(count_no_trans......
  • (C++)树状数组和线段树的VSCode Snippet
    学都学了,肯定要往snippet里塞好东西嘛{ //Placeyoursnippetsforcpphere.Eachsnippetisdefinedunderasnippetnameandhasaprefix,bodyand //description.Theprefixiswhatisusedtotriggerthesnippetandthebodywillbeexpandedandinserted.......
  • vscode-verilble
    参数名称说明默认值--column_limit目标行长度限制,用于指定格式化后的代码每行的最大字符数100--indentation_spaces每个缩进级别增加的空格数2--line_break_penalty每引入一行换行符的惩罚值2--over_column_limit_penalty超出列限制的基线惩罚值,超出此......
  • leetcode: 2129. 将标题首字母大写
    给你一个字符串 title ,它由单个空格连接一个或多个单词组成,每个单词都只包含英文字母。请你按以下规则将每个单词的首字母 大写 :如果单词的长度为 1 或者 2 ,所有字母变成小写。否则,将单词首字母大写,剩余字母变成小写。请你返回 大写后 的 title 。示例1:输入:ti......
  • vscode搭建Renesas开发环境,编译并下载调试
    0windows下安装环境安装pyocd,libusbpipinstallpyocdpipinstalllibusb1使用RASC创建工程(1)创建工程(2)选择CMake(3)创建demo例程,可以先选择NoRTOS,最后点击Finish,创建完成(4)适用于vscode的例程已经创建完成,可以在vscode中打开,进行代码编写2VSCode配置(1)编辑Config......
  • Toyota Programming Contest 2024#3(AtCoder Beginner Contest 344)
    C先预处理出三个数组能拼出的数,存放到map中。查询的时候只需要看这个数是否出现在map里即可。时间复杂度\(O(n^3\logv+Q\logv)\),\(n\leq100\),\(\logv\)是map的时间复杂度。#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongconstintN=3e......
  • leetcode2397. 被列覆盖的最多行数 回溯法/枝剪
    第一次手搓一个回溯法,超时后采用枝剪勉强通过classSolution{intmax=0;intnumSelect;publicintmaximumRows(int[][]matrix,intnumSelect){Set<String>stateSet=newHashSet<>();dfs(matrix,newboolean[matrix[0].length],0,numSele......
  • 运行golang测试无法读取环境变量[vscode]
    使用vscode运行golang测试,通常我们会发现无法读取到设置在系统的环境变量,其本质原因是使用vscode启动testing并不是常规的subshell,无法正常读取到系统的环境变量;解决方案:方案1:将环境变量配置在setting.json(适用于变量较少情况)"go.testEnvVars":{"NAME":"zimskyzeng",},......
  • Visual Studio Code 配置文件关联
    在编写Linux的.service文件的时候,我发现.service文件的本质是INI文件。然而VSCode却并没有使用INI格式进行语法高亮。于是我通过如下设置使VSCode在遇到.service文件时自动使用INI格式的语法高亮:打开设置,搜索:files.associations,并添加一个项目:ItemValue......
  • vscode+cmake开发,头文件显示找不到(转载)
    原文地址:https://blog.csdn.net/lizy_fish/article/details/106376080 vsc在cmake开发当中会遇到,编辑器的插件判断头文件包含路径有问题。但是实际上通过cmake编译是正常的。特别是qt+vscode开发的时候,这个问题会导致编辑器的智能提示完全没有或者有误(比如包含这个文件的容......