examtest_mp/pages/admin/login.js

128 lines
2.5 KiB
JavaScript

// pages/admin/login.js
const api = require("../../utils/request.js");
Page({
/**
* 页面的初始数据
*/
data: {
form:{
username:'',
password:''
}
},
usernameChange: function (e) {
this.data.form.username = e.detail.value
},
passwordChange: function (e) {
this.data.form.password = e.detail.value
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
if(options.type=='nologin'){
}else{
if(getApp().globalData.admintoken){
wx.redirectTo({
url: '/pages/admin/index',
})
}else{
this.mplogin()
}
}
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
},
denglu: function ( ) {
var form = this.data.form
api.requesta('/token/', 'POST', form).then(res => {
getApp().globalData.admintoken = res.data.token;
api.requesta('/rbac/user/info/', 'GET').then(res=>{
getApp().globalData.admininfo = res.data
wx.showToast({
title: '登录成功',
icon: 'none'
})
wx.redirectTo({
url: '/pages/admin/index',
})
})
})
},
mplogin: function(){
wx.showLoading({
title: '自动登陆中',
mask:true
})
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
api.requesta('/rbac/user/mplogin/','POST', {code:res.code}).then(res=>{
getApp().globalData.admintoken = res.data.token;
wx.setStorageSync('token', res.data.token)
wx.setStorageSync('session_key', res.data.session_key)
wx.setStorageSync('userinfo', res.data.userinfo)
api.requesta('/rbac/user/info/', 'GET').then(res=>{
wx.hideLoading()
getApp().globalData.admininfo = res.data
wx.redirectTo({
url: '/pages/admin/index',
})
})
})
}
})
}
})