-
2022-03-24 13:26:26
以前wx.getUserInfo可以获得 微信唯一认证ID(openID),微信信息(头像,微信名字,注册程序),后来一些原因,官方改成:
wx.getUserInfo获得:通过后台解密获得openID,不需要通过弹框
getUserProfile获得: 微信信息(头像,微信名字,注册程序),必须要通过弹框
//2、调用获取用户信息接口 wx.getUserInfo({ success: function (res) { console.log("到这里来了吗") console.log(res.userInfo) console.log(res.encryptedData) console.log(res.iv) wx.setStorageSync('dl_encryptedData', res.encryptedData) wx.setStorageSync('dl_iv', res.iv) getApp().globalData.userInfo = res.userInfo //3.请求自己的服务器,解密用户信息 获取unionId等加密信息 wx.request({ url: 'https://app.301.cn/601_dj/ussmx/ProcessRequest',//自己的服务接口地址 method: 'post', header: { 'content-type': 'application/x-www-form-urlencoded' }, data: { encryptedData: res.encryptedData, iv: res.iv, code: code }, success: function (data) { console.log('开始存储') console.log(data.data) console.log('看看工号的值') console.log(data.data[0].gonghao) wx.setStorageSync('openId', data.data[0].OpenId) wx.setStorageSync('nickName', getApp().globalData.userInfo.nickName) wx.setStorageSync('avatarUrl',getApp().globalData.userInfo.AvatarUrl) wx.setStorageSync('gonghao', data.data[0].gonghao) wx.setStorageSync('sjh', data.data[0].sjh) wx.setStorageSync('bmid', data.data[0].bmid) }, fail: function () { console.log('系统错误') } }) } })
<button bindtap="getUserProfile"> 获取头像昵称 </button> wx.getUserProfile({ desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写 success: (res) => { this.setData({ userInfo: res.userInfo, hasUserInfo: true }) } })
个人在实际应用中wx.getUserInfo一般必然用到,特别给企业做外围应用的时候,企业一般有自己的人员数据表,这时候只要通过第一次验证,绑定openid就能完成“下次登录记忆,知道你是谁”,即使没有自己的人员信息表,要保存用户,也需要调用wx.getUserInfo来获得openid.
getUserProfile获得微信用户名,头像在一些用“微信名注册会员”的场景中可能用到,它会弹出弹框,而且用户还有可能选择否,不是很友好,为了不每次都弹框,需要把获得的信息保存下来(但是这里用户更换了头像,是无法用原来保存的路径显示的),这种情况,个人更加喜欢填充一个用户名,头像,用户自己可以完善修改,不太喜欢用getUserProfile。
更多相关内容 -
微信小程序 获取用户信息(wx.getUserInfo)调整
2021-03-29 17:35:30在4月15号的时候,微信以提升用户体验,更改了获取用户信息的方式 这是4月15号的第一次公告 ...(此图片来源于网络,如有侵权,请联系删除! ) 在这次公告里面,获取微信用户信息只能通过button和open-data来获取了, ... -
微信小程序wx.getUserInfo授权获取用户信息(头像、昵称)的实现
2020-10-14 21:38:45主要介绍了微信小程序wx.getUserInfo授权获取用户信息(头像、昵称)的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 -
微信小程序获取用户信息的两种方法wx.getUserInfo与open-data实例分析
2020-10-17 02:03:53主要介绍了微信小程序获取用户信息的两种方法wx.getUserInfo与open-data,结合实例形式分析了wx.getUserInfo与open-data获取用户信息的相关操作技巧与使用注意事项,需要的朋友可以参考下 -
微信小程序 wx.getUserInfo引导用户授权问题实例分析
2020-10-15 13:16:19主要介绍了微信小程序 wx.getUserInfo引导用户授权问题,结合实例形式分析了微信小程序使用wx.getUserInfo引导用户授权问题的具体操作步骤与实现方法,需要的朋友可以参考下 -
小程序接口升级:wx.getUserinfo 替换 wx.getUserProfile遇到问题
2021-08-05 13:19:46**小程序wx.getUserInfo更新wx.getUserProfile ** JS中的这段 不咋会弄 我的参考的代码例子是↓ getUserInfo() { let that = this; wx.getSetting({ success(res) { wx.showModal({ title: '温馨提示', content: '... -
把wx.getUserInfo返回的拼音省名市名转成汉字
2021-03-29 21:05:20wx.getUserInfo 返回的省、市是拼音的,前台给客户看是要汉字的,用网络接口会影响小程序的效率,弄了个函数 传入城市拼音,转出汉字 var province = [{ name: "beijing", cities: ["xicheng", "dongcheng", ... -
微信小程序回收wx.getUserInfo,更新wx.getUserProfile
2021-04-23 17:43:36微信一周前发布公告,将于2021年4月28日24时回收 wx.getUserInfo 接口可获取用户授权的个人信息能力. 公告中关于开发相关的内容大概有两点: 1:wx.login获取的code将可以直接通过...微信一周前发布公告,将于2021年4月28日24时回收 wx.getUserInfo 接口可获取用户授权的个人信息能力.
公告中关于开发相关的内容大概有两点:
1:wx.login获取的code将可以直接通过https://api.weixin.qq.com/sns/jscode2session接口,获取到unoinId。对于多平台用户,这一步可以让老的用户获取数据时,节省掉不少服务器资源。
2:前版本下wx.getUserInfo将不可使用,而相关的数据使用wx.getUserProfile获取,而获取方式和流程不变。而2.10.4之前版本还需调用wx.getUserInfo.用户授权成功,返回参数:
简易判断方式如下:if (wx.getUserProfile){ wx.getUserProfile({ 。。。。。。。 }) } else { wx.getUserInfo({ 。。。。。。 }) }
其实与老版本的getUserInfo方法是相同的。
简记用户信息获取流程。
1:wx.login获取用户openId,unoinId.小程序内代码如下:wx.login({ success: res => { //获取code console.log('wx.login.res='&res); var vcode = res.code; //将code传到服务器进行解析,获取用户openId和unoinId wx.request({ url: "http://someserver.com/checkPlayer?code=" + vcode, data: {}, method: 'POST', header: app.globalData.httpHeader, success: e => { //如果用户openId在服务器端已有数据,将在此处返回用户数据,否则用户数据为openId和unoinId app.globalData.userInfo = e.data.data; that.pageload(); }, fail: ee => {}, complete: function() {} }) }, fail: function(e) {} });
2:服务器端通过code获取用户openId及unoinId.java代码public Result checkPlayer(String code) throws IOException { Result returnResult = new Result(); if (code == null || "".equalsIgnoreCase(code)){ returnResult.error(); returnResult.setMessage("code不可为空"); return returnResult; } JSONObject jo = getSessionKey(code); String openidTemp = (String)(jo.get("openid")); String sessionKey = (String)(jo.get("session_key")); if (StringUtils.isEmpty(openidTemp)){ returnResult.error(); returnResult.setReturn_code(101); returnResult.setMessage("code不可用"); return returnResult; } //在数据库中查找用户,此处各路大神各有神通不再详解 CsCustmer csCustmer = custmerService.findCustmerByOpenidTemp(openidTemp); if (csCustmer==null){ //返回用户openId及unoinId returnResult.success(); returnResult.setReturn_code(202); returnResult.setMessage("用户不存在"); returnResult.setData(jo); return returnResult; } else { //返回用户详情 returnResult.setData(csCustmer.getDetail()); returnResult.success(); return returnResult; } } public JSONObject getSessionKey(String code) { String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + WeChatConfig.APP_ID + "&secret=" + WeChatConfig.APP_SECRET + "&js_code=" + code + "&grant_type=authorization_code"; String result = HttpUtil.doGet(url, "utf-8"); logger.info("api/getSessionKey?code=" + code); logger.info("api/getSessionKey?url=" + url); logger.info("api/getSessionKey?result=" + result); JSONObject oppidObj = JSONObject.parseObject(result); return oppidObj; }
3:如果用户数据不存在,则需要让用户授权获取userInfo,调用getUserProfile方法,小程序调用方式如下:
if (wx.getUserProfile){ wx.getUserProfile({ desc:'正在获取',//不写不弹提示框 success: function(res) { console.log(res.userInfo); //将用户加密数据传到服务器进行解析,作保证,但单纯从前端的使用来看,此步可不再需要。 wx.request({ url: 'http://someserver.com/updateUserInfo', data: { openid: app.globalData.userInfo.openid, sessionKey: app.globalData.userInfo.session_key, encryptedData: res.encryptedData, iv: res.iv }, method: 'POST', success: e => { }, fail: ee => {}, complete: function() { } }) }, fail: function() { } }) }
官方流程图:
-
wx.getUserInfo替换成wx.getUserProfile注意事项
2021-09-14 00:13:58旧版的wx.getUserInfo已经无法获取用户的个人信息,比如名称、头像等,这里只能使用wx.getUserProfile 新的接口wx.getUserProfile只能用在点击事件中,例如catchtap或者bindtap进行调用(ps:可以再wx.showmodel中使用...下图是对于新的接口的官方文档详情
微信小程序开发文档wx.getUserProfile
旧版的wx.getUserInfo已经无法获取用户的个人信息,比如名称、头像等,这里只能使用wx.getUserProfile新的接口wx.getUserProfile只能用在点击事件中,例如catchtap或者bindtap进行调用(ps:可以再wx.showmodel中使用),并不能再onload、onshow等位置直接调用,貌似写在别的funciont中调用也不行。
在点击事件中,wx.getUserProfile与wx.login需同时调用才行,因此可以采用以下方法
function getUserProfile() { return new Promise(function(resolve, reject) { wx.getUserProfile({ desc: '用于完善会员资料', success: function(res) { resolve(res); }, fail: function(err) { reject(err); } }) }); }
function login() { return new Promise(function(resolve, reject) { wx.login({ success: function(res) { if (res.code) { //登录远程服务器 resolve(res); } else { reject(res); } }, fail: function(err) { reject(err); } }); }); }
getUserProfile: function() { let p1 = util.login() let p2 = util.getUserProfile() Promise.all([p1,p2]).then((res)=>{ let code = res[0].code; let userInfo = res[1]; console.log(res); util.request(api.AuthLoginByWeixin, { code: code, userInfo: userInfo }, 'POST').then(res => { if (res.errno === 0) { //存储用户信息 wx.setStorageSync('userInfo', res.data.userInfo); wx.setStorageSync('token', res.data.token); } wx.navigateBack(); }).catch((err) => { reject(err); }); }) },
参考来自
https://www.jb51.net/article/213990.htm -
小程序 正式 wx.getUserInfo 不起作用 wx.getUserProfile 解密获取不到 openId
2021-04-14 15:27:322021年4月13日后发布的新版本小程序,开发者通过组件调用wx.getUserInfo将不再弹出弹窗,直接返回匿名的用户个人信息,获取加密后的openID、unionID数据的能力不做调整;若开发者需要获取用户的个人信息(头像、昵称...最近微信小程序进行了调整
2021年4月13日后发布的新版本小程序,开发者通过组件调用wx.getUserInfo将不再弹出弹窗,直接返回匿名的用户个人信息,获取加密后的openID、unionID数据的能力不做调整;若开发者需要获取用户的个人信息(头像、昵称、性别与地区),可以通过wx.getUserProfile接口进行获取。微信公告
我这里的处理方法是使用之前的 getUserInfo (获取openID) 加 新的 wx.getUserProfile (获取头像、昵称、性别等信息)结合传给后台进行存储。
HTML
<view wx:if="{{pagestate == 1}}" class="but-box"> <!-- 未授权 --> <button bindtap="getUserInfo" bindgetuserinfo="onGotUserInfo" data-type="1" open-type='getUserInfo' lang="zh_CN" class="button button-dark">获取微信授权</button> <button bindtap="getUserInfo" bindgetuserinfo="onGotUserInfo" data-type="2" open-type='getUserInfo' lang="zh_CN" class="button button-blue">手机号验证码登录</button> </view> <view wx:else class="but-box"> <!-- 已授权 --> <button bindgetphonenumber="login" open-type='getPhoneNumber' lang="zh_CN" class="button button-dark ">微信一键登录</button> <button bindtap="toLogin" class="button button-blue">手机号验证码登录</button> </view>
JS
let userInfo = null; Page({ data: { pagestate: 1, // 获取用户信息授权状态,1未授权,2已授权 }, // 授权 获取用户头像昵称等信息 onGotUserInfo(e){ let that = this; if (typeof e.detail.userInfo !== 'undefined' && e.detail.userInfo) { // 记录微信授权返回的用户数据 this.getUserInfo1(()=>{}) } else { userinfo = null; this.setData({ pagestate: 1 }); // 授权失败 app.showToast('未同意授权!', () => { wx.switchTab({ url: '/pages/home/index' }) }) } }, getUserInfo(e){ let type = e.currentTarget.dataset.type; let that = this; wx.getUserProfile({ desc: '用于完善会员资料', success (res) { // console.log(96,res) // console.log(that) // 记录微信授权返回的用户数据 that.getUserInfoSuccess(res.userInfo); // userInfo = Object.assign({}, res.userInfo); // app.globalData.userInfo = userInfo; if(type == 2){ that.toLogin() } }, fail (res) { userInfo = null; that.setData({ pagestate: 1 }); },complete(res){ console.log(res,103) } }) }, // 获取用户信息成功处理 getUserInfoSuccess(userInfoData) { console.log(userInfoData) userinfo = Object.assign({}, userInfoData); app.globalData.userInfo = userinfo; wx.setStorageSync('userInfo', userinfo); // wx-登录 wx.login({ success: (res) => { if (res.code) { this.setData({ pagestate: 2, code: res.code }) } else { wx.showToast({ title: '服务器异常', icon: 'none', duration: 1500 }) } }, complete: (res) => { }, }) }, // 登录 wxLogin(callback){ wx.login({ success: (res) => { if (res.code) { this.setData({ code: res.code }) } else { wx.showToast({ title: '服务器异常', icon: 'none', duration: 1500 }) } }, complete: (res) => { typeof callback === 'function' && callback(res) }, }) }, // 登录 login(e){ let that = this; let dat = e.detail; // wx-登录 // this.getUserInfo1(); if ("getPhoneNumber:ok" != e.detail.errMsg) { wx.showModal({ title: '提示', showCancel: false, content: '获取用户手机号未授权', success: function (res) { console.log('未授权'); } }) } else { this.getUserInfo1(()=>{ that.wxLogin((res)=>{ // console.log(res.code) wx.showLoading({ mask: true, title: '加载中...', }) clearTimeout(time) time = setTimeout(()=>{ // 注册/登录 app.request('api_shop/login/oneClickRegister', { code: res.code, // 微信登录 code u_iv: that.data.u_iv, // u_iv 用户 微信加密码 u_encrypted_data: that.data.u_encrypted_data, // u_encrypted_data 用户 微信加密码 iv: dat.iv, // 微信加密码 encrypted_data: dat.encryptedData, // 微信加密码 head_img: userinfo.avatarUrl, // 头像 nick_name: userinfo.nickName, // 微信名称 inviteId: that.data.inviteId, // 邀请码 }, (res) => { wx.hideLoading() if (res.rc == 200) { // 成功 wx.showToast({ title: res.des || '登录成功!', mask: true, duration: 2000 }) // 记录token及用户信息 app.globalData.token = res.data.token; app.globalData.user_data = res.data.user_data; app.globalData.cabinet_id = null; // 选择的网点 id app.globalData.cabinet_info = null; // 选择的网点 信息 wx.setStorageSync('userData', res.data.user_data); wx.setStorageSync('token', res.data.token); } else { // 失败 app.globalData.token = ''; wx.showToast({ title: res.des + '', icon: 'none', duration: 3600 }) } }, (res) => { // 失败 wx.hideLoading() app.globalData.token = ''; wx.showToast({ title: res.des, icon: 'none', duration: 1500 }) }, (res) => { // 请求结束 }) },800) }) }) } }, toLogin(){ console.log('手机号验证码登陆') }, })
END
-
wx.getUserInfo与wx.getUserProfile
2021-04-20 16:34:46wx.getUserInfo() 官方文档是这样写哒~ 参数: 返回值: -
wx.getUserProfile 和 wx.getUserInfo 的区别
2021-08-30 17:13:292021年4月份,微信小程序官方宣布阉割 wx.getUserInfo 的权限,不再返回真实的用户信息 主要的调整如下: 不再推荐使用 wx.getUserInfo 或 <button open-type="getUserInfo"/> 将不再弹出弹窗 不可获取用户... -
微信小程序wx.getUserInfo接口调整适配
2021-04-07 10:21:51近期微信小程序wx.getUserInfo()接口做了调整了,相信很多做小程序开发的都知道了,在微信小程序开发工具控制台,如果你没有调整过来的话会出现下的警告啦~ 有关小程序登录、用户信息相关接口调整说明,微信官方... -
小程序的wx.getUserInfo和wx.getUserProfile
2021-05-06 15:56:00wx.getUserInfo 开发中遇到getUserInfo一直获取不到用户信息,查询后了解到: 考虑到近期开发者对小程序登录、用户信息相关接口调整的相关反馈,为优化开发者调整接口的体验,回收wx.getUserInfo接口可获取用户授权... -
小程序开发之 wx.getUserInfo获取用户信息方案介绍
2020-03-20 01:09:07======================...小程序一个比较重要的能力就是获取用户信息,也就是使用wx.getUserInfo接口。我们发现几乎所有的小程序都会调用这个接口。虽然我们在设计文档上有提出最好的设计是在真正要用户信息的情况... -
wx.getUserInfo获取数据后解密
2021-12-15 16:12:18} return decoded } module.exports = WXBizDataCrypt 注意下载下来的示例中可能写的是new Buffer() 这个已经废弃了 见下图: node-Buffer官方文档 只不过现在应该不用这个方法了(上文中getUserInfo这个方法获取到... -
【小程序】wx.showLoading()、wx.getUserInfo()
2020-08-25 19:03:11wx.showLoading(Object object) 显示 loading 提示框。需主动调用 wx.hideLoading才能关闭提示框 参数 Object object (红色框为常用) 代码示例 wx.showLoading({ title: '加载中', mask: true }) setTimeout... -
记录wx.getUserInfo被回收后,小程序升级成wx.getUserProfile的旅程
2021-05-14 00:13:34升级背景 官方说明点这里 两个重点 getUserProfile接口从2.10.4...即wx.getUserInfo接口的返回参数不变,但开发者获取的userInfo为匿名信息(标红部分)。 分析小程序全局待修改之处 第一步:找出所有使用了wx -
微信小程序最新wx.getUserProfile接口获取用户信息和登陆信息的方法,迭代wx.getUserInfo方法。
2021-04-13 10:54:142021年4月13日后发布的小程序新版本,无法通过wx.getUserInfo与获取用户个人信息(头像、昵称、性别与地区), 将直接获取匿名数据(包括userInfo与encryptedData中的用户个人信息), 获取加密后的openID与unionID... -
javascript - 小程序wx.getUserInfo 中的 encryptData 解密
2021-04-21 09:11:58这个该怎么解,能提供个思路吗微信小程序文档中,用户登陆返回的数据,需要进行解密 解密算法如下接口如果涉及敏感数据(如wx.getUserInfo当中的 openid ),接口的明文内容将不包含敏感数据。开发者如需要获取敏感数据... -
关于微信wx.getUserInfo使用不了的问题
2021-04-17 19:17:56今日在学习《微信小程序开发实战》一书时,需获取用户的个人信息(头像昵称性别地区),使用getUserInfo方法???? getUserInfo:function(e){ console.log(e.detail.userInfo) if(e.detail.userInfo){ this.setData... -
小程序wx.getUserInfo不能弹出授权窗口的解决方案
2021-09-07 23:24:16问题:“getUserInfo” 不弹授权框,直接走成功流程 原因:小程序登录、用户信息相关接口调整 小程序登录、用户信息相关接口调整说明:... -
微信小程序 wx.getUserInfo接口弃用
2021-04-04 17:44:28现在获取的为 匿名数据 -
小程序wx.getUserInfo获取不到用户信息
2021-04-20 09:43:44可以直接用这个方法获取用户头像和昵称 ... let userInfo = wx.getStorageSync('userInfo'); if(!userInfo) { this.setData({ hasUserInfo: true, Datas: { talk_username: userInfo.nickName, tal. -
微信小程序wx.getUserInfo授权获取用户信息(头像、昵称)
2021-06-12 05:58:55这个接口只能获得一些非敏感信息,例如用户昵称,用户头像,经过用户授权...index.wxml{{nickName}}未授权授权登录index.jsPage({data: {canIUse: wx.canIUse('button.open-type.getUserInfo')},onLoad: function() ... -
小程序wx.getUserInfo获取不到头像
2021-04-09 16:27:01小程序wx.getUserInfo接口改版了,只能获取到用户的身份标识,如果需要获取头像昵称等信息需要调用wx.getUserProfile 小程序公告地址:点击这里