pageHead.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. //本地启动
  200. // window.open('/topic', '_blank');
  201. //线上启动
  202. if (getToken()) {
  203. window.open($CwebUrl + '/topic', '_blank');
  204. } else {
  205. goLogin()
  206. }
  207. }
  208. //监听token状态
  209. watch(token1, (newval, oldval) => {
  210. //console.log(newval, oldval);
  211. showToken.value = newval
  212. }, {
  213. deep: true,
  214. immediate: true,
  215. })
  216. //获得网站logo
  217. const logo = ref("")
  218. const webSiteName = ref("")
  219. let getLogo = async () => {
  220. const responseStatus = await requestDataPromise('/web/getWebsiteHead', {
  221. method: 'GET',
  222. query: {},
  223. });
  224. if (responseStatus.code == 200) {
  225. logo.value = responseStatus.data.logo;
  226. webSiteName.value = responseStatus.data.website_name;
  227. } else if (responseStatus.code == 0) {
  228. window.location.href = $CwebUrl + '/404';
  229. }
  230. }
  231. getLogo();
  232. //3.跳转菜单逻辑 end ---------------------------------------->
  233. //4.获取广告 start ---------------------------------------->
  234. let adImg = ref({})
  235. //4.获取广告 end ---------------------------------------->
  236. //5.获取登录状态 start ---------------------------------------->
  237. onMounted(async () => {
  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. showToken.value = false;
  248. } else {
  249. //如果获取到了token 访问后端查询状态
  250. console.log("已获取到用户token,开始查询登录状态!")
  251. const queryParams = new URLSearchParams({
  252. token: getToken()
  253. });
  254. let url = `${$webUrl}/api/loginStatus?${queryParams.toString()}`
  255. const responseStatus = await fetch(url, {
  256. method: 'GET',
  257. headers: {
  258. 'Content-Type': 'application/json',
  259. 'Userurl': $CwebUrl,
  260. //'token': getToken(),
  261. 'Origin': $CwebUrl
  262. }
  263. });
  264. const result = await responseStatus.json();
  265. console.log(result)
  266. if (result.code == 200) {
  267. console.log("用户已经登录!");
  268. showToken.value = true;
  269. }
  270. if (result.code == -1) {
  271. isShow.value = false;
  272. showToken.value = false;
  273. removeToken();
  274. removeTicket();
  275. ElMessage.error('您没有权限登录该网站!')
  276. }
  277. if (result.code == -2) {
  278. isShow.value = false;
  279. showToken.value = false;
  280. removeToken();
  281. removeTicket();
  282. ElMessage.error('您已退出登录!')
  283. }
  284. }
  285. }
  286. getLoginStatus();
  287. setInterval(getLoginStatus, 3000);
  288. //从客户端获取广告
  289. //从客户端获取行政职能部门 加快打开速度
  290. const { $webUrl, $CwebUrl } = useNuxtApp();
  291. //广告1
  292. let url = `${$webUrl}/web/getWebsiteAdvertisement?ad_tag=nmgw_top`
  293. const responseAd1 = await fetch(url, {
  294. headers: {
  295. 'Content-Type': 'application/json',
  296. 'Userurl': $CwebUrl,
  297. 'Origin': $CwebUrl
  298. }
  299. });
  300. const resultAd1 = await responseAd1.json();
  301. adImg.value = resultAd1.data[0];
  302. //从客户端获取用户名
  303. let userInfoUrl = `${$webUrl}/user/getUserInfo`
  304. const userInfoResponse = await fetch(userInfoUrl, {
  305. headers: {
  306. 'Content-Type': 'application/json',
  307. 'Userurl': $CwebUrl,
  308. 'Origin': $CwebUrl
  309. }
  310. });
  311. const userInfo = await userInfoResponse.json();
  312. if (userInfo.code == 200) {
  313. username.value = userInfo.data.nickname
  314. }
  315. })
  316. //5.获取登录状态 end ---------------------------------------->
  317. </script>
  318. <style lang="less" scoped>
  319. @import url('@/assets/css/public/head.less');
  320. </style>