xinelu-applet-ui/store/index.js

128 lines
3.4 KiB
JavaScript
Raw Normal View History

2023-09-22 15:07:26 +08:00
import Vue from 'vue'
import Vuex from 'vuex'
2023-12-19 14:41:16 +08:00
import {
socketurl
} from '@/api/socketurl.js'
2023-09-22 15:07:26 +08:00
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
//公共的变量这里的变量不能随便修改只能通过触发mutations的方法才能改变
2023-12-19 14:41:16 +08:00
timeoutObj: null,
socketOpen: false,
SOCKETURL: '',
2023-09-22 15:07:26 +08:00
},
mutations: {
2023-12-19 14:41:16 +08:00
scoket(state) {
2024-01-05 17:03:15 +08:00
uni.closeSocket();
clearInterval(state.timeoutObj);
2023-12-19 14:41:16 +08:00
state.SOCKETURL = socketurl + uni.getStorageSync('patientId')
state.socketOpen = false
try {
uni.connectSocket({
url: state.SOCKETURL
})
uni.onSocketOpen(res => {
console.log('webScoket连接已打开', res);
state.socketOpen = true
clearInterval(state.timeoutObj);
state.timeoutObj = setInterval(() => {
uni.sendSocketMessage({
data: 'ping',
success(res) {
console.log('正在发送心跳');
},
fail(err) {
console.log('心跳发送失败,重新连接...');
state.socketOpen = true
uni.connectSocket({
url: state.SOCKETURL
})
}
})
2024-01-22 22:11:26 +08:00
}, 3000)
2023-12-19 14:41:16 +08:00
})
uni.onSocketError(err => {
console.log('webScoket连接打开失败', err);
if (err && err.code != 1000) {
setTimeout(() => {
state.socketOpen = true
uni.connectSocket({
url: state.SOCKETURL
})
2024-01-22 22:11:26 +08:00
}, 3000)
2023-12-19 14:41:16 +08:00
}
})
uni.onSocketClose(err => {
console.log('webScoket连接关闭', err);
if (err && err.code !== 1000) {
setTimeout(() => {
state.socketOpen = true
uni.connectSocket({
url: state.SOCKETURL
})
2024-01-22 22:11:26 +08:00
}, 3000)
2023-12-19 14:41:16 +08:00
}
})
} catch (e) {
console.log(e);
}
},
2024-01-22 22:11:26 +08:00
socketOpenfalse(state) {
state.socketOpen = false
},
2023-09-22 15:07:26 +08:00
//相当于同步的操作
//点击确认
2023-11-15 10:31:52 +08:00
integralsubscribesuccess(state) {
2023-09-22 15:07:26 +08:00
wx.requestSubscribeMessage({
tmplIds: [
2023-11-15 10:31:52 +08:00
// 'S_c9bR4znSWpXg-6ACIMn7AkaR11dzo113XM8w4CKz0'
2023-09-22 15:07:26 +08:00
],
success(res) {},
fail(err) {},
complete(scc) {}
})
},
2023-11-15 10:31:52 +08:00
subscribesuccess(state) {
2023-09-22 15:07:26 +08:00
wx.requestSubscribeMessage({
tmplIds: [
2023-11-15 10:31:52 +08:00
'8InV9jXDT5sMj9OWfXEvlLQGlw2UaWfZ9OBMFxufmfk',
'2wn1BssReKjZasxKKZRWxBN0VqSmGmkuhc3AhTr18I0',
'MSSKCxOdtMUkY1ACu-u3itz8Vh_w5xDkO2llAOGwElU',
2023-09-22 15:07:26 +08:00
],
success(res) {},
fail(err) {},
complete(scc) {}
})
},
},
actions: {
//相当于异步的操作,不能直接改变state的值只能通过触发mutations的方法才能改变
integralopenPopup(contxt) {
// 获取用户的当前设置,判断是否点击了“总是保持以上,不在询问”
wx.getSetting({
withSubscriptions: true, // 是否获取用户订阅消息的订阅状态默认false不返回
success(res) {
if (res.authSetting['scope.subscribeMessage']) {} else {
//因为没有选择总是保持,所以需要调起授权弹窗再次授权
contxt.commit('integralsubscribesuccess')
}
}
})
},
// 是否设置过授权
openPopup(contxt) {
// 获取用户的当前设置,判断是否点击了“总是保持以上,不在询问”
2024-06-17 16:33:34 +08:00
contxt.commit('subscribesuccess')
// wx.getSetting({
// withSubscriptions: true, // 是否获取用户订阅消息的订阅状态默认false不返回
// success(res) {
// console.log(res)
// if (res.authSetting['scope.subscribeMessage']) {} else {
// //因为没有选择总是保持,所以需要调起授权弹窗再次授权
// }
// }
// })
2023-09-22 15:07:26 +08:00
},
}
})
2023-11-15 10:31:52 +08:00
export default store