index.vue 5.8 KB

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