aqyj/app.js

109 lines
3.5 KiB
JavaScript

//app.js
App({
onLaunch: function () {
var that = this
that.mplogin()
setInterval(that.reflesh,10*60*1000)
},
mplogin: function () {
var that = this;
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
// if (that.callback) { //这个函数名字和你定义的一样即可
// that.callback() //执行定义的回调函数
// }
/**** KEY PART START ****/
var pages = getCurrentPages() // get all current pages
var currentPage = pages[pages.length - 1] // Get current loading page
if (currentPage.callback){
currentPage.callback(); // Callback
that.oncallback = false; // Callback called
}
/**** KEY PART END ****/
//获取是否是安全员
wx.request({
url: that.globalData.serverUrl + 'api/user?a=checkaqy',
header: {
'content-type': 'application/json', // 默认值
'Cookie': getApp().globalData.sessionId,
},
data: {},
success: res => {
if (res.data.code == 1) {
that.globalData.userInfo.isaqy = 1
}
}
});
} else {
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': getApp().globalData.sessionId,
},
success: res => {
if(res.data.code!=1){
that.mplogin()
}
}
})
},
globalData: {
userInfo: {
userid:0,
username:'',
name:'',
isaqy:0,
mpopenid:'',
companyid:'',
perms:[],
openid:null
},
serverUrl: 'https://safeyun.ctcshe.com/',
// serverUrl: 'http://127.0.0.1:8000/',
//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
},
})