index.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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:['uLang','pType','pKey','pProduct','pPageData'],
  14. components:{
  15. DefaultFooter,
  16. LucencyHeader,
  17. productBanner
  18. },
  19. async asyncData(ctx){
  20. // 获取数据
  21. let url = baseUrl + apiMap.searchProduct.path;
  22. url += `?type=all&p=1`
  23. let [err,res] = await handle(axios.get( url));
  24. if(err){
  25. console.log(err);
  26. return {};
  27. }
  28. let result = res.data;
  29. if(result.code === 1){
  30. let pageData = {
  31. limit: result.limit,
  32. page: result.page,
  33. total: result.total,
  34. count: result.count,
  35. }
  36. return {
  37. products: result.data,
  38. basePageData: pageData,
  39. }
  40. }else{
  41. console.error(result.msg);
  42. console.log(result);
  43. return {products:[]}
  44. }
  45. },
  46. data(){
  47. return {
  48. lang: this.uLang?this.uLang:langMap.lang.cn,
  49. type: this.pType?this.pType:'all',
  50. key: this.pKey?this.pKey:'',
  51. page: 1,
  52. nowCount: 199,
  53. nowTotal: 2,
  54. products: this.pProduct?this.pProduct:[],
  55. basePageData: {},
  56. pageSave: {},
  57. isPhone: false
  58. }
  59. },
  60. beforeMount() {
  61. let pageData;
  62. if (this.pPageData){
  63. this.basePageData = this.pPageData
  64. }
  65. this.type = this.pType?this.pType:'all';
  66. pageData = this.basePageData;
  67. this.updatePageData(pageData);
  68. },
  69. mounted() {
  70. this.isPhone = isMediaView(0,1024);
  71. this.$root.$on('changeLang',this.switchLang);
  72. this.$root.$on('searchProductKey',this.changeProductKeyHandle);
  73. this.$root.$on('changeProductType',this.selectType);
  74. this.$root.$on('changePage',this.changePageHandle);
  75. // this.loadData();
  76. },
  77. methods:{
  78. switchLang(nextLang){
  79. if(nextLang){
  80. this.lang = nextLang;
  81. }else{
  82. if(this.lang === langMap.lang.cn){
  83. this.lang = langMap.lang.en
  84. }else{
  85. this.lang = langMap.lang.cn
  86. }
  87. }
  88. },
  89. selectType(nextType){
  90. console.log(nextType)
  91. this.type = nextType;
  92. this.page = 1;
  93. this.searchProduct();
  94. },
  95. changeProductKeyHandle(key){
  96. if(this.key === key){
  97. return 0;
  98. }
  99. this.key = key;
  100. this.page = 1;
  101. this.searchProduct();
  102. },
  103. changePageHandle(nextPage){
  104. this.page = nextPage;
  105. this.searchProduct();
  106. },
  107. async searchProduct(){
  108. // 获取数据
  109. let url = apiMap.searchProduct.path;
  110. url += `?key=${this.key}&type=${this.type}&p=${this.page}`
  111. let [err,res] = await handle(axios.get(url));
  112. if(err){ console.log(err); return null; }
  113. let result = res.data;
  114. if(result.code === 1){
  115. this.products = result.data;
  116. let pageData;
  117. if(result.total){
  118. // 更新页面信息
  119. pageData = {
  120. limit: result.limit,
  121. page: result.page,
  122. total: result.total,
  123. count: result.count,
  124. }
  125. }else if(this.pageSave[this.type] && this.pageSave[this.type][this.key]){
  126. pageData = this.pageSave[this.type][this.key]
  127. }
  128. this.updatePageData(pageData);
  129. }else{
  130. console.error(result.msg);
  131. console.log(result);
  132. }
  133. },
  134. updatePageData(pageData){
  135. if(!this.pageSave[this.type]){
  136. this.pageSave[this.type] = {}
  137. }
  138. if(!this.pageSave[this.type][this.key]){
  139. this.pageSave[this.type][this.key] = pageData;
  140. }
  141. let nowTotal = Math.ceil(pageData.total / pageData.limit);
  142. let nowCount = pageData.total;
  143. this.nowTotal = nowTotal;
  144. this.nowCount = nowCount;
  145. }
  146. }
  147. }
  148. </script>
  149. <template>
  150. <div class="">
  151. <lucency-header :lang="lang" page-key="product" :is-phone="isPhone"/>
  152. <!-- 推荐广告-->
  153. <product-banner :lang="lang" />
  154. <!-- 产品类别 -->
  155. <product-types :lang="lang" :type="type"></product-types>
  156. <!-- 产品列表 -->
  157. <product-list :lang="lang" :product-list="products"></product-list>
  158. <page-select :page="page" :count="nowCount" :total="nowTotal"></page-select>
  159. <default-footer :lang="lang"/>
  160. <site-bar wechat-src="/image/wechat.jpg"></site-bar>
  161. </div>
  162. </template>
  163. <style scoped>
  164. </style>