category.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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范围的
  22. if (!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('/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. quantityMin: 1,
  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('mysql_test')
  372. } else {
  373. const client = algoliasearch(
  374. '2340OWI595',
  375. '2d4c53cdcf2bab0c361e589c2c2272fa'
  376. )
  377. this.index = client.initIndex('product_au')
  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. // attributesToRetrieve: ['cat_name'], // 限定只返回某个属性
  392. }
  393. if (
  394. this.$route.query.keyword &&
  395. this.$route.fullPath.includes('searchResult')
  396. ) {
  397. keyword = decodeURIComponent(this.$route.query.keyword).trim() || ''
  398. }
  399. this.listLoading = true
  400. this.productsList = []
  401. if (!this.$route.fullPath.includes('searchResult')) {
  402. const t = this.fakeLeadTimeList.slice()
  403. t.pop()
  404. t.push({
  405. // 用id 0 是会导致 checkedLeadtime 勾选all时值为0, 巧妙避开 computedProductList 中 leadTime相关的计算,
  406. // 让 leadtimeFlag 永远都是true, 从而顺利筛选出所有商品(筛选全部通过).
  407. id: 0,
  408. name: 'All',
  409. value: [],
  410. })
  411. this.leadTimeList = t
  412. if (
  413. this.$route.query.feature &&
  414. Number(this.$route.query.feature) === 54
  415. ) {
  416. keyword = 'new product'
  417. // config.facetFilters = ['filter:New product']
  418. config.restrictSearchableAttributes = 'filter'
  419. sortFunc = (a, b) => {
  420. if (a.hasNewProduct !== b.hasNewProduct) {
  421. return b.hasNewProduct - a.hasNewProduct
  422. }
  423. if (a.ranking !== b.ranking) {
  424. return b.ranking - a.ranking
  425. }
  426. }
  427. } else {
  428. config.restrictSearchableAttributes = 'cat_name'
  429. }
  430. } else {
  431. this.leadTimeList = this.fakeLeadTimeList.slice()
  432. }
  433. const p = this.index.search(keyword, config)
  434. p.then(({ hits }) => {
  435. this.leadtimeComputedFlag = false
  436. this.priceMin = 1000
  437. this.priceMax = 0.01
  438. // 这几个是临时数据
  439. const temp = []
  440. const filterList = []
  441. const colorList = []
  442. const collectionsList = []
  443. // 临时数据 end
  444. if (hits.length) {
  445. hits
  446. .filter(i => i.status === 1 || i.status === '1' || i.status)
  447. .forEach(item => {
  448. let colorImg = ''
  449. item.colour_imgs = JSON.parse(item.colour_imgs)
  450. item.colour_imgs.sort((a, b) => a.name.length - b.name.length)
  451. if (
  452. this.$route.fullPath.includes('searchResult') &&
  453. Array.isArray(item.colour_imgs) &&
  454. item.colour_imgs.length
  455. ) {
  456. item.colour_imgs.forEach(colorItem => {
  457. if (colorItem.name && colorItem.name.length) {
  458. if (
  459. new RegExp(colorItem.name.toLowerCase(), 'i').test(
  460. keyword
  461. )
  462. ) {
  463. console.log(item.product_code, colorItem.name, 'replace')
  464. colorImg = colorItem.img
  465. } else if (
  466. /\s/.test(colorItem.name) &&
  467. colorItem.name
  468. .split(' ')
  469. .filter(a => a.length > 0)
  470. .some(b =>
  471. new RegExp(b.toLowerCase(), 'i').test(keyword)
  472. )
  473. ) {
  474. console.log(
  475. item.product_code,
  476. colorItem.name,
  477. 'advance replace'
  478. )
  479. colorImg = colorItem.img
  480. }
  481. }
  482. })
  483. item.colour_imgs.forEach(i => {
  484. if (i.name && i.name.length) {
  485. if (keyword.toLowerCase().includes(i.name.toLowerCase())) {
  486. console.log(item.product_code, i.name, 'complete replace')
  487. colorImg = i.img
  488. }
  489. }
  490. })
  491. console.log('---')
  492. }
  493. const result = {
  494. // img: item.img || '',
  495. // colour_imgs: item.colour_imgs,
  496. price_min: parseFloat(item.price_min),
  497. price_max: parseFloat(item.price_max),
  498. product_code: item.product_code,
  499. icon: Array.isArray(item.icon)
  500. ? item.icon.filter(i => i.id)
  501. : typeof item.icon === 'string'
  502. ? JSON.parse(item.icon)
  503. : [],
  504. moq: item.moq,
  505. cycle:
  506. (typeof item.cycle === 'string'
  507. ? JSON.parse(item.cycle)
  508. : item.cycle) || [],
  509. cycle_icon:
  510. (typeof item.cycle === 'string'
  511. ? JSON.parse(item.cycle)
  512. : item.cycle) || [],
  513. collection_detail:
  514. (typeof item.collection_detail === 'string'
  515. ? JSON.parse(item.collection_detail)
  516. : item.collection_detail) || [],
  517. product_name: item.name || '',
  518. info: {
  519. image: colorImg || item.img || '',
  520. id: item.id,
  521. },
  522. main: {
  523. image: colorImg || item.img || '',
  524. id: item.id,
  525. },
  526. color: [],
  527. filter: [],
  528. // 不一定有
  529. collections: [],
  530. pricePoint: [],
  531. compliance: [],
  532. create_time: item.create_time
  533. ? new Date(item.create_time).getTime()
  534. : new Date().getTime(),
  535. ranking: item.rank
  536. ? parseInt(item.rank)
  537. : Math.floor(Math.random() * 10000),
  538. }
  539. const min = Number(item.price_min)
  540. const max = Number(item.price_max)
  541. if (min < this.priceMin && min !== 0) {
  542. this.priceMin = min
  543. }
  544. if (max > this.priceMax) {
  545. this.priceMax = max
  546. }
  547. this.priceRange1 = this.priceMin
  548. this.priceRange2 = this.priceMax
  549. if (item.colour) {
  550. try {
  551. item.colour_detail = JSON.parse(item.colour_detail)
  552. } catch (e) {
  553. console.log('解析 colour_detail 出错')
  554. }
  555. if (!Array.isArray(item.colour_detail)) {
  556. // 部分数据异常, 额外添加处理逻辑
  557. item.colour_detail = [item.colour_detail]
  558. }
  559. result.color = item.colour_detail
  560. item.colour_detail.forEach(a => {
  561. if (!colorList.some(i => i.id === a.id)) {
  562. colorList.push(a)
  563. }
  564. })
  565. }
  566. if (item.filter.length) {
  567. try {
  568. item.filter_detail = JSON.parse(item.filter_detail)
  569. } catch (e) {
  570. console.log('解析 filter_detail 出错')
  571. }
  572. result.filter = item.filter_detail
  573. item.filter_detail.forEach(a => {
  574. if (!filterList.some(i => i.id === a.id)) {
  575. filterList.push(a)
  576. }
  577. })
  578. }
  579. if (item.collection.length) {
  580. try {
  581. item.collection = JSON.parse(item.collection)
  582. } catch (e) {
  583. console.log('解析 collection 出错')
  584. }
  585. item.collection.forEach(a => {
  586. result.collections.push(a)
  587. if (!collectionsList.some(i => a === i.name)) {
  588. collectionsList.push({ name: a })
  589. }
  590. })
  591. }
  592. result.hasNewProduct = result.filter.some(i => i.id === 54)
  593. result.hasBestSeller = result.collection_detail.some(
  594. i => i.id === 209
  595. )
  596. temp.push(result)
  597. })
  598. }
  599. this.featureList = filterList
  600. this.collectionsList = collectionsList
  601. this.colourList = colorList
  602. // this.decorationList = []
  603. // this.complianceList = []
  604. if (this.$route.fullPath.includes('searchResult')) {
  605. temp.sort((a, b) => {
  606. if (a.hasBestSeller !== b.hasBestSeller) {
  607. return b.hasBestSeller - a.hasBestSeller
  608. }
  609. })
  610. } else {
  611. temp.sort(sortFunc)
  612. }
  613. this.productsList = temp
  614. this.loaded = true
  615. this.listLoading = false
  616. }).catch(() => {
  617. this.listLoading = false
  618. this.productsList = []
  619. })
  620. },
  621. getListold() {
  622. const params = {
  623. order_name: this.order_name,
  624. order_type: this.order_type,
  625. first_cat: this.$route.params.firstCategory,
  626. second_cat: this.$route.params.secondCategory,
  627. third_cat: this.$route.params.thirdCategory,
  628. }
  629. if (this.loaded || this.$route.query.qty) {
  630. params.moq = this.quantity
  631. }
  632. if (this.priceId) {
  633. params.price_id = this.priceId
  634. }
  635. if (this.checkedLeadtime) {
  636. params.filter_id = this.checkedLeadtime
  637. }
  638. if (
  639. this.$route.query.keyword &&
  640. this.$route.fullPath.includes('searchResult')
  641. ) {
  642. params.keyword =
  643. decodeURIComponent(this.$route.query.keyword).trim() || ''
  644. }
  645. const config = {}
  646. if (this.cancelSource) {
  647. this.cancelSource.cancel()
  648. config.cancelToken = this.cancelSource?.token
  649. }
  650. this.listLoading = true
  651. this.productsList = []
  652. if (!this.$route.fullPath.includes('searchResult')) {
  653. const t = this.fakeLeadTimeList.slice()
  654. t.pop()
  655. t.push({
  656. // 用id 0 是会导致 checkedLeadtime 勾选all时值为0, 巧妙避开 computedProductList 中 leadTime相关的计算,
  657. // 让 leadtimeFlag 永远都是true, 从而顺利筛选出所有商品(筛选全部通过).
  658. id: 0,
  659. name: 'All',
  660. value: [],
  661. })
  662. this.leadTimeList = t
  663. } else {
  664. this.leadTimeList = this.fakeLeadTimeList.slice()
  665. }
  666. this.$axios
  667. .get(
  668. '/goods_au/list',
  669. {
  670. params,
  671. },
  672. config
  673. )
  674. .then(res => {
  675. if (res.code === 1) {
  676. this.leadtimeComputedFlag = false
  677. this.priceMin = 1000
  678. this.priceMax = 0.01
  679. this.featureList = res.result.filter || []
  680. this.collectionsList = res.result.collections || []
  681. this.complianceList = res.result.compliance || []
  682. this.colourList = res.result.color || []
  683. this.decorationList = res.result.decorations || []
  684. // this.leadTimeList =
  685. // res.result.lead_time
  686. // .sort((a, b) => a.rank - b.rank)
  687. // .map(i => {
  688. // return {
  689. // ...i,
  690. // value: this.fakeLeadTimeList.filter(f => f.id === i.id)[0]
  691. // .value,
  692. // }
  693. // }) || []
  694. this.productsList =
  695. res.result.list.map(item => {
  696. const min = Number(item.price_min)
  697. const max = Number(item.price_max)
  698. if (min < this.priceMin && min !== 0) {
  699. this.priceMin = min
  700. }
  701. if (max > this.priceMax) {
  702. this.priceMax = max
  703. }
  704. this.priceRange1 = this.priceMin
  705. this.priceRange2 = this.priceMax
  706. return {
  707. ...item,
  708. cycle_icon: item.cycle,
  709. lead_time_id: item.lead_time_id || '',
  710. info: {
  711. image: item.main?.image,
  712. id: item.main?.id,
  713. },
  714. }
  715. }) || []
  716. // 排序是为了保证一级分类考前. 分类页面直接就取他的child展示, 即列出二级分类.
  717. // 二级分类页面大概率搜不出三级子分类, 一言难尽.
  718. this.cate = res.result.cate.sort((a, b) => a.pid - b.pid)
  719. // console.log(this.cate, 'this.cate')
  720. this.listLoading = false
  721. }
  722. // this.$nextTick(() => {
  723. // window.scroll(0, 0);
  724. // });
  725. this.loaded = true
  726. })
  727. .catch(() => {
  728. this.listLoading = false
  729. this.productsList = []
  730. })
  731. },
  732. handleChangeQuantity(value) {
  733. if (value) {
  734. this.quantity = value
  735. // this.$router.replace({
  736. // path: this.$route.path,
  737. // query: {
  738. // ...this.$route.query,
  739. // qty: value,
  740. // t: new Date().getTime(),
  741. // },
  742. // })
  743. this.getList()
  744. }
  745. },
  746. handleChangePrice(arr) {
  747. console.log('price change', arr)
  748. this.priceMin = arr[0]
  749. this.priceMax = arr[1]
  750. },
  751. handleChangeLeadtime(value) {
  752. this.checkedLeadtime = value
  753. // this.$router.replace({
  754. // path: this.$route.path,
  755. // query: {
  756. // ...this.$route.query,
  757. // lead_time: value,
  758. // t: new Date().getTime(),
  759. // },
  760. // })
  761. },
  762. handleChangeColor(value) {
  763. this.checkedColor = value
  764. },
  765. handleChangeFeature(value) {
  766. this.checkedFeature = value
  767. },
  768. handleChangeCollection(value) {
  769. this.checkedCollection = value
  770. },
  771. handleChangeDecoration(value) {
  772. this.checkedDecoration = value
  773. },
  774. handleChangeCompliance(value) {
  775. this.checkedCompliance = value
  776. },
  777. handleCommand(command) {
  778. let func = defaultSortFunc
  779. if (command === 'Default') {
  780. this.order_name = ''
  781. this.order_type = ''
  782. this.command = command
  783. } else if (command === 'Product Name:A-Z') {
  784. this.order_name = 'product_name'
  785. this.order_type = 'asc'
  786. func = (a, b) => a.product_name.localeCompare(b.product_name)
  787. this.command = command
  788. } else if (command === 'Product Name:Z-A') {
  789. this.order_name = 'product_name'
  790. this.order_type = 'desc'
  791. func = (a, b) => b.product_name.localeCompare(a.product_name)
  792. this.command = command
  793. } else if (command === 'Price: Low to high') {
  794. this.order_name = 'price_min'
  795. this.order_type = 'asc'
  796. func = (a, b) => a.price_min - b.price_min
  797. this.command = command
  798. } else if (command === 'Price: High to low') {
  799. this.order_name = 'price_min'
  800. this.order_type = 'desc'
  801. func = (a, b) => b.price_max - a.price_max
  802. this.command = command
  803. } else if (command === 'create_time') {
  804. this.order_name = 'create_time'
  805. this.order_type = 'desc'
  806. func = (a, b) => b.create_time - a.create_time
  807. this.command = command
  808. }
  809. this.productsList.sort(func)
  810. },
  811. reset() {
  812. if (!this.$route.query.qty) {
  813. this.quantity = 1000
  814. }
  815. this.priceMin = 1000
  816. this.priceMax = 0.01
  817. this.checkedCollection = []
  818. this.checkedCompliance = []
  819. this.checkedLeadtime = ''
  820. this.checkedColor = []
  821. this.checkedDecoraiton = []
  822. this.checkedFeature = []
  823. this.getList()
  824. },
  825. infiniteHandler($state) {
  826. if (this.total > 12) {
  827. const temp = this.computedProductList.slice(
  828. this.listQuery.page * this.listQuery.limit,
  829. this.listQuery.page * this.listQuery.limit + this.listQuery.limit
  830. )
  831. this.listQuery.page += 1 // 下一页
  832. setTimeout(() => {
  833. if (temp.length) {
  834. this.cardList = this.cardList.concat(temp)
  835. $state.loaded()
  836. } else {
  837. $state.loaded()
  838. $state.complete()
  839. }
  840. }, 800)
  841. } else {
  842. $state.loaded()
  843. $state.complete()
  844. }
  845. },
  846. transName(str) {
  847. return str.replace(/\s+/g, '-').replace('-&', '').toLowerCase()
  848. },
  849. // 从分类树数据里面找到对应分类
  850. getCategoryFromTree(p, tree) {
  851. let result = {}
  852. if (!tree.length) return result
  853. tree.forEach(i => {
  854. if (i.id === p.id || this.transName(i.name) === p.name) {
  855. result = cloneDeep(i)
  856. result.lev = 1
  857. }
  858. if (!Array.isArray(i.child)) return
  859. i.child.forEach(secondCate => {
  860. if (
  861. secondCate.id === p.id ||
  862. this.transName(secondCate.name) === p.name
  863. ) {
  864. result = cloneDeep(secondCate)
  865. result.parentName = i.name
  866. result.lev = 2
  867. result.root = {
  868. name: i.name,
  869. id: i.id,
  870. pid: 0,
  871. }
  872. }
  873. })
  874. })
  875. return result
  876. },
  877. },
  878. }