aqyj/app.js

136 lines
4.0 KiB
JavaScript

//app.js
App({
onLaunch: function () {
var that = this
that.mplogin()
setInterval(that.reflesh,10*60*1000)
},
mplogin: function () {
var that = this;
wx.showLoading({
title: '账号自动登录中...',
icon:'none',
mask:true
})
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
wx.request({
url: that.globalData.serverUrl + 'mplogin',
data: {
code: res.code
},
method: 'POST',
header: {
'Content-Type': 'application/x-www-form-urlencoded'
},
success: function (res) {
if (res.data.code == 1) {
if (res.header.hasOwnProperty('Set-Cookie')){
that.globalData.sessionId = res.header["Set-Cookie"]
}else{
that.globalData.sessionId = res.header["set-cookie"]
}
that.globalData.userInfo.name = res.data.name
that.globalData.userInfo.username = res.data.username
that.globalData.userInfo.userid = res.data.userid
that.globalData.userInfo.mpopenid = res.data.mpopenid
that.globalData.userInfo.perms = res.data.rights //拉取权限
that.globalData.userInfo.companyid = res.data.companyid
that.globalData.userInfo.openid = res.data.openid
//获取是否是安全员
wx.request({
url: that.globalData.serverUrl + 'api/user?a=checkaqy',
header: {
'content-type': 'application/json', // 默认值
'Cookie': that.globalData.sessionId,
},
data: {},
success: res => {
if (res.data.code == 1) {
that.globalData.userInfo.isaqy = 1
}
}
});
wx.hideLoading()
var pages = getCurrentPages()
var currentPage = pages[pages.length-1] //获取当前页面的对象
var url = currentPage.route
if(that.globalData.hopeUrl){
wx.redirectTo({
url: that.globalData.hopeUrl,
})
}else{
wx.redirectTo({
url:'/'+url,
fail: function(){
wx.reLaunch({
url:'/'+url
})
}
})
}
} else {
wx.hideLoading()
wx.showToast({
title: '未绑定账号,请手动登录',
icon:'none'
})
that.globalData.userInfo.mpopenid = res.data.mpopenid
wx.reLaunch({
url: '/pages/bind/binduser?mpopenid=' + res.data.mpopenid,
})
}
},
})
}
})
},
reflesh: function(){//刷新session
var that = this
wx.request({
url: that.globalData.serverUrl + 'api/check_session',
header: {
'content-type': 'application/json', // 默认值
'Cookie': that.globalData.sessionId,
},
success: res => {
// if(res.data.code!=1){
// that.mplogin()
// }
}
})
},
// wxlogin: function(){
// return new Promise(function(reslove,reject){
// wx.login({
// success (res) {
// reslove(res.code);
// }
// })
// })
// },
globalData: {
userInfo: {
userid:0,
username:'',
name:'',
isaqy:0,
mpopenid:'',
companyid:'',
perms:[],
openid:null
},
serverUrl: 'https://safeyun.ctcshe.com/',
// serverUrl: 'http://127.0.0.1:2222/',
//serverUrl: 'http://10.21.28.148:8000/',
//serverUrl: 'http://192.168.0.103:8000/',
//serverUrl:'http://10.0.11.195:8000/',
timer:null,//定时器
sessionId:null,
hopeUrl:null
},
})