commonVue.js 13 KB

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