index.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <script>
  2. import langMap from "@/map/langMap";
  3. import productBanner from "@/components/banner/productBanner";
  4. import qs from "qs"
  5. import axios from "axios"
  6. import {handle} from "~/until/handle";
  7. import {isMediaView} from "@/until/mediaView";
  8. import LucencyHeader from "~/components/header/lucencyHeader.vue";
  9. import DefaultFooter from "~/components/footer/defaultFooter.vue";
  10. import {apiMap, baseUrl} from "~/map/apiMap";
  11. export default {
  12. name: "index",
  13. props:[
  14. 'uLang','pType','pKey','pProduct','pPageData',
  15. 'pSeoKey','pSeoDescription'
  16. ],
  17. components:{
  18. DefaultFooter,
  19. LucencyHeader,
  20. productBanner
  21. },
  22. head(){
  23. return {
  24. title: `合方圆-产品中心`,
  25. meta: [
  26. {
  27. hid: 'description',
  28. name: 'description',
  29. content: this.pSeoDescription? this.pSeoDescription : `合方圆科技产品中心,可选择4g,或者poe摄像头.满足多种需求,以及定制化需求`
  30. },
  31. {
  32. hid: 'keywords',
  33. name: 'keywords',
  34. content: this.pSeoKey? this.pSeoKey : `合方圆-产品中心,合方圆科技-产品中心`
  35. },
  36. ]
  37. }
  38. },
  39. async asyncData(ctx){
  40. // 获取数据
  41. let url = baseUrl + apiMap.searchProduct.path;
  42. url += `?type=all&p=1`;
  43. let [err,res] = await handle(axios.get( url));
  44. if(err){
  45. console.log(err);
  46. return {};
  47. }
  48. let result = res.data;
  49. // 获取seo 数据
  50. if(result.code === 1){
  51. let pageData = {
  52. limit: result.limit,
  53. page: result.page,
  54. total: result.total,
  55. count: result.count,
  56. }
  57. return {
  58. products: result.data,
  59. basePageData: pageData,
  60. }
  61. }else{
  62. console.error(result.msg);
  63. console.log(result);
  64. return {products:[]}
  65. }
  66. },
  67. data(){
  68. return {
  69. lang: this.uLang?this.uLang:langMap.lang.cn,
  70. seoKey: '合方圆-产品中心,合方圆科技-产品中心',
  71. seoDescription: '深圳合方圆科技产品中心',
  72. type: this.pType?this.pType:'all',
  73. key: this.pKey?this.pKey:'',
  74. page: 1,
  75. nowCount: 199,
  76. nowTotal: 2,
  77. products: this.pProduct?this.pProduct:[],
  78. basePageData: {},
  79. pageSave: {},
  80. isPhone: false,
  81. types: this.$store.getters.productTypes
  82. }
  83. },
  84. beforeMount() {
  85. let pageData;
  86. if (this.pPageData){
  87. this.basePageData = this.pPageData
  88. }
  89. this.type = this.pType?this.pType:'all';
  90. pageData = this.basePageData;
  91. this.updatePageData(pageData);
  92. },
  93. mounted() {
  94. this.isPhone = isMediaView(0,1024);
  95. this.$root.$on('changeLang',this.switchLang);
  96. this.$root.$on('searchProductKey',this.changeProductKeyHandle);
  97. this.$root.$on('changeType',this.selectType);
  98. this.$root.$on('changePage',this.changePageHandle);
  99. // this.loadData();
  100. },
  101. methods:{
  102. switchLang(nextLang){
  103. if(nextLang){
  104. this.lang = nextLang;
  105. }else{
  106. if(this.lang === langMap.lang.cn){
  107. this.lang = langMap.lang.en
  108. }else{
  109. this.lang = langMap.lang.cn
  110. }
  111. }
  112. },
  113. selectType(nextType){
  114. console.log(nextType)
  115. this.type = nextType;
  116. this.page = 1;
  117. this.searchProduct();
  118. },
  119. changeProductKeyHandle(key){
  120. if(this.key === key){
  121. return 0;
  122. }
  123. this.key = key;
  124. this.page = 1;
  125. this.searchProduct();
  126. },
  127. changePageHandle(nextPage){
  128. this.page = nextPage;
  129. this.searchProduct();
  130. },
  131. async searchProduct(){
  132. // 获取数据
  133. let url = apiMap.searchProduct.path;
  134. url += `?key=${this.key}&type=${this.type}&p=${this.page}`
  135. let [err,res] = await handle(axios.get(url));
  136. if(err){ console.log(err); return null; }
  137. let result = res.data;
  138. if(result.code === 1){
  139. this.products = result.data;
  140. let pageData;
  141. if(result.total){
  142. // 更新页面信息
  143. pageData = {
  144. limit: result.limit,
  145. page: result.page,
  146. total: result.total,
  147. count: result.count,
  148. }
  149. }else if(this.pageSave[this.type] && this.pageSave[this.type][this.key]){
  150. pageData = this.pageSave[this.type][this.key]
  151. }
  152. this.updatePageData(pageData);
  153. }else{
  154. console.error(result.msg);
  155. console.log(result);
  156. }
  157. },
  158. updatePageData(pageData){
  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. let nowTotal = Math.ceil(pageData.total / pageData.limit);
  166. let nowCount = pageData.total;
  167. this.nowTotal = nowTotal;
  168. this.nowCount = nowCount;
  169. },
  170. changeTypeHandle(nextType){
  171. console.log(nextType)
  172. this.selectType(nextType);
  173. }
  174. }
  175. }
  176. </script>
  177. <template>
  178. <div class="">
  179. <lucency-header :lang="lang" page-key="product" :is-phone="isPhone"/>
  180. <!-- 推荐广告-->
  181. <product-banner :lang="lang" />
  182. <!-- 产品类别 -->
  183. <product-types :lang="lang" :type="type" :types="types" @changeType="changeTypeHandle"></product-types>
  184. <!-- 产品列表 -->
  185. <product-list :lang="lang" :product-list="products"></product-list>
  186. <page-select :page="page" :count="nowCount" :total="nowTotal"></page-select>
  187. <default-footer :lang="lang"/>
  188. <site-bar wechat-src="/image/wechat.jpg"></site-bar>
  189. </div>
  190. </template>
  191. <style scoped>
  192. </style>