index.vue 4.5 KB

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