pageHead.vue 16 KB

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