// pages/user/user.js const app = getApp() var host = app.globalData.host; const $api = require('../../utils/api.js').API; Page({ /** * 页面的初始数据 */ data: { usercode: '', openid: '', info: {}, userinfo:{} }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.getUserData() }, getUserData() { $api.getAuthinfo().then(res=>{ wx.setNavigationBarTitle({ title: '', }) this.setData({ usercode: res.data.data.usercode, userinfo: res.data.data }) if (res.data.data.need_fill){ wx.navigateTo({ url: '../style/style', }) } }) .catch(err=>{ this.setData({ userinfo: {} }) }) }, inputchange(e) { this.setData({ usercode: e.detail.value }) }, logout() { wx.removeStorage({ key: 'userInfo' }) wx.setNavigationBarTitle({ title: '登录', }) this.setData({ userinfo: {}, usercode: '' }) }, /**登陆 */ login: function () { var _this=this; if (!this.data.usercode) { wx.showToast({ icon: 'none', title: '请输入用户代码', }) return; } wx.getUserProfile({ desc: '用于完善会员资料', success: res=> { //获取基本信息 var data = { nickName: res.userInfo.nickName, avatarUrl: res.userInfo.avatarUrl, usercode: this.data.usercode } wx.showLoading({ title: '正在登陆', }) wx.login({ success(res) { //获取code $api.getOpenid({ code: res.code}) .then(res => { //获取openid if (res.data.code != 0) { wx.showToast({ icon: 'none', title: res.data.message, }) wx.hideLoading(); return; } data.openid = res.data.data.openid; $api.login(data) .then(res=>{ //登录成功 wx.hideLoading(); wx.setNavigationBarTitle({ title: '', }) if (res.data.code != 0) { wx.showToast({ icon: 'none', title: res.data.message, }) return; } wx.setStorage({ key: 'userInfo', data: res.data.data, }) _this.getUserData() }) .catch(err=>{ wx.hideLoading(); }) }) .catch(err => { //请求失败 wx.hideLoading(); }) } }) } }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })