index.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <template>
  2. <div class="">
  3. <lucency-header :lang="lang" page-key="product" />
  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. </div>
  13. </template>
  14. <script>
  15. import langMap from "@/map/langMap";
  16. import productBanner from "@/components/banner/productBanner";
  17. import qs from "qs"
  18. import axios from "axios"
  19. import handle from "~/until/handle";
  20. export default {
  21. name: "index",
  22. props:['uLang','pType','pKey','pProduct'],
  23. components:{
  24. productBanner
  25. },
  26. async asyncData(ctx){
  27. // ctx.searchProduct();
  28. const queryData = {};
  29. // console.log(ctx)
  30. queryData['key']=ctx.key;
  31. queryData['type']=ctx.type;
  32. queryData['page']=1;
  33. // 获取数据
  34. let url = 'http://szhfy.com.cn/api/searchProduct.php';
  35. let [err,res] = await handle(axios.post(
  36. url,
  37. qs.stringify(queryData)
  38. ));
  39. if(err){
  40. console.log(err);
  41. return {};
  42. }
  43. let result = res.data;
  44. if(result.rcode === 1){
  45. return {products:result.data}
  46. }else{
  47. console.error(result.msg);
  48. console.log(result);
  49. return {products:[]}
  50. }
  51. },
  52. data(){
  53. return {
  54. lang: this.uLang?this.uLang:langMap.lang.cn,
  55. type: this.pType?this.pType:'all',
  56. key: this.pKey?this.pKey:'',
  57. page: 1,
  58. nowCount: 199,
  59. nowTotal: 2,
  60. products: this.pProduct?this.pProduct:[],
  61. pageSave: {
  62. }
  63. }
  64. },
  65. mounted() {
  66. this.$root.$on('changeLang',this.switchLang);
  67. this.$root.$on('searchProductKey',this.changeProductKeyHandle);
  68. this.$root.$on('changeProductType',this.selectType);
  69. this.$root.$on('changePage',this.changePageHandle);
  70. // this.loadData();
  71. },
  72. methods:{
  73. switchLang(nextLang){
  74. if(nextLang){
  75. this.lang = nextLang;
  76. }else{
  77. if(this.lang === langMap.lang.cn){
  78. this.lang = langMap.lang.en
  79. }else{
  80. this.lang = langMap.lang.cn
  81. }
  82. }
  83. },
  84. selectType(nextType){
  85. console.log(nextType)
  86. this.type = nextType;
  87. this.page = 1;
  88. this.searchProduct();
  89. },
  90. changeProductKeyHandle(key){
  91. if(this.key === key){
  92. return 0;
  93. }
  94. this.key = key;
  95. this.page = 1;
  96. this.searchProduct();
  97. },
  98. changePageHandle(nextPage){
  99. this.page = nextPage;
  100. this.searchProduct();
  101. },
  102. async searchProduct(){
  103. // const formData = new FormData();
  104. // formData.append('key',this.key);
  105. // formData.append('type',this.type);
  106. // formData.append('page',this.page);
  107. const queryData = {};
  108. // console.log(ctx)
  109. queryData['key']=this.key;
  110. queryData['type']=this.type;
  111. queryData['page']=this.page;
  112. // 获取数据
  113. let url = '/api/searchProduct.php';
  114. let data = qs.stringify(queryData);
  115. let [err,res] = await handle(this.$axios.post(
  116. url,
  117. data
  118. ));
  119. if(err){
  120. console.log(err);
  121. return null;
  122. }
  123. let result = res.data;
  124. if(result.rcode === 1){
  125. this.products = result.data;
  126. this.loadPageData(data);
  127. }else{
  128. console.error(result.msg);
  129. console.log(result);
  130. }
  131. },
  132. async loadPageData(queryData){
  133. let url = '/api/getProductPage.php';
  134. // let
  135. let err,res;
  136. let pageData = null;
  137. if(this.pageSave[this.type] && this.pageSave[this.type][this.key]){
  138. pageData = this.pageSave[this.type][this.key]
  139. }else{
  140. [err,res] = await handle(this.$axios.post(
  141. url,
  142. queryData
  143. ));
  144. if(err){
  145. console.error(err);
  146. }else{
  147. let result = res.data;
  148. if(result.rcode === 1){
  149. pageData = result.data;
  150. }else{
  151. console.error(result.msg);
  152. console.log(result);
  153. }
  154. }
  155. }
  156. if (pageData){
  157. this.nowTotal = Math.ceil(pageData.count / pageData.limit);
  158. this.nowCount = pageData.count;
  159. if(!this.pageSave[this.type]){
  160. this.pageSave[this.type] = {}
  161. }
  162. if(!this.pageSave[this.type][this.key]){
  163. this.pageSave[this.type][this.key] = pageData;
  164. }
  165. }else{
  166. this.nowTotal = 1;
  167. this.nowCount = 1;
  168. }
  169. }
  170. }
  171. }
  172. </script>
  173. <style scoped>
  174. </style>