// pages/login/login.js const app = getApp() const util = require("../../utils/util") Page({ /** * 页面的初始数据 */ data: { isPage: '', isId: '', imageUrl: app.globalData.imageUrl, }, update: function () { const updateManager = wx.getUpdateManager() const that = this updateManager.onCheckForUpdate(function (res) { // 请求完新版本信息的回调 if(res.hasUpdate){//发现新版本,提示更新 updateManager.onUpdateReady(function () { wx.showModal({ title: '更新提示', content: '发现新版本,请重新启动小程序', showCancel: false, success(res) { if (res.confirm) { // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启 updateManager.applyUpdate() } } }) }) } else {//没有新版本,直接进行下一步获取相关参数 that.getVersion() } }) updateManager.onUpdateReady(function () { wx.showModal({ title: '更新提示', content: '新版本已经准备好,是否重启应用?', success(res) { if (res.confirm) { // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启 updateManager.applyUpdate() } } }) }) updateManager.onUpdateFailed(function () { // 新版本下载失败 wx.showModal({ title: '提示', content: '更新失败,请检查网络或者重新启动小程序', showCancel: false, success(res){ if(res.confirm){ updateManager.applyUpdate() } } }) }) }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { wx.showLoading({ title: '加载中', mask: true }) if (options.isPage) { this.setData({ isPage: options.isPage, isId: options.isId }) } this.update() }, //判断版本号 getVersion: function () { util.doPost( 'getVersion', { appId: app.globalData.appId } ).then(res => { if (res.version != app.globalData.ver) { wx.setStorageSync('isShow', false) } else { wx.setStorageSync('isShow', true) this.getOpenId() } }) }, // 登录 getOpenId: function () { wx.login({ success: res => { util.doPost( 'getOpenId', { appId: app.globalData.appId, code: res.code }, ).then(res => { if (res.success == 1) { wx.setStorageSync('openId', res.data.openId) } }) }, fail: res => { wx.hideLoading({ success: (res) => { wx.showToast({ title: '提示', icon: 'none', content: '连接失败,请重试' }) }, }) } }) }, //授权手机号 getPhoneNumber: function (e) { const that = this; if (e.detail.errMsg == "getPhoneNumber:ok") { wx.showLoading({ title: '正在登陆', mask: true }) util.doPost( 'getPhoneNumber', { encryptedData: e.detail.encryptedData, iv: e.detail.iv, openId: wx.getStorageSync('openId'), } ).then(rs => { if (rs.success > 0) { wx.setStorageSync('phone', rs.data.phoneNumber); // wx.setStorageSync('phone', '13871019618') this.autoLogin(); } else { wx.showToast({ title: '手机号验证失败', }) } }) } that.setData({ model: false, localtion: true }) }, autoLogin: function () { util.doPost( 'autoLogin', { openId: wx.getStorageSync('openId'), } ).then(rs => { wx.hideLoading() if (rs.success > 0) { wx.setStorageSync('sso-token', rs.data.token); wx.showToast({ title: '登录成功', }) if (this.data.isPage) { wx.redirectTo({ url: '/pages/' + this.data.isPage + "/" + this.data.isPage + "?proId=" + this.data.isId, }) } else { wx.switchTab({ url: '/pages/index/index' }) } } else { wx.showToast({ title: rs.errMsg, icon: 'none', duration: 3000 }) } }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })