index.vue 4.5 KB

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