// pages/login/index.js import util from '../../utils/util' import http from '../../utils/http' const app = getApp() Page({ /** * 页面的初始数据 */ data: { mobile: '', password: '', // mobile|wechat|forget type: 'mobile', title: '手机号登录', name: '' }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { }, getUserInfo: function(e) { var that = this if(e.detail.errMsg == 'getUserInfo:ok') { // wx.checkSession({ // success () { // that.wechatLogin(e.detail) // }, // fail () { // session_key 已经失效,需要重新执行登录流程 wx.login({ success (res) { if(res.code) { that.wechatLogin(Object.assign({}, e.detail, {code: res.code})) } } }) //重新登录 // } // }) } }, wechatLogin: function(data) { http({ url: 'loginByWechat', data: data, loadTitle: '登录中', success: function(res) { if(res.code == 0) { app.loginCallback(res.data) } } }) }, switchType: function(e) { var type = e.currentTarget.dataset.type var title = '手机号登录' if(type == 'wechat') { title = '微信授权登录' } else if(type == 'forget') { title = '忘记密码' } this.setData({ title, type }) }, updateValue: function(e) { var name = e.currentTarget.dataset.name this.setData({ [name]: e.detail }) }, login: function() { if(!util.checkMobile(this.data.mobile)) { util.showError('手机号格式错误') return false; } if(!this.data.password) { util.showError('密码必填') return false; } http({ url: 'login', data: { phone: this.data.mobile, password: this.data.password }, loadTitle: '登录中', success: function(res) { if(res.code == 0) { app.loginCallback(res.data) } } }) }, reset: function() { if(!util.checkMobile(this.data.mobile)) { util.showError('手机号格式错误') return false; } if(!this.data.name) { util.showError('真实姓名必填') return false; } http({ url: 'reset', data: { phone: this.data.mobile, name: this.data.name }, loadTitle: '提交中', success: function(res) { if(res.code == 0) { wx.showToast({ title: '提交成功', }) } } }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })