pageHead.vue 12 KB

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