pageHead.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <template>
  2. <header>
  3. <nav class="headerNav">
  4. <div class="inner">
  5. <div class="left">
  6. <span class="znxy">
  7. <a href="http://znxyw.org.cn/" target="_blank">中农兴业网团 {{ templateBaseStore.webSiteName }}</a>
  8. </span>
  9. <span>旗下网站 · 农业百强网站(科教文化类十强)</span>
  10. </div>
  11. <div class="right">
  12. <div class="menu">
  13. <button class="reg" @click="goTopic">
  14. <img src="http://img.bjzxtw.org.cn/master/bjzxtw/public/head/topicon1.png" alt="商圈">商圈
  15. </button>
  16. <button class="reg" @click="goAdvertising">
  17. <img src="http://img.bjzxtw.org.cn/master/bjzxtw/public/head/topicon2.png" alt="广告服务">广告服务
  18. </button>
  19. </div>
  20. <div class="right-top-menu">
  21. <button class="reg" @click="goLogin" v-show="!showToken">
  22. <img src="http://img.bjzxtw.org.cn/master/bjzxtw/public/head/topicon3.png" alt="登录">登录
  23. </button>
  24. <button class="reg" @click="goRegister" v-show="!showToken">
  25. <img src="http://img.bjzxtw.org.cn/master/bjzxtw/public/head/topicon4.png" alt="注册">注册
  26. </button>
  27. <button class="reg" @click="userCenter" v-show="showToken">
  28. <img src="http://img.bjzxtw.org.cn/master/bjzxtw/public/head/topicon3.png" alt="用户信息">{{
  29. username }}
  30. </button>
  31. <button class="reg" @click="goSearch">
  32. <img src="http://img.bjzxtw.org.cn/master/bjzxtw/public/head/searchicon.png" alt="搜索">搜索
  33. </button>
  34. </div>
  35. <ul class="userInfo11" v-if="isShow">
  36. <li @click="gotosystem">个人中心</li>
  37. <li @click="exit">退出</li>
  38. </ul>
  39. </div>
  40. </div>
  41. </nav>
  42. <div class="headerLogo">
  43. <div class="inner">
  44. <img :src="logo" :alt="webSiteName" class="left" @click="backHome">
  45. <div class="titlead" v-if="adImg">
  46. <a href="http://sannongdy.org.cn/" v-if="adImg.image_url == null" target="_blank"
  47. :title="adImg.introduce">
  48. <img :src="adImg.thumb" :alt="adImg.introduce" data-tag="imgurl.ad_tag" :id="adImg.ad_tag">
  49. </a>
  50. <a :href="adImg.image_url" v-else :title="adImg.introduce">
  51. <img :src="adImg.image_src" :alt="adImg.introduce" data-tag="imgurl.ad_tag" :id="adImg.ad_tag">
  52. </a>
  53. </div>
  54. </div>
  55. </div>
  56. <el-dialog v-model="dialogTableVisible" width="800">
  57. <div class="tips">
  58. <p>
  59. <img src="@/public/topic/tips.png"> 提示:注册请联系管理员操作!
  60. </p>
  61. <p>联系电话:010-56019387</p>
  62. <p>QQ : 2909421493 、213552413</p>
  63. </div>
  64. </el-dialog>
  65. </header>
  66. </template>
  67. <script setup>
  68. import { useTemplateBaseStore } from '@/stores/templateBase'
  69. const templateBaseStore = useTemplateBaseStore()
  70. console.log(123456)
  71. console.log(templateBaseStore.webSiteName)
  72. //1.加载基本依赖 start ---------------------------------------->
  73. import { ref, watch, onMounted } from 'vue'
  74. import { ElDialog } from 'element-plus'
  75. import { getToken, setToken, removeToken } from '@/store/useCookieStore'
  76. import { setTicket, removeTicket } from '@/store/useticketStore'
  77. //网站地址
  78. const { $webUrl, $CwebUrl, $BwebUrl, $LoginWebUrl } = useNuxtApp()
  79. //1.加载基本依赖 end ---------------------------------------->
  80. //1.登录逻辑 start ---------------------------------------->
  81. let ticket = ref('')
  82. let token = ref('')
  83. let route = useRoute()
  84. //页面每次刷新先判断一下用户状态是否已经过期了
  85. //如果没有过期再储存token
  86. ticket.value = route.query.ticket
  87. token.value = route.query.admintoken
  88. if (ticket.value) {
  89. setTicket(ticket.value)
  90. }
  91. if (token.value) {
  92. setToken(token.value)
  93. }
  94. //搜索框
  95. let keyword = useState('keyword', () => '')
  96. //用户名
  97. let username = useState('username', () => '用户中心')
  98. //是否删除
  99. let isDel = useState('isDel', () => 1)
  100. //是否显示用户中心
  101. let isShow = useState('isShow', () => false)
  102. let token1 = useState("token1", () => '')
  103. let showToken = useState("showToken", () => '')
  104. token1.value = getToken()
  105. //检测登录状态
  106. let tokenStatus = ref('');
  107. tokenStatus.value = getToken()
  108. if (tokenStatus.value == undefined) {
  109. console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
  110. console.log('错误:未获取到用户token,如果在本地测试请忽略!');
  111. console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
  112. } else {
  113. console.log(tokenStatus.value);
  114. }
  115. //点击登录按钮
  116. let goLogin = () => {
  117. //开启线上登录模式 start---------------------------------------->
  118. isDel.value = 0
  119. token1.value = getToken()
  120. //王鹏
  121. //window.open($BwebUrl + "/auth/login.php?backurl=" + $CwebUrl, '_blank');
  122. //刘佳伟
  123. //window.open($LoginWebUrl + "/api/goLogin?backurl=" + $CwebUrl, '_blank');
  124. //党云龙
  125. window.open($BwebUrl + "/#/login?backurl=" + $CwebUrl, '_blank');
  126. //开启线上登录模式 end---------------------------------------->
  127. //开启本地登录模式 start---------------------------------------->
  128. // window.location.href = $BwebUrl + "/auth/login.php?backurl=" + $CwebUrl;
  129. //"http://adminpre.bjzxtw.org.cn/auth/login.php?backurl=http://nwpre.bjzxtw.org.cn";
  130. //开启本地登录模式 end---------------------------------------->
  131. }
  132. let dialogTableVisible = ref(false)
  133. //点击注册按钮
  134. let goRegister = () => {
  135. dialogTableVisible.value = true
  136. }
  137. //点击用户中心
  138. let userCenter = () => {
  139. if (isShow.value) {
  140. isShow.value = false
  141. return
  142. } else {
  143. isShow.value = true
  144. return
  145. }
  146. }
  147. // 添加点击事件处理函数
  148. const handleClickOutside = (event) => {
  149. // 获取用户信息菜单元素
  150. const userInfoMenu = document.querySelector('.userInfo11');
  151. // 获取用户按钮元素
  152. const userButton = document.querySelector('.reg img[alt="用户信息"]')?.parentElement;
  153. // 如果点击的不是菜单本身且不是用户按钮,则隐藏菜单
  154. if (userInfoMenu &&
  155. !userInfoMenu.contains(event.target) &&
  156. !userButton?.contains(event.target)) {
  157. isShow.value = false;
  158. }
  159. }
  160. //2.登录逻辑 end ---------------------------------------->
  161. //3.跳转菜单逻辑 start ---------------------------------------->
  162. let gotosystem = () => {
  163. let url = $BwebUrl + '/#/login?userurl=' + $CwebUrl;
  164. //window.location.href = url;
  165. window.open(url, '_blank');
  166. }
  167. //点击退出按钮
  168. let exit = () => {
  169. window.location.href = $CwebUrl;
  170. isDel.value = 1
  171. token1.value = getToken()
  172. let tokendata = getToken()
  173. //王鹏
  174. // let url = $BwebUrl + '/auth/logout.php?backurl=' + $CwebUrl + '&admintoken=' + tokendata;
  175. // console.log("用户退出登录======token为:", tokendata,url)
  176. //刘佳伟
  177. let url = $LoginWebUrl + '/api/logout?backurl=' + $CwebUrl + '&admintoken=' + tokendata;
  178. window.location.href = url;
  179. removeToken()
  180. removeTicket()
  181. }
  182. //点击返回首页
  183. let backHome = () => {
  184. window.location.href = $CwebUrl;
  185. }
  186. //点击搜索按钮
  187. let goSearch = () => {
  188. const route = `/search/search?keyword=${keyword.value}`;
  189. window.location.href = route;
  190. }
  191. // 点击广告服务
  192. let goAdvertising = () => {
  193. //本地启动广告服务
  194. //window.open('/advertising?activeName=1', '_blank');
  195. //线上启动
  196. if (getToken()) {
  197. window.open($CwebUrl + '/advertising?activeName=1', '_blank');
  198. } else {
  199. goLogin()
  200. }
  201. }
  202. //点击商圈
  203. let goTopic = () => {
  204. if (getToken()) {
  205. window.open($CwebUrl + '/topic', '_blank');
  206. } else {
  207. goLogin()
  208. }
  209. }
  210. //监听token状态
  211. watch(token1, (newval, oldval) => {
  212. //console.log(newval, oldval);
  213. showToken.value = newval
  214. }, {
  215. deep: true,
  216. immediate: true,
  217. })
  218. //获得网站logo
  219. const logo = ref("")
  220. const webSiteName = ref("")
  221. let getLogo = async () => {
  222. const responseStatus = await requestDataPromise('/web/getWebsiteHead', {
  223. method: 'GET',
  224. query: {},
  225. });
  226. if (responseStatus.code == 200) {
  227. logo.value = responseStatus.data.logo;
  228. webSiteName.value = responseStatus.data.website_name;
  229. templateBaseStore.setWebSiteName(responseStatus.data.website_name)
  230. } else if (responseStatus.code == 0) {
  231. window.location.href = $CwebUrl + '/404';
  232. }
  233. }
  234. getLogo();
  235. //3.跳转菜单逻辑 end ---------------------------------------->
  236. //4.获取广告 start ---------------------------------------->
  237. let adImg = ref({})
  238. //4.获取广告 end ---------------------------------------->
  239. //5.获取登录状态 start ---------------------------------------->
  240. // 在组件卸载时移除事件监听
  241. onUnmounted(() => {
  242. document.removeEventListener('click', handleClickOutside);
  243. })
  244. onMounted(async () => {
  245. //添加监听关闭个人中心菜单
  246. document.addEventListener('click', handleClickOutside);
  247. //从客户端获取登录状态
  248. //从客户端阶段开始 持续查询登录状态
  249. let getLoginStatus = async () => {
  250. //获得token
  251. const token = getToken();
  252. const { $webUrl, $CwebUrl } = useNuxtApp();
  253. if (token == undefined) {
  254. //如果没有获取到token 不访问后端获取在线状态
  255. console.log("没有获取到token!无需查询登录状态!")
  256. showToken.value = false;
  257. } else {
  258. //如果获取到了token 访问后端查询状态
  259. console.log("已获取到用户token,开始查询登录状态!")
  260. const queryParams = new URLSearchParams({
  261. token: getToken()
  262. });
  263. let url = `${$webUrl}/api/loginStatus?${queryParams.toString()}`
  264. const responseStatus = await fetch(url, {
  265. method: 'GET',
  266. headers: {
  267. 'Content-Type': 'application/json',
  268. 'Userurl': $CwebUrl,
  269. //'token': getToken(),
  270. 'Origin': $CwebUrl
  271. }
  272. });
  273. const result = await responseStatus.json();
  274. console.log(result)
  275. if (result.code == 200) {
  276. console.log("用户已经登录!");
  277. showToken.value = true;
  278. }
  279. if (result.code == -1) {
  280. isShow.value = false;
  281. showToken.value = false;
  282. removeToken();
  283. removeTicket();
  284. ElMessage.error('您没有权限登录该网站!')
  285. }
  286. if (result.code == -2) {
  287. isShow.value = false;
  288. showToken.value = false;
  289. removeToken();
  290. removeTicket();
  291. ElMessage.error('您已退出登录!')
  292. }
  293. }
  294. }
  295. getLoginStatus();
  296. setInterval(getLoginStatus, 3000);
  297. //从客户端获取广告
  298. //从客户端获取行政职能部门 加快打开速度
  299. const { $webUrl, $CwebUrl } = useNuxtApp();
  300. //广告1
  301. let url = `${$webUrl}/web/getWebsiteAdvertisement?ad_tag=nmw_top`
  302. const responseAd1 = await fetch(url, {
  303. headers: {
  304. 'Content-Type': 'application/json',
  305. 'Userurl': $CwebUrl,
  306. 'Origin': $CwebUrl
  307. }
  308. });
  309. const resultAd1 = await responseAd1.json();
  310. adImg.value = resultAd1.data[0];
  311. //从客户端获取用户名
  312. let userInfoUrl = `${$webUrl}/user/getUserInfo`
  313. const userInfoResponse = await fetch(userInfoUrl, {
  314. headers: {
  315. 'Content-Type': 'application/json',
  316. 'Userurl': $CwebUrl,
  317. 'Origin': $CwebUrl
  318. }
  319. });
  320. const userInfo = await userInfoResponse.json();
  321. if (userInfo.code == 200) {
  322. username.value = userInfo.data.nickname
  323. }
  324. })
  325. //5.获取登录状态 end ---------------------------------------->
  326. </script>
  327. <style lang="less" scoped>
  328. @import url('@/assets/css/public/head.less');
  329. </style>