pageHead.vue 12 KB

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