index.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <template>
  2. <div class="content">
  3. <lucency-header :lang="lang" page-key="news" :is-phone="isPhone"/>
  4. <item-banner :lang="lang" :title="`新闻中心`" :sub-title="`行业资讯,高新技术一应俱全`"/>
  5. <show-types :lang="lang" :type="type" :types="types"></show-types>
  6. <solution-list :lang="lang" :solution-list="news" :parent-type="'news'"></solution-list>
  7. <page-select :page="page" :count="nowCount" :total="nowTotal"></page-select>
  8. <default-footer :lang="lang"/>
  9. <site-bar></site-bar>
  10. </div>
  11. </template>
  12. <script>
  13. import axios from "axios";
  14. import qs from "qs";
  15. import langMap from "@/map/langMap";
  16. import {handle} from "@/until/handle";
  17. import {isMediaView} from "@/until/mediaView";
  18. import LucencyHeader from "~/components/header/lucencyHeader.vue";
  19. import ItemBanner from "~/components/banner/itemBanner.vue";
  20. import DefaultFooter from "~/components/footer/defaultFooter.vue";
  21. import {apiMap, baseUrl} from "~/map/apiMap";
  22. const pageLimit = 5;
  23. export default {
  24. name: "newIndex",
  25. components: {DefaultFooter, ItemBanner, LucencyHeader},
  26. props:['uLang','pType','pKey','pNews','pPageData'],
  27. head(){
  28. let headInfo = {
  29. meta: []
  30. }
  31. if (this.title)
  32. {
  33. headInfo.title = this.title;
  34. }
  35. if (this.seoDescription)
  36. {
  37. headInfo.meta.push({
  38. hid: "description",
  39. name: "description",
  40. content: this.seoDescription,
  41. })
  42. }
  43. if(this.seo_key)
  44. {
  45. headInfo.meta.push({
  46. hid: "keywords",
  47. name: "keywords",
  48. content: this.seo_key,
  49. })
  50. }
  51. return headInfo
  52. },
  53. async asyncData(ctx){
  54. let url = baseUrl + apiMap.searchNews.path;
  55. url += `?type=all&p=1`
  56. let [err,res] = await handle(axios.get( url));
  57. if(err){
  58. console.log(err);
  59. return {};
  60. }
  61. let title = "合方圆-新闻中心"
  62. let seo_key = "合方圆,合方圆科技,合方圆新闻";
  63. let seoDescription = "合方圆科技最新新闻,公司动态,产品应用";
  64. let result = res.data;
  65. if(result.code === 1){
  66. let pageData = {
  67. limit: result.limit,
  68. page: result.page,
  69. total: result.total,
  70. count: result.count,
  71. }
  72. return {
  73. title,
  74. seo_key,
  75. seoDescription,
  76. news: result.data,
  77. basePageData: pageData,
  78. }
  79. }else{
  80. console.error(result.msg);
  81. console.log(result);
  82. return {
  83. title,
  84. seo_key,
  85. seoDescription,
  86. news:[]
  87. }
  88. }
  89. },
  90. data(){
  91. return {
  92. title: '',
  93. seo_key: '',
  94. seoDescription: '',
  95. lang: this.uLang?this.uLang:langMap.lang.cn,
  96. type: this.pType?this.pType:'all',
  97. types: this.$store.getters.newsTypes,
  98. key: this.pKey?this.pKey:'',
  99. page: 1,
  100. nowCount: 199,
  101. nowTotal: 2,
  102. limit:pageLimit,
  103. news: this.pNews?this.pNews:[],
  104. basePageData: {},
  105. pageSave: {
  106. },
  107. isPhone: false
  108. }
  109. },
  110. beforeMount() {
  111. let pageData;
  112. if (this.pPageData){
  113. this.basePageData = this.pPageData
  114. }
  115. pageData = this.basePageData;
  116. this.updatePageData(pageData);
  117. },
  118. mounted() {
  119. this.isPhone = isMediaView(0,1024);
  120. this.$root.$on('changeLang',this.switchLang);
  121. this.$root.$on('changeType',this.selectType);
  122. this.$root.$on('changePage',this.changePageHandle);
  123. // this.$root.$on('changeProductType',this.selectType);
  124. },
  125. methods: {
  126. switchLang(nextLang) {
  127. if (nextLang) {
  128. this.lang = nextLang;
  129. } else {
  130. if (this.lang === langMap.lang.cn) {
  131. this.lang = langMap.lang.en
  132. } else {
  133. this.lang = langMap.lang.cn
  134. }
  135. }
  136. },
  137. selectType(nextType) {
  138. console.log(nextType)
  139. this.type = nextType;
  140. this.page = 1;
  141. this.searchNews();
  142. },
  143. changePageHandle(nextPage){
  144. this.page = nextPage;
  145. this.searchNews();
  146. },
  147. async searchNews(){
  148. // 获取数据
  149. let url = apiMap.searchNews.path;
  150. url += `?key=${this.key}&type=${this.type}&p=${this.page}`
  151. let [err,res] = await handle(axios.get(url));
  152. if(err){ console.log(err); return null; }
  153. let result = res.data;
  154. if(result.code === 1){
  155. this.news = result.data;
  156. let pageData;
  157. if(result.total){
  158. // 更新页面信息
  159. pageData = {
  160. limit: result.limit,
  161. page: result.page,
  162. total: result.total,
  163. count: result.count,
  164. }
  165. }else if(this.pageSave[this.type] && this.pageSave[this.type][this.key]){
  166. pageData = this.pageSave[this.type][this.key]
  167. }
  168. this.updatePageData(pageData);
  169. }else{
  170. console.error(result.msg);
  171. console.log(result);
  172. }
  173. },
  174. updatePageData(pageData){
  175. if(!this.pageSave[this.type]){
  176. this.pageSave[this.type] = {}
  177. }
  178. if(!this.pageSave[this.type][this.key]){
  179. this.pageSave[this.type][this.key] = pageData;
  180. }
  181. let nowTotal = Math.ceil(pageData.total / pageData.limit);
  182. let nowCount = pageData.total;
  183. this.nowTotal = nowTotal;
  184. this.nowCount = nowCount;
  185. }
  186. }
  187. }
  188. </script>
  189. <style scoped>
  190. </style>