// pages/login/login.js import{ baseUrl }from '../../utils/util' import { imgUrl } from "../api/request" Page({ /** * 页面的初始数据 */ data: { imgUrl: imgUrl, agreementImg: '', agree: false, canIUse: wx.canIUse('button.open-type.getUserInfo') }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { this.setData({ agree: false }) this.setData({ agreementImg: imgUrl + 'login-allow.png' }) }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, agreementSuccess(e) { if (this.data.agree === true) { this.setData({ agree: false }) this.setData({ agreementImg: imgUrl + 'login-allow.png' }) } else { this.setData({ agree: true }) this.setData({ agreementImg: imgUrl + 'login-select-allow.png' }) } }, getUserProfile() { if (this.data.agree === false) { wx.showToast({ title: '请同意服务协议', icon: 'error', duration: 1500//持续的时间 }) return } wx.setStorageSync('nickname', ""); wx.setStorageSync('avatarUrl', ""); wx.getUserProfile({ desc: '获取用户信息', success: (ress) => { console.log(ress.userInfo); wx.setStorageSync('nickname', ress.userInfo.nickName); wx.setStorageSync('avatarUrl', ress.userInfo.avatarUrl); wx.login({ success(res) { if (res.code) { //发起网络请求 wx.request({ url: baseUrl + 'mini/login', method:'POST', data: { code: res.code, userInfo: ress.userInfo }, success(res) { wx.showToast({ title: '登陆成功', }) wx.setStorageSync("userId", res.data.data) wx.navigateBack({ delta: 0 }) } }) } else { console.log('登录失败!' + res.errMsg) } } }) }, fail(res) { console.log("获取用户信息失败", res) } }) }, showSettingToast: function (e) { wx.showModal({ title: '提示!', confirmText: '去设置', showCancel: false, content: e, success: function (res) { if (res.confirm) { wx.navigateTo({ // url: '../setting/setting', }) } } }) }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })