commonVue.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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].sname == '/' && (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 ((_this.columnList[i].sname == window.location.pathname)||(_this.columnList[i].sname == 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 => obj.sname == 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 (_this.columnList[i].sname == '/product/' && window.location.pathname == '/product/read.html') {
  175. getDetailCommon()
  176. getSeoCommon()
  177. }
  178. if (_this.columnList[i].sname == '/solution/' && window.location.pathname == '/solution/read.html') {
  179. getDetailCommon()
  180. getSeoCommon()
  181. }
  182. if (_this.columnList[i].sname == '/news/' && window.location.pathname == '/news/read.html') {
  183. getDetailCommon()
  184. getSeoCommon()
  185. }
  186. function getDetailCommon() {
  187. //获取内容详情
  188. _this.getArticalData(_this.requestParams)
  189. //获取详情相关数据
  190. setTimeout(() => {
  191. _this.requestParams.id = ''
  192. _this.requestParams.articleid = ''
  193. _this.getArticalData(_this.requestParams, 1)
  194. }, 100)
  195. _this.currentPath = _this.columnList[i].sname //产品详情时栏目高亮
  196. _this.modelType = _this.columnList[i].modelType; //栏目图片获取
  197. _this.columnImage = _this.columnList[i].imagePath; //栏目图片获取
  198. setTimeout(() => {
  199. if (_this.modelType == 2) {
  200. document.getElementById("videoPlay").src = _this.columnImage; //url为你需要播放的视频地址
  201. document.getElementById("videoPlay").setAttribute('poster', '/assets/img/banner/poster.jpg');
  202. }
  203. }, 100)
  204. }
  205. }
  206. function getSeoCommon() {
  207. $('title').text(_this.columnList[i].categoryName);
  208. $('meta[name="description"]').attr('content', _this.columnList[i].metadescription);
  209. $('meta[name="keywords"]').attr('content', _this.columnList[i].metakeywords);
  210. }
  211. // 获取反馈弹框类型数据
  212. if (_this.columnList[i].sname == '/solution/') {
  213. _this.dialogDataArray = (_this.columnList[i].children)
  214. _this.ruleForm.platName = _this.dialogDataArray[0].categoryName;
  215. }
  216. }
  217. },
  218. //获取内容
  219. getArticalData(requestParams, type) {
  220. var _this = this
  221. if (window.location.pathname == '/news/') {
  222. _this.requestParams.order = ''
  223. }
  224. $.ajax({
  225. type: 'GET',
  226. dataType: 'json',
  227. url: window.FQDN2 + 'siteArticle/siteArticleList',
  228. data: requestParams
  229. }).done(function (res) {
  230. _this.lazyFunc()
  231. var aa
  232. var bb
  233. if (type == 1) {
  234. bb = res.data.records
  235. } else {
  236. aa = res.data.records;
  237. _this.articalArray = aa;
  238. }
  239. if (bb) {
  240. _this.articalArrayRelated = bb.filter(obj => obj.title != _this.articalArray[0].title);
  241. // _this.articalArrayRelated = bb
  242. }
  243. // _this.loading = false;
  244. _this.totalPageSzie = res.data.total
  245. _this.totalPageNum = Math.ceil(res.data.total / res.data.size);
  246. }).fail(function (err) {});
  247. },
  248. //类型点击
  249. facilityClick(e, item) {
  250. this.requestParams.categoryid = item.id
  251. this.requestParams.pageNum = 1
  252. this.getArticalData(this.requestParams)
  253. },
  254. // 分页
  255. oneInnerBox: function (param) {
  256. window.location = "./read.html?categoryid=" + param.categoryid + "&id=" + param.id + "&isUrlId=1";
  257. },
  258. enterPage: function (res) {
  259. if (this.goPage >= 1 && this.goPage <= this.totalPageNum) {
  260. this.requestParams.pageNum = this.goPage
  261. this.getArticalData(this.requestParams)
  262. } else {
  263. this.goPage = 1
  264. alert('输入页数有误!')
  265. }
  266. },
  267. currentChange: function (res) {
  268. this.requestParams.pageNum = res
  269. this.getArticalData(this.requestParams)
  270. },
  271. //申请试用
  272. goApply: function () {
  273. this.centerDialogVisible = true
  274. },
  275. lazyFunc:function(){
  276. setTimeout(function(){
  277. // 1 获取全部图片的DOM节点
  278. // 注意:querySelectorAll 值为伪数组利用扩展运算符转为真数组
  279. const images = $('.lazyContainer img')
  280. console.log(images)
  281. // 2 监听页面滚动事件
  282. window.addEventListener('scroll', lazyLoad)
  283. // 3 定义页面滚动的处理函数
  284. function lazyLoad(){
  285. for (let i = 0; i < images.length; i++) {
  286. // isVisible是否该图片位于可视区域 返回true 或false
  287. if (isVisible(images[i])) {
  288. // 将元素的自定义属性 data-src 赋值给元素的 src 属性
  289. // dataset.src 此为元素的自定义属性 data-src
  290. images[i].src = images[i].dataset.src // 等价于:img.setAttribute('src', img.getAttribute('data-src'))
  291. // 防止重复被遍历 加载完之后 删除元素不再加载
  292. images.splice(i, 1)
  293. i--
  294. }
  295. }
  296. }
  297. lazyLoad()
  298. // 4 可视区域判断函数
  299. function isVisible(img) {
  300. // 判断是否在可视区域
  301. const imgRect = img.getBoundingClientRect() // getBoundingClientRect 获取图片的动态信息
  302. return imgRect.bottom > 0 && imgRect.top < window.innerHeight && imgRect.right > 0 && imgRect.left < window.innerWidth
  303. }
  304. },100)
  305. },
  306. //弹框显示隐藏(子组件向父组件传值)
  307. handleChildEvent: function (value) {
  308. this.centerDialogVisible = value; // 处理从子组件接收到的数据
  309. },
  310. // 排序
  311. compare(property) {
  312. return function (a, b) {
  313. var value1 = a[property];
  314. var value2 = b[property];
  315. return value1 - value2;
  316. }
  317. },
  318. // 获取路由参数
  319. getQuery(name) {
  320. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  321. var r = window.location.search.substr(1).match(reg);
  322. if (r != null) return decodeURI(r[2]);
  323. return null;
  324. },
  325. //获得年月日时分秒 //传入日期//例:2020-10-27T14:36:23
  326. timeFormatSeconds(time) {
  327. var d = time ? new Date(time) : new Date();
  328. var year = d.getFullYear();
  329. var month = d.getMonth() + 1;
  330. var day = d.getDate();
  331. var hours = d.getHours();
  332. var min = d.getMinutes();
  333. var seconds = d.getSeconds();
  334. if (month < 10) month = '0' + month;
  335. if (day < 10) day = '0' + day;
  336. if (hours < 0) hours = '0' + hours;
  337. if (min < 10) min = '0' + min;
  338. if (seconds < 10) seconds = '0' + seconds;
  339. return (year + '-' + month + '-' + day + ' ' + hours + ':' + min + ':' + seconds);
  340. }
  341. }
  342. }
  343. Vue.createApp(App).use(ElementPlus).mount("#app");