微信小程序之商城

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

});

},

});

},

最終效果圖如下:

微信小程序之商城

微信小程序之商城

需要代碼的請私信我


分享到:


相關文章: