diff --git a/components/tabBar/tabBar.vue b/components/tabBar/tabBar.vue index d5ef467..29ad98e 100644 --- a/components/tabBar/tabBar.vue +++ b/components/tabBar/tabBar.vue @@ -29,9 +29,16 @@ import { getToken } from '@/utils/auth' + let innerAudioContext =''; + // #ifdef APP || H5 + const jyJPush = uni.requireNativePlugin('JY-JPush'); + // #endif export default { data() { return { + msgSocket: this.$msgSocket, + // 是否正在播放 + ifPlay:false, aindex: 1, msgNum: null, identity: 'user', @@ -64,10 +71,92 @@ // #ifdef APP || H5 this.identity = uni.getStorageSync('identity'); // #endif + if (!this.nowPageInterval) { + this.nowPageInterval = setInterval(() => { + // console.log('this.$msgSocket: ',this.$msgSocket); + this.msgSocket = this.$msgSocket + }, 3000); + } + console.log(this.aindex); + setTimeout(() => { + this.msgInfo() + }, 2000); this.aindex = this.msg }, + watch: { + msgSocket(newVal, oldVal) { + console.log('msgSocket值发生变化', newVal, oldVal) + this.msgInfo() + }, + deep: true, + }, methods: { + dianyidain() { + if(!this.ifPlay){ + console.log('执行了,dianyidain'); + if(innerAudioContext!=""){ + try { + console.log('调用前先销毁'); + innerAudioContext.stop(); + innerAudioContext.destroy(); + innerAudioContext=""; + }catch (e){ + console.log('销毁出错'); + } + } + innerAudioContext = uni.createInnerAudioContext(); + this.ifPlay=true + // #ifdef APP-PLUS + innerAudioContext.src = '../../static/msgV.mp3'; + // #endif + // #ifndef APP-PLUS + innerAudioContext.src = 'https://www.nuoyunr.com/lananRsc/rescue/msgV.mp3'; + // #endif + // 设置播放次数和计数器 + const playCount = 3; + let currentCount = 0; + // 初次播放 + innerAudioContext.play(); + innerAudioContext.onError((err) => { + console.error('播放错误', err); + this.ifPlay=false + innerAudioContext.stop(); + innerAudioContext.destroy(); // 播放错误后释放实例 + }); + // 监听音频播放结束事件 + innerAudioContext.onEnded(() => { + // 播放计数加一 + currentCount++; + // 判断是否达到播放次数上限 + if (currentCount < playCount) { + // 继续播放 + innerAudioContext.play(); + } else { + // 播放完成,可以在这里添加额外的逻辑 + console.log('播放完成'); + this.ifPlay=false + //及时释放资源 + innerAudioContext.stop(); + innerAudioContext.destroy(); + } + }); + }else{ + console.log('正在播放音频,拒绝播放请求'); + } + }, + msgInfo() { + console.log(this.msgSocket, 'msgSocket') + if (this.msgSocket) { + console.log('消息回调启动成功') + this.msgSocket.onMessage(res => { + console.log("发出提示音", res); + // this.getbottom(); + this.dianyidain() + }) + } + + }, async getbottom() { if (!getToken()) { return diff --git a/config.js b/config.js index 983916f..cbd9a75 100644 --- a/config.js +++ b/config.js @@ -6,7 +6,7 @@ module.exports = { imagesUrl: 'http://shequ.0315e.com/static/images/pages/', baseImageUrl: 'https://www.nuoyunr.com/minio/', shareUrl: 'https://www.lighting-it.cn/share?inviteId=', - wsUrl: 'wss://www.nuoyunr.com', + wsUrl: 'ws://192.168.1.17:48080', // 应用信息 appInfo: { // 应用名称 diff --git a/main.js b/main.js index b2573a8..cbd80a5 100644 --- a/main.js +++ b/main.js @@ -3,10 +3,48 @@ import uView from "uview-ui"; import config from '@/config' const baseUrl = config.baseUrl Vue.prototype.$baseUrl = baseUrl; +const baseImageUrl = config.baseImageUrl +Vue.prototype.$baseImageUrl = baseImageUrl; Vue.use(uView); +// 如此配置即可 +import { + tabBarconnect, + closeMsgSocket, + sendMsg +} from '@/utils/tebBarSocket' +import { + getStrData +} from '@/utils/auth' +let msgTimer; +let msgSocket; +export function startMsgSocket(tenantId,userId) { + msgSocket = null + msgSocket = tabBarconnect(tenantId,userId); + if (!msgTimer && getStrData('userId')) { + msgTimer = setInterval(() => { + sendMsg(msgSocket) + }, 10000); + } + Vue.prototype.$msgSocket = msgSocket +} +Vue.prototype.$startMsgSocket = startMsgSocket; //userid +export function tabBarcloseSocket() { + closeMsgSocket; +} +export function getclearInterval() { + console.log('执行关闭请求'); + clearInterval(msgTimer); + msgTimer = undefined; + +} +Vue.prototype.$getclearInterval = getclearInterval; // #ifndef VUE3 import Vue from 'vue' import './uni.promisify.adaptor' +import { + request +} from "@/utils/request.js" +Vue.prototype.$request = request Vue.config.productionTip = false App.mpType = 'app' const app = new Vue({ @@ -23,4 +61,4 @@ export function createApp() { app } } -// #endif \ No newline at end of file +// #endif diff --git a/pages-home/home/home.vue b/pages-home/home/home.vue index 488aaf5..9417d05 100644 --- a/pages-home/home/home.vue +++ b/pages-home/home/home.vue @@ -47,7 +47,9 @@ import OrderCard from "@/components/orderCard.vue"; import config from '@/config' import { getToken, - getUserInfo + getUserInfo, + getStrData, + getTenantId } from '@/utils/auth' export default { @@ -109,6 +111,17 @@ export default { defaultAvatar: require('@/static/icons/avatar.png') } }, + onLoad(){ + if(!getToken()){ + uni.reLaunch({ + url: '/pages/login/login' + }) + }else{ + if(!this.$msgSocket){ + this.$startMsgSocket(getTenantId(),getStrData("userId")) + } + } + }, onShow() { if(!getToken()){ uni.reLaunch({ diff --git a/pages/index/index.vue b/pages/index/index.vue index f4d597c..5d75175 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -16,9 +16,16 @@ } }, onLoad() { - uni.reLaunch({ - url: '/pages/login/login' - }) + if(getToken()){ + //跳转首页 + uni.navigateTo({ + url: '/pages-home/home/home' + }) + }else{ + uni.navigateTo({ + url: '/pages/login/login' + }) + } }, methods: { @@ -28,4 +35,4 @@ \ No newline at end of file + diff --git a/pages/login/login.vue b/pages/login/login.vue index 378d8f8..e09055f 100644 --- a/pages/login/login.vue +++ b/pages/login/login.vue @@ -51,7 +51,8 @@ setToken, setTenantId, setUserInfo, - setJSONData + setJSONData, + setStrData } from '@/utils/auth' import { login, @@ -155,6 +156,11 @@ }) setToken(res.data.loginResult.accessToken) setUserInfo(res.data.userinfo) + setStrData("userId",res.data.userinfo.id) + if(res.data.userinfo.roleCodes.indexOf("repair_staff")){ + //维修工角色,需要再查一下是否是班组长 + this.getIdLeader() + } this.getStaffInfo(res.data.userinfo.id) //跳转首页 uni.navigateTo({ @@ -203,6 +209,20 @@ setJSONData("staffInfo",res.data) } }) + }, + /** + * 查是否班组长 + */ + getIdLeader(){ + request({ + url: '/admin-api/repair/worker/getIfLeader', + method: 'get' + }).then((res) => { + console.log(res) + if (res.code == 200) { + setStrData("ifLeader",res.data) + } + }) }, getxz(index) { this.activeid = index diff --git a/pages/my/myInfo.vue b/pages/my/myInfo.vue index 993c6d5..dab4629 100644 --- a/pages/my/myInfo.vue +++ b/pages/my/myInfo.vue @@ -50,7 +50,8 @@ import { getToken, getUserInfo, getJSONData, - removeToken,removeUserInfo,removeJSONData + removeTenantId, + removeToken,removeUserInfo,removeJSONData,removeStrData } from '@/utils/auth' const UQRCode = require('uqrcodejs'); export default { @@ -78,6 +79,10 @@ export default { //直接取缓存中的用户信息 this.customInfo = getUserInfo() this.staff = getJSONData("staffInfo") + uni.showToast({ + title: this.staff.uniqueCode, + icon: 'none' + }) if(this.staff.uniqueCode){ this.showUniCode =true this.generateUniCode(this.staff.uniqueCode) @@ -117,6 +122,8 @@ export default { removeToken() removeUserInfo() removeJSONData("staffInfo") + removeStrData("userId") + removeTenantId() //跳转首页 uni.navigateTo({ url: '/pages/login/login' diff --git a/static/msgV.mp3 b/static/msgV.mp3 new file mode 100644 index 0000000..ddb07b3 Binary files /dev/null and b/static/msgV.mp3 differ diff --git a/utils/auth.js b/utils/auth.js index 4ac65e2..e17e8cc 100644 --- a/utils/auth.js +++ b/utils/auth.js @@ -19,7 +19,9 @@ export function setTenantId(TenantId) { export function getTenantId(){ return uni.getStorageSync(TenantIdKey) } - +export function removeTenantId() { + return uni.removeStorageSync(TenantIdKey); +} export function hasRole(roleCode) { const roleList = uni.getStorageSync('role') if (roleList && roleList.length > 0) { @@ -63,3 +65,15 @@ export function removeJSONData(keyStr) { return uni.removeStorageSync(keyStr) } +export function getStrData(keyStr) { + return uni.getStorageSync(keyStr) +} + +export function setStrData(keyStr,dataStr) { + return uni.setStorageSync(keyStr, dataStr) +} + +export function removeStrData(keyStr) { + return uni.removeStorageSync(keyStr) +} + diff --git a/utils/tebBarSocket.js b/utils/tebBarSocket.js index 9f09425..2b6e6db 100644 --- a/utils/tebBarSocket.js +++ b/utils/tebBarSocket.js @@ -1,23 +1,26 @@ import { startMsgSocket } from '../main.js' +import { + getTenantId, + getStrData +} from '@/utils/auth' import config from '@/config' -export function tabBarconnect(userId) { - console.log("通知socket链接", userId); + +export function tabBarconnect(tenantId, userId) { + console.log("通知socket链接", tenantId, userId); var socketTask = uni.connectSocket({ - url: config.wsUrl + '/websocket/announcement/' + userId, + url: config.wsUrl + '/websocket/message/'+tenantId+'/' + userId, success(res) { }, fail(err) { } } - ); return socketTask; } - export function sendMsg(SocketTask) { if (SocketTask) { SocketTask.send({ @@ -26,19 +29,15 @@ export function sendMsg(SocketTask) { console.log('消息链接正常') }, fail: err => { - console.log('消息链接异常 重新链接' + err, uni.getStorageSync('userId')); - - startMsgSocket(uni.getStorageSync('userId')); + console.log('消息链接异常 重新链接' + err, getStrData('userId')); + startMsgSocket(getTenantId(),getStrData('userId')); } }); } else { - startMsgSocket(uni.getStorageSync('userId')); + startMsgSocket(getTenantId(),getStrData('userId')); } - - - } export function closeMsgSocket(SocketTask) { SocketTask.close() -} \ No newline at end of file +}