commonVue.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. import footerCom from '/assets/js/component/footerCom.js'
  2. import feedFix from '/assets/js/component/feedFix.js'
  3. import dialogCom from '/assets/js/component/dialogCom.js'
  4. import loadingCom from '/assets/js/component/loadingCom.js'
  5. import headerCom from '/assets/js/component/headerCom.js'
  6. const App = {
  7. components: {
  8. footerCom,
  9. feedFix,
  10. dialogCom,
  11. loadingCom,
  12. headerCom
  13. },
  14. data() {
  15. return {
  16. title: 'Hello Vue',
  17. count: 0,
  18. articalArray: [], //详情数组
  19. mobileListBoolean:false,
  20. //公共
  21. id: null,
  22. columnList: [],
  23. requestParams: { //内容接口请求参数
  24. categoryid: 0, //栏目id
  25. pageNum: 1,
  26. pageSize: 9,
  27. order: 'sortindex',
  28. articleid: 0, //访问量统计
  29. id: 0, //内容详情id
  30. },
  31. dup: '',
  32. currentPath: '',
  33. articalArray: [], //详情数组
  34. articalArrayRelated: [], //相关推荐
  35. columnTypes: [], //栏目类型
  36. colL: 0,
  37. modelType: 1,
  38. columnImage: '', //栏目图片
  39. footerBoolean: false, //底部延时加载(解决页面闪烁)
  40. contentBoolean: false, //中间延时加载(优化banner图片加载慢的问题)
  41. // 公共弹框
  42. centerDialogVisible: false,
  43. dialogDataArray: [],
  44. ruleForm: {
  45. platName: '智慧安防',
  46. consultName: '',
  47. consultPhone: '',
  48. mail: '',
  49. company: '',
  50. consultContent: "",
  51. },
  52. rules: {
  53. platName: [{
  54. required: true,
  55. message: '请选择类型',
  56. trigger: 'change'
  57. }, ],
  58. consultName: [{
  59. required: true,
  60. message: '请输入姓名',
  61. trigger: 'blur'
  62. }, ],
  63. consultPhone: [{
  64. required: true,
  65. message: '请输入正确的电话号码',
  66. pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
  67. trigger: 'change'
  68. }],
  69. mail: [{
  70. required: false,
  71. type: "email",
  72. message: "请输入正确的邮箱地址",
  73. trigger: 'blur'
  74. }],
  75. },
  76. //首页
  77. homeList: [],
  78. status: false,
  79. // 新闻
  80. goPage: 1,
  81. totalPageNum: 1,
  82. totalPageSzie: 1,
  83. loading: true,
  84. categoryid: 0,
  85. }
  86. },
  87. computed: {
  88. },
  89. created: function () {
  90. this.dup = this.getQuery('dup')
  91. this.requestParams.categoryid = this.getQuery('categoryid') ? this.getQuery('categoryid') : ''
  92. this.requestParams.id = this.getQuery('id') ? this.getQuery('id') : ''
  93. this.requestParams.articleid = this.getQuery('id') ? this.getQuery('id') : ''
  94. },
  95. mounted: function () {
  96. this.getColumnData()
  97. if (window.location.hash) {
  98. // 方法一
  99. setTimeout(()=>{
  100. var aa=window.location.hash.replace("#", "")
  101. document.getElementById(aa).scrollIntoView({
  102. behavior: 'smooth' // 可选,平滑滚动效果
  103. });
  104. },100)
  105. //方法二
  106. setTimeout(()=>{
  107. this.goAnchor(window.location.hash)
  108. },100)
  109. }
  110. },
  111. methods: {
  112. goAnchor (selector) {
  113. let anchor=this.$refs.myElement
  114. console.log(anchor.offsetTop)
  115. console.log(document.documentElement.scrollTop)
  116. document.documentElement.scrollTop = anchor.offsetTop; // chrome
  117. console.log(document.documentElement.scrollTop)
  118. },
  119. goMobileIconClick(){
  120. this.mobileListBoolean=!this.mobileListBoolean
  121. },
  122. handleOpen(){
  123. },
  124. handleClose(){
  125. },
  126. //获取栏目数据
  127. getColumnData() {
  128. var _this = this;
  129. if (!localStorage.getItem('storeColumnList') || (window.location.pathname == '/' && !this.dup) || window.location.pathname == '/index.html') {
  130. $.ajax({
  131. type: 'get',
  132. dataType: 'json',
  133. url: window.FQDN2 + 'siteCategory/siteCategoryList',
  134. }).done(function (res) {
  135. _this.lazyFunc()
  136. _this.columnList = res.data;
  137. let expires = Date.now() + 1000 * 60 * 30; //半小时过期时间
  138. localStorage.setItem('storeColumnList', JSON.stringify({
  139. data: _this.columnList,
  140. expires
  141. }));
  142. _this.loading = false
  143. _this.handleColumnData()
  144. })
  145. } else {
  146. this.loading = false;
  147. this.handleColumnData()
  148. }
  149. setTimeout(function () {
  150. _this.contentBoolean = true
  151. }, 900)
  152. setTimeout(function () {
  153. _this.footerBoolean = true
  154. }, 1000)
  155. },
  156. handleColumnData() {
  157. if (window.location.pathname == '/' && !this.dup) { //if 首页模板
  158. this.currentPath = '/template1/'
  159. } else if (window.location.pathname == '/' && this.dup) { //if 首页复用模板
  160. this.currentPath = '/template1/?dup=' + this.dup
  161. } else if (window.location.href.indexOf("dup") > 0) { // if 非首页模板
  162. this.currentPath = `${window.location.pathname}?dup=` + this.getQuery('dup')
  163. } else { //非首页复用模板
  164. this.currentPath = window.location.pathname;
  165. }
  166. this.columnList = JSON.parse(localStorage.getItem('storeColumnList')).data;
  167. // 读取并检查是否过期
  168. if (JSON.parse(localStorage.getItem('storeColumnList')).expires < Date.now()) {
  169. localStorage.removeItem('storeColumnList');
  170. }
  171. var _this = this;
  172. for (let i = 0; i < _this.columnList.length; i++) {
  173. // 获取反馈弹框类型数据
  174. if (_this.columnList[i].categoryStyle == '2' && _this.columnList[i].sname == '') {
  175. _this.dialogDataArray = (_this.columnList[i].children)
  176. _this.ruleForm.platName = _this.dialogDataArray[0].categoryName;
  177. }
  178. //栏目处理
  179. //进入首页
  180. if (_this.columnList[i].categoryStyle == '1' && (window.location.pathname == '/' || window.location.pathname == '' || window.location.pathname == '/index.html')) {
  181. if ('/?dup=' + _this.columnList[i].sname == `${window.location.pathname}?dup=` + this.getQuery('dup')) {
  182. console.log('首页复用模板')
  183. getSeoCommon()
  184. var objData = _this.columnList.filter(obj => obj.categoryStyle == 1 ? '/?dup=' + obj.sname == `${window.location.pathname}?dup=` + _this.getQuery('dup') : '');
  185. _this.requestParams.categoryid = objData[0].id
  186. _this.getArticalData(_this.requestParams)
  187. _this.columnImage = _this.columnList[i].imagePath;
  188. getBannerCommon()
  189. }
  190. if (window.location.href.indexOf("dup") == -1 && !_this.columnList[i].sname) {
  191. console.log('首页模板')
  192. getSeoCommon()
  193. this.requestParams.categoryid = this.columnList[0].id
  194. _this.getArticalData(_this.requestParams)
  195. _this.columnImage = _this.columnList[0].imagePath;
  196. getBannerCommon()
  197. }
  198. }
  199. //进入其他栏目页
  200. if (('/template' + _this.columnList[i].categoryStyle + '/' == window.location.pathname) || (_this.columnList[i].categoryStyle == window.location.pathname.replace('index.html', ''))) {
  201. if (window.location.href.indexOf("dup") && ('/template' + _this.columnList[i].categoryStyle + '/?dup=' + _this.columnList[i].sname == `${window.location.pathname}?dup=` + this.getQuery('dup'))) {
  202. if (_this.columnList[i].children.length > 0) {
  203. _this.columnTypes = _this.columnList[i].children
  204. _this.requestParams.categoryid = (_this.getQuery('isUrlId') == 1) ? _this.getQuery('categoryid') : _this.columnTypes[0].id;
  205. _this.getArticalData(_this.requestParams)
  206. _this.columnImage = _this.columnList[i].imagePath;
  207. } else {
  208. var objData = _this.columnList.filter(obj => '/template' + obj.categoryStyle + '/?dup=' + obj.sname == `${window.location.pathname}?dup=` + _this.getQuery('dup'));
  209. _this.requestParams.categoryid = objData[0].id
  210. _this.getArticalData(_this.requestParams)
  211. _this.columnImage = _this.columnList[i].imagePath;
  212. }
  213. getSeoCommon()
  214. getBannerCommon()
  215. }
  216. if (window.location.href.indexOf("dup") == -1 && !_this.columnList[i].sname && ('/template' + _this.columnList[i].categoryStyle + '/' == window.location.pathname)) {
  217. if (_this.columnList[i].children.length > 0) {
  218. _this.columnTypes = _this.columnList[i].children
  219. _this.requestParams.categoryid = (_this.getQuery('isUrlId') == 1) ? _this.getQuery('categoryid') : _this.columnTypes[0].id;
  220. _this.getArticalData(_this.requestParams)
  221. _this.columnImage = _this.columnList[i].imagePath;
  222. } else {
  223. var objData = _this.columnList.filter(obj => '/template' + obj.categoryStyle + '/' == window.location.pathname.replace('index.html', ''));
  224. _this.requestParams.categoryid = objData[0].id
  225. _this.getArticalData(_this.requestParams)
  226. _this.columnImage = _this.columnList[i].imagePath;
  227. }
  228. getSeoCommon()
  229. getBannerCommon()
  230. }
  231. } else {
  232. // 进入详情页
  233. if ('/template' + _this.columnList[i].categoryStyle + '/read.html' == window.location.pathname) {
  234. getDetailCommon()
  235. }
  236. function getDetailCommon() {
  237. //获取内容详情
  238. _this.getArticalData(_this.requestParams)
  239. //获取详情相关数据
  240. setTimeout(() => {
  241. _this.requestParams.id = ''
  242. _this.requestParams.articleid = ''
  243. _this.getArticalData(_this.requestParams, 1)
  244. }, 100)
  245. //产品详情时栏目高亮
  246. if (window.location.href.indexOf("dup") && ('/template' + _this.columnList[i].categoryStyle + '/read.html' + '?dup=' + _this.columnList[i].sname == `${window.location.pathname}?dup=` + _this.getQuery('dup'))) {
  247. _this.currentPath = `/template${ _this.columnList[i].categoryStyle}/?dup=` + _this.getQuery('dup')
  248. _this.columnImage = _this.columnList[i].imagePath; //栏目图片获取
  249. getSeoCommon()
  250. getBannerCommon()
  251. }
  252. if (window.location.href.indexOf("dup") == -1 && !_this.columnList[i].sname && ('/template' + _this.columnList[i].categoryStyle + '/read.html' == window.location.pathname)) {
  253. _this.currentPath = `/template${ _this.columnList[i].categoryStyle}/`
  254. _this.columnImage = _this.columnList[i].imagePath; //栏目图片获取
  255. getSeoCommon()
  256. getBannerCommon()
  257. }
  258. }
  259. }
  260. function getBannerCommon(){
  261. _this.modelType = _this.columnList[i].modelType; //栏目图片获取
  262. setTimeout(() => {
  263. if (_this.modelType == 2) {
  264. document.getElementById("videoPlay").src = _this.columnImage; //url为你需要播放的视频地址
  265. if(!document.getElementById("videoPlay").getAttribute('poster')){
  266. document.getElementById("videoPlay").setAttribute('poster', '/assets/img/banner/poster.jpg');
  267. }
  268. }
  269. }, 100)
  270. }
  271. function getSeoCommon() {
  272. $('title').text(_this.columnList[i].categoryName);
  273. $('meta[name="description"]').attr('content', _this.columnList[i].metadescription);
  274. $('meta[name="keywords"]').attr('content', _this.columnList[i].metakeywords);
  275. }
  276. }
  277. },
  278. //获取内容
  279. getArticalData(requestParams, type) {
  280. var _this = this
  281. if (window.location.pathname == '/template4/') {
  282. _this.requestParams.order = ''
  283. }
  284. $.ajax({
  285. type: 'GET',
  286. dataType: 'json',
  287. url: window.FQDN2 + 'siteArticle/siteArticleList',
  288. data: requestParams
  289. }).done(function (res) {
  290. _this.lazyFunc()
  291. var aa
  292. var bb
  293. if (type == 1) {
  294. bb = res.data.records
  295. } else {
  296. aa = res.data.records;
  297. _this.articalArray = aa;
  298. }
  299. if (bb) {
  300. _this.articalArrayRelated = bb.filter(obj => obj.title != _this.articalArray[0].title);
  301. }
  302. _this.totalPageSzie = res.data.total
  303. _this.totalPageNum = Math.ceil(res.data.total / res.data.size);
  304. }).fail(function (err) {});
  305. },
  306. //类型点击
  307. facilityClick(e, item) {
  308. this.requestParams.categoryid = item.id
  309. this.requestParams.pageNum = 1
  310. this.getArticalData(this.requestParams)
  311. },
  312. // 分页
  313. oneInnerBox: function (param) {
  314. var aa = this.getQuery('dup')
  315. if (this.getQuery('dup')) {
  316. window.location = "./read.html?dup=" + aa + "&categoryid=" + param.categoryid + "&id=" + param.id + "&isUrlId=1";
  317. } else {
  318. window.location = "./read.html?categoryid=" + param.categoryid + "&id=" + param.id + "&isUrlId=1";
  319. }
  320. },
  321. enterPage: function (res) {
  322. if (this.goPage >= 1 && this.goPage <= this.totalPageNum) {
  323. this.requestParams.pageNum = this.goPage
  324. this.getArticalData(this.requestParams)
  325. } else {
  326. this.goPage = 1
  327. alert('输入页数有误!')
  328. }
  329. },
  330. currentChange: function (res) {
  331. this.requestParams.pageNum = res
  332. this.getArticalData(this.requestParams)
  333. },
  334. // 首页箭头下拉
  335. goDown:function (){
  336. let scrollElement = this.$refs.scrollElement;
  337. scrollElement.scrollIntoView({ behavior: 'smooth' })
  338. },
  339. //申请试用
  340. goApply: function () {
  341. this.centerDialogVisible = true
  342. },
  343. lazyFunc: function () {
  344. setTimeout(function () {
  345. // 1 获取全部图片的DOM节点
  346. // 注意:querySelectorAll 值为伪数组利用扩展运算符转为真数组
  347. const images = $('.lazyContainer img')
  348. // 2 监听页面滚动事件
  349. window.addEventListener('scroll', lazyLoad)
  350. // 3 定义页面滚动的处理函数
  351. function lazyLoad() {
  352. for (let i = 0; i < images.length; i++) {
  353. // isVisible是否该图片位于可视区域 返回true 或false
  354. if (isVisible(images[i])) {
  355. // 将元素的自定义属性 data-src 赋值给元素的 src 属性
  356. // dataset.src 此为元素的自定义属性 data-src
  357. if (images[i].dataset.src) {
  358. images[i].src = images[i].dataset.src // 等价于:img.setAttribute('src', img.getAttribute('data-src'))
  359. }
  360. // 防止重复被遍历 加载完之后 删除元素不再加载
  361. images.splice(i, 1)
  362. i--
  363. }
  364. }
  365. }
  366. lazyLoad()
  367. // 4 可视区域判断函数
  368. function isVisible(img) {
  369. // 判断是否在可视区域
  370. const imgRect = img.getBoundingClientRect() // getBoundingClientRect 获取图片的动态信息
  371. return imgRect.bottom > 0 && imgRect.top < window.innerHeight && imgRect.right > 0 && imgRect.left < window.innerWidth
  372. }
  373. }, 100)
  374. },
  375. //弹框显示隐藏(子组件向父组件传值)
  376. handleChildEvent: function (value) {
  377. this.centerDialogVisible = value; // 处理从子组件接收到的数据
  378. },
  379. // 排序
  380. compare(property) {
  381. return function (a, b) {
  382. var value1 = a[property];
  383. var value2 = b[property];
  384. return value1 - value2;
  385. }
  386. },
  387. // 获取路由参数
  388. getQuery(name) {
  389. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  390. var r = window.location.search.substr(1).match(reg);
  391. if (r != null) return decodeURI(r[2]);
  392. return null;
  393. },
  394. //获得年月日时分秒 //传入日期//例:2020-10-27T14:36:23
  395. timeFormatSeconds(time) {
  396. var d = time ? new Date(time) : new Date();
  397. var year = d.getFullYear();
  398. var month = d.getMonth() + 1;
  399. var day = d.getDate();
  400. var hours = d.getHours();
  401. var min = d.getMinutes();
  402. var seconds = d.getSeconds();
  403. if (month < 10) month = '0' + month;
  404. if (day < 10) day = '0' + day;
  405. if (hours < 0) hours = '0' + hours;
  406. if (min < 10) min = '0' + min;
  407. if (seconds < 10) seconds = '0' + seconds;
  408. return (year + '-' + month + '-' + day + ' ' + hours + ':' + min + ':' + seconds);
  409. }
  410. }
  411. }
  412. Vue.createApp(App).use(ElementPlus).mount("#app");