index.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <script>
  2. import langMap from "@/map/langMap";
  3. import productBanner from "@/components/banner/productBanner";
  4. import qs from "qs"
  5. import axios from "axios"
  6. import {handle} from "~/until/handle";
  7. import {isMediaView} from "@/until/mediaView";
  8. import LucencyHeader from "~/components/header/lucencyHeader.vue";
  9. import DefaultFooter from "~/components/footer/defaultFooter.vue";
  10. import {apiMap, baseUrl} from "~/map/apiMap";
  11. export default {
  12. name: "index",
  13. props:[
  14. 'uLang','pType','pKey','pProduct','pPageData',
  15. 'pSeoKey','pSeoDescription'
  16. ],
  17. components:{
  18. DefaultFooter,
  19. LucencyHeader,
  20. productBanner
  21. },
  22. head(){
  23. return {
  24. title: `合方圆-产品中心`,
  25. meta: [
  26. {
  27. hid: 'description',
  28. name: 'description',
  29. content: this.pSeoDescription? this.pSeoDescription : `合方圆科技产品中心,可选择4g,或者poe摄像头.满足多种需求,以及定制化需求`
  30. },
  31. {
  32. hid: 'keywords',
  33. name: 'keywords',
  34. content: this.pSeoKey? this.pSeoKey : `合方圆-产品中心,合方圆科技-产品中心`
  35. },
  36. ]
  37. }
  38. },
  39. async asyncData(ctx){
  40. // 获取数据
  41. let url = baseUrl + apiMap.searchProduct.path;
  42. url += `?type=all&p=1`;
  43. let [err,res] = await handle(axios.get( url));
  44. if(err){
  45. console.log(err);
  46. return {};
  47. }
  48. let result = res.data;
  49. // 获取seo 数据
  50. if(result.code === 1){
  51. let pageData = {
  52. limit: result.limit,
  53. page: result.page,
  54. total: result.total,
  55. count: result.count,
  56. }
  57. return {
  58. products: result.data,
  59. basePageData: pageData,
  60. }
  61. }else{
  62. console.error(result.msg);
  63. console.log(result);
  64. return {products:[]}
  65. }
  66. },
  67. data(){
  68. return {
  69. lang: this.uLang?this.uLang:langMap.lang.cn,
  70. seoKey: '合方圆-产品中心,合方圆科技-产品中心',
  71. seoDescription: '深圳合方圆科技产品中心',
  72. type: this.pType?this.pType:'all',
  73. key: this.pKey?this.pKey:'',
  74. page: 1,
  75. nowCount: 199,
  76. nowTotal: 2,
  77. products: this.pProduct?this.pProduct:[],
  78. basePageData: {},
  79. pageSave: {},
  80. isPhone: false
  81. }
  82. },
  83. beforeMount() {
  84. let pageData;
  85. if (this.pPageData){
  86. this.basePageData = this.pPageData
  87. }
  88. this.type = this.pType?this.pType:'all';
  89. pageData = this.basePageData;
  90. this.updatePageData(pageData);
  91. },
  92. mounted() {
  93. this.isPhone = isMediaView(0,1024);
  94. this.$root.$on('changeLang',this.switchLang);
  95. this.$root.$on('searchProductKey',this.changeProductKeyHandle);
  96. this.$root.$on('changeProductType',this.selectType);
  97. this.$root.$on('changePage',this.changePageHandle);
  98. // this.loadData();
  99. },
  100. methods:{
  101. switchLang(nextLang){
  102. if(nextLang){
  103. this.lang = nextLang;
  104. }else{
  105. if(this.lang === langMap.lang.cn){
  106. this.lang = langMap.lang.en
  107. }else{
  108. this.lang = langMap.lang.cn
  109. }
  110. }
  111. },
  112. selectType(nextType){
  113. console.log(nextType)
  114. this.type = nextType;
  115. this.page = 1;
  116. this.searchProduct();
  117. },
  118. changeProductKeyHandle(key){
  119. if(this.key === key){
  120. return 0;
  121. }
  122. this.key = key;
  123. this.page = 1;
  124. this.searchProduct();
  125. },
  126. changePageHandle(nextPage){
  127. this.page = nextPage;
  128. this.searchProduct();
  129. },
  130. async searchProduct(){
  131. // 获取数据
  132. let url = apiMap.searchProduct.path;
  133. url += `?key=${this.key}&type=${this.type}&p=${this.page}`
  134. let [err,res] = await handle(axios.get(url));
  135. if(err){ console.log(err); return null; }
  136. let result = res.data;
  137. if(result.code === 1){
  138. this.products = result.data;
  139. let pageData;
  140. if(result.total){
  141. // 更新页面信息
  142. pageData = {
  143. limit: result.limit,
  144. page: result.page,
  145. total: result.total,
  146. count: result.count,
  147. }
  148. }else if(this.pageSave[this.type] && this.pageSave[this.type][this.key]){
  149. pageData = this.pageSave[this.type][this.key]
  150. }
  151. this.updatePageData(pageData);
  152. }else{
  153. console.error(result.msg);
  154. console.log(result);
  155. }
  156. },
  157. updatePageData(pageData){
  158. if(!this.pageSave[this.type]){
  159. this.pageSave[this.type] = {}
  160. }
  161. if(!this.pageSave[this.type][this.key]){
  162. this.pageSave[this.type][this.key] = pageData;
  163. }
  164. let nowTotal = Math.ceil(pageData.total / pageData.limit);
  165. let nowCount = pageData.total;
  166. this.nowTotal = nowTotal;
  167. this.nowCount = nowCount;
  168. }
  169. }
  170. }
  171. </script>
  172. <template>
  173. <div class="">
  174. <lucency-header :lang="lang" page-key="product" :is-phone="isPhone"/>
  175. <!-- 推荐广告-->
  176. <product-banner :lang="lang" />
  177. <!-- 产品类别 -->
  178. <product-types :lang="lang" :type="type"></product-types>
  179. <!-- 产品列表 -->
  180. <product-list :lang="lang" :product-list="products"></product-list>
  181. <page-select :page="page" :count="nowCount" :total="nowTotal"></page-select>
  182. <default-footer :lang="lang"/>
  183. <site-bar wechat-src="/image/wechat.jpg"></site-bar>
  184. </div>
  185. </template>
  186. <style scoped>
  187. </style>