1、小程序授权登录

这里我直接复制代码:

login.js

const app = getApp()
Page({
data: {
//判断小程序的API,回调,参数,组件等是否在当前版本可用。
canIUse: wx.canIUse('button.open-type.getUserInfo'),
userinfoObj: {}
},
// 授权按钮
bindGetUserInfo: function (e) {
wx.getUserProfile({
desc: '业务需要',
lang: 'zh_CN',
success: res1 => {
// wx.setStorageSync('nickName', res1.userInfo.nickName) // 授权缓存用户昵称
// wx.setStorageSync('avatarUrl', res1.userInfo.avatarUrl) // 授权缓存用户头像
this.setData({
userinfoObj: {
...this.data.userinfoObj,
nickName: res1.userInfo.nickName,
avatarUrl: res1.userInfo.avatarUrl
}
})
wx.login({
success: res => {
this.getUserOpenId(res.code)
}
});
},
fail: function () {
wx.showModal({
title: '提示',
content: '您拒绝了授权,无法正常使用小程序 重新授权',
showCancel: false,
confirmText: '重新授权',
success: function (res) {
if (res.confirm) {
console.log('用户点击了"重新授权"')
}
}
})
}
})
},
// 通过code获取openid
getUserOpenId(code) {
let postData = {
code,
appid: app.globalData.appid,
parentUserid: wx.getStorageSync('parentUserid') || ''
}
app.requestData('xxx', {
...postData
}).then((res) => {
if (res.result === 'success') {
// wx.setStorageSync('sessionKey', res.data.sessionKey) // 缓存sessionKey
wx.setStorageSync('openid', res.data.wxId) // 缓存openid
this.setData({
userinfoObj: {
...this.data.userinfoObj,
openid: res.data.wxId
}
})
console.log('wxId', res.data.wxId)
this.insertUserInfo()
} else {
app.showTip(res.msg)
}
})
},
// 将 openid 头像、昵称 入库
insertUserInfo() {
app.requestData('xxx', {
nickName: this.data.userinfoObj.nickName,
openid: this.data.userinfoObj.openid,
avatarUrl: this.data.userinfoObj.avatarUrl,
parentUserid: wx.getStorageSync('parentUserid') || ''
}).then((res) => {
if (res.result === 'success') {
var value = wx.getStorageSync('urlWithArgs')
app.checkIslogin(function () {
if (value !== '') {
wx.redirectTo({
url: value,
})
} else {
wx.switchTab({
url: '../mine/index',
})
}
})
} else {
app.showTip(res.msg)
}
})
},
// 通过openid获取手机号
updateWinxinUserMobile() {},
// onLoad() {
// wx.showModal({
// title: '提示',
// content: wx.getStorageSync('parentUserid'),
// success (res) {
// if (res.confirm) {
// console.log('用户点击确定')
// } else if (res.cancel) {
// console.log('用户点击取消')
// }
// }
// })
// }
})

login.xml

<!--pages/login/index.wxml-->
<view wx:if="{{canIUse}}">
<view class='header'>
<image src='../../image/logo.jpg'></image>
</view> <view class='content'>
<view>申请获取以下权限</view>
<text>获得你的公开信息(昵称,头像等)</text>
</view> <button class='bottom' type='primary' bindtap="bindGetUserInfo">
微信一键登录
</button>
</view> <view wx:else>请升级微信版本</view>

2、众所周知,小程序进去之后不能立马弹出授权页面,要让用户可以浏览部分页面,这里就要监测用户是否有授权了

可以在app.js里写一个全局的函数

  onLaunch() {
this.checkIslogin()
},
// 检测用户是否已经授权登录过, 登录过就获取用户信息
checkIslogin(callback) {
const openid = wx.getStorageSync('openid')
const _this = this
if (!openid) {
this.globalData.isLogin = false
} else {
this.globalData.isLogin = true
this.requestData('xxx', {
openid: openid,
parentUserid: wx.getStorageSync('parentUserid') || ''
}).then((response) => {
if (response.result === 'success') {
_this.globalData.userInfo = response.data
console.log(response.data)
// if (!response.data.nickName) {
// _this.globalData.isLogin = false
// }
typeof callback == 'function' && callback()
} else {
_this.showTip(response.msg)
}
})
}
},

我这里是定义了一个全局变量 isLogin

在需要授权的页面写入

  onLoad() {
if (!app.globalData.isLogin) {
wx.navigateTo({
url: '/pages/login/index',
})
} else {
//this.queryuserdetail() // 用户详情
//this.queryCount() // 用户计数
}
},

参考链接:https://www.cnblogs.com/yxg2852/p/16281923.html

版权申明:内容来源网络,版权归原创者所有。除非无法确认,都会标明作者及出处,如有侵权,烦请告知,我们会立即删除并致歉!

微信扫一扫 关注公众号

最新文章

  1. C#串口通讯实例
  2. No.017:Letter Combinations of a Phone Number
  3. python之Excel操作
  4. java随笔 乱腾腾的 一些东西
  5. stl::search
  6. mysql的过程和Oracle的区别
  7. Android ADT离线更新办法
  8. DevExpress控件之RepositoryItemComboBox
  9. TCP传输协议使用
  10. asp.net 动态压缩、切割图片,并做缓存处理机制
  11. 计蒜客模拟赛D1T2 蒜头君的树:树上节点之间最短距离和
  12. Less命名空间
  13. (译)Web是如何工作的(2):客户端-服务器模型,以及Web应用程序的结构
  14. JDK环境部署
  15. SVM-sklearn
  16. angular笔记_2
  17. C++ 在继承中虚函数、纯虚函数、普通函数,三者的区别
  18. VKD224B触摸芯片调试笔记
  19. hdu2089-不要62-(数位dp)
  20. netstat统计的tcp连接数与⁄proc⁄pid⁄fd下socket类型fd数量不一致的分析

热门文章

  1. sudo漏洞解决方案--源码转rpm包(spec文件编写)
  2. ABAP 拆批拣货交货bapi以及实例
  3. PHP 发送application/json POST请求
  4. vue移动端禁止弹层穿透、点击元素滚动到视图中心杂文日志
  5. SVN 之切换账号
  6. layui富文本编辑器提交时无法获取到值
  7. Unity 使用IO流获取PNG/JPG/GIF/BMP的宽高【转】
  8. Server2008通过bat命令自动定时备份MySQL数据库
  9. 【Frida】打印方法的调用堆栈
  10. 禁止Edge升级