// pages/login.js
const app = getApp();
let md5 = require('../../../utils/zhxymd5.js'); //md5的js引用
var api = require('../../../config/api.js');
var util = require('../../../utils/util.js');
Page({
/**
* 页面的初始数据
*/
data: {
tip: '',
username: '',
password1: '',
data: '',
token: '',
checked:false
// phValue:'请输入账号'
},
handleAgree() {
if (!this.data.checked) {
wx.showToast({
icon: "none",
title: '请阅读并同意使用协议及隐私政策',
duration: 2000
})
return false
}
},
handleChecked(e){
let index = e.target.dataset.index
this.setData({
checked: !index
})
},
formSubmit: function(e) {
var that=this;
//判断账号不为空
if (app.wx_trim(e.detail.value.username).length == 0) {
wx.showToast({
title: '请输入账号',
icon: 'none',
duration: 1000
});
} else if (app.wx_trim(e.detail.value.password1).length == 0) {
wx.showToast({
title: '请输入密码',
icon: 'none',
duration: 1000
});
} else {
//md5加密密码
e.detail.value.password = md5.zhxy_encrypt(e.detail.value.password1);
//云平台登录后,获取传入的token方法
e.detail.value.redirect = "";
wx.showLoading({
title: '正在登录',
mask:true
})
that.doLogin(e.detail.value);
}
},
Reset: function() {
this.setData({
tip: '',
userName: '',
psw: ''
})
},
doLogin:function(account)
{
var that=this;
wx.request({
url: api.loginUrl,
method: "POST",
data: account,
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
success: function (res) {
var jsonText = res.data;
console.log(jsonText);
if (res.statusCode == "200") {
app.access_token = jsonText.access_token; //将token传给app,进行全局存储
console.log(jsonText.access_token);
//wx.setStorageSync("access_token", jsonText.access_token); //设置缓存token;
wx.setStorageSync("username", account.username);
wx.setStorageSync("password1", account.password1);
that.getUserinfoIdentity(jsonText.access_token);
} else if (res.statusCode == "400") {
wx.showToast({
title: '登录失败:' + res.data.error_description,
icon: 'none',
duration: 2000
});
} else {
//获取返回的Html,获取登陆失败原因
var htmlData = JSON.stringify(jsonText);
//因返回页面有可能出现填写验证码,所以将其过滤,使得用户下次登录,不需要进行验证码的验证就可以进入登录入口
htmlData = htmlData.replace('j_random', 'removeRandom');
var bigenNum = htmlData.indexOf('登录失败:用户名或密码不正确');
var randomNum = htmlData.indexOf('请输入验证码');
if (bigenNum > -1 || randomNum > -1) {
wx.showToast({
title: '用户名或密码不正确',
icon: 'none',
duration: 2000
});
} else {
//多身份提示登陆异常
wx.showToast({
title: '登录异常,请联系管理员',
icon: 'none',
duration: 2000
});
}
}
},
fail: function (res) {
wx.hideLoading();
wx.showToast({
title: '登录失败:' + res.errMsg,
icon: 'none',
duration: 2000
});
},
complete:function()
{
wx.hideLoading();
}
});
},
getUserinfoIdentity: function (access_token)
{
//获取个人身份(有可能是多身份)
wx.request({
url: api.getIdentityListUrl,
method: "POST",
data: { access_token: access_token },
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
success: function (res) {
if (res.statusCode == '200' && res.data.code == '0') {
var users = res.data.result;
for (var i = 0; i < users.length; i++) {
//删除没有登陆权限的用户、无效的、过滤掉非本学校的
if (users[i].idenType != "5" || users[i].idenStatus == "1") {
//删除该坐标下的一个元素,i--是因为,每次删除一位元素,后面元素会向前移动一位,这样防止索引错乱
users.splice(i--, 1);
}
}
}
else
{
//无身份提示
wx.showModal({
title: "提示",
content: "当前账号无法登录,当前仅支持学生家长使用,其它身份等待后续版本完善,感谢您的支持。",
showCancel: false,
})
return false;
}
wx.setStorageSync("userCount", users.length)
//如果是多身份进行页面跳转
if (users.length > 1) {
// wx.showToast({
// title: '暂时不支持多身份账号登录',
// icon: 'none',
// duration: 2000
// });
wx.redirectTo({
url: '../'
})
}
else if (users.length == 1) {
wx.setStorageSync("access_token", access_token); //设置缓存token;
//进行身份选择(只有一个身份,所以默认选中该身份)
wx.request({
url: api.getUserInfoUrl + "/" + users[0].idenId + "?access_token=" + access_token,
method: "GET",
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
success: function (res) {
wx.showLoading({
title: '获取用户信息',
mask: true
})
//查询infoid
wx.request({
url: api.getSchMorningCheck + '?access_token=' + app.access_token,
method: 'GET',
dataType: JSON,
success: function (res) {
var jsonText = JSON.parse(res.data);
app.infoId = jsonText.result;
},
complete: function () {
wx.hideLoading();
}
})
wx.request({
url: api.getIdentityDetailUrl + "/" + users[0].idenId + "?access_token=" + access_token,
method: "GET",
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
success: function (res) {
if (res.data.code == "0") {
var userinfo = res.data.result;
wx.setStorageSync("userinfo", userinfo);
wx.switchTab({
url: '../',
})
}
}
})
}
})
}
else {
//无身份提示
wx.showModal({
title: "提示",
content: "当前账号无法登录,当前仅支持学生家长使用,其它身份等待后续版本完善,感谢您的支持。",
showCancel: false,
})
}
}
})
},
//原始登录
getPhoneNumber: function (e) {
var that = this;
wx.login({
success: res => {
app.globalData.sessionCode = res.code
// 发送 res.code 到后台换取 openId, sessionKey, unionId
}
})
var sessionCode=app.globalData.sessionCode;
console.log(e.detail.errMsg);
if (e.detail.errMsg == 'getPhoneNumber:fail user deny' || e.detail.errMsg == 'getPhoneNumber:fail:user deny') {
wx.showModal({
title: '提示',
content: '您已拒绝授权用户信息,请重新点击并授权