category.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. import axios from 'axios'
  2. import algoliasearch from 'algoliasearch'
  3. import cloneDeep from 'lodash.clonedeep'
  4. const defaultSortFunc = (a, b) => {
  5. if (a.hasBestSeller !== b.hasBestSeller) {
  6. return b.hasBestSeller - a.hasBestSeller
  7. }
  8. if (a.hasNewProduct !== b.hasNewProduct) {
  9. return b.hasNewProduct - a.hasNewProduct
  10. }
  11. if (a.ranking !== b.ranking) {
  12. return b.ranking - a.ranking
  13. }
  14. return b.icon.length - a.icon.length
  15. }
  16. export default {
  17. computed: {
  18. computedProductList() {
  19. return this.productsList.filter(item => {
  20. let moqFlag = true
  21. // moq 过滤. 要筛出1到最大输入quantities范围的. 直接进入页面忽视这个值, 只有手动更改moq才会触发enableQuantity验证.
  22. if (this.enableQuantity && (!item.moq || item.moq > this.quantity)) {
  23. moqFlag = false
  24. }
  25. // 筛选基础价格, 只要有一个价格符合价格区间都算符合条件.
  26. let priceFlag = true
  27. const min = Number(item.price_min)
  28. const max = Number(item.price_max)
  29. if (max < this.priceMin || min > this.priceMax) {
  30. priceFlag = false
  31. }
  32. // 筛选颜色
  33. let colorFlag = true
  34. if (this.checkedColor.length) {
  35. if (Array.isArray(item.color) && item.color.length) {
  36. colorFlag = item.color.some(i => this.checkedColor.includes(i.id))
  37. } else {
  38. colorFlag = false
  39. }
  40. }
  41. // collection
  42. let collectionFlag = true
  43. if (this.checkedCollection.length) {
  44. if (Array.isArray(item.collections) && item.collections.length) {
  45. collectionFlag = item.collections.some(i =>
  46. this.checkedCollection.includes(i)
  47. )
  48. } else {
  49. collectionFlag = false
  50. }
  51. }
  52. // compliance flag
  53. let complianceFlag = true
  54. if (this.checkedCompliance.length) {
  55. if (Array.isArray(item.compliance) && item.compliance.length) {
  56. complianceFlag = item.compliance.some(i =>
  57. this.checkedCompliance.includes(i.id)
  58. )
  59. } else {
  60. complianceFlag = false
  61. }
  62. }
  63. let featureFlag = true
  64. if (this.checkedFeature.length) {
  65. if (Array.isArray(item.filter) && item.filter.length) {
  66. featureFlag = item.filter.some(i =>
  67. this.checkedFeature.includes(i.id)
  68. )
  69. } else {
  70. featureFlag = false
  71. }
  72. }
  73. const decorationFlag = true
  74. // if (this.checkedDecoration.length && item.pricePoint.length) {
  75. // decorationFlag = item.pricePoint.some(i => {
  76. // return i.decorationList.some(j =>
  77. // this.checkedDecoration.includes(j.id)
  78. // )
  79. // })
  80. // }
  81. let leadtimeFlag = true
  82. if (this.checkedLeadtime) {
  83. const cycleMap = Array.from(
  84. new Set(item.cycle.map(i => Number(i.id)))
  85. )
  86. const currentLeadtimeValue = this.leadTimeList.filter(
  87. i => i.id === this.checkedLeadtime
  88. )[0].value
  89. leadtimeFlag = currentLeadtimeValue.some(id => cycleMap.includes(id))
  90. }
  91. // console.log(moqFlag)
  92. // console.log(priceFlag)
  93. // console.log(leadtimeFlag)
  94. // console.log(colorFlag)
  95. // console.log(collectionFlag)
  96. // console.log(featureFlag)
  97. return (
  98. moqFlag &&
  99. priceFlag &&
  100. leadtimeFlag &&
  101. colorFlag &&
  102. collectionFlag &&
  103. complianceFlag &&
  104. featureFlag &&
  105. decorationFlag
  106. )
  107. })
  108. },
  109. },
  110. watch: {
  111. $route: {
  112. immediate: true,
  113. handler(to) {
  114. if (to.query.lead_time) {
  115. const temp = Number(to.query.lead_time)
  116. this.checkedLeadtime = typeof temp === 'number' ? temp : ''
  117. } else {
  118. this.checkedLeadtime = ''
  119. }
  120. if (to.query.pricetype) {
  121. this.priceId = Number(to.query.pricetype)
  122. } else {
  123. this.priceId = ''
  124. }
  125. if (to.query.feature) {
  126. this.checkedFeature = [Number(to.query.feature)]
  127. } else {
  128. this.checkedFeature = []
  129. }
  130. if (to.query.qty) {
  131. const qty = Number(to.query.qty)
  132. if (typeof qty === 'number') {
  133. this.quantity = qty
  134. }
  135. } else {
  136. this.quantity = 1000
  137. }
  138. // 刷新浏览器, 获得的参数是字符串类型, 点页面上的链接, 跳转过来取到的参数是数字类型...
  139. if (to.query.feature && Number(to.query.feature) === 54) {
  140. this.order_name = ''
  141. this.order_type = ''
  142. // this.command = 'Released: Newest to Oldest'
  143. }
  144. if (this.index) {
  145. this.getList()
  146. }
  147. },
  148. },
  149. computedProductList: {
  150. immediate: true,
  151. handler() {
  152. this.featureList = this.featureList.map(item => {
  153. return { ...item, count: 0 }
  154. })
  155. this.collectionsList = this.collectionsList.map(item => {
  156. return { ...item, count: 0 }
  157. })
  158. this.colourList = this.colourList.map(item => {
  159. return { ...item, count: 0 }
  160. })
  161. this.decorationList = this.decorationList.map(item => {
  162. return { ...item, count: 0 }
  163. })
  164. if (!this.leadtimeComputedFlag) {
  165. this.leadTimeList.forEach(item => {
  166. item.count = 0
  167. })
  168. }
  169. this.computedProductList.forEach(item => {
  170. // color 颜色
  171. // console.log(item.color, 'color')
  172. item.color.forEach(color => {
  173. const idx = this.colourList.findIndex(i => i.id === color.id)
  174. if (idx > -1) {
  175. this.colourList[idx].count++
  176. }
  177. })
  178. // collection
  179. item.collections.forEach(collection => {
  180. const idx = this.collectionsList.findIndex(
  181. i => i.name === collection
  182. )
  183. if (idx > -1) {
  184. this.collectionsList[idx].count++
  185. }
  186. })
  187. // filter / feature
  188. item.filter.forEach(filter => {
  189. const idx = this.featureList.findIndex(i => i.id === filter.id)
  190. if (idx > -1) {
  191. this.featureList[idx].count++
  192. }
  193. })
  194. // 打印服务
  195. item.pricePoint.forEach(decoration => {
  196. const idx = this.decorationList.findIndex(
  197. i => i.id === decoration.id
  198. )
  199. if (idx > -1) {
  200. this.decorationList[idx].count++
  201. }
  202. })
  203. // 周期 / leadtime
  204. if (!this.leadtimeComputedFlag) {
  205. const cycleMap = Array.from(
  206. new Set(item.cycle.map(i => Number(i.id)))
  207. )
  208. for (const i of this.leadTimeList) {
  209. if (i.id === 0) i.count++
  210. let flag = false
  211. cycleMap.forEach(cycle => {
  212. if (i.value.includes(cycle)) {
  213. flag = true
  214. }
  215. })
  216. if (flag) {
  217. i.count++
  218. }
  219. }
  220. }
  221. })
  222. this.cardList = this.computedProductList.slice(0, 12)
  223. this.total = this.computedProductList.length
  224. this.listQuery = {
  225. page: 1,
  226. limit: 12,
  227. }
  228. this.leadtimeComputedFlag = true
  229. },
  230. },
  231. categoryList() {
  232. const keyword =
  233. this.$route.params.secondCategory ||
  234. this.$route.params.firstCategory ||
  235. ''
  236. if (
  237. !this.$route.fullPath.includes('searchResult') &&
  238. !this.$route.params.thirdCategory &&
  239. this.categoryList.length &&
  240. keyword.length
  241. ) {
  242. const currentCate = this.getCategoryFromTree(
  243. { name: keyword },
  244. this.categoryList
  245. )
  246. this.cate = Array.isArray(currentCate.child) ? currentCate.child : []
  247. }
  248. },
  249. },
  250. async fetch() {
  251. const res = await this.$axios.post('/uk-api/common/shopProductList')
  252. this.categoryList = res.code === 1 ? res.result : []
  253. return true
  254. },
  255. data() {
  256. return {
  257. command: 'Default',
  258. commandList: [
  259. {
  260. label: 'Default',
  261. value: 'Default',
  262. },
  263. {
  264. label: 'Price: Low to high',
  265. value: 'Price: Low to high',
  266. },
  267. {
  268. label: 'Price: High to low',
  269. value: 'Price: High to low',
  270. },
  271. {
  272. label: 'Name: A to Z',
  273. value: 'Product Name:A-Z',
  274. },
  275. {
  276. label: 'Name: Z to A',
  277. value: 'Product Name:Z-A',
  278. },
  279. {
  280. label: 'Released: Newest to Oldest',
  281. value: 'create_time',
  282. },
  283. ],
  284. total: 0,
  285. listQuery: {
  286. page: 1,
  287. limit: 12,
  288. },
  289. blackList: ['111', '111.00', '999', '999.00', 111, 999],
  290. // 筛选器数据
  291. leadTimeList: [],
  292. // 非实体数据. 里面包含虚拟字段, 可能不会跟数据表一一对应, 用来做lead times集合筛选的.
  293. fakeLeadTimeList: [
  294. {
  295. name: '1 Week or less',
  296. id: 1,
  297. value: [34, 40, 24, 22, 41, 28],
  298. },
  299. {
  300. name: '2 Weeks or less',
  301. id: 2,
  302. value: [34, 40, 24, 22, 41, 16, 35, 28],
  303. },
  304. {
  305. name: '3 Weeks or less',
  306. id: 3,
  307. value: [34, 40, 24, 22, 41, 16, 35, 23, 42, 28],
  308. },
  309. {
  310. name: '4 Weeks or less',
  311. id: 4,
  312. value: [34, 40, 24, 22, 41, 16, 35, 23, 42, 18, 33, 28],
  313. },
  314. {
  315. name: '5 Weeks or less',
  316. id: 5,
  317. value: [34, 40, 24, 22, 41, 16, 35, 23, 42, 18, 33, 26, 28],
  318. },
  319. {
  320. name: '6 Weeks or less',
  321. id: 6,
  322. value: [34, 40, 24, 22, 41, 16, 35, 23, 42, 18, 33, 26, 21, 28],
  323. },
  324. {
  325. name: '6 Weeks or more',
  326. id: 7,
  327. value: [20, 19, 29, 31, 30, 32, 36, 37, 38, 39, 25],
  328. },
  329. ],
  330. colourList: [],
  331. collectionsList: [],
  332. featureList: [],
  333. decorationList: [],
  334. complianceList: [],
  335. // 筛选器数据 end
  336. productsList: [],
  337. cardList: [],
  338. // 顶部分类数据总和, 未筛选过的
  339. cate: [],
  340. order_name: '',
  341. order_type: '',
  342. enableQuantity: false,
  343. quantity: 1000,
  344. priceMin: 1000,
  345. priceMax: 0.01,
  346. priceRange1: 1,
  347. priceRange2: 1,
  348. checkedLeadtime: '',
  349. checkedColor: [],
  350. checkedDecoration: [],
  351. checkedFeature: [],
  352. checkedCollection: [],
  353. checkedCompliance: [],
  354. priceId: '',
  355. cancelSource: null,
  356. loaded: false,
  357. // leadtime的计数仅每次请求接口后重新计算一次
  358. leadtimeComputedFlag: false,
  359. index: null,
  360. categoryList: [],
  361. }
  362. },
  363. beforeMount() {
  364. const CancelToken = axios.CancelToken
  365. this.cancelSource = CancelToken.source()
  366. if (process.env.NODE_ENV === 'development') {
  367. const client = algoliasearch(
  368. '7KGEFE6I2Z',
  369. 'e39e202ace0a2fa12ea61095e6ede35d'
  370. )
  371. this.index = client.initIndex('test_uk')
  372. } else {
  373. const client = algoliasearch(
  374. '2340OWI595',
  375. '2d4c53cdcf2bab0c361e589c2c2272fa'
  376. )
  377. this.index = client.initIndex('product_uk')
  378. }
  379. this.getList()
  380. },
  381. methods: {
  382. getList() {
  383. let sortFunc = defaultSortFunc
  384. let keyword =
  385. this.$route.params.thirdCategory ||
  386. this.$route.params.secondCategory ||
  387. this.$route.params.firstCategory ||
  388. ''
  389. const config = {
  390. hitsPerPage: 1000,
  391. restrictSearchableAttributes: [
  392. 'name',
  393. 'keywords',
  394. 'colour',
  395. 'moq',
  396. 'collection',
  397. 'filter',
  398. ],
  399. // attributesToRetrieve: ['cat_name'], // 限定只返回某个属性
  400. }
  401. if (
  402. this.$route.query.keyword &&
  403. this.$route.fullPath.includes('searchResult')
  404. ) {
  405. keyword = decodeURIComponent(this.$route.query.keyword).trim() || ''
  406. }
  407. this.listLoading = true
  408. this.productsList = []
  409. if (!this.$route.fullPath.includes('searchResult')) {
  410. const t = this.fakeLeadTimeList.slice()
  411. t.pop()
  412. t.push({
  413. // 用id 0 是会导致 checkedLeadtime 勾选all时值为0, 巧妙避开 computedProductList 中 leadTime相关的计算,
  414. // 让 leadtimeFlag 永远都是true, 从而顺利筛选出所有商品(筛选全部通过).
  415. id: 0,
  416. name: 'All',
  417. value: [],
  418. })
  419. this.leadTimeList = t
  420. if (
  421. this.$route.query.feature &&
  422. Number(this.$route.query.feature) === 54
  423. ) {
  424. keyword = 'new product'
  425. // config.facetFilters = ['filter:New product']
  426. config.restrictSearchableAttributes = 'filter'
  427. sortFunc = (a, b) => {
  428. if (a.hasNewProduct !== b.hasNewProduct) {
  429. return b.hasNewProduct - a.hasNewProduct
  430. }
  431. if (a.ranking !== b.ranking) {
  432. return b.ranking - a.ranking
  433. }
  434. }
  435. } else {
  436. config.restrictSearchableAttributes = 'cat_name'
  437. }
  438. } else {
  439. this.leadTimeList = this.fakeLeadTimeList.slice()
  440. }
  441. const p = this.index.search(keyword, config)
  442. p.then(({ hits }) => {
  443. this.leadtimeComputedFlag = false
  444. this.priceMin = 1000
  445. this.priceMax = 0.01
  446. // 这几个是临时数据
  447. const temp = []
  448. const filterList = []
  449. const colorList = []
  450. const collectionsList = []
  451. // 临时数据 end
  452. if (hits.length) {
  453. hits
  454. .filter(i => i.status === 1 || i.status === '1' || i.status)
  455. .forEach(item => {
  456. let colorImg = ''
  457. item.colour_imgs = JSON.parse(item.colour_imgs)
  458. item.colour_imgs.sort((a, b) => a.name.length - b.name.length)
  459. if (
  460. this.$route.fullPath.includes('searchResult') &&
  461. Array.isArray(item.colour_imgs) &&
  462. item.colour_imgs.length
  463. ) {
  464. item.colour_imgs.forEach(colorItem => {
  465. if (colorItem.name && colorItem.name.length) {
  466. if (
  467. new RegExp(colorItem.name.toLowerCase(), 'i').test(
  468. keyword
  469. )
  470. ) {
  471. console.log(item.product_code, colorItem.name, 'replace')
  472. colorImg = colorItem.img
  473. } else if (
  474. /\s/.test(colorItem.name) &&
  475. colorItem.name
  476. .split(' ')
  477. .filter(a => a.length > 0)
  478. .some(b =>
  479. new RegExp(b.toLowerCase(), 'i').test(keyword)
  480. )
  481. ) {
  482. console.log(
  483. item.product_code,
  484. colorItem.name,
  485. 'advance replace'
  486. )
  487. colorImg = colorItem.img
  488. }
  489. }
  490. })
  491. item.colour_imgs.forEach(i => {
  492. if (i.name && i.name.length) {
  493. if (keyword.toLowerCase().includes(i.name.toLowerCase())) {
  494. console.log(item.product_code, i.name, 'complete replace')
  495. colorImg = i.img
  496. }
  497. }
  498. })
  499. console.log('---')
  500. }
  501. const result = {
  502. ...item,
  503. // img: item.img || '',
  504. // colour_imgs: item.colour_imgs,
  505. price_min: parseFloat(item.price_min),
  506. price_max: parseFloat(item.price_max),
  507. product_code: item.product_code,
  508. icon: Array.isArray(item.icon)
  509. ? item.icon.filter(i => i.id)
  510. : typeof item.icon === 'string'
  511. ? JSON.parse(item.icon)
  512. : [],
  513. moq: item.moq,
  514. cycle:
  515. (typeof item.cycle === 'string'
  516. ? JSON.parse(item.cycle)
  517. : item.cycle) || [],
  518. cycle_icon:
  519. (typeof item.cycle === 'string'
  520. ? JSON.parse(item.cycle)
  521. : item.cycle) || [],
  522. collection_detail:
  523. (typeof item.collection_detail === 'string'
  524. ? JSON.parse(item.collection_detail)
  525. : item.collection_detail) || [],
  526. product_name: item.name || '',
  527. info: {
  528. image: colorImg || item.img || '',
  529. id: item.id,
  530. },
  531. main: {
  532. image: colorImg || item.img || '',
  533. id: item.id,
  534. },
  535. color: [],
  536. filter: [],
  537. // 不一定有
  538. collections: [],
  539. pricePoint: [],
  540. compliance: [],
  541. create_time: item.create_time
  542. ? new Date(item.create_time).getTime()
  543. : new Date().getTime(),
  544. ranking: item.rank
  545. ? parseInt(item.rank)
  546. : Math.floor(Math.random() * 10000),
  547. sale_time: item.sale_time
  548. ? new Date(item.sale_time).getTime()
  549. : 0,
  550. }
  551. const min = Number(item.price_min)
  552. const max = Number(item.price_max)
  553. if (min < this.priceMin && min !== 0) {
  554. this.priceMin = min
  555. }
  556. if (max > this.priceMax) {
  557. this.priceMax = max
  558. }
  559. this.priceRange1 = this.priceMin
  560. this.priceRange2 = this.priceMax
  561. if (item.colour) {
  562. try {
  563. item.colour_detail = JSON.parse(item.colour_detail)
  564. } catch (e) {
  565. console.log('解析 colour_detail 出错')
  566. }
  567. if (!Array.isArray(item.colour_detail)) {
  568. // 部分数据异常, 额外添加处理逻辑
  569. item.colour_detail = [item.colour_detail]
  570. }
  571. result.color = item.colour_detail
  572. item.colour_detail.forEach(a => {
  573. if (!colorList.some(i => i.id === a.id)) {
  574. colorList.push(a)
  575. }
  576. })
  577. }
  578. if (item.filter.length) {
  579. try {
  580. item.filter_detail = JSON.parse(item.filter_detail)
  581. } catch (e) {
  582. console.log('解析 filter_detail 出错')
  583. }
  584. result.filter = item.filter_detail
  585. item.filter_detail.forEach(a => {
  586. if (!filterList.some(i => i.id === a.id)) {
  587. filterList.push(a)
  588. }
  589. })
  590. }
  591. if (item.collection.length) {
  592. try {
  593. item.collection = JSON.parse(item.collection)
  594. } catch (e) {
  595. console.log('解析 collection 出错')
  596. }
  597. item.collection.forEach(a => {
  598. result.collections.push(a)
  599. if (!collectionsList.some(i => a === i.name)) {
  600. collectionsList.push({ name: a })
  601. }
  602. })
  603. }
  604. result.hasNewProduct = result.filter.some(i => i.id === 54)
  605. result.hasBestSeller = result.collection_detail.some(
  606. i => i.id === 209
  607. )
  608. temp.push(result)
  609. })
  610. }
  611. this.featureList = filterList
  612. this.collectionsList = collectionsList
  613. this.colourList = colorList
  614. // this.decorationList = []
  615. // this.complianceList = []
  616. if (!this.$route.fullPath.includes('searchResult')) {
  617. if (
  618. this.$route.query.feature &&
  619. Number(this.$route.query.feature) === 54
  620. ) {
  621. temp.sort((a, b) => b.sale_time - a.sale_time)
  622. } else {
  623. temp.sort(sortFunc)
  624. }
  625. }
  626. this.productsList = temp
  627. this.loaded = true
  628. this.listLoading = false
  629. }).catch(() => {
  630. this.listLoading = false
  631. this.productsList = []
  632. })
  633. },
  634. getListold() {
  635. const params = {
  636. order_name: this.order_name,
  637. order_type: this.order_type,
  638. first_cat: this.$route.params.firstCategory,
  639. second_cat: this.$route.params.secondCategory,
  640. third_cat: this.$route.params.thirdCategory,
  641. }
  642. if (this.loaded || this.$route.query.qty) {
  643. params.moq = this.quantity
  644. }
  645. if (this.priceId) {
  646. params.price_id = this.priceId
  647. }
  648. if (this.checkedLeadtime) {
  649. params.filter_id = this.checkedLeadtime
  650. }
  651. if (
  652. this.$route.query.keyword &&
  653. this.$route.fullPath.includes('searchResult')
  654. ) {
  655. params.keyword =
  656. decodeURIComponent(this.$route.query.keyword).trim() || ''
  657. }
  658. const config = {}
  659. if (this.cancelSource) {
  660. this.cancelSource.cancel()
  661. config.cancelToken = this.cancelSource?.token
  662. }
  663. this.listLoading = true
  664. this.productsList = []
  665. if (!this.$route.fullPath.includes('searchResult')) {
  666. const t = this.fakeLeadTimeList.slice()
  667. t.pop()
  668. t.push({
  669. // 用id 0 是会导致 checkedLeadtime 勾选all时值为0, 巧妙避开 computedProductList 中 leadTime相关的计算,
  670. // 让 leadtimeFlag 永远都是true, 从而顺利筛选出所有商品(筛选全部通过).
  671. id: 0,
  672. name: 'All',
  673. value: [],
  674. })
  675. this.leadTimeList = t
  676. } else {
  677. this.leadTimeList = this.fakeLeadTimeList.slice()
  678. }
  679. this.$axios
  680. .get(
  681. '/goods_au/list',
  682. {
  683. params,
  684. },
  685. config
  686. )
  687. .then(res => {
  688. if (res.code === 1) {
  689. this.leadtimeComputedFlag = false
  690. this.priceMin = 1000
  691. this.priceMax = 0.01
  692. this.featureList = res.result.filter || []
  693. this.collectionsList = res.result.collections || []
  694. this.complianceList = res.result.compliance || []
  695. this.colourList = res.result.color || []
  696. this.decorationList = res.result.decorations || []
  697. // this.leadTimeList =
  698. // res.result.lead_time
  699. // .sort((a, b) => a.rank - b.rank)
  700. // .map(i => {
  701. // return {
  702. // ...i,
  703. // value: this.fakeLeadTimeList.filter(f => f.id === i.id)[0]
  704. // .value,
  705. // }
  706. // }) || []
  707. this.productsList =
  708. res.result.list.map(item => {
  709. const min = Number(item.price_min)
  710. const max = Number(item.price_max)
  711. if (min < this.priceMin && min !== 0) {
  712. this.priceMin = min
  713. }
  714. if (max > this.priceMax) {
  715. this.priceMax = max
  716. }
  717. this.priceRange1 = this.priceMin
  718. this.priceRange2 = this.priceMax
  719. return {
  720. ...item,
  721. cycle_icon: item.cycle,
  722. lead_time_id: item.lead_time_id || '',
  723. info: {
  724. image: item.main?.image,
  725. id: item.main?.id,
  726. },
  727. }
  728. }) || []
  729. // 排序是为了保证一级分类考前. 分类页面直接就取他的child展示, 即列出二级分类.
  730. // 二级分类页面大概率搜不出三级子分类, 一言难尽.
  731. this.cate = res.result.cate.sort((a, b) => a.pid - b.pid)
  732. // console.log(this.cate, 'this.cate')
  733. this.listLoading = false
  734. }
  735. // this.$nextTick(() => {
  736. // window.scroll(0, 0);
  737. // });
  738. this.loaded = true
  739. })
  740. .catch(() => {
  741. this.listLoading = false
  742. this.productsList = []
  743. })
  744. },
  745. handleChangeQuantity(value) {
  746. if (value) {
  747. this.quantity = value
  748. this.enableQuantity = true
  749. // this.$router.replace({
  750. // path: this.$route.path,
  751. // query: {
  752. // ...this.$route.query,
  753. // qty: value,
  754. // t: new Date().getTime(),
  755. // },
  756. // })
  757. // this.getList()
  758. }
  759. },
  760. handleChangePrice(arr) {
  761. console.log('price change', arr)
  762. this.priceMin = arr[0]
  763. this.priceMax = arr[1]
  764. },
  765. handleChangeLeadtime(value) {
  766. this.checkedLeadtime = value
  767. // this.$router.replace({
  768. // path: this.$route.path,
  769. // query: {
  770. // ...this.$route.query,
  771. // lead_time: value,
  772. // t: new Date().getTime(),
  773. // },
  774. // })
  775. },
  776. handleChangeColor(value) {
  777. this.checkedColor = value
  778. },
  779. handleChangeFeature(value) {
  780. this.checkedFeature = value
  781. },
  782. handleChangeCollection(value) {
  783. this.checkedCollection = value
  784. },
  785. handleChangeDecoration(value) {
  786. this.checkedDecoration = value
  787. },
  788. handleChangeCompliance(value) {
  789. this.checkedCompliance = value
  790. },
  791. handleCommand(command) {
  792. let func = defaultSortFunc
  793. if (command === 'Default') {
  794. this.order_name = ''
  795. this.order_type = ''
  796. this.command = command
  797. } else if (command === 'Product Name:A-Z') {
  798. this.order_name = 'product_name'
  799. this.order_type = 'asc'
  800. func = (a, b) => a.product_name.localeCompare(b.product_name)
  801. this.command = command
  802. } else if (command === 'Product Name:Z-A') {
  803. this.order_name = 'product_name'
  804. this.order_type = 'desc'
  805. func = (a, b) => b.product_name.localeCompare(a.product_name)
  806. this.command = command
  807. } else if (command === 'Price: Low to high') {
  808. this.order_name = 'price_min'
  809. this.order_type = 'asc'
  810. func = (a, b) => a.price_min - b.price_min
  811. this.command = command
  812. } else if (command === 'Price: High to low') {
  813. this.order_name = 'price_min'
  814. this.order_type = 'desc'
  815. func = (a, b) => b.price_max - a.price_max
  816. this.command = command
  817. } else if (command === 'create_time') {
  818. this.order_name = 'create_time'
  819. this.order_type = 'desc'
  820. func = (a, b) => b.create_time - a.create_time
  821. this.command = command
  822. }
  823. this.productsList.sort(func)
  824. },
  825. reset() {
  826. if (!this.$route.query.qty) {
  827. this.quantity = 1000
  828. }
  829. this.priceMin = 1000
  830. this.priceMax = 0.01
  831. this.checkedCollection = []
  832. this.checkedCompliance = []
  833. this.checkedLeadtime = ''
  834. this.checkedColor = []
  835. this.checkedDecoraiton = []
  836. this.checkedFeature = []
  837. this.getList()
  838. },
  839. infiniteHandler($state) {
  840. if (this.total > 12) {
  841. const temp = this.computedProductList.slice(
  842. this.listQuery.page * this.listQuery.limit,
  843. this.listQuery.page * this.listQuery.limit + this.listQuery.limit
  844. )
  845. this.listQuery.page += 1 // 下一页
  846. setTimeout(() => {
  847. if (temp.length) {
  848. this.cardList = this.cardList.concat(temp)
  849. $state.loaded()
  850. } else {
  851. $state.loaded()
  852. $state.complete()
  853. }
  854. }, 800)
  855. } else {
  856. $state.loaded()
  857. $state.complete()
  858. }
  859. },
  860. transName(str) {
  861. return str.replace(/\s+/g, '-').replace('-&', '').toLowerCase()
  862. },
  863. // 从分类树数据里面找到对应分类
  864. getCategoryFromTree(p, tree) {
  865. let result = {}
  866. if (!tree.length) return result
  867. tree.forEach(i => {
  868. if (i.id === p.id || this.transName(i.name) === p.name) {
  869. result = cloneDeep(i)
  870. result.lev = 1
  871. }
  872. if (!Array.isArray(i.child)) return
  873. i.child.forEach(secondCate => {
  874. if (
  875. secondCate.id === p.id ||
  876. this.transName(secondCate.name) === p.name
  877. ) {
  878. result = cloneDeep(secondCate)
  879. result.parentName = i.name
  880. result.lev = 2
  881. result.root = {
  882. name: i.name,
  883. id: i.id,
  884. pid: 0,
  885. }
  886. }
  887. })
  888. })
  889. return result
  890. },
  891. },
  892. }