Goods.js 20.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
import NP from 'number-precision'
import { mergeSimpleQuotationGoodsSelection } from '../../services/simpleQuotationCatalog.helpers.js'

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      observer: function (n) {
        console.log(n, 'show', this.data.isFlag)
        if (n) {
          this.setData({
            keyWord: '',
            strTemp: ''
          })
          if (this.data.chooseGoodsList.length == 0) {
            this.setData({
              isFlag: true
            })
          }
          this.triggerEvent('refreshRequested', { force: true })
        }
      }
    },
    goodsData: {
      type: Array,
      observer: function (n) {
        if (n.length > 0) {
          this.getOrderBaseGoodsList(n)
        }
      }
    },

    chooseGoodsList: {
      type: Array,
      observer: function (n) {
        console.log(n, 'chooseGoodsList')
        // wx.showLoading('请求中...')

        this.setGoodsActive()
      }
    },
    minPrice: {
      type: Number
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    isFlag: false,
    goodsList: [],
    recordList: [],
    specificBox: false
    // totalGoodsList:[],
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //规格选择
    handlePickerChange(e) {
      console.log(e)
      let value = e.detail.value, //下标
        { index, specindex, item } = e.currentTarget.dataset,
        { goodsList, recordList } = this.data,
        selectValue

      selectValue =
        goodsList[index].multipleSpecificationsGoods.specificationList[
          specindex
        ].specificationSelectValueList[value]
      let target = `goodsList[${index}]multipleSpecificationsGoods.specificationList[${specindex}].value`
      this.setData(
        {
          [target]: selectValue
        },
        () => {
          //监听规格是否全部选中
          this.watchAllChose(index, true)
        }
      )
    },
    async watchAllChose(index, type) {
      let { goodsList, recordList, strTemp } = this.data
      let result = goodsList[
        index
      ].multipleSpecificationsGoods.specificationList.every(item => item.value)
      let active = goodsList[index].active
      //规格全部选中逻辑处理

      if (result) {
        let arr = []
        goodsList[index].multipleSpecificationsGoods.specificationList.forEach(
          spec => {
            arr.push(spec.value)
          }
        )
        let str = arr.join(';~;')
        goodsList[index].active = true
        //  strTemp用来存储上一次选中的值,判断规格是否重新选择 ,避免页面交互闪烁
        // if (type && str != strTemp) {
        console.log(str)
        console.log(
          goodsList[index].multipleSpecificationsGoods.specificationGoodsMap[
            str
          ]
        )
        goodsList[index].multipleSpecificationsGoods.skuGoodsList = []
        if (
          goodsList[index].multipleSpecificationsGoods.specificationGoodsMap[
            str
          ]
        ) {
          let goodsArr =
            goodsList[index].multipleSpecificationsGoods.specificationGoodsMap[
              str
            ]
          //当多规格商品只有一个时,直接选中
          if (goodsArr.length == 1) {
            goodsList[index].active = true
            goodsList[index].multipleSpecificationsGoods.goods = goodsArr[0]
            goodsList[index].multipleSpecificationsGoods.goods.goodsId =
              goodsList[index].multipleSpecificationsGoods.goods.merchantGoodsId
            goodsList[
              index
            ].multipleSpecificationsGoods.goods.specificationStr = goodsList[
              index
            ].multipleSpecificationsGoods.goods.specificationData.replace(
              /;~;/g,
              '/'
            )
            recordList.forEach(record => {
              if (record.goodsFlag == 2) {
                if (
                  goodsList[index].multipleSpecificationsGoods.id ==
                  record.multipleSpecificationsGoods.id
                ) {
                  record.active = true
                  record.multipleSpecificationsGoods =
                    goodsList[index].multipleSpecificationsGoods
                }
              }
            })
          } else {
            goodsList[index].active = false
            goodsArr.forEach(item => {
              item.count = 1
              item.active = false
              item.goodsId = item.merchantGoodsId
              item.specificationStr = item.specificationData.replace(
                /;~;/g,
                '/'
              )
            })
            goodsList[index].multipleSpecificationsGoods.skuGoodsList = goodsArr
            let chooseList = await this.getChooseList(false)
            const mergedGoodsList = mergeSimpleQuotationGoodsSelection(
              chooseList,
              this.data.chooseGoodsList
            )
            this.setData(
              {
                show: false,
                goodsList,
                isFlag: false
              },
              () => {
                this.triggerEvent('skuRequested', {
                  goodsList: mergedGoodsList,
                  goodsData: goodsList[index]
                })
              }
            )
          }
        } else {
          wx.$util.showToast('暂无该规格商品')
          goodsList[index].active = false
          goodsList[index].multipleSpecificationsGoods.goods = ''
          goodsList[
            index
          ].multipleSpecificationsGoods.specificationList.forEach(item => {
            item.value = ''
          })
          recordList.forEach(record => {
            if (record.goodsFlag == 2) {
              if (
                goodsList[index].multipleSpecificationsGoods.id ==
                record.multipleSpecificationsGoods.id
              ) {
                record.active = false
                record.multipleSpecificationsGoods =
                  goodsList[index].multipleSpecificationsGoods
              }
            }
          })
          this.setData({
            goodsList,
            recordList
          })
        }

        // }

        this.setData(
          {
            goodsList,
            strTemp: str
          },
          () => {
            if (goodsList[index].multipleSpecificationsGoods.goods && type) {
              this.handleGoods(index)
            }
          }
        )
      }

      if (active && !type) {
        goodsList[index].multipleSpecificationsGoods.goods = ''
        goodsList[index].multipleSpecificationsGoods.specificationList.forEach(
          item => {
            item.value = ''
          }
        )
        recordList.forEach(record => {
          if (record.goodsFlag == 2) {
            if (
              goodsList[index].multipleSpecificationsGoods.id ==
              record.multipleSpecificationsGoods.id
            ) {
              record.multipleSpecificationsGoods =
                goodsList[index].multipleSpecificationsGoods
            }
          }
        })
        this.setData({
          goodsList
        })
      }

      return result
    },
    //选规格后获取商品,以单个商品的逻辑处理商品
    handleGoods(index) {
      let { goodsList } = this.data
      let ele = goodsList[index]

      let item = ele.multipleSpecificationsGoods.goods
      let serveName
      if (
        item?.upgradeGoodsServiceItemsList &&
        item.upgradeGoodsServiceItemsList.length
      ) {
        serveName = item.upgradeGoodsServiceItemsList.reduce(
          (label, ele, index) => {
            let unit = index == 0 ? '' : '/'
            label = label + unit + ele?.serviceName
            return label
          },
          ''
        )
      }
      if (item.position) {
        const fieldMap = {
          '4大件': '燃油车4大件服务',
          '3电': '新能源3电服务',
          '6大件': '新能源6大件服务'
          // 在这里可以添加更多的替换规则
        }
        // 如果输入字段在字段映射中有匹配项,则进行替换,否则保持不变
        fieldMap[item.position] && (serveName = fieldMap[item.position])
      }
      ele.multipleSpecificationsGoods.goods = {
        ...item,
        goodsId: item.merchantGoodsId,
        serveName,
        goodsNameAlias: item.upgradeGoodsAlias,
        appreciation: item?.appreciation,
        goodsName: item?.upgradeGoodsName,
        specificationNameSequence:
          item.specificationTypeSequence && item.specificationNameSequence
            ? item.specificationNameSequence.replace(/;~;/g, '/')
            : '',
        specificationData:
          item.specificationTypeSequence && item.specificationData
            ? item.specificationData.replace(/;~;/g, '/')
            : '',
        quotationGoodsServiceItemsRelationDTOList:
          item?.upgradeGoodsServiceItemsList,
        titlePictureUrl: item.goodsInfo?.thumbUrl
      }
      console.log(goodsList, 'goodsList')
      this.setData({
        goodsList
      })
    },
    cancel() {
      let { goodsData } = this.data
      this.setData({
        goodsList: goodsData,
        isFlag: false
      })
      this.setGoodsActive()
      this.triggerEvent('cancel')
    },
    setGoodsActive() {
      let { chooseGoodsList, goodsList, recordList, show } = this.data
      if (show) {
        wx.$util.showLoading()
        // wx.showLoading('加载中...')
        if (chooseGoodsList.length == 0) {
          goodsList.forEach(e => {
            e.active = false
          })
          recordList.forEach(e => {
            e.active = false
          })
        } else {
          // let indexList = []
          goodsList.map((e, index) => {
            e.active = false
            chooseGoodsList.map(s => {
              if (s.goods && e.goods) {
                // console.log(e);
                if (s.goods.goodsId == e.goods.goodsId) {
                  console.log(
                    s.goods.goodsId,
                    e.goods.goodsId,
                    's.goods.goodsId'
                  )
                  e.active = true
                }
              }
              if (s.packageGoods && e.packageGoods) {
                if (s.packageGoods.packageCode == e.packageGoods.packageCode) {
                  e.active = true
                }
              }
              //多规格不需要回显逻辑
              // if(s.multipleSpecificationsGoods&&e.multipleSpecificationsGoods){
              //   if (s.multipleSpecificationsGoods.id == e.multipleSpecificationsGoods.id) {
              //         e.active = true
              //         e.multipleSpecificationsGoods.goods=JSON.parse(JSON.stringify(s.multipleSpecificationsGoods.goods))
              //         e.multipleSpecificationsGoods.goods.sellPrice=s.multipleSpecificationsGoods.goods.price
              //         e.multipleSpecificationsGoods.specificationList={...s.multipleSpecificationsGoods.specificationList}
              //   }
              // }
            })
          })
          recordList.map((e, index) => {
            e.active = false
            e.count = 1
            chooseGoodsList.map(s => {
              if (s.goods && e.goods) {
                if (s.goods.goodsId == e.goods.goodsId) {
                  e.active = true
                }
              }
              if (s.packageGoods && e.packageGoods) {
                if (s.packageGoods.packageCode == e.packageGoods.packageCode) {
                  e.active = true
                }
              }
              // if(s.multipleSpecificationsGoods&&e.multipleSpecificationsGoods){
              //   if (s.multipleSpecificationsGoods.id == e.multipleSpecificationsGoods.id) {
              //         e.active = true
              //         e.multipleSpecificationsGoods.goods=JSON.parse(JSON.stringify(s.multipleSpecificationsGoods.goods))
              //         e.multipleSpecificationsGoods.specificationList=JSON.parse(JSON.stringify(s.multipleSpecificationsGoods.specificationList))
              //   }
              // }
            })
          })
        }
        this.setData(
          {
            goodsList,
            recordList,
            isFlag: true
          },
          () => {
            wx.hideLoading()
          }
        )
      }
    },
    getOrderBaseGoodsList(data) {
      let _this = this
      wx.getRendererUserAgent({
        success(res) {
          let isPad = /iPad|Android.*Tablet|Kindle|Silk/i.test(res.userAgent)
          _this.setData({
            isPad
          })
        }
      })
      this.setData({
        keyWord: '',
        goodsList: data,
        recordList: data
      })
    },
    getChooseList() {
      let { recordList, chooseGoodsList } = this.data
      let chooseList = []
      recordList.map(c => {
        if (c.active) {
          if (c.goods) {
            if (!c.count) c.count = 1
            c.goods.price = NP.times(c.goods.sellPrice || 0, 100)
            c.goods.basePrice = undefined
            if (chooseGoodsList.length > 0) {
              chooseGoodsList.map(x => {
                if (x.goods && x.goods.goodsId == c.goods.goodsId) {
                  c.goods.price = x.goods.price || 0
                }
              })
            }
            chooseList.push(c)
          }
          if (c.packageGoods) {
            if (!c.count) c.count = 1
            if (chooseGoodsList.length > 0) {
              chooseGoodsList.map(x => {
                if (
                  x.packageGoods &&
                  x.packageGoods.packageCode == c.packageGoods.packageCode
                ) {
                  c.packageGoods.goodsList = c.packageGoods.goodsList.map(
                    item => {
                      x.packageGoods.goodsList.map(ele => {
                        if (
                          item.merchantGoodsId == ele.goodsId ||
                          item.upgradeGoodsId == ele.upgradeGoodsId
                        ) {
                          item.price = ele.price || 0
                          item.isCents = true
                        }
                      })
                      return item
                    }
                  )
                }
              })
            }
            c.packageGoods.goodsList = c.packageGoods.goodsList.map(ele => {
              if (ele.isCents) {
                return ele
              }
              // 总是从 sellPrice 重新计算,防止重复乘以 100
              ele.price = NP.times(ele.sellPrice || 0, 100)
              ele.isCents = true
              return ele
            })
            // }
            c.price = c.packageGoods.goodsList.reduce((total, item) => {
              total = NP.plus(item.price, total)
              return total
            }, 0)
            chooseList.push(c)
          }
          if (c.multipleSpecificationsGoods) {
            if (c.multipleSpecificationsGoods.goods) {
              if (!c.count) c.count = 1
              c.multipleSpecificationsGoods.goods.price = NP.times(
                c.multipleSpecificationsGoods.goods.sellPrice || 0,
                100
              )
              c.multipleSpecificationsGoods.goods.basePrice = undefined
              if (chooseGoodsList.length > 0) {
                chooseGoodsList.map(x => {
                  if (x.goodsFlag == 2) {
                    if (
                      x.multipleSpecificationsGoods.goods &&
                      x.multipleSpecificationsGoods.goods.goodsId ==
                        c.multipleSpecificationsGoods.goods.goodsId
                    ) {
                      c.multipleSpecificationsGoods.goods.price =
                        x.multipleSpecificationsGoods.goods.price || 0
                    }
                  }
                })
              }
              chooseList.push(c)
            }
          }
        }
      })
      let arr = chooseList.filter(
        item =>
          item.multipleSpecificationsGoods &&
          item.multipleSpecificationsGoods.goods
      )
      chooseGoodsList.forEach(oldval => {
        if (oldval.multipleSpecificationsGoods) {
          if (
            !arr.some(
              newVal =>
                newVal.multipleSpecificationsGoods.goods.goodsId ==
                oldval.multipleSpecificationsGoods.goods.goodsId
            )
          )
            chooseList.push(oldval)
        }
      })
      return chooseList
    },
    async choose(e) {
      let { item, index, ind } = e.currentTarget.dataset
      let { active } = item

      let { minPrice, recordList } = this.data
      //单独处理多规格商品点击事件
      if (item.goodsFlag == 2) {
        if (!(await this.watchAllChose(ind))) {
          wx.$util.showToast('请先选择该商品规格')
          return
        }
      }
      // console.log(item, 'item');
      // console.log(item.goodsInfo.shopMinPrice)
      // console.log(minPrice && minPrice < item.shopMinPrice)
      // 如果有设置订单价格 那就不能选单个成本价比总价还高的
      if (minPrice && minPrice / 100 < item.shopMinPrice) return false
      let goodsType =
        item.goodsFlag == 1
          ? 'goods'
          : item.goodsFlag == 2
            ? 'multipleSpecificationsGoods'
            : 'packageGoods'
      let target = `goodsList[${ind}].active`
      let recordIndex = recordList.findIndex(el => {
        if (item.goods && el.goods) {
          return el[goodsType].goodsId == item[goodsType].goodsId
        }
        if (item.packageGoods && el.packageGoods) {
          // console.log(el[goodsType], 'el[goodsType]');
          return el[goodsType].packageCode == item[goodsType].packageCode
        }
        if (
          item.multipleSpecificationsGoods &&
          el.multipleSpecificationsGoods
        ) {
          // console.log(el[goodsType], 'el[goodsType]');
          return el[goodsType].id == item[goodsType].id
        }
      })
      // console.log(recordIndex, 'recordIndex');
      this.setData({
        [`recordList[${recordIndex}].active`]: !active,
        [target]: !active
      })
    },
    addCount(e) {
      let { index, count, item, ind } = e.currentTarget.dataset
      let target = `goodsList[${index}].upgradeGoodsList[${ind}].count`
      if (!item.count) item.count = 1
      // item.count += count
      item.count = NP.plus(item.count, count)
      if (item.count < 1) item.count = 1
      if (item.count > 999) item.count = 999
      this.setData({
        [target]: item.count
      })
    },
    input(e) {
      let { index, ind } = e.currentTarget.dataset
      let target = `goodsList[${index}].upgradeGoodsList[${ind}].count`
      let { value } = e.detail
      if (value < 1) value = 1
      if (value > 999) value = 999
      this.setData({
        [target]: Number(value)
      })
    },
    searchGoods(e) {
      let { goodsData, recordList } = this.data
      let { value } = e.detail
      if (!value) {
        this.getOrderBaseGoodsList(recordList)
        return
      }
      let _DATA_ = JSON.parse(JSON.stringify(recordList))
      let arr = []
      _DATA_.map(c => {
        console.log(c)
        // let nameList = []
        let name
        // debugger
        if (c.goodsFlag == 1 && c.goods) {
          // nameList.push(c.goods.goodsAlias,c.goods.upgradeGoodsName,c.goods.goodsName)
          name =
            c.goods.goodsAlias ||
            c.goods.upgradeGoodsName ||
            c.goods.goodsName ||
            ''
        }
        if (c.goodsFlag == 3 && c.packageGoods) {
          name = c.packageGoods.packageAlias || c.packageGoods.packageName
        }
        if (c.goodsFlag == 2 && c.multipleSpecificationsGoods) {
          name = c.multipleSpecificationsGoods.baseGoodsName
        }

        console.log(name, c, 'name')
        if (name.indexOf(value) != -1) {
          arr.push(c)
        }
      })
      // console.log(arr)
      this.setData({
        goodsList: arr
      })
    },
    confirm() {
      let chooseList = this.getChooseList()
      let { goodsList } = this.data
      if (goodsList.length < 1) return
      if (!chooseList.length) return wx.$util.showToast('请勾选商品')
      const mergedList = mergeSimpleQuotationGoodsSelection(
        chooseList,
        this.data.chooseGoodsList
      )
      this.triggerEvent('confirm', mergedList)

      this.setData({
        show: false,
        isFlag: false
      })
    }
  }
})