1、新接入微信小程序需要进行用户手动授权操作(弹框),否则是无法获取用户信息的,参照官网Api,接下来我们就来聊聊关于微信小程序的商城?以下内容大家不妨参考一二希望能帮到您!
微信小程序的商城
1、新接入微信小程序需要进行用户手动授权操作(弹框),否则是无法获取用户信息的,参照官网Api
具体实现如下(新建Dialog/index.js 第一次加载该页面,用户必须授权才能进入小程序):
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
let that=this;
wx.login({
success: res => {
app.globalData.code=res.code;
console.log(app.globalData.code);
// 取出本地存储用户信息,解决需要每次进入小程序弹框获取用户信息
app.globalData.userInfo = wx.getStorageSync('userInfo')
// wx.getuserinfo接口不再支持
wx.getSetting({
success: (res) => {
// 判断用户已经授权。不需要弹框
if (!res.authSetting['scope.userInfo']) {
that.setData({
showModel: true
})
} else {// 没有授权需要弹框
that.setData({
showModel: false
})
wx.showLoading({
title: '加载中...'
})
that.getOP(app.globalData.userInfo)
}
},
fail: function () {
wx.showToast({
title: '系统提示:网络错误',
icon: 'warn',
duration: 1500,
})
}
})
},
fail: function () {
wx.showToast({
title: '系统提示:网络错误',
icon: 'warn',
duration: 1500,
})
}
})
},
// 获取用户信息新接口
agreeGetUser: function (e) {
// 设置用户信息本地存储
console.log('eeeeee' e.detail.userInfo)
if (e.detail.userInfo == undefined){
wx.hideLoading()
wx.showToast({
title: '没有授权,不能进入小程序',
icon: 'none',
duration: 2000
})
}else{
try {
console.log('agree---' e.detail.userInfo)
wx.setStorageSync('userInfo', e.detail.userInfo)
} catch (e) {
wx.showToast({
title: '系统提示:网络错误',
icon: 'warn',
duration: 1500,
})
}
wx.showLoading({
title: '加载中...'
})
let that = this
that.setData({
showModel: false
})
that.getOP(e.detail.userInfo)
}
},
getOP: function (res) {// 提交用户信息 获取用户id
let that = this
let userInfo = res
app.globalData.userInfo = userInfo
app.getUserSessionKey(app.globalData.code);
},
//App.js代码如下
getUserSessionKey:function(code){
//用户的订单状态
var that = this;
wx.request({
url: that.d.ceshiUrl '/Api/Login/getsessionkey',
method:'post',
data: {
code: code
},
header: {
'Content-Type': 'application/x-www-form-urlencoded'
},
success: function (res) {
//--init data
var data = res.data;
console.log('ssssss' data);
if(data.status==0){
wx.showToast({
title: data.err,
duration: 2000
});
return false;
}
that.globalData.userInfo['sessionId'] = data.session_key;
that.globalData.userInfo['openid'] = data.openid;
console.log(data.openid '----------openid-----');
that.onLoginUser();
},
fail:function(e){
console.log(e)
wx.showToast({
title: '网络异常!err:getsessionkeys',
duration: 2000
});
},
});
},
onLoginUser:function(){
var that = this;
var user = that.globalData.userInfo;
console.log('ttt' user.sessionId);
wx.request({
url: that.d.ceshiUrl '/Api/Login/authlogin',
method: 'post',
data: {
SessionId: user.sessionId,
gender: user.gender,
NickName: user.nickName,
HeadUrl: user.avatarUrl,
openid: user.openid
},
header: {
'Content-Type': 'application/x-www-form-urlencoded'
},
success: function (res) {
//--init data
var data=res.data.arr;
console.log('data----' res.data.arr)
var status = res.data.status;
if (status != 1) {
wx.showToast({
title: res.data.err,
duration: 3000
});
return false;
}
that.globalData.userInfo['id'] = data.ID;
that.globalData.userInfo['NickName'] = data.NickName;
that.globalData.userInfo['HeadUrl'] = data.HeadUrl;
var userId = data.ID;
console.log('------' userId)
if (!userId) {
wx.showToast({
title: '登录失败!',
duration: 3000
});
return false;
}
that.d.userId = userId;
console.log('ttttt')
wx.switchTab({
url: '/pages/index/index',
})
},
fail: function (e) {
console.log(e)
wx.showToast({
title: '网络异常!err:authlogin',
duration: 2000
});
},
});
},
最终效果图如下:
需要代码的请私信我