123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413 |
- <template>
- <div class="header" :class="[collapse ? 'collapse' : 'expand']">
- <div class="el-fl-left"><v-tags></v-tags></div>
- <div class="header-right">
- <div class="header-user-con">
- <div>欢迎你,{{ cruUserInfo.username }}</div>
- <!-- 用户头像 -->
- <div class="user-avator" @click="editUserInfo">
- <i class="iconfont" v-show="!cruUserInfo.photo" v-bottom-txt-tip data-txt="编辑用户信息"></i>
- <img v-show="cruUserInfo.photo" :src="envConfig.baseImgApi + cruUserInfo.photo" style="object-fit: cover" />
- </div>
- <div class="message">
- <span class="main" @click="goMessage('unread')">
- <img src="@assets/img/btn_news.png" alt="" />
- <span class="iconfont point" v-if="messageNumber"></span>
- </span>
- <div class="msg-content" v-if="messageList && messageList.length">
- <p>消息通知</p>
- <ul>
- <li v-for="(items, index) in messageList" :key="index" @click="toUrl(items.typeId)" style="cursor: pointer">
- <img :src="imgList[items.typeId]" style="width: 30px; height: 30px" />
- <div>
- <span class="cont">【{{ items.shortName }}】{{ items.messageContent }}</span>
- <span class="time">{{ items.dateCreate }}</span>
- </div>
- </li>
- </ul>
- <el-button type="text" class="more" @click="goMessage('more')">查看更多</el-button>
- </div>
- <div class="msg-content" v-else>
- <p>消息通知</p>
- <ul>
- <li style="text-align: center; display: block; padding: 0 rem(20); line-height: rem(60)">暂无消息</li>
- </ul>
- <el-button type="text" class="more" disabled>查看更多</el-button>
- </div>
- </div>
- <span class="border"></span>
- <div class="logout" @click="logOut">
- <el-tooltip placement="bottom" content="退出">
- <i class="zoniot_font zoniot-icon-tuichu"></i>
- </el-tooltip>
- </div>
- </div>
- </div>
- </div>
- </template>
- <script>
- import { mapState, mapGetters, mapMutations, mapActions } from 'vuex';
- import envConfig from '@/config';
- import vTags from './Tags.vue';
- export default {
- data() {
- return {
- name: '',
- envConfig: envConfig,
- imgList: {
- 1: require('@/assets/img/icon_msg1.png'),
- 2: require('@/assets/img/icon_msg7.png'),
- 10: require('@/assets/img/icon_msg9.png'),
- 11: require('@/assets/img/icon_msg9.png'),
- 13: require('@/assets/img/icon_msg9.png')
- }
- };
- },
- components: {
- vTags
- },
- computed: {
- cruUserInfo() {
- return this.$store.getters['getCruUserInfo'];
- },
- collapse() {
- return this.$store.getters['getCollapse'];
- },
- ...mapState(['messageNumber']),
- ...mapState(['messageList'])
- },
- methods: {
- toUrl(type) {
- this.$router.push({
- path: '/msg'
- });
- },
- // 退出登录
- logOut() {
- var access_token = localStorage.getItem('SC_token');
- this.$http.postForm('/sc-user-auth/user/logout', { access_token: access_token }).then(({ status, data, msg }) => {
- if (0 === status) {
- this.$message({
- type: 'success',
- message: '您已退出登录'
- });
- localStorage.removeItem('SC_token');
- sessionStorage.removeItem('SC_listMuen');
- this.$store.commit('setloginInfo', '');
- this.$store.dispatch('tags', []);
- sessionStorage.removeItem('tabs');
- window.location.href = this.envConfig.loginUrl;
- } else {
- this.$message.error(msg);
- }
- });
- },
- getUserInfo(resolve) {
- this.$http.postForm('/sc-user-center/user/findLoginUserById').then(({ status, data, msg }) => {
- if (status === 0) {
- this.$store.commit('setcCruUserInfo', data);
- } else {
- this.$message.error('获取用户信息失败');
- }
- resolve && resolve(true);
- });
- },
- editUserInfo() {
- new Promise((resolve) => {
- this.$store.dispatch('addPopup', {
- url: '/system/users/popups/edituser.vue',
- width: '500px',
- height: '500px',
- props: {
- data: JSON.parse(JSON.stringify(this.cruUserInfo)),
- callback: resolve
- },
- title: '编辑用户信息'
- });
- }).then(() => {
- this.getUserInfo();
- });
- },
- goMessage(msg) {
- let messageStatus = 2; //全部
- if (msg == 'unread') {
- messageStatus = 0; //未读
- }
- this.$store.commit('setmessageStatus', messageStatus);
- this.$router.push({
- path: '/msg'
- });
- },
- //获取最新消息列表
- getMessageList() {
- this.$http.postForm('/sc-message/message/queryLastMessage', { num: '5' }).then(({ status, data, msg }) => {
- if (status === 0) {
- this.$store.commit('setmessageList', data);
- }
- });
- },
- //查询未读消息数量
- getUnreadNumber() {
- this.$http.postForm('/sc-message/message/queryUnreadMessageStatic').then(({ status, data, msg }) => {
- if (status === 0) {
- data.map((item, index) => {
- if (item.cn) {
- this.$store.commit('setmessageNumber', true);
- }
- });
- }
- });
- },
- //初始化websoket
- initWebSocket(id) {
- if ('WebSocket' in window) {
- var serviceIp = this.envConfig.websoketUrl;
- this.websocket = new WebSocket('ws://' + serviceIp + '/sc-message/webSocket/' + id);
- } else {
- console.log('当前浏览器 Not support websocket');
- }
- let that = this;
- this.interval = window.setInterval(function () {
- //每隔30秒钟发送一次心跳,避免websocket连接因超时而自动断开
- if (that.websocket != null) {
- // that.websocket.send('HeartBeat');
- // console.log('发送心跳包:HeartBeat');
- }
- }, 30000);
- //连接发生错误的回调方法
- this.websocket.onerror = function (ev) {
- console.log('WebSocket连接发生错误');
- };
- //连接成功建立的回调方法
- this.websocket.onopen = function (ev) {
- console.log('WebSocket连接成功');
- // this.send('addsocket');
- };
- //接收到消息的回调方法
- this.websocket.onmessage = function (event) {
- try {
- let msg = JSON.parse(event.data);
- // let i = msg.typeId - 1;
- let i = msg.typeId;
- if (msg.userId) {
- that.$notify({
- dangerouslyUseHTMLString: true,
- showClose: true,
- customClass: 'notice_icon',
- offset: 50,
- duration: 3000,
- message:
- "<div class='notice'><img src=" +
- that.imgList[i] +
- " class='img'/><div class='notice-content'><span class='title'>新消息通知</span><span class='nowrap'>" +
- '【' +
- msg.type +
- '】' +
- msg.content +
- '</span></div></div>'
- });
- that.getMessageList();
- that.getUnreadNumber();
- }
- } catch (e) {}
- };
- //连接关闭的回调方法
- this.websocket.onclose = function (ev) {
- console.log('WebSocket连接关闭');
- this.websocket = null;
- };
- },
- send(message) {
- if (this.websocket && this.websocket != null) {
- // this.websocket.send(message);
- console.log('发送的消息:' + message);
- }
- },
- //关闭WebSocket连接
- closeWebSocket() {
- if (this.websocket != null) {
- this.websocket.close();
- }
- if (this.interval) {
- window.clearInterval(this.interval);
- }
- }
- },
- mounted() {
- let vm = this;
- window.onbeforeunload = function () {
- vm.closeWebSocket();
- };
- },
- destoryed() {
- this.closeWebSocket();
- },
- created() {
- new Promise((resolve) => {
- this.getUserInfo(resolve);
- }).then((_) => {
- let id = this.$store.state.cruUserInfo.id;
- this.initWebSocket(id);
- this.getMessageList();
- this.getUnreadNumber();
- });
- }
- };
- </script>
- <style lang='scss' scoped>
- @import '@assets/css/public-style.scss';
- .border {
- width: 1px;
- height: rem(10);
- background: #ffffffa5;
- }
- .header {
- height: rem(46);
- font-size: rem(14);
- color: #fff;
- background: #2c354a;
- display: flex;
- align-items: center;
- justify-content: space-between;
- &.expand::before {
- content: '';
- display: inline-block;
- width: rem(220);
- height: rem(46);
- background: #2c354a;
- position: absolute;
- left: 0;
- }
- &.collapse::before {
- content: '';
- display: inline-block;
- width: rem(85);
- height: rem(46);
- background: #2c354a;
- position: absolute;
- left: 0;
- }
- }
- .header-right {
- padding-right: rem(20);
- color: rgba(255, 255, 255, 0.6);
- .name-txt {
- font-size: rem(12);
- font-family: PingFangSC-Regular, PingFang SC;
- font-weight: 400;
- color: #ffffff;
- }
- }
- .header-user-con {
- display: flex;
- align-items: center;
- }
- .logout {
- margin-left: rem(20);
- i {
- color: white;
- font-size: rem(20);
- }
- }
- .iconfont {
- font-size: rem(20);
- color: rgba(255, 255, 255, 0.6);
- &:hover {
- color: rgba(255, 255, 255, 1);
- }
- }
- .user-avator {
- margin-left: rem(10);
- margin-right: rem(20);
- }
- .user-avator img {
- display: block;
- width: rem(20);
- height: rem(20);
- cursor: pointer;
- }
- .message {
- margin-right: rem(20);
- color: #000000;
- cursor: pointer;
- &:hover {
- .msg-content {
- display: block;
- background: #ffffff;
- box-shadow: 0px rem(5) rem(20) 0px rgba(144, 144, 144, 0.4);
- border-radius: rem(6);
- z-index: 200;
- }
- }
- .main {
- height: rem(46);
- line-height: rem(46);
- position: relative;
- img {
- width: rem(20);
- height: rem(20);
- vertical-align: text-bottom;
- }
- .point {
- position: absolute;
- top: rem(-4);
- right: -1px;
- width: rem(4);
- height: rem(4);
- background: #fe7271;
- border-radius: 50%;
- display: block;
- }
- }
- .msg-content {
- position: absolute;
- width: rem(380);
- background: #ffffff;
- z-index: 20;
- right: rem(20);
- top: rem(40);
- transition: all 0.3s;
- display: none;
- p {
- height: rem(40);
- line-height: rem(40);
- padding-left: rem(20);
- color: #212226;
- }
- li {
- display: flex;
- align-items: center;
- height: rem(60);
- padding-left: rem(20);
- border-top: 1px solid #f6f6f6;
- font-size: rem(12);
- cursor: auto;
- span {
- width: rem(300);
- overflow: hidden; //超出的文本隐藏
- text-overflow: ellipsis; //溢出用省略号显示
- white-space: nowrap; //溢出不换行
- display: block;
- line-height: rem(20);
- padding-left: 1rem (5);
- }
- .cont {
- color: #424656;
- text-indent: -0.4em;
- }
- .time {
- color: #7d7f87;
- }
- }
- .more {
- width: 100%;
- }
- }
- }
- </style>
|