|
@@ -4,17 +4,17 @@ Vue.use(Vuex)
|
|
|
|
|
|
let lifeData = {};
|
|
|
|
|
|
-try{
|
|
|
+try {
|
|
|
// 尝试获取本地是否存在lifeData变量,第一次启动APP时是不存在的
|
|
|
lifeData = uni.getStorageSync('lifeData');
|
|
|
-}catch(e){
|
|
|
-
|
|
|
+} catch (e) {
|
|
|
+
|
|
|
}
|
|
|
|
|
|
// 需要永久存储,且下次APP启动需要取出的,在state中的变量名
|
|
|
let saveStateKeys = [
|
|
|
- 'vuex_user',
|
|
|
- 'vuex_token',
|
|
|
+ 'vuex_user',
|
|
|
+ 'vuex_token',
|
|
|
'vuex_userData',
|
|
|
'vuex_payType',
|
|
|
'vuex_payStatus',
|
|
@@ -26,9 +26,9 @@ let saveStateKeys = [
|
|
|
];
|
|
|
|
|
|
// 保存变量到本地存储中
|
|
|
-const saveLifeData = function(key, value){
|
|
|
+const saveLifeData = function(key, value) {
|
|
|
// 判断变量名是否在需要存储的数组中
|
|
|
- if(saveStateKeys.indexOf(key) != -1) {
|
|
|
+ if (saveStateKeys.indexOf(key) != -1) {
|
|
|
// 获取本地存储的lifeData对象,将变量添加到对象中
|
|
|
let tmp = uni.getStorageSync('lifeData');
|
|
|
// 第一次打开APP,不存在lifeData变量,故放一个{}空对象
|
|
@@ -43,7 +43,10 @@ const store = new Vuex.Store({
|
|
|
state: {
|
|
|
// 如果上面从本地获取的lifeData对象下有对应的属性,就赋值给state中对应的变量
|
|
|
// 加上vuex_前缀,是防止变量名冲突,也让人一目了然
|
|
|
- vuex_user: lifeData.vuex_user ? lifeData.vuex_user : {account:'',password:''},
|
|
|
+ vuex_user: lifeData.vuex_user ? lifeData.vuex_user : {
|
|
|
+ account: '',
|
|
|
+ password: ''
|
|
|
+ },
|
|
|
vuex_token: lifeData.vuex_token ? lifeData.vuex_token : '',
|
|
|
vuex_userData: lifeData.vuex_userData ? lifeData.vuex_userData : '',
|
|
|
vuex_payType: lifeData.vuex_payType ? lifeData.vuex_payType : [], // 支付方式数据字典
|
|
@@ -61,12 +64,14 @@ const store = new Vuex.Store({
|
|
|
bizId: '', // 设备编号
|
|
|
itemCode: '', // 服务类型
|
|
|
duration: 0, // 不同服务的机器运行时间
|
|
|
- orderNo: '',// 订单编号
|
|
|
+ orderNo: '', // 订单编号
|
|
|
orderStatus: '' // 订单状态
|
|
|
},
|
|
|
// 设备运行页面的计时器
|
|
|
orderDjs: 0, // 订单完成倒计时
|
|
|
- loadingDjs: 0 // 进度条倒计时
|
|
|
+ loadingDjs: 0, // 进度条倒计时
|
|
|
+ // 优惠券码
|
|
|
+ vuex_couponCode: ''
|
|
|
},
|
|
|
mutations: {
|
|
|
$uStore(state, payload) {
|
|
@@ -74,9 +79,9 @@ const store = new Vuex.Store({
|
|
|
let nameArr = payload.name.split('.');
|
|
|
let saveKey = '';
|
|
|
let len = nameArr.length;
|
|
|
- if(nameArr.length >= 2) {
|
|
|
+ if (nameArr.length >= 2) {
|
|
|
let obj = state[nameArr[0]];
|
|
|
- for(let i = 1; i < len - 1; i ++) {
|
|
|
+ for (let i = 1; i < len - 1; i++) {
|
|
|
obj = obj[nameArr[i]];
|
|
|
}
|
|
|
obj[nameArr[len - 1]] = payload.value;
|
|
@@ -94,60 +99,64 @@ const store = new Vuex.Store({
|
|
|
let _this = this
|
|
|
let token = getApp().globalData.token
|
|
|
let url = getApp().globalData.baseUrl
|
|
|
- let wsBaseUrl = url.indexOf("https:")>=0 ? url.replace("https:","wss:") : url.replace("http:","ws:")
|
|
|
+ let wsBaseUrl = url.indexOf("https:") >= 0 ? url.replace("https:", "wss:") : url.replace("http:", "ws:")
|
|
|
let wsUrl = wsBaseUrl + 'websocket/weixin/' + state.vuex_orderInfo.bizId
|
|
|
// 开始连接
|
|
|
state.vuex_socket = uni.connectSocket({
|
|
|
- header: {'X-AUTH-TOKEN':token},
|
|
|
- url: wsUrl, // ws 请求地址
|
|
|
- complete: ()=> {
|
|
|
+ header: {
|
|
|
+ 'X-AUTH-TOKEN': token
|
|
|
+ },
|
|
|
+ url: wsUrl, // ws 请求地址
|
|
|
+ complete: () => {
|
|
|
console.log('连接complete回调!')
|
|
|
}
|
|
|
});
|
|
|
// 打开连接
|
|
|
- state.vuex_socket.onOpen(function(e){
|
|
|
+ state.vuex_socket.onOpen(function(e) {
|
|
|
console.log('连接成功!')
|
|
|
})
|
|
|
// 连接关闭
|
|
|
- state.vuex_socket.onClose(function(e){
|
|
|
+ state.vuex_socket.onClose(function(e) {
|
|
|
console.log('ws关闭!')
|
|
|
})
|
|
|
// 连接错误
|
|
|
- state.vuex_socket.onError(function(e){
|
|
|
+ state.vuex_socket.onError(function(e) {
|
|
|
console.log('ws错误!', JSON.stringify(e))
|
|
|
})
|
|
|
// 接受消息
|
|
|
- state.vuex_socket.onMessage(function(e){
|
|
|
- if (typeof(e.data) == 'object'){
|
|
|
- let ret = JSON.parse(e.data)
|
|
|
- console.log('ws接收消息!', ret)
|
|
|
- uni.$emit('wsMessage',ret)
|
|
|
+ state.vuex_socket.onMessage(function(e) {
|
|
|
+ if (typeof(e.data) == 'object') {
|
|
|
+ let ret = JSON.parse(e.data)
|
|
|
+ console.log('ws接收消息!', ret)
|
|
|
+ uni.$emit('wsMessage', ret)
|
|
|
}
|
|
|
- if(typeof(e.data) == 'string'){
|
|
|
- uni.$emit('wsMessage',e.data)
|
|
|
+ if (typeof(e.data) == 'string') {
|
|
|
+ uni.$emit('wsMessage', e.data)
|
|
|
}
|
|
|
})
|
|
|
// 发送心跳包
|
|
|
- state.vuex_heartInterId = setInterval(function () {
|
|
|
+ state.vuex_heartInterId = setInterval(function() {
|
|
|
console.log(state.vuex_socket.readyState)
|
|
|
- if(state.vuex_socket.readyState != 1){
|
|
|
+ if (state.vuex_socket.readyState != 1) {
|
|
|
state.vuex_socket = uni.connectSocket({
|
|
|
- header: {'X-AUTH-TOKEN':token},
|
|
|
- url: wsUrl, // ws 请求地址
|
|
|
- complete: ()=> {
|
|
|
+ header: {
|
|
|
+ 'X-AUTH-TOKEN': token
|
|
|
+ },
|
|
|
+ url: wsUrl, // ws 请求地址
|
|
|
+ complete: () => {
|
|
|
console.log('连接complete回调!')
|
|
|
}
|
|
|
});
|
|
|
- }else{
|
|
|
- _this.commit("$sendWebsocket","ping")
|
|
|
+ } else {
|
|
|
+ _this.commit("$sendWebsocket", "ping")
|
|
|
}
|
|
|
}, 10000)
|
|
|
},
|
|
|
// 关闭websocket
|
|
|
- $closeWebsocket(state){
|
|
|
- if(state.vuex_socket){
|
|
|
+ $closeWebsocket(state) {
|
|
|
+ if (state.vuex_socket) {
|
|
|
state.vuex_socket.close({
|
|
|
- complete: function(e){
|
|
|
+ complete: function(e) {
|
|
|
console.log('关闭websocket完成', e)
|
|
|
// 停止心跳
|
|
|
clearInterval(state.vuex_heartInterId)
|
|
@@ -157,9 +166,9 @@ const store = new Vuex.Store({
|
|
|
},
|
|
|
// 发送消息
|
|
|
$sendWebsocket(state, msg) {
|
|
|
- state.vuex_socket.send({
|
|
|
+ state.vuex_socket.send({
|
|
|
data: msg,
|
|
|
- complete: function(e){
|
|
|
+ complete: function(e) {
|
|
|
console.log('ws发送完成', e)
|
|
|
}
|
|
|
})
|
|
@@ -167,4 +176,4 @@ const store = new Vuex.Store({
|
|
|
}
|
|
|
})
|
|
|
|
|
|
-export default store
|
|
|
+export default store
|