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.

162 lines
4.4 KiB

//app.js
App({
gw: {
hostUrl: 'https://www.1hjz.cn',
// hostUrl: 'https://test.1hjz.cn',
mapUrl:'https://api.map.baidu.com',
systemInfo : null,//系统信息
},
onLaunch: function () {
// 展示本地存储能力
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
// 登录
wx.login({
success: res => {
var code = res.code;
this.getSessionKey(code);
}
})
this.gw.systemInfo = wx.getSystemInfoSync();
},
getSessionKey: function (code) {
var that = this;
wx.request({
url: this.gw.hostUrl + '/mall/wxa/auth/session',
method: 'post',
data: {
code: code,
state: '5'
},
header: {
'Content-Type': 'application/x-www-form-urlencoded'
},
success: function (res) {
//--init data
var data = res.data;
if (data.code != 0) {
wx.showToast({
title: data.msg,
icon: 'none',
duration: 2000
});
return false;
}
// that.globalData.userInfo['sessionId'] = data.response.session_key;
that.globalData.openid = data.response.openid;
that.onLoginUser();
// that.prepareOrder();
// 获取用户信息
wx.getSetting({
success: res => {
// if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserInfo({
success: res => {
console.log(res.userInfo)
// 可以将 res 发送给后台解码出 unionId
that.globalData.userInfo = res.userInfo;
// this.onLoginUser();
// 发送 res.code 到后台换取 openId, sessionKey, unionId
that.commitUserInfo(that.globalData.openid, res.encryptedData, res.iv);
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
if (that.userInfoReadyCallback) {
that.userInfoReadyCallback(res)
}
}
})
// }
}
})
},
fail: function (e) {
wx.showToast({
title: '网络异常!err:getsessionkeys',
icon:'none',
duration: 2000
});
},
});
},
onLoginUser: function () {
var that = this;
wx.request({
url: this.gw.hostUrl + '/mall/wxa/auth/login',
method: 'post',
data: {
openid: this.globalData.openid
},
header: {
'Content-Type': 'application/x-www-form-urlencoded'
},
success: function (res) {
//--init data
var data = res.data;
var status = data.code;
if (status != 0) {
wx.showToast({
title: data.msg,
icon: 'none',
duration: 3000
});
return false;
}
that.globalData.sessionId= data.response.sessionId;
wx.setStorageSync('sessionId', data.response.sessionId);
},
fail: function (e) {
wx.showToast({
title: '网络异常!err:authlogin',
icon: 'none',
duration: 2000
});
},
});
},
commitUserInfo: function (openid, encryptedData, iv) {
var that = this;
wx.request({
url: this.gw.hostUrl + '/mall/wxa/auth/userinfo',
method: 'POST',
data: {
openid: openid,
encryptedData: encryptedData,
iv: iv
},
header: {
'Content-Type': 'application/x-www-form-urlencoded',
},
success: function (res) {
//--init data
var data = res.data;
var status = data.code;
if (status != 0) {
wx.showToast({
title: data.msg,
icon: 'none',
duration: 3000
});
return false;
}
},
fail: function (e) {
wx.showToast({
title: '网络异常!err:authlogin',
icon: 'none',
duration: 2000
});
},
});
},
globalData: {
userInfo: null,
sessionId: '',
openid: ''
}
})