pageHead.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. //开启线上登录模式 end---------------------------------------->
  131. //开启本地登录模式 start---------------------------------------->
  132. // window.location.href = $BwebUrl + "/auth/login.php?backurl=" + $CwebUrl;
  133. //"http://adminpre.bjzxtw.org.cn/auth/login.php?backurl=http://nwpre.bjzxtw.org.cn";
  134. //开启本地登录模式 end---------------------------------------->
  135. }
  136. let dialogTableVisible = ref(false)
  137. //点击注册按钮
  138. let goRegister = () => {
  139. dialogTableVisible.value = true
  140. }
  141. //点击用户中心
  142. let userCenter = () => {
  143. if (isShow.value) {
  144. isShow.value = false
  145. return
  146. } else {
  147. isShow.value = true
  148. return
  149. }
  150. }
  151. //2.登录逻辑 end ---------------------------------------->
  152. //3.跳转菜单逻辑 start ---------------------------------------->
  153. let gotosystem = () => {
  154. let url = $BwebUrl + '/#/login?userurl=' + $CwebUrl;
  155. isShow.value = false
  156. //window.location.href = url;
  157. window.open(url, '_blank');
  158. }
  159. //点击退出按钮
  160. let exit = () => {
  161. window.location.href = $CwebUrl;
  162. isShow.value = false
  163. isDel.value = 1
  164. token1.value = getToken()
  165. let tokendata = getToken()
  166. console.log("用户退出登录======token为:", tokendata)
  167. //王鹏
  168. //let url = $BwebUrl + '/auth/logout.php?backurl=' + $CwebUrl + '&admintoken=' + tokendata;
  169. //刘佳伟
  170. let url = $LoginWebUrl + '/api/logout?backurl=' + $CwebUrl + '&admintoken=' + tokendata;
  171. window.location.href = url;
  172. removeToken()
  173. removeTicket()
  174. }
  175. //点击返回首页
  176. let backHome = () => {
  177. window.location.href = $CwebUrl;
  178. }
  179. //点击搜索按钮
  180. let goSearch = () => {
  181. const route = `/search/search?keyword=${keyword.value}`;
  182. window.location.href = route;
  183. }
  184. // 点击广告服务
  185. let goAdvertising = () => {
  186. //本地启动广告服务
  187. //window.open('/advertising?activeName=1', '_blank');
  188. //线上启动
  189. if (getToken()) {
  190. window.open($CwebUrl + '/advertising?activeName=1', '_blank');
  191. } else {
  192. goLogin()
  193. }
  194. }
  195. //点击商圈
  196. let goTopic = () => {
  197. if (getToken()) {
  198. window.open($CwebUrl + '/topic', '_blank');
  199. } else {
  200. goLogin()
  201. }
  202. }
  203. //监听token状态
  204. watch(token1, (newval, oldval) => {
  205. //console.log(newval, oldval);
  206. showToken.value = newval
  207. }, {
  208. deep: true,
  209. immediate: true,
  210. })
  211. //获得网站logo
  212. const logo = ref("")
  213. const webSiteName = ref("")
  214. let getLogo = async () => {
  215. const responseStatus = await requestDataPromise('/web/getWebsiteHead', {
  216. method: 'GET',
  217. query: {},
  218. });
  219. logo.value = responseStatus.data.logo;
  220. webSiteName.value = responseStatus.data.website_name;
  221. }
  222. getLogo();
  223. //3.跳转菜单逻辑 end ---------------------------------------->
  224. //4.获取广告 start ---------------------------------------->
  225. let adImg = ref({})
  226. //4.获取广告 end ---------------------------------------->
  227. //5.获取登录状态 start ---------------------------------------->
  228. onMounted(async () => {
  229. //从客户端获取登录状态
  230. //从客户端阶段开始 持续查询登录状态
  231. let getLoginStatus = async () => {
  232. //获得token
  233. const token = getToken();
  234. const { $webUrl, $CwebUrl } = useNuxtApp();
  235. if(token == undefined){
  236. //如果没有获取到token 不访问后端获取在线状态
  237. console.log("没有获取到token!无需查询登录状态!")
  238. showToken.value = false;
  239. }else{
  240. //如果获取到了token 访问后端查询状态
  241. console.log("已获取到用户token,开始查询登录状态!")
  242. const queryParams = new URLSearchParams({
  243. token: getToken()
  244. });
  245. let url = `${$webUrl}/api/loginStatus?${queryParams.toString()}`
  246. const responseStatus = await fetch(url, {
  247. method: 'GET',
  248. headers: {
  249. 'Content-Type': 'application/json',
  250. 'Userurl': $CwebUrl,
  251. //'token': getToken(),
  252. 'Origin': $CwebUrl
  253. }
  254. });
  255. const result = await responseStatus.json();
  256. console.log(result)
  257. if (result.code == 200) {
  258. console.log("用户已经登录!");
  259. showToken.value = true;
  260. }
  261. if (result.code == -1) {
  262. isShow.value = false;
  263. showToken.value = false;
  264. removeToken();
  265. removeTicket();
  266. ElMessage.error('您没有权限登录该网站!')
  267. }
  268. if (result.code == -2) {
  269. isShow.value = false;
  270. showToken.value = false;
  271. removeToken();
  272. removeTicket();
  273. ElMessage.error('您已退出登录!')
  274. }
  275. }
  276. }
  277. getLoginStatus();
  278. setInterval(getLoginStatus, 3000);
  279. //从客户端获取广告
  280. //从客户端获取行政职能部门 加快打开速度
  281. const { $webUrl, $CwebUrl } = useNuxtApp();
  282. //广告1
  283. let url = `${$webUrl}/web/getWebsiteAdvertisement?ad_tag=nmgw_top`
  284. const responseAd1 = await fetch(url, {
  285. headers: {
  286. 'Content-Type': 'application/json',
  287. 'Userurl': $CwebUrl,
  288. 'Origin': $CwebUrl
  289. }
  290. });
  291. const resultAd1 = await responseAd1.json();
  292. adImg.value = resultAd1.data[0];
  293. //从客户端获取用户名
  294. let userInfoUrl = `${$webUrl}/user/getUserInfo`
  295. const userInfoResponse = await fetch(userInfoUrl, {
  296. headers: {
  297. 'Content-Type': 'application/json',
  298. 'Userurl': $CwebUrl,
  299. 'Origin': $CwebUrl
  300. }
  301. });
  302. const userInfo = await userInfoResponse.json();
  303. if (userInfo.code == 200) {
  304. username.value = userInfo.data.nickname
  305. }
  306. })
  307. //5.获取登录状态 end ---------------------------------------->
  308. </script>
  309. <style lang="less" scoped>
  310. @import url('@/assets/css/public/head.less');
  311. </style>