pageHead.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. <template>
  2. <header>
  3. <nav class="headerNav">
  4. <div class="inner">
  5. <div class="left">
  6. <span>
  7. <a href="http://znxyw.org.cn/" target="_blank">中农兴业网团</a>
  8. </span>
  9. <span>旗下网站:<span>农业百强网站(政府类30强)</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="http://img.bjzxtw.org.cn/master/bjzxtw/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. //1.加载基本依赖 start ---------------------------------------->
  69. import { ref, watch, onMounted } from 'vue'
  70. import { ElDialog } from 'element-plus'
  71. import { getToken, setToken, removeToken } from '@/store/useCookieStore'
  72. import { setTicket, removeTicket } from '@/store/useticketStore'
  73. //网站地址
  74. const { $webUrl, $CwebUrl, $BwebUrl, $LoginWebUrl } = useNuxtApp()
  75. //1.加载基本依赖 end ---------------------------------------->
  76. //1.登录逻辑 start ---------------------------------------->
  77. let ticket = ref('')
  78. let token = ref('')
  79. let route = useRoute()
  80. //页面每次刷新先判断一下用户状态是否已经过期了
  81. //如果没有过期再储存token
  82. ticket.value = route.query.ticket
  83. token.value = route.query.admintoken
  84. if (ticket.value) {
  85. setTicket(ticket.value)
  86. }
  87. if (token.value) {
  88. setToken(token.value)
  89. }
  90. //搜索框
  91. let keyword = useState('keyword', () => '')
  92. //用户名
  93. let username = useState('username', () => '用户中心')
  94. //是否删除
  95. let isDel = useState('isDel', () => 1)
  96. //是否显示用户中心
  97. let isShow = useState('isShow', () => false)
  98. let token1 = useState("token1", () => '')
  99. let showToken = useState("showToken", () => '')
  100. token1.value = getToken()
  101. //检测登录状态
  102. let tokenStatus = ref('');
  103. tokenStatus.value = getToken()
  104. if (tokenStatus.value == undefined) {
  105. console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
  106. console.log('错误:未获取到用户token,如果在本地测试请忽略!');
  107. console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
  108. } else {
  109. console.log(tokenStatus.value);
  110. }
  111. //点击登录按钮
  112. let goLogin = () => {
  113. //开启线上登录模式 start---------------------------------------->
  114. isDel.value = 0
  115. token1.value = getToken()
  116. //王鹏
  117. //window.open($BwebUrl + "/auth/login.php?backurl=" + $CwebUrl, '_blank');
  118. //刘佳伟
  119. //window.open($LoginWebUrl + "/api/goLogin?backurl=" + $CwebUrl, '_blank');
  120. //党云龙
  121. window.open($BwebUrl + "/#/login?backurl=" + $CwebUrl, '_blank');
  122. //开启线上登录模式 end---------------------------------------->
  123. //开启本地登录模式 start---------------------------------------->
  124. // window.location.href = $BwebUrl + "/auth/login.php?backurl=" + $CwebUrl;
  125. //"http://adminpre.bjzxtw.org.cn/auth/login.php?backurl=http://nwpre.bjzxtw.org.cn";
  126. //开启本地登录模式 end---------------------------------------->
  127. }
  128. let dialogTableVisible = ref(false)
  129. //点击注册按钮
  130. let goRegister = () => {
  131. dialogTableVisible.value = true
  132. }
  133. //点击用户中心
  134. let userCenter = () => {
  135. if (isShow.value) {
  136. isShow.value = false
  137. return
  138. } else {
  139. isShow.value = true
  140. return
  141. }
  142. }
  143. // 添加点击事件处理函数
  144. const handleClickOutside = (event) => {
  145. // 获取用户信息菜单元素
  146. const userInfoMenu = document.querySelector('.userInfo11');
  147. // 获取用户按钮元素
  148. const userButton = document.querySelector('.reg img[alt="用户信息"]')?.parentElement;
  149. // 如果点击的不是菜单本身且不是用户按钮,则隐藏菜单
  150. if (userInfoMenu &&
  151. !userInfoMenu.contains(event.target) &&
  152. !userButton?.contains(event.target)) {
  153. isShow.value = false;
  154. }
  155. }
  156. //2.登录逻辑 end ---------------------------------------->
  157. //3.跳转菜单逻辑 start ---------------------------------------->
  158. let gotosystem = () => {
  159. let url = $BwebUrl + '/#/login?userurl=' + $CwebUrl;
  160. //window.location.href = url;
  161. window.open(url, '_blank');
  162. }
  163. //点击退出按钮
  164. let exit = () => {
  165. window.location.href = $CwebUrl;
  166. isDel.value = 1
  167. token1.value = getToken()
  168. let tokendata = getToken()
  169. //王鹏
  170. // let url = $BwebUrl + '/auth/logout.php?backurl=' + $CwebUrl + '&admintoken=' + tokendata;
  171. // console.log("用户退出登录======token为:", tokendata,url)
  172. //刘佳伟
  173. let url = $LoginWebUrl + '/api/logout?backurl=' + $CwebUrl + '&admintoken=' + tokendata;
  174. window.location.href = url;
  175. removeToken()
  176. removeTicket()
  177. }
  178. //点击返回首页
  179. let backHome = () => {
  180. window.location.href = $CwebUrl;
  181. }
  182. //点击搜索按钮
  183. let goSearch = () => {
  184. const route = `/search/search?keyword=${keyword.value}`;
  185. window.location.href = route;
  186. }
  187. // 点击广告服务
  188. let goAdvertising = () => {
  189. //本地启动广告服务
  190. //window.open('/advertising?activeName=1', '_blank');
  191. //线上启动
  192. if (getToken()) {
  193. window.open($CwebUrl + '/advertising?activeName=1', '_blank');
  194. } else {
  195. goLogin()
  196. }
  197. }
  198. //点击商圈
  199. let goTopic = () => {
  200. if (getToken()) {
  201. window.open($CwebUrl + '/topic', '_blank');
  202. } else {
  203. goLogin()
  204. }
  205. }
  206. //监听token状态
  207. watch(token1, (newval, oldval) => {
  208. //console.log(newval, oldval);
  209. showToken.value = newval
  210. }, {
  211. deep: true,
  212. immediate: true,
  213. })
  214. //获得网站logo
  215. const logo = ref("")
  216. const webSiteName = ref("")
  217. let getLogo = async () => {
  218. const responseStatus = await requestDataPromise('/web/getWebsiteHead', {
  219. method: 'GET',
  220. query: {},
  221. });
  222. logo.value = responseStatus.data.logo;
  223. webSiteName.value = responseStatus.data.website_name;
  224. }
  225. getLogo();
  226. //3.跳转菜单逻辑 end ---------------------------------------->
  227. //4.获取广告 start ---------------------------------------->
  228. let adImg = ref({})
  229. //4.获取广告 end ---------------------------------------->
  230. //5.获取登录状态 start ---------------------------------------->
  231. // 在组件卸载时移除事件监听
  232. onUnmounted(() => {
  233. document.removeEventListener('click', handleClickOutside);
  234. })
  235. onMounted(async () => {
  236. //添加监听关闭个人中心菜单
  237. document.addEventListener('click', handleClickOutside);
  238. //从客户端获取登录状态
  239. //从客户端阶段开始 持续查询登录状态
  240. let getLoginStatus = async () => {
  241. //获得token
  242. const token = getToken();
  243. const { $webUrl, $CwebUrl } = useNuxtApp();
  244. if(token == undefined){
  245. //如果没有获取到token 不访问后端获取在线状态
  246. console.log("没有获取到token!无需查询登录状态!")
  247. }else{
  248. //如果获取到了token 访问后端查询状态
  249. console.log("已获取到用户token,开始查询登录状态!")
  250. const queryParams = new URLSearchParams({
  251. token: getToken()
  252. });
  253. let url = `${$webUrl}/api/loginStatus?${queryParams.toString()}`
  254. const responseStatus = await fetch(url, {
  255. method: 'GET',
  256. headers: {
  257. 'Content-Type': 'application/json',
  258. 'Userurl': $CwebUrl,
  259. //'token': getToken(),
  260. 'Origin': $CwebUrl
  261. }
  262. });
  263. const result = await responseStatus.json();
  264. console.log(result)
  265. if (result.code == 200) {
  266. if (result.data.isLogin == true) {
  267. console.log("用户已经登录!");
  268. showToken.value = true;
  269. } else {
  270. console.log("用户已退出登录!");
  271. showToken.value = false;
  272. //退出登录
  273. isShow.value = false;
  274. removeToken();
  275. removeTicket();
  276. }
  277. } else {
  278. console.log("用户已退出登录!");
  279. //退出登录
  280. isShow.value = false;
  281. showToken.value = false;
  282. removeToken();
  283. removeTicket();
  284. }
  285. }
  286. }
  287. getLoginStatus();
  288. setInterval(getLoginStatus, 3000);
  289. //从客户端获取广告
  290. //从客户端获取行政职能部门 加快打开速度
  291. const { $webUrl, $CwebUrl } = useNuxtApp();
  292. //广告1
  293. let url = `${$webUrl}/web/getWebsiteAdvertisement?ad_tag=snzxw_top`
  294. const responseAd1 = await fetch(url, {
  295. headers: {
  296. 'Content-Type': 'application/json',
  297. 'Userurl': $CwebUrl,
  298. 'Origin': $CwebUrl
  299. }
  300. });
  301. const resultAd1 = await responseAd1.json();
  302. adImg.value = resultAd1.data[0];
  303. //从客户端获取用户名
  304. let userInfoUrl = `${$webUrl}/user/getUserInfo`
  305. const userInfoResponse = await fetch(userInfoUrl, {
  306. headers: {
  307. 'Content-Type': 'application/json',
  308. 'Userurl': $CwebUrl,
  309. 'Origin': $CwebUrl
  310. }
  311. });
  312. const userInfo = await userInfoResponse.json();
  313. if (userInfo.code == 200) {
  314. username.value = userInfo.data.nickname
  315. }
  316. })
  317. //5.获取登录状态 end ---------------------------------------->
  318. </script>
  319. <style lang="less" scoped>
  320. .userInfo11 {
  321. width: 100px;
  322. height: 80px;
  323. background-color: #fff;
  324. box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  325. border-radius: 3px;
  326. position: absolute;
  327. top: 40px;
  328. right: 35px;
  329. font-size: 14px;
  330. z-index: 999;
  331. li {
  332. height: 40px;
  333. line-height: 40px;
  334. color: #333333;
  335. padding-left: 10px;
  336. }
  337. >li:hover {
  338. color: #028E21;
  339. }
  340. }
  341. .tips {
  342. padding: 20px 0;
  343. text-align: center;
  344. font-size: 20px;
  345. color: #40663B;
  346. font-weight: bold;
  347. img {
  348. width: 78px;
  349. height: 78px;
  350. vertical-align: middle;
  351. margin-right: 20px;
  352. p {
  353. line-height: 30px;
  354. }
  355. }
  356. p {
  357. line-height: 40px;
  358. }
  359. >p:first-child {
  360. margin-bottom: 30px;
  361. }
  362. }
  363. /* 页面头部 */
  364. header {
  365. width: 100%;
  366. // height: 201px;
  367. font-size: 12px;
  368. font-family: PingFang SC-Regular;
  369. color: #666666;
  370. background: url("http://img.bjzxtw.org.cn/master/bjzxtw/public/head/headtopbg.png") repeat-x;
  371. }
  372. .headerNav {
  373. width: 100%;
  374. height: 33px;
  375. line-height: 33px;
  376. }
  377. .headerNav .left>span {
  378. margin-right: 20px;
  379. a{
  380. color: #666666;
  381. }
  382. }
  383. .headerNav .right {
  384. display: flex;
  385. margin-right: 10px;
  386. position: relative;
  387. button {
  388. font-size: 12px;
  389. color: #666666;
  390. border: none;
  391. background: none;
  392. cursor: pointer;
  393. }
  394. .login {
  395. width: 36px;
  396. height: 19px;
  397. color: #fff;
  398. border-radius: 4px;
  399. background-color: #028E21;
  400. margin-right: 15px;
  401. cursor: pointer;
  402. }
  403. .menu,
  404. .right-top-menu {
  405. display: flex;
  406. }
  407. .reg {
  408. margin-right: 20px;
  409. display: flex;
  410. align-items: center;
  411. justify-content: center;
  412. height: 33px;
  413. img {
  414. margin-right: 5px;
  415. }
  416. }
  417. }
  418. .headerNav .right {
  419. float: right;
  420. font-size: 12px;
  421. span {
  422. margin-right: 20px;
  423. }
  424. .home,
  425. .collection {
  426. display: inline-block;
  427. width: 16px;
  428. height: 16px;
  429. vertical-align: -3px;
  430. }
  431. .home {
  432. background-image: url("http://img.bjzxtw.org.cn/master/bjzxtw/public/image/search.png");
  433. }
  434. .collection {
  435. background-image: url("http://img.bjzxtw.org.cn/master/bjzxtw/public/image/search.png");
  436. }
  437. .exit {
  438. line-height: 30px;
  439. }
  440. // .exit:hover{
  441. // color: #028E21;
  442. // }
  443. }
  444. /* // 头部logo */
  445. .headerLogo {
  446. height: 145px;
  447. position: relative;
  448. //background: url("../../public/head/topBg.png") no-repeat center;
  449. .titlead {
  450. position: absolute;
  451. bottom: 5px;
  452. right: 0;
  453. width: 830px;
  454. height: 120px;
  455. overflow: hidden;
  456. a {
  457. display: block;
  458. height: 110px;
  459. overflow: hidden;
  460. }
  461. img {
  462. width: 100%;
  463. height: 110px;
  464. }
  465. }
  466. .inner {
  467. position: relative;
  468. height: 145px;
  469. }
  470. .inner>img {
  471. // width: 342px;
  472. //height: 120px;
  473. margin-top: 22px;
  474. cursor: pointer;
  475. }
  476. /* // 搜索框部分 */
  477. .search {
  478. float: left;
  479. width: 440px;
  480. height: 40px;
  481. background-color: #fff;
  482. line-height: 40px;
  483. margin-left: 39px;
  484. margin: 71px 39px 48px;
  485. position: relative;
  486. em {
  487. display: inline-block;
  488. width: 30px;
  489. height: 30px;
  490. background: url("http://img.bjzxtw.org.cn/master/bjzxtw/public/image/search.png") no-repeat;
  491. position: absolute;
  492. top: 5px;
  493. left: 15px;
  494. }
  495. input {
  496. float: left;
  497. width: 351px;
  498. height: 40px;
  499. border: none;
  500. outline: none;
  501. padding-left: 65px;
  502. box-sizing: border-box;
  503. border: 1px solid #028E21;
  504. border-right: none;
  505. border-radius: 4px 0px 0px 4px;
  506. }
  507. button {
  508. float: right;
  509. width: 89px;
  510. height: 40px;
  511. background-color: #028E21;
  512. border-radius: 0px 4px 4px 0px;
  513. border: none;
  514. font-size: 14px;
  515. color: #fff;
  516. font-family: PingFang SC, PingFang SC;
  517. font-weight: 500;
  518. }
  519. }
  520. /* // 右侧小图标 */
  521. .serve {
  522. float: right;
  523. height: 60px;
  524. margin-top: 60px;
  525. margin-right: 60px;
  526. >li {
  527. float: left;
  528. height: 64px;
  529. margin-left: 48px;
  530. }
  531. >li>a {
  532. display: inline-block;
  533. height: 64px;
  534. }
  535. >li img {
  536. width: 29px;
  537. height: 29px;
  538. padding: 8px 14px;
  539. }
  540. >li p {
  541. height: 28px;
  542. text-align: center;
  543. color: #666666;
  544. }
  545. }
  546. }
  547. </style>