首页 > 百科知识 正文

小程序自动授权登录(小程序登录授权整个流程源码)

时间:2023-11-20 19:36:05 阅读:927 作者:白茶熙

微信插件后台需要开启自动登录模式

小程序自动授权登录(小程序登录授权整个流程源码)-第1张

授权登录代码示例:

wx.login({ success: function (res) { console.log(res); if (res.code) { wx.getUserInfo({ success: function (userRes) { //发起网络请求D wx.request({ url: "http://www.xr.com/index.php?appid=11111&appsecret=11111111&s=weixin&c=member&m=xcx", data: { json: userRes.rawData, js_code: res.code }, method: 'post', header: { 'Content-Type': 'application/x-www-form-urlencoded', }, success: function (res) { console.log(res.data); if (res.data.code) { if (res.data.msg == 'login') { // 登录成功 console.log("登录成功了"); wx.setStorageSync('member_uid', res.data.data.member.id); wx.setStorageSync('member_auth', res.data.data.auth); wx.setStorageSync('member', res.data.data.member); wx.showToast({ title: "登录成功", icon: 'success', success: function () { // 跳转新页面 //wx.reLaunch({ url: "../member/index" }); } }) } else { // 绑定账号注册 wx.showModal({ showCancel: false, content: "迅睿cms后台微信插件中需要开启直接登录模式" }) } } else { // 失败了 wx.showModal({ showCancel: false, content: res.data.msg }) } } }); } }); } else { console.log('登录失败:' res.errMsg) } } });

把上面代码放到小程序页面js里面

/** * 生命周期函数--监听页面加载 */ onLoad: function (options) { // 登录代码开始 var member = wx.getStorageSync('member'); //console.log(member); if (member == "") { // 未登录跳转登录界面,这里写登录代码 } // 登录代码结束 },

版权声明:该问答观点仅代表作者本人。如有侵犯您版权权利请告知 cpumjj@hotmail.com,我们将尽快删除相关内容。