pageHead.vue 11 KB

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