import WxStorage from "../static/lib/wxStorage.js" //微信Storage
|
// 需要永久存储,且下次APP启动需要取出的,在state中的变量名
|
let saveStateKeys = ['userInfo', 'accessToken', 'isLogin'];
|
|
// 保存变量到本地存储中
|
const saveLifeData = function(key, value) {
|
// 判断变量名是否在需要存储的数组中
|
if (saveStateKeys.indexOf(key) != -1) {
|
// 获取本地存储的lifeData对象,将变量添加到对象中
|
let tmp = uni.getStorageSync('lifeData');
|
// 第一次打开APP,不存在lifeData变量,故放一个{}空对象
|
tmp = tmp ? tmp : {};
|
tmp[key] = value;
|
// 执行这一步后,所有需要存储的变量,都挂载在本地的lifeData对象中
|
uni.setStorageSync(key, value);
|
uni.setStorageSync('lifeData', tmp);
|
}
|
}
|
const mutations = {
|
$uStore(state, payload) {
|
// 判断是否多层级调用,state中为对象存在的情况,诸如user.info.score = 1
|
let nameArr = payload.name.split('.');
|
let saveKey = '';
|
let len = nameArr.length;
|
if (nameArr.length >= 2) {
|
let obj = state[nameArr[0]];
|
for (let i = 1; i < len - 1; i++) {
|
obj = obj[nameArr[i]];
|
}
|
obj[nameArr[len - 1]] = payload.value;
|
saveKey = nameArr[0];
|
} else {
|
// 单层级变量,在state就是一个普通变量的情况
|
state[payload.name] = payload.value;
|
saveKey = payload.name;
|
}
|
// 保存变量到本地,见顶部函数定义
|
saveLifeData(saveKey, state[saveKey])
|
},
|
login(state, data) {
|
// console.log(data);
|
state.loging = data.isit; //登录状态
|
if (data.isit) { //写入用户数据
|
state.message.useName = data.userName;
|
state.puserName = data.userName;
|
state.puserID = data.userID;
|
state.avatar = data.avatar;
|
}
|
console.log('登入数据传输成功 开始进入连接')
|
|
var url = 'ws://61.131.136.25:2086/websocket';
|
|
uni.connectSocket({
|
url: url
|
// url: 'ws://192.168.0.111:2086/websocket'
|
});
|
|
var socketOpen = false;
|
|
var heartbeatInterval = null;
|
var reconnect = null;
|
|
|
|
uni.onSocketOpen(function(res) {
|
socketOpen = true;
|
console.log('持续连接成功')
|
|
if (reconnect && reconnect != null) {
|
clearInterval(reconnect)
|
}
|
|
sendSocketMessage();
|
|
// 5秒发送一次心跳
|
heartbeatInterval = setInterval(() => {
|
|
console.log('发送心跳中')
|
|
uni.sendSocketMessage({
|
data: 'ping'
|
});
|
|
}, 5000)
|
|
});
|
|
|
// 监听连接失败
|
uni.onSocketError(function(res) {
|
console.log('WebSocket连接打开失败,请检查!');
|
//停止发送心跳
|
|
if (heartbeatInterval && heartbeatInterval != null) {
|
clearInterval(heartbeatInterval)
|
}
|
//如果不是人为关闭的话,进行重连
|
// if (!_this.isSocketClose) {}
|
reconnect = setInterval(() => { // 断线重连
|
|
console.log(`断线重连中...123`)
|
// 连接
|
uni.connectSocket({
|
url: url,
|
});
|
|
}, 3000)
|
});
|
|
|
|
|
function sendSocketMessage() {
|
var data = {
|
// type: state.puserName,
|
|
id: state.puserID,
|
type: 'login'
|
}
|
|
if (socketOpen) {
|
uni.sendSocketMessage({
|
data: JSON.stringify(data)
|
});
|
}
|
}
|
|
uni.onSocketMessage(function(res) {
|
console.log(res.data);
|
var data = JSON.parse(res.data)
|
if (data.type == 'video') {
|
// this.receiveVideo('Receiver', );
|
console.log('进入接受 跳转', data.type)
|
uni.navigateTo({
|
url: '../videoCall/videoCall?state=Receiver&type=' + data.type + '&room=' + data
|
.roomId + '&name=' + data.name + '&faqiid=' + data.faqiid
|
});
|
}
|
// console.log('收到服务器内容:' + res.data);
|
});
|
|
|
//储存用户
|
WxStorage.set("init", "true");
|
WxStorage.set("name", data.userName);
|
WxStorage.set("ids", data.userID);
|
WxStorage.set("accounts", data.accounts);
|
WxStorage.set("pass", data.pass);
|
},
|
// useWebSocket(state){
|
// uni.onSocketMessage(function(res) {
|
// console.log(res.data);
|
// var data = JSON.parse(res.data)
|
// if (data.type == 'video') {
|
// // this.receiveVideo('Receiver', );
|
// console.log('进入接受 跳转',data.type)
|
// uni.navigateTo({
|
// url: '../videoCall/videoCall?state=Receiver&type=' + data.type +'&room=' + data.roomId + '&name=' + data.name + '&faqiid=' + data.faqiid
|
// });
|
// }
|
// // console.log('收到服务器内容:' + res.data);
|
// });
|
// }
|
loginReset(state) { //重置
|
state.loging = false;
|
state.message.useName = '过客 ';
|
WxStorage.clear();
|
},
|
getUse(state, data) {
|
state.puserName = data.userName;
|
state.puserID = data.userID;
|
},
|
getUserData(state, data) {
|
state.UserData = data;
|
}
|
}
|
|
export default mutations
|