index.vue 5.8 KB

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