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

211 lines
5.9 KiB
JavaScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// const app = getApp();
// const mqUtils = require('./mqttSubstribe.js');
/*********************** */
/**
* 校验用户session信息是否有效
*/
const checkLogin = function(userInfo) {
if (userInfo) {
wx.setStorageSync("userInfo", userInfo);
}
console.log(userInfo);
return;
var that = this;
wx.checkSession({
success: function(res) {
console.log("检测session", res);
var openId = wx.getStorageSync("openId");
var userId = wx.getStorageSync("userId");
var loginKey = wx.getStorageSync("loginKey");
if (app.utils.isBlank(openId) || app.utils.isBlank(userId) || app.utils.isBlank(loginKey)) {
that.login();
} else {
that.checkLoginKey(userId, loginKey);
}
},
fail: function(res) {
that.login();
}
});
};
/**
* 校验loginKey是否有效
* 1调用服务器接口校验loginkey是否有效
* 1登录有效 重置mqtt订阅topic并连接mqtt
* 0无效 重新登录 获取loginkey
*
*/
const checkLoginKey = function(userId, loginKey) {
var that = this;
wx.request({
url: app.globalData.serverUrlExtend,
method: "POST",
header: {
'content-type': 'application/x-www-form-urlencoded'
},
data: {
"type": "wxdcechecksession",
"userId": userId,
"loginKey": loginKey
},
success: function(res) {
var result = res.data;
console.log(">>>>checkLoginKey :" + result);
if (result.status == 0) {
that.login();
} else {
// // 刷新mqtt订阅topic
// mqUtils.reaportConnect();
console.log("登录校验通过");
}
},
fail: function(res) {
console.log("校验loginKey是否有效: ");
}
});
};
/**
* 登录
* 1调用微信登录
* 1调用服务器接口 获取sessionkey和登录态 放入缓存(有缓存时间)
* 1调用微信获取用户信息并调用服务器接口解密用户信息
*/
const login = function() {
var that = this;
wx.login({
success: function(loginResult) {
var code = loginResult.code;
// that.getMemberSession(code);
// 调用服务器接口解密数据并根据unionId获取/创建user
wx.request({
url: app.globalData.serverUrlExtend,
method: "POST",
header: {
'content-type': 'application/x-www-form-urlencoded'
},
data: {
"type": "wxdcjscode2session",
"code": code,
"appid": app.globalData.appid,
"secret": app.globalData.secret
},
success: function(res) {
console.log("session", res);
var result = res.data;
if (result.status == 1) {
app.openId = result.data.openId;
app.loginKey = result.data.loginKey;
app.wc.put("openId", result.data.openId, app.validTime);
app.wc.put("loginKey", result.data.loginKey, app.validTime);
// 获取用户信息
that.decodeUserData();
} else {
app.msg.showMsg("提示", "请求session_key失败");
}
},
fail: function(res) {
app.msg.showMsg("提示", "获取/创建user信息失败");
}
});
}
});
};
/**
* 获取运维存储用户信息
*/
const decodeUserData = function() {
var that = this;
wx.getUserInfo({
withCredentials: true,
success: function(res) {
var nickName = res.userInfo.nickName;
var encryptedData = res.encryptedData;
var iv = res.iv;
// 调用服务器接口解密数据并根据unionId获取/创建user
// 发起登录请求
wx.request({
url: app.globalData.serverUrlExtend,
method: "POST",
header: {
'content-type': 'application/x-www-form-urlencoded'
},
data: {
"type": "wxdcgetuser",
"tenantId": app.tenantId,
"wid": app.wid,
"openId": app.openId,
"loginKey": wx.getStorageSync("loginKey"),
"encryptedData": encryptedData,
"iv": iv,
"nickName": nickName,
"sex": 1
},
success: function(res) {
var result = res.data;
if (result.status == 1) {
wx.setStorageSync("userId", result.data.user.id);
wx.setStorageSync("vipMobile", result.data.user.vipMobile);
wx.setStorageSync("unionid", result.data.user.unionid);
app.userId = result.data.user.id;
app.globalData.appKey = result.data.appKey;
app.globalData.appSecret = result.data.appSecret;
app.vipMobile = result.data.user.vipMobile;
app.unionid = result.data.user.unionid;
// 刷新mqtt订阅topic
// app.topic = app.baseTopic + "/" + app.wid + "/" + app.userId;
// 重新连接mqtt
// mqUtils.reaportConnect();
}
},
fail: function(res) {
app.msg.showMsg("提示", "解析user信息失败");
}
});
},
fail: function(res) {
app.msg.showMsg("提示", "获取user信息失败");
}
});
};
/**
* 获取会员session
*/
const getMemberSession = function(code) {
var params = {
'appid': app.globalData.appid,
'secret': app.globalData.secret,
'code': code,
'method': 'weixin.small.jscode2session'
}
var ignores = [];
app.jsapi.memberApi(app.globalData.appMemberKey, app.globalData.appMemberSecret, app.globalData.serverMemberUrl).ajax(params, ignores,
function(json) {
console.log(json);
},
function(error) {
}
);
}
module.exports = {
checkLogin: checkLogin,
login: login,
decodeUserData: decodeUserData,
checkLoginKey: checkLoginKey,
getMemberSession: getMemberSession
}