首頁先判斷是否登陸數據庫
index.wxmljson
<!-- 判斷是否登陸 --> <modal class="modal" hidden="{{is_login}}" no-cancel bindconfirm="close" confirmText=" "> <view class="dew"> <image src='/images/login.png' /> <button class='bottom' type='primary' open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo"> 請您受權登陸,不然沒法正常操做 </button> </view> </modal>
index.js小程序
Page({ /** * 頁面的初始數據 */ data: { //判斷小程序的API,回調,參數,組件等是否在當前版本可用。 canIUse: wx.canIUse('button.open-type.getUserInfo'), is_login:true }, /** * 生命週期函數--監聽頁面加載 */ onLoad: function (options) {// 查看是否受權 wx.getSetting({ success: function (res) { if (!res.authSetting['scope.userInfo']) { that.setData({ is_login: false }) } } }) }, //執行登錄 bindGetUserInfo: function (e) { if (e.detail.userInfo) { //用戶按了容許受權按鈕 var that = this; //插入登陸的用戶的相關信息到數據庫 var openid = getApp().globalData.openid; getApp().globalData.userInfo = e.detail.userInfo; // console.log(getApp().globalData.userInfo) wx.request({ url: api.wx.UserLogin, data: { userinfo: e.detail.userInfo, openid: openid }, header: { 'content-type': 'application/json' // 默認值 }, method: 'post', success(res) { if (res.data.result == 1) { wx.switchTab({ url: '/pages/index/index' }) that.setData({ is_login: true }) } else { console.log("寫入失敗") } } }) //受權成功後,跳轉進入小程序首頁 } else { //用戶按了拒絕按鈕 wx.showModal({ title: '警告', content: '您點擊了拒絕受權,將沒法進入小程序,請受權以後再進入!!!', showCancel: false, confirmText: '返回受權', success: function (res) { if (res.confirm) { console.log('用戶點擊了「返回受權」') } } }) } }, })
app.jsapi
onLaunch: function () { var that = this; wx.login({ success: res => { wx.request({ url: api.wx.jsCode + '?jsCode=' + res.code, data: { }, success: res => { var obj = {}; obj.openid = res.data.openid; ... wx.setStorageSync('user', obj); //存儲openid } }) } }); },
獲取用戶手機號:session
index.wxmlapp
<modal class="modal" hidden="{{is_login}}" no-cancel bindconfirm="close" confirmText=" ">
<view class="dew">
<image src='/images/login.png' />
<button class='bottom' type='primary' open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">
請您受權登陸,不然沒法正常操做
</button>
</view>
</modal>
index.js函數
/** * 生命週期函數--監聽頁面加載 */ onLoad: function (options) { var that = this; ... //是否登陸 if (!fzuser){ that.setData({ is_login: false }) } }, //獲取用戶手機號 getPhoneNumber: function (e) { var that = this; console.log(e) if (e.detail.errMsg == 'getPhoneNumber:fail user deny' || e.detail.encryptedData == null || e.detail.iv == null) { wx.showModal({ title: '提示', showCancel: false, content: '未受權,您不能使用服務!', success: function (res) { console.log(res) } }) } else { ... wx.request({ url:api.wx.UserLogin, data: { encryptedData: e.detail.encryptedData, sessionKey: user.session_key, iv: e.detail.iv, openid: openid, version: api.wx.version, }, method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT success: function (res) { console.log(res); ... that.setData({ is_login: true }) } }); } }, /** * 生命週期函數--監聽頁面顯示 */ onShow: function () { var that = this; ...
//驗證登陸是否過時 wx.checkSession({ success() { // session_key 未過時,而且在本生命週期一直有效 console.log('登陸未過時') }, fail() { // session_key 已經失效,須要從新執行登陸流程 // wx.login() // 從新登陸 that.setData({ is_login: false }) } }) },