index.vue 4.7 KB

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