index.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. pageData = this.basePageData;
  80. this.updatePageData(pageData);
  81. },
  82. mounted() {
  83. this.isPhone = isMediaView(0,1024);
  84. this.$root.$on('changeLang',this.switchLang);
  85. this.$root.$on('searchProductKey',this.changeProductKeyHandle);
  86. this.$root.$on('changeProductType',this.selectType);
  87. this.$root.$on('changePage',this.changePageHandle);
  88. // this.loadData();
  89. },
  90. methods:{
  91. switchLang(nextLang){
  92. if(nextLang){
  93. this.lang = nextLang;
  94. }else{
  95. if(this.lang === langMap.lang.cn){
  96. this.lang = langMap.lang.en
  97. }else{
  98. this.lang = langMap.lang.cn
  99. }
  100. }
  101. },
  102. selectType(nextType){
  103. console.log(nextType)
  104. this.type = nextType;
  105. this.page = 1;
  106. this.searchProduct();
  107. },
  108. changeProductKeyHandle(key){
  109. if(this.key === key){
  110. return 0;
  111. }
  112. this.key = key;
  113. this.page = 1;
  114. this.searchProduct();
  115. },
  116. changePageHandle(nextPage){
  117. this.page = nextPage;
  118. this.searchProduct();
  119. },
  120. async searchProduct(){
  121. // 获取数据
  122. let url = apiMap.searchProduct.path;
  123. url += `?key=${this.key}&type=${this.type}&p=${this.page}`
  124. let [err,res] = await handle(axios.get(url));
  125. if(err){ console.log(err); return null; }
  126. let result = res.data;
  127. if(result.code === 1){
  128. this.products = result.data;
  129. let pageData;
  130. if(result.total){
  131. // 更新页面信息
  132. pageData = {
  133. limit: result.limit,
  134. page: result.page,
  135. total: result.total,
  136. count: result.count,
  137. }
  138. }else if(this.pageSave[this.type] && this.pageSave[this.type][this.key]){
  139. pageData = this.pageSave[this.type][this.key]
  140. }
  141. this.updatePageData(pageData);
  142. }else{
  143. console.error(result.msg);
  144. console.log(result);
  145. }
  146. },
  147. updatePageData(pageData){
  148. if(!this.pageSave[this.type]){
  149. this.pageSave[this.type] = {}
  150. }
  151. if(!this.pageSave[this.type][this.key]){
  152. this.pageSave[this.type][this.key] = pageData;
  153. }
  154. let nowTotal = Math.ceil(pageData.total / pageData.limit);
  155. let nowCount = pageData.total;
  156. this.nowTotal = nowTotal;
  157. this.nowCount = nowCount;
  158. }
  159. }
  160. }
  161. </script>
  162. <style scoped>
  163. </style>