index.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const util = require('../../utils/util')
  2. Component({
  3. properties: {
  4. show: {
  5. type: Boolean,
  6. value: false
  7. },
  8. goods: { // 商品
  9. type: Object
  10. }
  11. },
  12. data: {
  13. curSpec: {},
  14. specName: [],
  15. specValue: []
  16. },
  17. observers: {
  18. goods: function(val) {
  19. if (val) {
  20. const { products, curSpec } = val
  21. let result = []
  22. const specs = products.map(p => p.specifications.join())
  23. const specName = specs[0].split(',').map(s => s.split(':')[0])
  24. const vList = specs.map(sp => sp.split(',').map(s => s.split(':')[1] ))
  25. vList.forEach(v => {
  26. v.forEach((d, i) => {
  27. const a = (result[i] = result[i] || [])
  28. if (a.findIndex(a => a == d) == -1) a.push(d)
  29. })
  30. })
  31. const curSpecValue = curSpec.specifications[0].split(',').map(s => s.split(':')[1])
  32. const specValue = result.map(res => res.map(name => ({ name, is_active: curSpecValue.findIndex(c => c == name) != -1 ? true : false })))
  33. this.setData({ specName, specValue, curSpec })
  34. }
  35. }
  36. },
  37. methods: {
  38. prevent() {
  39. return false
  40. },
  41. changeSpec({ currentTarget }) {
  42. const { index, nameindex } = currentTarget.dataset
  43. this.data.specValue[nameindex].map((spec, spec_i) => {
  44. spec.is_active = spec_i == index ? true : false
  45. return spec
  46. })
  47. this.setData({ specValue: this.data.specValue })
  48. const spec = this.data.specName.map((name, spec_index) => name + ':' + this.data.specValue[spec_index].filter(val => val.is_active)[0].name).join(',')
  49. const i = this.data.goods.products.findIndex(p => p.specifications.join() == spec)
  50. this.setData({ curSpec: this.data.goods.products[i] })
  51. },
  52. hideSpec() {
  53. this.setData({ goods: this.data.goods })
  54. this.triggerEvent('hideSpec')
  55. }
  56. }
  57. })