博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
小程序获取微信用户的openid
阅读量:4358 次
发布时间:2019-06-07

本文共 2228 字,大约阅读时间需要 7 分钟。

小程序获取微信用户的openid

//index.js//获取应用实例const app = getApp()Page({  globalData: {    appid: '11121221a89e0',//appid需自己提供    secret: '1212',//secret需自己提供,此处的  },  data: {    motto: '技术带来改变',    userInfo: {},    hasUserInfo: false,    canIUse: wx.canIUse('button.open-type.getUserInfo')  },  //事件处理函数  bindViewTap: function() {    wx.switchTab({      url: '../task/task'    })  },      onLoad: function () {    var that = this    var user = wx.getStorageSync('user') || {};    var userInfo = wx.getStorageSync('userInfo') || {};    if ((!user.openid || (user.expires_in || Date.now()) < (Date.now() + 600)) && (!userInfo.nickName)) {      wx.login({        success: function (res) {          if (res.code) {            wx.getUserInfo({              success: function (res) {                var objz = {};                objz.avatarUrl = res.userInfo.avatarUrl;                objz.nickName = res.userInfo.nickName;                console.log(objz);                wx.setStorageSync('userInfo', objz);//存储userInfo              }            });            var d = that.globalData;//这里存储了appid、secret、token串              var l = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + d.appid + '&secret=' + d.secret + '&js_code=' + res.code + '&grant_type=authorization_code';            wx.request({              url: l,              data: {},              method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT                // header: {}, // 设置请求的 header                success: function (res) {                var obj = {};                obj.openid = res.data.openid;                obj.expires_in = Date.now() + res.data.expires_in;                console.log(obj);                wx.setStorageSync('user', obj);//存储openid                }            });          } else {            console.log('获取用户登录态失败!' + res.errMsg)          }        }      });    }  },  getUserInfo: function(e) {    app.globalData.userInfo = e.detail.userInfo    this.setData({      userInfo: e.detail.userInfo,      hasUserInfo: true    })  },  LoadUserSub: function (res) {    var openid=res;    console.log(openid);  }})

app.js中的代码  

posted on
2019-04-03 14:17  阅读(
...) 评论(
...) 收藏

转载于:https://www.cnblogs.com/baker95935/p/10648504.html

你可能感兴趣的文章
POJ1258Agri-Net
查看>>
浅谈SPFA(队列优化的Bellman-Ford算法)
查看>>
python从文件读取数据
查看>>
c++11 中的注意事项
查看>>
The great pleasure in life is doing what people say you cannot do.
查看>>
C基础知识(11):错误处理
查看>>
探偵ガリレオ1
查看>>
J2EE修炼之四书五经[转自2004年程序员]
查看>>
[zz]LXC 网络配置实例(Redhat)
查看>>
Linux文件系统介绍
查看>>
[.net 面向对象程序设计深入](9).NET Core 跨平台开发环境搭建
查看>>
mysql 下 计算 两点 经纬度 之间的距离 含具体sql语句
查看>>
SpringMVC_中文乱码的配置 --跟海涛学SpringMVC(和自己在项目中的实际使用的对比)...
查看>>
apache使用总结
查看>>
getopt、getopt_long 简介
查看>>
Linux eject 命令
查看>>
Python 常用函数
查看>>
作为布尔表达式的时候会被解释器当做False的值
查看>>
linux 网络编程:客户端与服务器通过TCP协议相互通信 + UDP
查看>>
程序人生之我们的故事:十年如歌(9)
查看>>