首页 > 其他分享 >前端微信登录获取code,userInfo,openid

前端微信登录获取code,userInfo,openid

时间:2022-09-20 10:02:03浏览次数:62  
标签:openid code res userInfo 微信 uni wx

getUser(e) {
                wx.getUserProfile({
                    desc: '用户完善会员资料',
                    success: res => {
                        let userInfo = res.userInfo;
                        uni.showLoading({
                            title: '登录中'
                        });
                        wx.login({
                            //成功放回
                            success: (res) => {
                                let code = res.code
                                console.log(code);
                                wx.request({
                                    url: `https://api.weixin.qq.com/sns/jscode2session?appid=123123123&
                                    secret=123123123213&js_code=${code}&grant_type=authorization_code`,
                                    success: (res) => {
                                        userInfo.openid = res.data.openid
                                        //存储用户信息
                                        wx.setStorage({
                                            key: "userinfo",
                                            data: JSON.stringify(userInfo)
                                        })
                                        //页面跳转, 回到首页
                                        wx.reLaunch({
                                            url: "/pages/index/index"
                                        })
                                        uni.hideLoading();
                                    }
                                })
                            }
                        })
                    },
                    fail: res => {
                        uni.$showMsg("您取消了登录授权!")
                    }
                })
            },

 

页面:

<button class="btn_log" open-type="getUserInfo" @click="getUser">
微信授权登录
</button>

!!!uni.$showMsg是uni-app的弹窗方法,这里我封装了一下,appId,secret这两个可以去小程序后端获取

 

标签:openid,code,res,userInfo,微信,uni,wx
From: https://www.cnblogs.com/zsbb/p/16710015.html

相关文章

  • 微信小程序picker组件使用示例
    示例:wxml页面:<pickername="picker_machine11"class="cybm_pic_1"range="{{categoryList}}"value="{{'categoryid'}}"range-key='name'bindchange="bindChangeCat......
  • 微信公众号如何上传附件文件
    说到微信公众号,相信大家都非常熟悉。微信公众号是开发者或商家在微信公众平台上申请的应用账号,该账号与QQ账号互通,平台上实现和特定群体的文字、图片、语音、视频的全方位......
  • [LeetCode] 954. Array of Doubled Pairs
    Givenanintegerarrayofevenlength arr,return true ifitispossibletoreorder arr suchthat arr[2*i+1]=2*arr[2*i] forevery 0<=i<le......
  • Codeforces Round #640 (Div. 4) C. K-th Not Divisible by n
    CodeforcesRound#640(Div.4)翻译岛田小雅C.K-thNotDivisiblebyn出题人MikeMirzayanov有两个正整数\(n\)和\(k\),输出第\(k\)个不能被\(n\)整除的正整......
  • AtCoder Beginner Contest 269
    比赛链接A模拟即可。点击查看代码#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;inta,b,c,d;signedmain(){ cin>>a>>b>>c>>d; cout<<(......
  • 在Linux环境下使用vscode配置C++调试环境
    在Linux环境下使用vscode配置C++调试环境序起因在课程CMU15445LAB0的编写以及debug过程中充斥着assert以及printf这种不优雅的debug方式,因此决定直接进行工业革命!使用......
  • python在vscode中的调试--
    如果你使用fromnumpyimport*调试时会把numpy库包含的包都给调试了,产生很多不必要的调试信息如果你只从库中导出你需要的包,会产生更加干净的调试信息比如说你只用了......
  • LeetCode448. Find All Numbers Disappeared in an Array
    题意n个数,统计1-n中未出现的数方法遍历和标记代码classSolution{public:vector<int>findDisappearedNumbers(vector<int>&nums){sort(nums.beg......
  • [Leetcode Weekly Contest]311
    链接:LeetCode[Leetcode]2413.最小偶倍数给你一个正整数n,返回2和n的最小公倍数(正整数)。根据题意,当n为奇数时,答案为2n,当n为偶数时,答案为n。classSolution......
  • Codeforces Round #819 D
    D.EdgeSplitn+2条边并且连通图就证明最多多3条边我们可以想到的是要是连成了环的话不如拆一条边给其他颜色所以我们一定要无环我们先跑一遍最小生成树确定成红色......