You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.1 KiB

3 years ago
//app.js
App({
onLaunch: function (e) {
var that = this;
//获取设备信息
this.setMenuHeight()
},
onShow(e){
var query = {};
if(e.query.q){
//通过扫码进来获取二维码上的参数并存入storage
let urlSpread = e.query.q;
if (urlSpread) {
if (urlSpread.indexOf('%3F') != -1) {
// 通过扫桌面二维码进来
urlSpread = urlSpread.split("%3F")[1].replace(/%3D/g, ":").replace(/%26/g, ",").split(",").map((item, index) => {
item = item.split(":");
return `"${item[0]}":"${item[1]}"`;
}).join(",");
query = JSON.parse("{" + urlSpread + "}");
}
console.log('query1',query)
this.globalData.inviterId = query.id
}
} else{
query = e.query
console.log('query2',query)
this.globalData.inviterId = query.id
}
},
setMenuHeight(){
wx.getSystemInfo({
success: res => {
// console.log(res)
this.globalData.navTopHeight = res.statusBarHeight;
this.globalData.CustomBar = res.statusBarHeight + 45;
}
});
let menuButtonObj = wx.getMenuButtonBoundingClientRect();
this.globalData.menuTop = menuButtonObj.top;
// console.log(menuButtonObj)
this.globalData.menuHeight = menuButtonObj.height;
this.globalData.navHeight = menuButtonObj.height + (menuButtonObj.top - this.globalData.navTopHeight);
},
getToken(){
// console.log('getToken')
var tokentime = wx.getStorageSync('tokentime') || '';//过期时间
var timestamp = Date.parse(new Date());// 当前时间
var expiration = timestamp + 60000 * 60 * 12; //缓存12小时
var token = wx.getStorageSync('token') || '';
this.login(expiration);
},
login(expiration){
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
// console.log('code:',res.code)
this.http('GET','wechat/auth',{code : res.code,spread:0,login_type:0}).then(
response =>{
if(response.data.isSuccess){
wx.setStorageSync('token', 'Bearer '+ response.data.data.token);
wx.setStorageSync('uid', response.data.data.userId);
wx.setStorageSync('tokentime', expiration);
} else{
wx.showToast({
title: response.data.msg,
icon : 'none'
})
}
}
)
}
})
},
getInfo(){
this.http('get','userinfo').then((res)=>{
if(res.data.success){
wx.setStorageSync('userInfo',res.data.data)
var pages = getCurrentPages().pop();//当前页面
if (pages.getUser){
pages.getUser();
}
}
})
},
http(mathods,url, params) {
var that = this;
var header = {
'content-type': 'application/json',
'Authorization' : wx.getStorageSync('token') || ''
}
return new Promise(
(resolve,reject) => {
wx.showLoading({
title: "正在加载中...",
})
wx.request({
url: this.globalData.baseURL + url, //请求地址
method: mathods, //请求方法
header: header,
data: params || {}, //请求参数
success: res => {
wx.hideLoading();
//判断token是否过期 res.data.code == 40001
if(!res.data.isSuccess && res.data.code == 40001){
console.log('token过期')
that.getToken()
}
resolve(res);
//成功执行方法,参数值为res.data,直接将返回的数据传入
},
fail: function() {
//请求失败
wx.hideLoading();
wx.showToast({
title: '服务器错误,请稍后再试!',
icon : 'none'
})
reject(err)
},
})
}
)
},
globalData: {
// baseURL : 'http://192.168.0.114:8092/api/',
baseURL : 'https://www.cyjyyjy.com:8093/api/',
userInfo: null,
navHeight : 0,
navTopHeight: 0,
menuTop: 0,
menuHeight: 0,
CustomBar: 0,
inviterId: null , //分享码
}
})