xzrkz-web/src/pages/NewsView/NewsViewUtil.js
2023-07-27 16:53:21 +08:00

135 lines
4.2 KiB
JavaScript

import common from '@/common/components/common.js'
import axios from 'axios';
export const goDetail = (news, navTitle, navPath, router) => {
// 只要有链接就跳转
if (news.newsContentLink) {
window.open(news.newsContentLink);
return;
}
// 文章
if (news.newsContentType === '1') {
router.push(`/newsViewDetailText/${news.newsContentId}?navTitle=${navTitle}&navPath=${encodeURIComponent(navPath)}`)
return;
}
// 图片
if (news.newsContentType === '2') {
router.push(`/newsViewDetailImage/${news.newsContentId}?navTitle=${navTitle}&navPath=${encodeURIComponent(navPath)}`)
return;
}
// 音频
if (news.newsContentType === '3') {
router.push(`/newsViewDetailAudio/${news.newsContentId}?navTitle=${navTitle}&navPath=${encodeURIComponent(navPath)}`)
return;
}
// 视频
if (news.newsContentType === '4') {
router.push(`/newsViewDetailVideo/${news.newsContentId}?navTitle=${navTitle}&navPath=${encodeURIComponent(navPath)}`)
return;
}
// PDF
if (news.newsContentType === '5') {
router.push(`/newsViewDetailPdf/${news.newsContentId}?navTitle=${navTitle}&navPath=${encodeURIComponent(navPath)}`)
return;
}
// 链接
if (news.newsContentType === '6') {
return;
}
}
/**
* 内容认证,必须登录或扫码关注公众号
* @param {*} vueSelf
* @param {*} data
* @param {*} callback
* @returns
*/
export const conetntAuth = (vueSelf, data, callback) => {
const getUserByToken = (token) => {
// 获取登录人信息
axios.get(`${common.url}app/user/get-app-user`, { headers: { 'token': token } }).then(res => {
vueSelf.userData = res.data; // 记录当前登录人信息
// 判断用户是否已关注公众号, 接口返回true或false
axios.get(`${common.url}app/wxopen/attentionrelease/${vueSelf.userData.userId}`, {}, {}).then(res => {
if(!res.data) {
// 如果没有关注公众号, 获取公众号二维码, 提示让用户扫描才能继续观看
axios.get(`${common.url}app/wxopen/qrcoderelease/${vueSelf.userData.userId}`, {}, {}).then(res => {
// 弹出遮罩, 显示公众号二维码, 接口返回的为base64图片
vueSelf.qrCodeImage = res.data
vueSelf.isQrCodeShow = true
// 打开公众号关注Sokect的监听
wxSocketInit(vueSelf.userData.userId);
}).catch(res => {
console.error(res)
}).finally(() => {
})
}
}).catch(res => {
console.error(res)
}).finally(() => {
})
}).catch(res => {
console.error(res)
}).finally(() => {
})
}
const wxSocketInit = (userId) => {
var url = `${common.url}app/wxeventrelease/${vueSelf.userData.userId}`;
var socket = new WebSocket(url.replace('https', 'wss').replace('http', 'ws'));
//打开事件
socket.onopen = function () {
console.log("sokect连接成功");
};
//获得消息事件
socket.onmessage = function ({data}) {
data = JSON.parse(data)
console.log("sokect收到消息:", data);
// 扫码后或者取消关注后会收到消息
if (data.attention) {
vueSelf.isQrCodeShow = false;
callback ? callback() : '';
} else { // 取消关注
if (!window.sessionStorage.getItem('token')) {
//跳到登录页面上让用户登录
vueSelf.$router.push('/login');
} else {
vueSelf.isQrCodeShow = true
}
}
};
//关闭事件
socket.onclose = function () {
console.log("sokect连接关闭");
};
//发生了错误事件
socket.onerror = function () {
console.log("sokect连接错误");
}
}
// 判断逻辑
if (data.newsViewAuth.indexOf('1') != -1) {
if (!window.sessionStorage.getItem('token')) {
alert("必须登录才能查看");
vueSelf.$router.push('/login');
return;
}
}
// 需要登录并且关注公众号才可以查看该新闻
if (data.newsViewAuth.indexOf('2') != -1) {
if (!window.sessionStorage.getItem('token')) {
alert("必须登录才能查看");
vueSelf.$router.push('/login');
return;
} else {
getUserByToken(window.sessionStorage.getItem('token'));
return;
}
}
callback ? callback() : '';
}